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

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.cms;
  2. import org.bouncycastle.asn1.ASN1Sequence;
  3. import org.bouncycastle.asn1.ASN1OctetString;
  4. import org.bouncycastle.asn1.ASN1TaggedObject;
  5. import org.bouncycastle.asn1.DERSequence;
  6. import org.bouncycastle.asn1.DERGeneralizedTime;
  7. import org.bouncycastle.asn1.DERObject;
  8. import org.bouncycastle.asn1.DEREncodable;
  9. import org.bouncycastle.asn1.ASN1EncodableVector;
  10. public class RecipientKeyIdentifier
  11.     implements DEREncodable
  12. {
  13. private ASN1OctetString      subjectKeyIdentifier;
  14. private DERGeneralizedTime   date;
  15. private OtherKeyAttribute    other;
  16. public RecipientKeyIdentifier(
  17.         ASN1OctetString         subjectKeyIdentifier,
  18.         DERGeneralizedTime      date,
  19.         OtherKeyAttribute       other)
  20.     {
  21.         this.subjectKeyIdentifier = subjectKeyIdentifier;
  22.         this.date = date;
  23.         this.other = other;
  24. }
  25. public RecipientKeyIdentifier(
  26.         ASN1Sequence seq)
  27.     {
  28. subjectKeyIdentifier = ASN1OctetString.getInstance(
  29.                                                     seq.getObjectAt(0));
  30. switch(seq.size())
  31.         {
  32.         case 1:
  33.             break;
  34.         case 2:
  35.             if (seq.getObjectAt(1) instanceof DERGeneralizedTime)
  36.             {
  37.                 date = (DERGeneralizedTime)seq.getObjectAt(1); 
  38.             }
  39.             else
  40.             {
  41.                 other = OtherKeyAttribute.getInstance(seq.getObjectAt(2));
  42.             }
  43.             break;
  44.         case 3:
  45.             date  = (DERGeneralizedTime)seq.getObjectAt(1);
  46.             other = OtherKeyAttribute.getInstance(seq.getObjectAt(2));
  47.             break;
  48.         default:
  49.             throw new IllegalArgumentException("Invalid KEKIdentifier");
  50. }
  51. }
  52.     /**
  53.      * return a RecipientKeyIdentifier object from a tagged object.
  54.      *
  55.      * @param _ato the tagged object holding the object we want.
  56.      * @param _explicit true if the object is meant to be explicitly
  57.      *              tagged false otherwise.
  58.      * @exception IllegalArgumentException if the object held by the
  59.      *          tagged object cannot be converted.
  60.      */
  61. public static RecipientKeyIdentifier getInstance(ASN1TaggedObject _ato, boolean _explicit) {
  62. return getInstance(ASN1Sequence.getInstance(_ato, _explicit));
  63. }
  64.     /**
  65.      * return a RecipientKeyIdentifier object from the given object.
  66.      *
  67.      * @param _obj the object we want converted.
  68.      * @exception IllegalArgumentException if the object cannot be converted.
  69.      */
  70. public static RecipientKeyIdentifier getInstance(Object _obj) {
  71. if(_obj == null || _obj instanceof RecipientKeyIdentifier) {
  72. return (RecipientKeyIdentifier)_obj;
  73. }
  74. if(_obj instanceof ASN1Sequence) {
  75. return new RecipientKeyIdentifier((ASN1Sequence)_obj);
  76. }
  77. throw new IllegalArgumentException("Invalid RecipientKeyIdentifier: " + _obj.getClass().getName());
  78. public ASN1OctetString getSubjectKeyIdentifier()
  79.     {
  80. return subjectKeyIdentifier;
  81. }
  82. public DERGeneralizedTime getDate()
  83.     {
  84. return date;
  85. }
  86. public OtherKeyAttribute getOtherKeyAttribute()
  87.     {
  88. return other;
  89. }
  90.     /** 
  91.      * <pre>
  92.      * RecipientKeyIdentifier ::= SEQUENCE {
  93.      *  subjectKeyIdentifier SubjectKeyIdentifier,
  94.      *  date GeneralizedTime OPTIONAL,
  95.      *  other OtherKeyAttribute OPTIONAL 
  96.      * }
  97.      *
  98.      * SubjectKeyIdentifier ::= OCTET STRING
  99.      * </pre>
  100.      */
  101. public DERObject getDERObject()
  102.     {
  103.         ASN1EncodableVector  v = new ASN1EncodableVector();
  104. v.add(subjectKeyIdentifier);
  105. if (date != null)
  106.         {
  107. v.add(date);
  108. }
  109. if (other != null)
  110.         {
  111. v.add(other);
  112. }
  113. return new DERSequence(v);
  114. }
  115. }