SubjectPublicKeyInfo.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:3k
源码类别:

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.x509;
  2. import java.io.*;
  3. import java.util.Enumeration;
  4. import java.math.BigInteger;
  5. import org.bouncycastle.asn1.*;
  6. import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
  7. /**
  8.  * The object that contains the public key stored in a certficate.
  9.  * <p>
  10.  * The getEncoded() method in the public keys in the JCE produces a DER
  11.  * encoded one of these.
  12.  */
  13. public class SubjectPublicKeyInfo
  14.     implements DEREncodable
  15. {
  16.     private AlgorithmIdentifier     algId;
  17.     private DERBitString            keyData;
  18.     public static SubjectPublicKeyInfo getInstance(
  19.         ASN1TaggedObject obj,
  20.         boolean          explicit)
  21.     {
  22.         return getInstance(ASN1Sequence.getInstance(obj, explicit));
  23.     }
  24.     public static SubjectPublicKeyInfo getInstance(
  25.         Object  obj)
  26.     {
  27.         if (obj instanceof SubjectPublicKeyInfo)
  28.         {
  29.             return (SubjectPublicKeyInfo)obj;
  30.         }
  31.         else if (obj instanceof ASN1Sequence)
  32.         {
  33.             return new SubjectPublicKeyInfo((ASN1Sequence)obj);
  34.         }
  35.         throw new IllegalArgumentException("unknown object in factory");
  36.     }
  37.     public SubjectPublicKeyInfo(
  38.         AlgorithmIdentifier algId,
  39.         DEREncodable        publicKey)
  40.     {
  41.         this.keyData = new DERBitString(publicKey);
  42.         this.algId = algId;
  43.     }
  44.     public SubjectPublicKeyInfo(
  45.         AlgorithmIdentifier algId,
  46.         byte[]              publicKey)
  47.     {
  48.         this.keyData = new DERBitString(publicKey);
  49.         this.algId = algId;
  50.     }
  51.     public SubjectPublicKeyInfo(
  52.         ASN1Sequence  seq)
  53.     {
  54.         Enumeration         e = seq.getObjects();
  55.         this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
  56.         this.keyData = (DERBitString)e.nextElement();
  57.     }
  58.     public AlgorithmIdentifier getAlgorithmId()
  59.     {
  60.         return algId;
  61.     }
  62.     /**
  63.      * for when the public key is an encoded object - if the bitstring
  64.      * can't be decoded this routine throws an IOException.
  65.      *
  66.      * @exception IOException - if the bit string doesn't represent a DER
  67.      * encoded object.
  68.      */
  69.     public DERObject getPublicKey()
  70.         throws IOException
  71.     {
  72.         ByteArrayInputStream    bIn = new ByteArrayInputStream(keyData.getBytes());
  73.         DERInputStream          dIn = new DERInputStream(bIn);
  74.         return dIn.readObject();
  75.     }
  76.     /**
  77.      * for when the public key is raw bits...
  78.      */
  79.     public DERBitString getPublicKeyData()
  80.     {
  81.         return keyData;
  82.     }
  83.     /**
  84.      * <pre>
  85.      * SubjectPublicKeyInfo ::= SEQUENCE {
  86.      *                          algorithm AlgorithmIdentifier,
  87.      *                          publicKey BIT STRING }
  88.      * </pre>
  89.      */
  90.     public DERObject getDERObject()
  91.     {
  92.         ASN1EncodableVector  v = new ASN1EncodableVector();
  93.         v.add(algId);
  94.         v.add(keyData);
  95.         return new DERSequence(v);
  96.     }
  97. }