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: */package org.apache.geronimo.crypto.asn1.x509;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.IOException;
020: import java.util.Enumeration;
021:
022: import org.apache.geronimo.crypto.asn1.ASN1Encodable;
023: import org.apache.geronimo.crypto.asn1.ASN1EncodableVector;
024: import org.apache.geronimo.crypto.asn1.ASN1InputStream;
025: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
026: import org.apache.geronimo.crypto.asn1.ASN1TaggedObject;
027: import org.apache.geronimo.crypto.asn1.DERBitString;
028: import org.apache.geronimo.crypto.asn1.DEREncodable;
029: import org.apache.geronimo.crypto.asn1.DERObject;
030: import org.apache.geronimo.crypto.asn1.DERSequence;
031:
032: /**
033: * The object that contains the public key stored in a certficate.
034: * <p>
035: * The getEncoded() method in the public keys in the JCE produces a DER
036: * encoded one of these.
037: */
038: public class SubjectPublicKeyInfo extends ASN1Encodable {
039: private AlgorithmIdentifier algId;
040: private DERBitString keyData;
041:
042: public static SubjectPublicKeyInfo getInstance(
043: ASN1TaggedObject obj, boolean explicit) {
044: return getInstance(ASN1Sequence.getInstance(obj, explicit));
045: }
046:
047: public static SubjectPublicKeyInfo getInstance(Object obj) {
048: if (obj instanceof SubjectPublicKeyInfo) {
049: return (SubjectPublicKeyInfo) obj;
050: } else if (obj instanceof ASN1Sequence) {
051: return new SubjectPublicKeyInfo((ASN1Sequence) obj);
052: }
053:
054: throw new IllegalArgumentException("unknown object in factory");
055: }
056:
057: public SubjectPublicKeyInfo(AlgorithmIdentifier algId,
058: DEREncodable publicKey) {
059: this .keyData = new DERBitString(publicKey);
060: this .algId = algId;
061: }
062:
063: public SubjectPublicKeyInfo(AlgorithmIdentifier algId,
064: byte[] publicKey) {
065: this .keyData = new DERBitString(publicKey);
066: this .algId = algId;
067: }
068:
069: public SubjectPublicKeyInfo(ASN1Sequence seq) {
070: Enumeration e = seq.getObjects();
071:
072: this .algId = AlgorithmIdentifier.getInstance(e.nextElement());
073: this .keyData = (DERBitString) e.nextElement();
074: }
075:
076: public AlgorithmIdentifier getAlgorithmId() {
077: return algId;
078: }
079:
080: /**
081: * for when the public key is an encoded object - if the bitstring
082: * can't be decoded this routine throws an IOException.
083: *
084: * @exception IOException - if the bit string doesn't represent a DER
085: * encoded object.
086: */
087: public DERObject getPublicKey() throws IOException {
088: ByteArrayInputStream bIn = new ByteArrayInputStream(keyData
089: .getBytes());
090: ASN1InputStream aIn = new ASN1InputStream(bIn);
091:
092: return aIn.readObject();
093: }
094:
095: /**
096: * for when the public key is raw bits...
097: */
098: public DERBitString getPublicKeyData() {
099: return keyData;
100: }
101:
102: /**
103: * Produce an object suitable for an ASN1OutputStream.
104: * <pre>
105: * SubjectPublicKeyInfo ::= SEQUENCE {
106: * algorithm AlgorithmIdentifier,
107: * publicKey BIT STRING }
108: * </pre>
109: */
110: public DERObject toASN1Object() {
111: ASN1EncodableVector v = new ASN1EncodableVector();
112:
113: v.add(algId);
114: v.add(keyData);
115:
116: return new DERSequence(v);
117: }
118: }
|