RSAESOAEPparams.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. {
  36. hashAlgorithm = DefaultHashAlgorithm;
  37. maskGenAlgorithm = DefaultMaskGenFunction;
  38. pSourceAlgorithm = DefaultPSourceAlgorithm;
  39. }
  40. public RsaesOaepParameters(
  41. AlgorithmIdentifier hashAlgorithm,
  42. AlgorithmIdentifier maskGenAlgorithm,
  43. AlgorithmIdentifier pSourceAlgorithm)
  44. {
  45. this.hashAlgorithm = hashAlgorithm;
  46. this.maskGenAlgorithm = maskGenAlgorithm;
  47. this.pSourceAlgorithm = pSourceAlgorithm;
  48. }
  49. public RsaesOaepParameters(
  50. Asn1Sequence seq)
  51. {
  52. hashAlgorithm = DefaultHashAlgorithm;
  53. maskGenAlgorithm = DefaultMaskGenFunction;
  54. pSourceAlgorithm = DefaultPSourceAlgorithm;
  55. for (int i = 0; i != seq.Count; i++)
  56. {
  57. Asn1TaggedObject o = (Asn1TaggedObject)seq[i];
  58. switch (o.TagNo)
  59. {
  60. case 0:
  61. hashAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  62. break;
  63. case 1:
  64. maskGenAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  65. break;
  66. case 2:
  67. pSourceAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  68. break;
  69. default:
  70. throw new ArgumentException("unknown tag");
  71. }
  72. }
  73. }
  74. public AlgorithmIdentifier HashAlgorithm
  75. {
  76. get { return hashAlgorithm; }
  77. }
  78. public AlgorithmIdentifier MaskGenAlgorithm
  79. {
  80. get { return maskGenAlgorithm; }
  81. }
  82. public AlgorithmIdentifier PSourceAlgorithm
  83. {
  84. get { return pSourceAlgorithm; }
  85. }
  86. /**
  87. * <pre>
  88. * RSAES-OAEP-params ::= SEQUENCE {
  89. * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1,
  90. * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
  91. * pSourceAlgorithm [2] PKCS1PSourceAlgorithms DEFAULT pSpecifiedEmpty
  92. * }
  93. *
  94. * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
  95. * { OID id-sha1 PARAMETERS NULL }|
  96. * { OID id-sha256 PARAMETERS NULL }|
  97. * { OID id-sha384 PARAMETERS NULL }|
  98. * { OID id-sha512 PARAMETERS NULL },
  99. * ... -- Allows for future expansion --
  100. * }
  101. * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
  102. * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
  103. * ... -- Allows for future expansion --
  104. * }
  105. * PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= {
  106. * { OID id-pSpecified PARAMETERS OCTET STRING },
  107. * ... -- Allows for future expansion --
  108. * }
  109. * </pre>
  110. * @return the asn1 primitive representing the parameters.
  111. */
  112. public override Asn1Object ToAsn1Object()
  113. {
  114. Asn1EncodableVector v = new Asn1EncodableVector();
  115. if (!hashAlgorithm.Equals(DefaultHashAlgorithm))
  116. {
  117. v.Add(new DerTaggedObject(true, 0, hashAlgorithm));
  118. }
  119. if (!maskGenAlgorithm.Equals(DefaultMaskGenFunction))
  120. {
  121. v.Add(new DerTaggedObject(true, 1, maskGenAlgorithm));
  122. }
  123. if (!pSourceAlgorithm.Equals(DefaultPSourceAlgorithm))
  124. {
  125. v.Add(new DerTaggedObject(true, 2, pSourceAlgorithm));
  126. }
  127. return new DerSequence(v);
  128. }
  129. }
  130. }
  131. #pragma warning restore
  132. #endif