001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.pkcs8;
019:
020: import org.apache.harmony.security.asn1.ASN1Implicit;
021: import org.apache.harmony.security.asn1.ASN1Integer;
022: import org.apache.harmony.security.asn1.ASN1OctetString;
023: import org.apache.harmony.security.asn1.ASN1Sequence;
024: import org.apache.harmony.security.asn1.ASN1SetOf;
025: import org.apache.harmony.security.asn1.ASN1Type;
026: import org.apache.harmony.security.asn1.BerInputStream;
027:
028: import org.apache.harmony.security.x501.AttributeTypeAndValue;
029:
030: import org.apache.harmony.security.x509.AlgorithmIdentifier;
031:
032: import java.util.List;
033:
034: /**
035: * The class implements the ASN.1 DER encoding and decoding of the PKCS#8
036: * PrivateKeyInfo having the following ASN.1 notation:
037: *
038: * PrivateKeyInfo ::= SEQUENCE {
039: * version Version,
040: * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
041: * privateKey PrivateKey,
042: * attributes [0] IMPLICIT Attributes OPTIONAL }
043: *
044: * Version ::= INTEGER
045: *
046: * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
047: *
048: * PrivateKey ::= OCTET STRING
049: *
050: * Attributes ::= SET OF Attribute
051: */
052:
053: public class PrivateKeyInfo {
054:
055: private int version;
056:
057: private AlgorithmIdentifier privateKeyAlgorithm;
058:
059: private byte[] privateKey;
060:
061: private List attributes;
062:
063: private byte[] encoding;
064:
065: public PrivateKeyInfo(int version,
066: AlgorithmIdentifier privateKeyAlgorithm, byte[] privateKey,
067: List attributes) {
068:
069: this .version = version;
070: this .privateKeyAlgorithm = privateKeyAlgorithm;
071: this .privateKey = privateKey;
072: this .attributes = attributes;
073: }
074:
075: private PrivateKeyInfo(int version,
076: AlgorithmIdentifier privateKeyAlgorithm, byte[] privateKey,
077: List attributes, byte[] encoding) {
078: this (version, privateKeyAlgorithm, privateKey, attributes);
079: this .encoding = encoding;
080: }
081:
082: /**
083: * @return Returns version.
084: */
085: public int getVersion() {
086: return version;
087: }
088:
089: /**
090: * @return Returns AlgorithmIdentifier.
091: */
092: public AlgorithmIdentifier getAlgorithmIdentifier() {
093: return privateKeyAlgorithm;
094: }
095:
096: /**
097: * @return Returns List of attributes.
098: */
099: public List getAttributes() {
100: return attributes;
101: }
102:
103: /**
104: * @return Returns the OCTET STRING.
105: */
106: public byte[] getPrivateKey() {
107: return privateKey;
108: }
109:
110: /**
111: * Returns ASN.1 encoded form of this PrivateKeyInfo.
112: * @return a byte array containing ASN.1 encode form.
113: */
114: public byte[] getEncoded() {
115: if (encoding == null) {
116: encoding = ASN1.encode(this );
117: }
118: return encoding;
119: }
120:
121: public static final ASN1Sequence ASN1 = new ASN1Sequence(
122: new ASN1Type[] {
123:
124: ASN1Integer.getInstance(), // version
125: AlgorithmIdentifier.ASN1, // AlgorithmIdentifier
126: ASN1OctetString.getInstance(), // privateKey
127:
128: new ASN1Implicit(0, new ASN1SetOf(
129: AttributeTypeAndValue.ASN1)) // attributes
130: }) {
131:
132: {
133: setOptional(3); // attributes are OPTIONAL
134: }
135:
136: protected Object getDecodedObject(BerInputStream in) {
137:
138: Object[] values = (Object[]) in.content;
139:
140: return new PrivateKeyInfo(
141: ASN1Integer.toIntValue(values[0]),
142: (AlgorithmIdentifier) values[1],
143: (byte[]) values[2], (List) values[3], in
144: .getEncoded());
145: }
146:
147: protected void getValues(Object object, Object[] values) {
148:
149: PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) object;
150:
151: values[0] = ASN1Integer
152: .fromIntValue(privateKeyInfo.version);
153: values[1] = privateKeyInfo.privateKeyAlgorithm;
154: values[2] = privateKeyInfo.privateKey;
155: values[3] = privateKeyInfo.attributes;
156: }
157: };
158:
159: }
|