Home | History | Annotate | Download | only in pkcs10
      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, WITHOUT
     13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     14  * License for the specific language governing permissions and limitations under
     15  * the License.
     16  */
     17 
     18 package org.apache.harmony.security.pkcs10;
     19 
     20 import java.util.List;
     21 import javax.security.auth.x500.X500Principal;
     22 import org.apache.harmony.security.asn1.ASN1Implicit;
     23 import org.apache.harmony.security.asn1.ASN1Integer;
     24 import org.apache.harmony.security.asn1.ASN1Sequence;
     25 import org.apache.harmony.security.asn1.ASN1SetOf;
     26 import org.apache.harmony.security.asn1.ASN1Type;
     27 import org.apache.harmony.security.asn1.BerInputStream;
     28 import org.apache.harmony.security.x501.AttributeTypeAndValue;
     29 import org.apache.harmony.security.x501.Name;
     30 import org.apache.harmony.security.x509.SubjectPublicKeyInfo;
     31 
     32 /**
     33  * CertificationRequestInfo ::= SEQUENCE {
     34  *   version Version,
     35  *   subject Name,
     36  *   subjectPublicKeyInfo SubjectPublicKeyInfo,
     37  *   attributes [0] IMPLICIT Attributes }
     38  *
     39  * Version ::= INTEGER
     40  *
     41  * Attributes ::= SET OF Attribute
     42  */
     43 public final class CertificationRequestInfo {
     44     private final int version;
     45 
     46     /** the value of subject field of the structure */
     47     private final Name subject;
     48 
     49     /** the value of subjectPublicKeyInfo field of the structure */
     50     private final SubjectPublicKeyInfo subjectPublicKeyInfo;
     51 
     52     /** the value of attributes field of the structure */
     53     private final List<?> attributes;
     54 
     55     /** the ASN.1 encoded form of CertificationRequestInfo */
     56     private byte[] encoding;
     57 
     58     private CertificationRequestInfo(int version, Name subject,
     59             SubjectPublicKeyInfo subjectPublicKeyInfo, List<?> attributes, byte [] encoding) {
     60         this.version = version;
     61         this.subject = subject;
     62         this.subjectPublicKeyInfo = subjectPublicKeyInfo;
     63         this.attributes = attributes;
     64         this.encoding = encoding;
     65     }
     66 
     67     public Name getSubject() {
     68         return subject;
     69     }
     70 
     71     public int getVersion() {
     72         return version;
     73     }
     74 
     75     /**
     76      * Returns ASN.1 encoded form of this CertificationRequestInfo.
     77      * @return a byte array containing ASN.1 encode form.
     78      */
     79     public byte[] getEncoded() {
     80         if (encoding == null) {
     81             encoding = ASN1.encode(this);
     82         }
     83         return encoding;
     84     }
     85 
     86     @Override public String toString() {
     87         StringBuilder res = new StringBuilder();
     88         res.append("-- CertificationRequestInfo:");
     89         res.append("\n version: ");
     90         res.append(version);
     91         res.append("\n subject: ");
     92         res.append(subject.getName(X500Principal.CANONICAL));
     93         res.append("\n subjectPublicKeyInfo: ");
     94         res.append("\n\t algorithm: ");
     95         res.append(subjectPublicKeyInfo.getAlgorithmIdentifier().getAlgorithm());
     96         res.append("\n\t public key: ").append(subjectPublicKeyInfo.getPublicKey());
     97         res.append("\n attributes: ");
     98         if (attributes != null) {
     99             res.append(attributes.toString());
    100         } else {
    101             res.append("none");
    102         }
    103         res.append("\n-- CertificationRequestInfo End\n");
    104         return res.toString();
    105     }
    106 
    107     public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
    108             ASN1Integer.getInstance(),              // version
    109             Name.ASN1,                              // subject
    110             SubjectPublicKeyInfo.ASN1,              // subjectPublicKeyInfo
    111             new ASN1Implicit(0, new ASN1SetOf(
    112                     AttributeTypeAndValue.ASN1))    // attributes
    113             }) {
    114 
    115         @Override protected Object getDecodedObject(BerInputStream in) {
    116             Object[] values = (Object[]) in.content;
    117             return new CertificationRequestInfo(
    118                     ASN1Integer.toIntValue(values[0]),
    119                     (Name) values[1],
    120                     (SubjectPublicKeyInfo) values[2],
    121                     (List<?>) values[3],
    122                     in.getEncoded());
    123         }
    124 
    125         @Override protected void getValues(Object object, Object[] values) {
    126             CertificationRequestInfo certReqInfo = (CertificationRequestInfo) object;
    127             values[0] = ASN1Integer.fromIntValue(certReqInfo.version);
    128             values[1] = certReqInfo.subject;
    129             values[2] = certReqInfo.subjectPublicKeyInfo;
    130             values[3] = certReqInfo.attributes;
    131         }
    132     };
    133 }
    134 
    135