001: package org.bouncycastle.x509.extension;
002:
003: import java.io.IOException;
004: import java.security.InvalidKeyException;
005: import java.security.PublicKey;
006: import java.security.cert.CertificateParsingException;
007: import java.security.cert.X509Certificate;
008:
009: import org.bouncycastle.asn1.ASN1InputStream;
010: import org.bouncycastle.asn1.ASN1OctetString;
011: import org.bouncycastle.asn1.ASN1Sequence;
012: import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
013: import org.bouncycastle.asn1.x509.GeneralName;
014: import org.bouncycastle.asn1.x509.GeneralNames;
015: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
016: import org.bouncycastle.asn1.x509.X509Extensions;
017: import org.bouncycastle.jce.PrincipalUtil;
018:
019: /**
020: * A high level authority key identifier.
021: */
022: public class AuthorityKeyIdentifierStructure extends
023: AuthorityKeyIdentifier {
024: /**
025: * Constructor which will take the byte[] returned from getExtensionValue()
026: *
027: * @param encodedValue a DER octet encoded string with the extension structure in it.
028: * @throws IOException on parsing errors.
029: */
030: public AuthorityKeyIdentifierStructure(byte[] encodedValue)
031: throws IOException {
032: super ((ASN1Sequence) X509ExtensionUtil
033: .fromExtensionValue(encodedValue));
034: }
035:
036: private static ASN1Sequence fromCertificate(
037: X509Certificate certificate)
038: throws CertificateParsingException {
039: try {
040: if (certificate.getVersion() != 3) {
041: GeneralName genName = new GeneralName(PrincipalUtil
042: .getIssuerX509Principal(certificate));
043: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
044: (ASN1Sequence) new ASN1InputStream(certificate
045: .getPublicKey().getEncoded())
046: .readObject());
047:
048: return (ASN1Sequence) new AuthorityKeyIdentifier(info,
049: new GeneralNames(genName), certificate
050: .getSerialNumber()).toASN1Object();
051: } else {
052: GeneralName genName = new GeneralName(PrincipalUtil
053: .getIssuerX509Principal(certificate));
054:
055: byte[] ext = certificate
056: .getExtensionValue(X509Extensions.SubjectKeyIdentifier
057: .getId());
058:
059: if (ext != null) {
060: ASN1OctetString str = (ASN1OctetString) X509ExtensionUtil
061: .fromExtensionValue(ext);
062:
063: return (ASN1Sequence) new AuthorityKeyIdentifier(
064: str.getOctets(), new GeneralNames(genName),
065: certificate.getSerialNumber())
066: .toASN1Object();
067: } else {
068: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
069: (ASN1Sequence) new ASN1InputStream(
070: certificate.getPublicKey()
071: .getEncoded()).readObject());
072:
073: return (ASN1Sequence) new AuthorityKeyIdentifier(
074: info, new GeneralNames(genName),
075: certificate.getSerialNumber())
076: .toASN1Object();
077: }
078: }
079: } catch (Exception e) {
080: throw new CertificateParsingException(
081: "Exception extracting certificate details: "
082: + e.toString());
083: }
084: }
085:
086: private static ASN1Sequence fromKey(PublicKey pubKey)
087: throws InvalidKeyException {
088: try {
089: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
090: (ASN1Sequence) new ASN1InputStream(pubKey
091: .getEncoded()).readObject());
092:
093: return (ASN1Sequence) new AuthorityKeyIdentifier(info)
094: .toASN1Object();
095: } catch (Exception e) {
096: throw new InvalidKeyException("can't process key: " + e);
097: }
098: }
099:
100: /**
101: * Create an AuthorityKeyIdentifier using the passed in certificate's public
102: * key, issuer and serial number.
103: *
104: * @param certificate the certificate providing the information.
105: * @throws CertificateParsingException if there is a problem processing the certificate
106: */
107: public AuthorityKeyIdentifierStructure(X509Certificate certificate)
108: throws CertificateParsingException {
109: super (fromCertificate(certificate));
110: }
111:
112: /**
113: * Create an AuthorityKeyIdentifier using just the hash of the
114: * public key.
115: *
116: * @param pubKey the key to generate the hash from.
117: * @throws InvalidKeyException if there is a problem using the key.
118: */
119: public AuthorityKeyIdentifierStructure(PublicKey pubKey)
120: throws InvalidKeyException {
121: super(fromKey(pubKey));
122: }
123: }
|