Home | History | Annotate | Download | only in pkcs8
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 
     19 package org.apache.harmony.security.pkcs8;
     20 
     21 import java.util.List;
     22 import org.apache.harmony.security.asn1.ASN1Implicit;
     23 import org.apache.harmony.security.asn1.ASN1Integer;
     24 import org.apache.harmony.security.asn1.ASN1OctetString;
     25 import org.apache.harmony.security.asn1.ASN1Sequence;
     26 import org.apache.harmony.security.asn1.ASN1SetOf;
     27 import org.apache.harmony.security.asn1.ASN1Type;
     28 import org.apache.harmony.security.asn1.BerInputStream;
     29 import org.apache.harmony.security.x501.AttributeTypeAndValue;
     30 import org.apache.harmony.security.x509.AlgorithmIdentifier;
     31 
     32 /**
     33  * The class implements the ASN.1 DER encoding and decoding of the PKCS#8
     34  * PrivateKeyInfo having the following ASN.1 notation:
     35  *
     36  *  PrivateKeyInfo ::= SEQUENCE {
     37  *      version Version,
     38  *      privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
     39  *      privateKey PrivateKey,
     40  *      attributes [0] IMPLICIT Attributes OPTIONAL }
     41  *
     42  *  Version ::= INTEGER
     43  *
     44  *  PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
     45  *
     46  *  PrivateKey ::= OCTET STRING
     47  *
     48  *  Attributes ::= SET OF Attribute
     49  */
     50 public final class PrivateKeyInfo {
     51     private final int version;
     52     private final AlgorithmIdentifier privateKeyAlgorithm;
     53     private final byte[] privateKey;
     54     private final List<?> attributes;
     55     private byte[] encoding;
     56 
     57     public PrivateKeyInfo(int version, AlgorithmIdentifier privateKeyAlgorithm,
     58             byte[] privateKey, List attributes) {
     59         this.version = version;
     60         this.privateKeyAlgorithm = privateKeyAlgorithm;
     61         this.privateKey = privateKey;
     62         this.attributes = attributes;
     63     }
     64 
     65     private PrivateKeyInfo(int version,
     66             AlgorithmIdentifier privateKeyAlgorithm, byte[] privateKey,
     67             List attributes, byte[] encoding) {
     68         this(version, privateKeyAlgorithm, privateKey, attributes);
     69         this.encoding = encoding;
     70     }
     71 
     72     public int getVersion() {
     73         return version;
     74     }
     75 
     76     public AlgorithmIdentifier getAlgorithmIdentifier() {
     77         return privateKeyAlgorithm;
     78     }
     79 
     80     public List getAttributes() {
     81         return attributes;
     82     }
     83 
     84     /**
     85      * Returns the OCTET STRING.
     86      */
     87     public byte[] getPrivateKey() {
     88         return privateKey;
     89     }
     90 
     91     /**
     92      * Returns ASN.1 encoded form of this PrivateKeyInfo.
     93      */
     94     public byte[] getEncoded() {
     95         if (encoding == null) {
     96             encoding = ASN1.encode(this);
     97         }
     98         return encoding;
     99     }
    100 
    101     public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
    102 
    103     ASN1Integer.getInstance(), // version
    104             AlgorithmIdentifier.ASN1, // AlgorithmIdentifier
    105             ASN1OctetString.getInstance(), // privateKey
    106 
    107             new ASN1Implicit(0, new ASN1SetOf(AttributeTypeAndValue.ASN1)) // attributes
    108             }) {
    109 
    110         {
    111             setOptional(3); // attributes are OPTIONAL
    112         }
    113 
    114         protected Object getDecodedObject(BerInputStream in) {
    115             Object[] values = (Object[]) in.content;
    116             return new PrivateKeyInfo(ASN1Integer.toIntValue(values[0]),
    117                     (AlgorithmIdentifier) values[1], (byte[]) values[2],
    118                     (List) values[3], in.getEncoded());
    119         }
    120 
    121         protected void getValues(Object object, Object[] values) {
    122             PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) object;
    123             values[0] = ASN1Integer.fromIntValue(privateKeyInfo.version);
    124             values[1] = privateKeyInfo.privateKeyAlgorithm;
    125             values[2] = privateKeyInfo.privateKey;
    126             values[3] = privateKeyInfo.attributes;
    127         }
    128     };
    129 }
    130