J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.crypto.provider; |
| 27 | |
| 28 | import java.math.*; |
| 29 | import java.io.*; |
| 30 | import sun.security.x509.AlgorithmId; |
| 31 | import sun.security.util.*; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * This class implements the <code>PrivateKeyInfo</code> type, |
| 36 | * which is defined in PKCS #8 as follows: |
| 37 | * |
| 38 | * <pre> |
| 39 | * PrivateKeyInfo ::= SEQUENCE { |
| 40 | * version INTEGER, |
| 41 | * privateKeyAlgorithm AlgorithmIdentifier, |
| 42 | * privateKey OCTET STRING, |
| 43 | * attributes [0] IMPLICIT Attributes OPTIONAL } |
| 44 | * </pre> |
| 45 | * |
| 46 | * @author Jan Luehe |
| 47 | */ |
| 48 | final class PrivateKeyInfo { |
| 49 | |
| 50 | // the version number defined by the PKCS #8 standard |
| 51 | private static final BigInteger VERSION = BigInteger.ZERO; |
| 52 | |
| 53 | // the private-key algorithm |
| 54 | private AlgorithmId algid; |
| 55 | |
| 56 | // the private-key value |
| 57 | private byte[] privkey; |
| 58 | |
| 59 | /** |
| 60 | * Constructs a PKCS#8 PrivateKeyInfo from its ASN.1 encoding. |
| 61 | */ |
| 62 | PrivateKeyInfo(byte[] encoded) throws IOException { |
| 63 | DerValue val = new DerValue(encoded); |
| 64 | |
| 65 | if (val.tag != DerValue.tag_Sequence) |
| 66 | throw new IOException("private key parse error: not a sequence"); |
| 67 | |
| 68 | // version |
| 69 | BigInteger parsedVersion = val.data.getBigInteger(); |
| 70 | if (!parsedVersion.equals(VERSION)) { |
| 71 | throw new IOException("version mismatch: (supported: " + |
| 72 | VERSION + ", parsed: " + parsedVersion); |
| 73 | } |
| 74 | |
| 75 | // privateKeyAlgorithm |
| 76 | this.algid = AlgorithmId.parse(val.data.getDerValue()); |
| 77 | |
| 78 | // privateKey |
| 79 | this.privkey = val.data.getOctetString(); |
| 80 | |
| 81 | // OPTIONAL attributes not supported yet |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the private-key algorithm. |
| 86 | */ |
| 87 | AlgorithmId getAlgorithm() { |
| 88 | return this.algid; |
| 89 | } |
| 90 | } |