CertificationRequestInfo.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Pkcs
  6. {
  7. /**
  8. * Pkcs10 CertificationRequestInfo object.
  9. * <pre>
  10. * CertificationRequestInfo ::= Sequence {
  11. * version Integer { v1(0) } (v1,...),
  12. * subject Name,
  13. * subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
  14. * attributes [0] Attributes{{ CRIAttributes }}
  15. * }
  16. *
  17. * Attributes { ATTRIBUTE:IOSet } ::= Set OF Attr{{ IOSet }}
  18. *
  19. * Attr { ATTRIBUTE:IOSet } ::= Sequence {
  20. * type ATTRIBUTE.&amp;id({IOSet}),
  21. * values Set SIZE(1..MAX) OF ATTRIBUTE.&amp;Type({IOSet}{\@type})
  22. * }
  23. * </pre>
  24. */
  25. public class CertificationRequestInfo
  26. : Asn1Encodable
  27. {
  28. internal DerInteger version = new DerInteger(0);
  29. internal X509Name subject;
  30. internal SubjectPublicKeyInfo subjectPKInfo;
  31. internal Asn1Set attributes;
  32. public static CertificationRequestInfo GetInstance(object obj)
  33. {
  34. if (obj is CertificationRequestInfo)
  35. return (CertificationRequestInfo)obj;
  36. if (obj != null)
  37. return new CertificationRequestInfo(Asn1Sequence.GetInstance(obj));
  38. return null;
  39. }
  40. public CertificationRequestInfo(
  41. X509Name subject,
  42. SubjectPublicKeyInfo pkInfo,
  43. Asn1Set attributes)
  44. {
  45. this.subject = subject;
  46. this.subjectPKInfo = pkInfo;
  47. this.attributes = attributes;
  48. ValidateAttributes(attributes);
  49. if (subject == null || version == null || subjectPKInfo == null)
  50. {
  51. throw new ArgumentException(
  52. "Not all mandatory fields set in CertificationRequestInfo generator.");
  53. }
  54. }
  55. private CertificationRequestInfo(
  56. Asn1Sequence seq)
  57. {
  58. version = (DerInteger) seq[0];
  59. subject = X509Name.GetInstance(seq[1]);
  60. subjectPKInfo = SubjectPublicKeyInfo.GetInstance(seq[2]);
  61. //
  62. // some CertificationRequestInfo objects seem to treat this field
  63. // as optional.
  64. //
  65. if (seq.Count > 3)
  66. {
  67. DerTaggedObject tagobj = (DerTaggedObject) seq[3];
  68. attributes = Asn1Set.GetInstance(tagobj, false);
  69. }
  70. ValidateAttributes(attributes);
  71. if (subject == null || version == null || subjectPKInfo == null)
  72. {
  73. throw new ArgumentException(
  74. "Not all mandatory fields set in CertificationRequestInfo generator.");
  75. }
  76. }
  77. public DerInteger Version
  78. {
  79. get { return version; }
  80. }
  81. public X509Name Subject
  82. {
  83. get { return subject; }
  84. }
  85. public SubjectPublicKeyInfo SubjectPublicKeyInfo
  86. {
  87. get { return subjectPKInfo; }
  88. }
  89. public Asn1Set Attributes
  90. {
  91. get { return attributes; }
  92. }
  93. public override Asn1Object ToAsn1Object()
  94. {
  95. Asn1EncodableVector v = new Asn1EncodableVector(version, subject, subjectPKInfo);
  96. v.AddOptionalTagged(false, 0, attributes);
  97. return new DerSequence(v);
  98. }
  99. private static void ValidateAttributes(Asn1Set attributes)
  100. {
  101. if (attributes == null)
  102. return;
  103. foreach (Asn1Encodable ae in attributes)
  104. {
  105. Asn1Object obj = ae.ToAsn1Object();
  106. AttributePkcs attr = AttributePkcs.GetInstance(obj);
  107. if (attr.AttrType.Equals(PkcsObjectIdentifiers.Pkcs9AtChallengePassword))
  108. {
  109. if (attr.AttrValues.Count != 1)
  110. throw new ArgumentException("challengePassword attribute must have one value");
  111. }
  112. }
  113. }
  114. }
  115. }
  116. #pragma warning restore
  117. #endif