PKIHeaderBuilder.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Cmp
  6. {
  7. public class PkiHeaderBuilder
  8. {
  9. private DerInteger pvno;
  10. private GeneralName sender;
  11. private GeneralName recipient;
  12. private DerGeneralizedTime messageTime;
  13. private AlgorithmIdentifier protectionAlg;
  14. private Asn1OctetString senderKID; // KeyIdentifier
  15. private Asn1OctetString recipKID; // KeyIdentifier
  16. private Asn1OctetString transactionID;
  17. private Asn1OctetString senderNonce;
  18. private Asn1OctetString recipNonce;
  19. private PkiFreeText freeText;
  20. private Asn1Sequence generalInfo;
  21. public PkiHeaderBuilder(
  22. int pvno,
  23. GeneralName sender,
  24. GeneralName recipient)
  25. : this(new DerInteger(pvno), sender, recipient)
  26. {
  27. }
  28. private PkiHeaderBuilder(
  29. DerInteger pvno,
  30. GeneralName sender,
  31. GeneralName recipient)
  32. {
  33. this.pvno = pvno;
  34. this.sender = sender;
  35. this.recipient = recipient;
  36. }
  37. public virtual PkiHeaderBuilder SetMessageTime(DerGeneralizedTime time)
  38. {
  39. messageTime = time;
  40. return this;
  41. }
  42. public virtual PkiHeaderBuilder SetProtectionAlg(AlgorithmIdentifier aid)
  43. {
  44. protectionAlg = aid;
  45. return this;
  46. }
  47. public virtual PkiHeaderBuilder SetSenderKID(byte[] kid)
  48. {
  49. return SetSenderKID(kid == null ? null : new DerOctetString(kid));
  50. }
  51. public virtual PkiHeaderBuilder SetSenderKID(Asn1OctetString kid)
  52. {
  53. senderKID = kid;
  54. return this;
  55. }
  56. public virtual PkiHeaderBuilder SetRecipKID(byte[] kid)
  57. {
  58. return SetRecipKID(kid == null ? null : new DerOctetString(kid));
  59. }
  60. public virtual PkiHeaderBuilder SetRecipKID(Asn1OctetString kid)
  61. {
  62. recipKID = kid;
  63. return this;
  64. }
  65. public virtual PkiHeaderBuilder SetTransactionID(byte[] tid)
  66. {
  67. return SetTransactionID(tid == null ? null : new DerOctetString(tid));
  68. }
  69. public virtual PkiHeaderBuilder SetTransactionID(Asn1OctetString tid)
  70. {
  71. transactionID = tid;
  72. return this;
  73. }
  74. public virtual PkiHeaderBuilder SetSenderNonce(byte[] nonce)
  75. {
  76. return SetSenderNonce(nonce == null ? null : new DerOctetString(nonce));
  77. }
  78. public virtual PkiHeaderBuilder SetSenderNonce(Asn1OctetString nonce)
  79. {
  80. senderNonce = nonce;
  81. return this;
  82. }
  83. public virtual PkiHeaderBuilder SetRecipNonce(byte[] nonce)
  84. {
  85. return SetRecipNonce(nonce == null ? null : new DerOctetString(nonce));
  86. }
  87. public virtual PkiHeaderBuilder SetRecipNonce(Asn1OctetString nonce)
  88. {
  89. recipNonce = nonce;
  90. return this;
  91. }
  92. public virtual PkiHeaderBuilder SetFreeText(PkiFreeText text)
  93. {
  94. freeText = text;
  95. return this;
  96. }
  97. public virtual PkiHeaderBuilder SetGeneralInfo(InfoTypeAndValue genInfo)
  98. {
  99. return SetGeneralInfo(MakeGeneralInfoSeq(genInfo));
  100. }
  101. public virtual PkiHeaderBuilder SetGeneralInfo(InfoTypeAndValue[] genInfos)
  102. {
  103. return SetGeneralInfo(MakeGeneralInfoSeq(genInfos));
  104. }
  105. public virtual PkiHeaderBuilder SetGeneralInfo(Asn1Sequence seqOfInfoTypeAndValue)
  106. {
  107. generalInfo = seqOfInfoTypeAndValue;
  108. return this;
  109. }
  110. private static Asn1Sequence MakeGeneralInfoSeq(
  111. InfoTypeAndValue generalInfo)
  112. {
  113. return new DerSequence(generalInfo);
  114. }
  115. private static Asn1Sequence MakeGeneralInfoSeq(
  116. InfoTypeAndValue[] generalInfos)
  117. {
  118. Asn1Sequence genInfoSeq = null;
  119. if (generalInfos != null)
  120. {
  121. Asn1EncodableVector v = new Asn1EncodableVector();
  122. for (int i = 0; i < generalInfos.Length; ++i)
  123. {
  124. v.Add(generalInfos[i]);
  125. }
  126. genInfoSeq = new DerSequence(v);
  127. }
  128. return genInfoSeq;
  129. }
  130. /**
  131. * <pre>
  132. * PKIHeader ::= SEQUENCE {
  133. * pvno INTEGER { cmp1999(1), cmp2000(2) },
  134. * sender GeneralName,
  135. * -- identifies the sender
  136. * recipient GeneralName,
  137. * -- identifies the intended recipient
  138. * messageTime [0] GeneralizedTime OPTIONAL,
  139. * -- time of production of this message (used when sender
  140. * -- believes that the transport will be "suitable"; i.e.,
  141. * -- that the time will still be meaningful upon receipt)
  142. * protectionAlg [1] AlgorithmIdentifier OPTIONAL,
  143. * -- algorithm used for calculation of protection bits
  144. * senderKID [2] KeyIdentifier OPTIONAL,
  145. * recipKID [3] KeyIdentifier OPTIONAL,
  146. * -- to identify specific keys used for protection
  147. * transactionID [4] OCTET STRING OPTIONAL,
  148. * -- identifies the transaction; i.e., this will be the same in
  149. * -- corresponding request, response, certConf, and PKIConf
  150. * -- messages
  151. * senderNonce [5] OCTET STRING OPTIONAL,
  152. * recipNonce [6] OCTET STRING OPTIONAL,
  153. * -- nonces used to provide replay protection, senderNonce
  154. * -- is inserted by the creator of this message; recipNonce
  155. * -- is a nonce previously inserted in a related message by
  156. * -- the intended recipient of this message
  157. * freeText [7] PKIFreeText OPTIONAL,
  158. * -- this may be used to indicate context-specific instructions
  159. * -- (this field is intended for human consumption)
  160. * generalInfo [8] SEQUENCE SIZE (1..MAX) OF
  161. * InfoTypeAndValue OPTIONAL
  162. * -- this may be used to convey context-specific information
  163. * -- (this field not primarily intended for human consumption)
  164. * }
  165. * </pre>
  166. * @return a basic ASN.1 object representation.
  167. */
  168. public virtual PkiHeader Build()
  169. {
  170. Asn1EncodableVector v = new Asn1EncodableVector(pvno, sender, recipient);
  171. AddOptional(v, 0, messageTime);
  172. AddOptional(v, 1, protectionAlg);
  173. AddOptional(v, 2, senderKID);
  174. AddOptional(v, 3, recipKID);
  175. AddOptional(v, 4, transactionID);
  176. AddOptional(v, 5, senderNonce);
  177. AddOptional(v, 6, recipNonce);
  178. AddOptional(v, 7, freeText);
  179. AddOptional(v, 8, generalInfo);
  180. messageTime = null;
  181. protectionAlg = null;
  182. senderKID = null;
  183. recipKID = null;
  184. transactionID = null;
  185. senderNonce = null;
  186. recipNonce = null;
  187. freeText = null;
  188. generalInfo = null;
  189. return PkiHeader.GetInstance(new DerSequence(v));
  190. }
  191. private void AddOptional(Asn1EncodableVector v, int tagNo, Asn1Encodable obj)
  192. {
  193. if (obj != null)
  194. {
  195. v.Add(new DerTaggedObject(true, tagNo, obj));
  196. }
  197. }
  198. }
  199. }
  200. #pragma warning restore
  201. #endif