RSAESOAEPparams.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Oiw;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Pkcs
  8. {
  9. public class RsaesOaepParameters
  10. : Asn1Encodable
  11. {
  12. private AlgorithmIdentifier hashAlgorithm;
  13. private AlgorithmIdentifier maskGenAlgorithm;
  14. private AlgorithmIdentifier pSourceAlgorithm;
  15. public readonly static AlgorithmIdentifier DefaultHashAlgorithm = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  16. public readonly static AlgorithmIdentifier DefaultMaskGenFunction = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, DefaultHashAlgorithm);
  17. public readonly static AlgorithmIdentifier DefaultPSourceAlgorithm = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdPSpecified, new DerOctetString(new byte[0]));
  18. public static RsaesOaepParameters GetInstance(
  19. object obj)
  20. {
  21. if (obj is RsaesOaepParameters)
  22. {
  23. return (RsaesOaepParameters)obj;
  24. }
  25. else if (obj is Asn1Sequence)
  26. {
  27. return new RsaesOaepParameters((Asn1Sequence)obj);
  28. }
  29. throw new ArgumentException("Unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  30. }
  31. /**
  32. * The default version
  33. */
  34. public RsaesOaepParameters()
  35. : this(DefaultHashAlgorithm, DefaultMaskGenFunction, DefaultPSourceAlgorithm)
  36. {
  37. }
  38. public RsaesOaepParameters(
  39. AlgorithmIdentifier hashAlgorithm,
  40. AlgorithmIdentifier maskGenAlgorithm)
  41. : this(hashAlgorithm, maskGenAlgorithm, DefaultPSourceAlgorithm)
  42. {
  43. }
  44. public RsaesOaepParameters(
  45. AlgorithmIdentifier hashAlgorithm,
  46. AlgorithmIdentifier maskGenAlgorithm,
  47. AlgorithmIdentifier pSourceAlgorithm)
  48. {
  49. this.hashAlgorithm = hashAlgorithm;
  50. this.maskGenAlgorithm = maskGenAlgorithm;
  51. this.pSourceAlgorithm = pSourceAlgorithm;
  52. }
  53. public RsaesOaepParameters(
  54. Asn1Sequence seq)
  55. {
  56. hashAlgorithm = DefaultHashAlgorithm;
  57. maskGenAlgorithm = DefaultMaskGenFunction;
  58. pSourceAlgorithm = DefaultPSourceAlgorithm;
  59. for (int i = 0; i != seq.Count; i++)
  60. {
  61. Asn1TaggedObject o = (Asn1TaggedObject)seq[i];
  62. switch (o.TagNo)
  63. {
  64. case 0:
  65. hashAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  66. break;
  67. case 1:
  68. maskGenAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  69. break;
  70. case 2:
  71. pSourceAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  72. break;
  73. default:
  74. throw new ArgumentException("unknown tag");
  75. }
  76. }
  77. }
  78. public AlgorithmIdentifier HashAlgorithm
  79. {
  80. get { return hashAlgorithm; }
  81. }
  82. public AlgorithmIdentifier MaskGenAlgorithm
  83. {
  84. get { return maskGenAlgorithm; }
  85. }
  86. public AlgorithmIdentifier PSourceAlgorithm
  87. {
  88. get { return pSourceAlgorithm; }
  89. }
  90. /**
  91. * <pre>
  92. * RSAES-OAEP-params ::= SEQUENCE {
  93. * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1,
  94. * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
  95. * pSourceAlgorithm [2] PKCS1PSourceAlgorithms DEFAULT pSpecifiedEmpty
  96. * }
  97. *
  98. * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
  99. * { OID id-sha1 PARAMETERS NULL }|
  100. * { OID id-sha256 PARAMETERS NULL }|
  101. * { OID id-sha384 PARAMETERS NULL }|
  102. * { OID id-sha512 PARAMETERS NULL },
  103. * ... -- Allows for future expansion --
  104. * }
  105. * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
  106. * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
  107. * ... -- Allows for future expansion --
  108. * }
  109. * PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= {
  110. * { OID id-pSpecified PARAMETERS OCTET STRING },
  111. * ... -- Allows for future expansion --
  112. * }
  113. * </pre>
  114. * @return the asn1 primitive representing the parameters.
  115. */
  116. public override Asn1Object ToAsn1Object()
  117. {
  118. Asn1EncodableVector v = new Asn1EncodableVector();
  119. if (!hashAlgorithm.Equals(DefaultHashAlgorithm))
  120. {
  121. v.Add(new DerTaggedObject(true, 0, hashAlgorithm));
  122. }
  123. if (!maskGenAlgorithm.Equals(DefaultMaskGenFunction))
  124. {
  125. v.Add(new DerTaggedObject(true, 1, maskGenAlgorithm));
  126. }
  127. if (!pSourceAlgorithm.Equals(DefaultPSourceAlgorithm))
  128. {
  129. v.Add(new DerTaggedObject(true, 2, pSourceAlgorithm));
  130. }
  131. return new DerSequence(v);
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif