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 java.io.IOException;
024: import java.math.BigInteger;
025:
026: import org.apache.harmony.security.asn1.ASN1Implicit;
027: import org.apache.harmony.security.asn1.ASN1Integer;
028: import org.apache.harmony.security.asn1.ASN1OctetString;
029: import org.apache.harmony.security.asn1.ASN1Sequence;
030: import org.apache.harmony.security.asn1.ASN1Type;
031: import org.apache.harmony.security.asn1.BerInputStream;
032: import org.apache.harmony.security.utils.Array;
033:
034: /**
035: * The class encapsulates the ASN.1 DER encoding/decoding work
036: * with Authority Key Identifier Extension (OID = 2.5.29.35).
037: * (as specified in RFC 3280 -
038: * Internet X.509 Public Key Infrastructure.
039: * Certificate and Certificate Revocation List (CRL) Profile.
040: * http://www.ietf.org/rfc/rfc3280.txt):
041: *
042: * <pre>
043: * id-ce-authorityKeyIdentifier OBJECT IDENTIFIER ::= { id-ce 35 }
044: *
045: * AuthorityKeyIdentifier ::= SEQUENCE {
046: * keyIdentifier [0] KeyIdentifier OPTIONAL,
047: * authorityCertIssuer [1] GeneralNames OPTIONAL,
048: * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
049: *
050: * KeyIdentifier ::= OCTET STRING
051: * </pre>
052: */
053: public class AuthorityKeyIdentifier extends ExtensionValue {
054:
055: private final byte[] keyIdentifier;
056: private final GeneralNames authorityCertIssuer;
057: private final BigInteger authorityCertSerialNumber;
058:
059: public AuthorityKeyIdentifier(byte[] keyIdentifier,
060: GeneralNames authorityCertIssuer,
061: BigInteger authorityCertSerialNumber) {
062: this .keyIdentifier = keyIdentifier;
063: this .authorityCertIssuer = authorityCertIssuer;
064: this .authorityCertSerialNumber = authorityCertSerialNumber;
065: }
066:
067: public static AuthorityKeyIdentifier decode(byte[] encoding)
068: throws IOException {
069: AuthorityKeyIdentifier aki = (AuthorityKeyIdentifier) ASN1
070: .decode(encoding);
071: aki.encoding = encoding;
072: return aki;
073: }
074:
075: public byte[] getEncoded() {
076: if (encoding == null) {
077: encoding = ASN1.encode(this );
078: }
079: return encoding;
080: }
081:
082: /**
083: * Places the string representation of extension value
084: * into the StringBuffer object.
085: */
086: public void dumpValue(StringBuffer buffer, String prefix) {
087: buffer.append(prefix).append("AuthorityKeyIdentifier [\n"); //$NON-NLS-1$
088: if (keyIdentifier != null) {
089: buffer.append(prefix).append(" keyIdentifier:\n"); //$NON-NLS-1$
090: buffer.append(Array
091: .toString(keyIdentifier, prefix + " ")); //$NON-NLS-1$
092: }
093: if (authorityCertIssuer != null) {
094: buffer.append(prefix).append(" authorityCertIssuer: [\n"); //$NON-NLS-1$
095: authorityCertIssuer.dumpValue(buffer, prefix + " "); //$NON-NLS-1$
096: buffer.append(prefix).append(" ]\n"); //$NON-NLS-1$
097: }
098: if (authorityCertSerialNumber != null) {
099: buffer.append(prefix).append(
100: " authorityCertSerialNumber: ") //$NON-NLS-1$
101: .append(authorityCertSerialNumber).append('\n');
102: }
103: buffer.append(prefix).append("]\n"); //$NON-NLS-1$
104: }
105:
106: public static final ASN1Type ASN1 = new ASN1Sequence(
107: new ASN1Type[] {
108: new ASN1Implicit(0, ASN1OctetString.getInstance()),
109: new ASN1Implicit(1, GeneralNames.ASN1),
110: new ASN1Implicit(2, ASN1Integer.getInstance()), }) {
111: {
112: setOptional(0);
113: setOptional(1);
114: setOptional(2);
115: }
116:
117: protected Object getDecodedObject(BerInputStream in)
118: throws IOException {
119: Object[] values = (Object[]) in.content;
120:
121: byte[] enc = (byte[]) values[2];
122: BigInteger authorityCertSerialNumber = null;
123: if (enc != null) {
124: authorityCertSerialNumber = new BigInteger(enc);
125: }
126:
127: return new AuthorityKeyIdentifier((byte[]) values[0],
128: (GeneralNames) values[1], authorityCertSerialNumber);
129: }
130:
131: protected void getValues(Object object, Object[] values) {
132:
133: AuthorityKeyIdentifier akid = (AuthorityKeyIdentifier) object;
134:
135: values[0] = akid.keyIdentifier;
136: values[1] = akid.authorityCertIssuer;
137: if (akid.authorityCertSerialNumber != null) {
138: values[2] = akid.authorityCertSerialNumber
139: .toByteArray();
140: }
141: }
142: };
143: }
|