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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.security.pkcs10;
019:
020: import org.apache.harmony.security.asn1.ASN1BitString;
021: import org.apache.harmony.security.asn1.ASN1Sequence;
022: import org.apache.harmony.security.asn1.ASN1Type;
023: import org.apache.harmony.security.asn1.BerInputStream;
024: import org.apache.harmony.security.asn1.BitString;
025: import org.apache.harmony.security.x509.AlgorithmIdentifier;
026:
027: /**
028: * The class implements the ASN.1 DER encoding and decoding of the PKCS#10
029: * Certificate Signing Request (CSR). Its ASN notation is as follows:
030: *
031: * CertificationRequest ::= SEQUENCE {
032: * certificationRequestInfo CertificationRequestInfo,
033: * signatureAlgorithm SignatureAlgorithmIdentifier,
034: * signature Signature
035: * }
036: *
037: * SignatureAlgorithmIdentifier ::= AlgorithmIdentifier
038: *
039: * Signature ::= BIT STRING
040: */
041: public class CertificationRequest {
042:
043: // the value of certificationRequestInfo field of the structure
044: private CertificationRequestInfo info;
045:
046: // the value of signatureAlgorithm field of the structure
047: private AlgorithmIdentifier algId;
048:
049: // the value of signature field of the structure
050: private byte[] signature;
051:
052: // the ASN.1 encoded form of CertificationRequest
053: private byte[] encoding;
054:
055: public CertificationRequest(CertificationRequestInfo info,
056: AlgorithmIdentifier algId, byte[] signature) {
057: this .info = info;
058: this .algId = algId;
059: this .signature = new byte[signature.length];
060: System.arraycopy(signature, 0, this .signature, 0,
061: signature.length);
062: }
063:
064: // private constructor with encoding given
065: private CertificationRequest(CertificationRequestInfo info,
066: AlgorithmIdentifier algId, byte[] signature, byte[] encoding) {
067: this (info, algId, signature);
068: this .encoding = encoding;
069: }
070:
071: /**
072: * @return Returns the algId.
073: */
074: public AlgorithmIdentifier getAlgId() {
075: return algId;
076: }
077:
078: /**
079: * @return Returns the info.
080: */
081: public CertificationRequestInfo getInfo() {
082: return info;
083: }
084:
085: /**
086: * @return Returns the signature.
087: */
088: public byte[] getSignature() {
089: byte[] result = new byte[signature.length];
090: System.arraycopy(signature, 0, result, 0, signature.length);
091: return result;
092: }
093:
094: /**
095: * Returns ASN.1 encoded form of this CertificationRequest value.
096: * @return a byte array containing ASN.1 encode form.
097: */
098: public byte[] getEncoded() {
099: if (encoding == null) {
100: encoding = CertificationRequest.ASN1.encode(this );
101: }
102: return encoding;
103: }
104:
105: public static final ASN1Sequence ASN1 = new ASN1Sequence(
106: new ASN1Type[] { CertificationRequestInfo.ASN1, // info
107: AlgorithmIdentifier.ASN1, // signatureAlgorithm
108: ASN1BitString.getInstance() }) // signature
109: {
110:
111: public Object getDecodedObject(BerInputStream in) {
112: Object[] values = (Object[]) in.content;
113:
114: return new CertificationRequest(
115: (CertificationRequestInfo) values[0],
116: (AlgorithmIdentifier) values[1],
117: ((BitString) values[2]).bytes, 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: }
|