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 org.apache.harmony.security.asn1.ASN1BitString;
     21 import org.apache.harmony.security.asn1.ASN1Sequence;
     22 import org.apache.harmony.security.asn1.ASN1Type;
     23 import org.apache.harmony.security.asn1.BerInputStream;
     24 import org.apache.harmony.security.asn1.BitString;
     25 import org.apache.harmony.security.x509.AlgorithmIdentifier;
     26 
     27 /**
     28  * The class implements the ASN.1 DER encoding and decoding of the PKCS#10
     29  * Certificate Signing Request (CSR). Its ASN notation is as follows:
     30  *
     31  * CertificationRequest ::= SEQUENCE {
     32  *   certificationRequestInfo CertificationRequestInfo,
     33  *   signatureAlgorithm SignatureAlgorithmIdentifier,
     34  *   signature Signature
     35  * }
     36  *
     37  * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
     38  *
     39  * Signature ::= BIT STRING
     40  */
     41 public class CertificationRequest {
     42 
     43     // the value of certificationRequestInfo field of the structure
     44     private CertificationRequestInfo info;
     45 
     46     // the value of signatureAlgorithm field of the structure
     47     private AlgorithmIdentifier algId;
     48 
     49     // the value of signature field of the structure
     50     private byte[] signature;
     51 
     52     // the ASN.1 encoded form of CertificationRequest
     53     private byte[] encoding;
     54 
     55     public CertificationRequest(CertificationRequestInfo info,
     56             AlgorithmIdentifier algId, byte[] signature) {
     57         this.info = info;
     58         this.algId = algId;
     59         this.signature = new byte[signature.length];
     60         System.arraycopy(signature, 0, this.signature, 0, signature.length);
     61     }
     62 
     63     // private constructor with encoding given
     64     private CertificationRequest(CertificationRequestInfo info,
     65             AlgorithmIdentifier algId, byte[] signature, byte[] encoding) {
     66         this(info, algId, signature);
     67         this.encoding = encoding;
     68     }
     69 
     70     /**
     71      * @return Returns the algId.
     72      */
     73     public AlgorithmIdentifier getAlgId() {
     74         return algId;
     75     }
     76 
     77     /**
     78      * @return Returns the info.
     79      */
     80     public CertificationRequestInfo getInfo() {
     81         return info;
     82     }
     83 
     84     /**
     85      * @return Returns the signature.
     86      */
     87     public byte[] getSignature() {
     88         byte[] result = new byte[signature.length];
     89         System.arraycopy(signature, 0, result, 0, signature.length);
     90         return result;
     91     }
     92 
     93     /**
     94      * Returns ASN.1 encoded form of this CertificationRequest value.
     95      * @return a byte array containing ASN.1 encode form.
     96      */
     97     public byte[] getEncoded() {
     98         if (encoding == null) {
     99             encoding = CertificationRequest.ASN1.encode(this);
    100         }
    101         return encoding;
    102     }
    103 
    104     public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
    105             CertificationRequestInfo.ASN1,  // info
    106             AlgorithmIdentifier.ASN1,       // signatureAlgorithm
    107             ASN1BitString.getInstance() })  // signature
    108     {
    109 
    110         public Object getDecodedObject(BerInputStream in) {
    111             Object[] values = (Object[]) in.content;
    112 
    113             return new CertificationRequest(
    114                     (CertificationRequestInfo) values[0],
    115                     (AlgorithmIdentifier) values[1],
    116                     ((BitString) values[2]).bytes,
    117                     in.getEncoded());
    118         }
    119 
    120         protected void getValues(Object object, Object[] values) {
    121             CertificationRequest certReq = (CertificationRequest) object;
    122 
    123             values[0] = certReq.info;
    124             values[1] = certReq.algId;
    125             values[2] = new BitString(certReq.signature, 0);
    126         }
    127     };
    128 }
    129 
    130