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

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.cms;
  2. import org.bouncycastle.asn1.ASN1OctetString;
  3. import org.bouncycastle.asn1.ASN1Sequence;
  4. import org.bouncycastle.asn1.ASN1TaggedObject;
  5. import org.bouncycastle.asn1.DERSequence;
  6. import org.bouncycastle.asn1.DERTaggedObject;
  7. import org.bouncycastle.asn1.DERObject;
  8. import org.bouncycastle.asn1.DERInteger;
  9. import org.bouncycastle.asn1.DEREncodable;
  10. import org.bouncycastle.asn1.ASN1EncodableVector;
  11. import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
  12. public class KeyAgreeRecipientInfo
  13.     implements DEREncodable
  14. {
  15. private DERInteger                  version;
  16. private OriginatorIdentifierOrKey   originator;
  17. private ASN1OctetString             ukm;
  18. private AlgorithmIdentifier         keyEncryptionAlgorithm;
  19. private ASN1Sequence                recipientEncryptedKeys;
  20. public KeyAgreeRecipientInfo(
  21.         OriginatorIdentifierOrKey   originator,
  22.         ASN1OctetString             ukm,
  23.         AlgorithmIdentifier         keyEncryptionAlgorithm,
  24.         ASN1Sequence                recipientEncryptedKeys)
  25.     {
  26.         this.version = new DERInteger(3);
  27.         this.originator = originator;
  28.         this.ukm = ukm;
  29.         this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
  30. }
  31. public KeyAgreeRecipientInfo(
  32.         ASN1Sequence seq)
  33.     {
  34. int index = 0;
  35. version = (DERInteger)seq.getObjectAt(index++);
  36.         originator = OriginatorIdentifierOrKey.getInstance(
  37.                             (ASN1TaggedObject)seq.getObjectAt(index++), true);
  38.         if (seq.getObjectAt(index) instanceof ASN1TaggedObject)
  39.         {
  40.             ukm = ASN1OctetString.getInstance(
  41.                             (ASN1TaggedObject)seq.getObjectAt(index++), true);
  42.         }
  43. keyEncryptionAlgorithm = AlgorithmIdentifier.getInstance(
  44.                                                 seq.getObjectAt(index++));
  45. recipientEncryptedKeys = (ASN1Sequence)seq.getObjectAt(index++);
  46. }
  47.     /**
  48.      * return a KeyAgreeRecipientInfo object from a tagged object.
  49.      *
  50.      * @param obj the tagged object holding the object we want.
  51.      * @param explicit true if the object is meant to be explicitly
  52.      *              tagged false otherwise.
  53.      * @exception IllegalArgumentException if the object held by the
  54.      *          tagged object cannot be converted.
  55.      */
  56. public static KeyAgreeRecipientInfo getInstance(
  57.         ASN1TaggedObject    obj,
  58.         boolean             explicit)
  59.     {
  60. return getInstance(ASN1Sequence.getInstance(obj, explicit));
  61. }
  62.     /**
  63.      * return a KeyAgreeRecipientInfo object from the given object.
  64.      *
  65.      * @param obj the object we want converted.
  66.      * @exception IllegalArgumentException if the object cannot be converted.
  67.      */
  68. public static KeyAgreeRecipientInfo getInstance(
  69.         Object obj)
  70.     {
  71. if (obj == null || obj instanceof KeyAgreeRecipientInfo)
  72.         {
  73. return (KeyAgreeRecipientInfo)obj;
  74. }
  75. if (obj instanceof ASN1Sequence)
  76.         {
  77. return new KeyAgreeRecipientInfo((ASN1Sequence)obj);
  78. }
  79. throw new IllegalArgumentException(
  80.         "Illegal object in KeyAgreeRecipientInfo: " + obj.getClass().getName());
  81. public DERInteger getVersion()
  82.     {
  83. return version;
  84. }
  85. public OriginatorIdentifierOrKey getOriginator()
  86.     {
  87. return originator;
  88. }
  89. public ASN1OctetString getUserKeyingMaterial()
  90.     {
  91. return ukm;
  92. }
  93. public AlgorithmIdentifier getKeyEncryptionAlgorithm()
  94.     {
  95. return keyEncryptionAlgorithm;
  96. }
  97. public ASN1Sequence getRecipientEncryptedKeys()
  98.     {
  99. return recipientEncryptedKeys;
  100. }
  101.     /** 
  102.      * <pre>
  103.      * KeyAgreeRecipientInfo ::= SEQUENCE {
  104.      *  version CMSVersion,  -- always set to 3
  105.      *  originator [0] EXPLICIT OriginatorIdentifierOrKey,
  106.      *  ukm [1] EXPLICIT UserKeyingMaterial OPTIONAL,
  107.      *  keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
  108.      *  recipientEncryptedKeys RecipientEncryptedKeys 
  109.      * }
  110.      * </pre>
  111.      */
  112. public DERObject getDERObject()
  113.     {
  114.         ASN1EncodableVector  v = new ASN1EncodableVector();
  115. v.add(version);
  116. v.add(new DERTaggedObject(true, 0, originator));
  117. if (ukm != null)
  118.         {
  119. v.add(new DERTaggedObject(true, 1, ukm));
  120. }
  121. v.add(keyEncryptionAlgorithm);
  122. v.add(recipientEncryptedKeys);
  123. return new DERSequence(v);
  124. }
  125. }