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: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package org.apache.harmony.security.x509;
022:
023: import org.apache.harmony.security.asn1.ASN1BitString;
024: import org.apache.harmony.security.asn1.ASN1Sequence;
025: import org.apache.harmony.security.asn1.ASN1Type;
026: import org.apache.harmony.security.asn1.BerInputStream;
027: import org.apache.harmony.security.asn1.BitString;
028: import org.apache.harmony.security.utils.Array;
029:
030: /**
031: * The class encapsulates the ASN.1 DER encoding/decoding work
032: * with the X.509 certificate. Its ASN notation is as follows
033: * (as specified in RFC 3280 -
034: * Internet X.509 Public Key Infrastructure.
035: * Certificate and Certificate Revocation List (CRL) Profile.
036: * http://www.ietf.org/rfc/rfc3280.txt):
037: *
038: * <pre>
039: * Certificate ::= SEQUENCE {
040: * tbsCertificate TBSCertificate,
041: * signatureAlgorithm AlgorithmIdentifier,
042: * signatureValue BIT STRING
043: * }
044: * </pre>
045: */
046: public class Certificate {
047:
048: // the value of tbsCertificate field of the structure
049: private final TBSCertificate tbsCertificate;
050: // the value of signatureAlgorithm field of the structure
051: private final AlgorithmIdentifier signatureAlgorithm;
052: // the value of signatureValue field of the structure
053: private final byte[] signatureValue;
054: // the ASN.1 encoded form of Certificate
055: private byte[] encoding;
056:
057: /**
058: * TODO
059: * @param tbsCertificate: TBSCertificate
060: * @param signatureAlgorithm: AlgorithmIdentifier
061: * @param signatureValue: byte[]
062: */
063: public Certificate(TBSCertificate tbsCertificate,
064: AlgorithmIdentifier signatureAlgorithm,
065: byte[] signatureValue) {
066: this .tbsCertificate = tbsCertificate;
067: this .signatureAlgorithm = signatureAlgorithm;
068: this .signatureValue = new byte[signatureValue.length];
069: System.arraycopy(signatureValue, 0, this .signatureValue, 0,
070: signatureValue.length);
071: }
072:
073: //
074: // TODO
075: // @param tbsCertificate: TBSCertificate
076: // @param signatureAlgorithm: AlgorithmIdentifier
077: // @param signatureValue: byte[]
078: // @param encoding: byte[]
079: //
080: private Certificate(TBSCertificate tbsCertificate,
081: AlgorithmIdentifier signatureAlgorithm,
082: byte[] signatureValue, byte[] encoding) {
083: this (tbsCertificate, signatureAlgorithm, signatureValue);
084: this .encoding = encoding;
085: }
086:
087: /**
088: * Returns the value of tbsCertificate field of the structure.
089: * @return tbsCertificate
090: */
091: public TBSCertificate getTbsCertificate() {
092: return tbsCertificate;
093: }
094:
095: /**
096: * Returns the value of signatureAlgorithm field of the structure.
097: * @return signatureAlgorithm
098: */
099: public AlgorithmIdentifier getSignatureAlgorithm() {
100: return signatureAlgorithm;
101: }
102:
103: /**
104: * Returns the value of signatureValue field of the structure.
105: * @return signatureValue
106: */
107: public byte[] getSignatureValue() {
108: byte[] result = new byte[signatureValue.length];
109: System.arraycopy(signatureValue, 0, result, 0,
110: signatureValue.length);
111: return result;
112: }
113:
114: public String toString() {
115: StringBuffer buffer = new StringBuffer();
116: buffer.append("X.509 Certificate:\n[\n"); //$NON-NLS-1$
117: tbsCertificate.dumpValue(buffer);
118: buffer.append("\n Algorithm: ["); //$NON-NLS-1$
119: signatureAlgorithm.dumpValue(buffer);
120: buffer.append(']');
121: buffer.append("\n Signature Value:\n"); //$NON-NLS-1$
122: buffer.append(Array.toString(signatureValue, "")); //$NON-NLS-1$
123: buffer.append(']');
124: return buffer.toString();
125: }
126:
127: /**
128: * Returns ASN.1 encoded form of this X.509 TBSCertificate value.
129: * @return a byte array containing ASN.1 encode form.
130: */
131: public byte[] getEncoded() {
132: if (encoding == null) {
133: encoding = Certificate.ASN1.encode(this );
134: }
135: return encoding;
136: }
137:
138: /**
139: * X.509 Certificate encoder/decoder.
140: */
141: public static final ASN1Sequence ASN1 = new ASN1Sequence(
142: new ASN1Type[] { TBSCertificate.ASN1,
143: AlgorithmIdentifier.ASN1,
144: ASN1BitString.getInstance() }) {
145:
146: protected Object getDecodedObject(BerInputStream in) {
147: Object[] values = (Object[]) in.content;
148: return new Certificate((TBSCertificate) values[0],
149: (AlgorithmIdentifier) values[1],
150: ((BitString) values[2]).bytes, // FIXME keep as BitString object
151: in.getEncoded());
152: }
153:
154: protected void getValues(Object object, Object[] values) {
155:
156: Certificate cert = (Certificate) object;
157:
158: values[0] = cert.tbsCertificate;
159: values[1] = cert.signatureAlgorithm;
160: values[2] = new BitString(cert.signatureValue, 0);
161: }
162: };
163: }
|