RSASSAPSSparams.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 RsassaPssParameters
  10. : Asn1Encodable
  11. {
  12. private AlgorithmIdentifier hashAlgorithm;
  13. private AlgorithmIdentifier maskGenAlgorithm;
  14. private DerInteger saltLength;
  15. private DerInteger trailerField;
  16. public readonly static AlgorithmIdentifier DefaultHashAlgorithm = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
  17. public readonly static AlgorithmIdentifier DefaultMaskGenFunction = new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, DefaultHashAlgorithm);
  18. public readonly static DerInteger DefaultSaltLength = new DerInteger(20);
  19. public readonly static DerInteger DefaultTrailerField = new DerInteger(1);
  20. public static RsassaPssParameters GetInstance(
  21. object obj)
  22. {
  23. if (obj == null || obj is RsassaPssParameters)
  24. {
  25. return (RsassaPssParameters)obj;
  26. }
  27. if (obj is Asn1Sequence)
  28. {
  29. return new RsassaPssParameters((Asn1Sequence)obj);
  30. }
  31. throw new ArgumentException("Unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  32. }
  33. /**
  34. * The default version
  35. */
  36. public RsassaPssParameters()
  37. {
  38. hashAlgorithm = DefaultHashAlgorithm;
  39. maskGenAlgorithm = DefaultMaskGenFunction;
  40. saltLength = DefaultSaltLength;
  41. trailerField = DefaultTrailerField;
  42. }
  43. public RsassaPssParameters(
  44. AlgorithmIdentifier hashAlgorithm,
  45. AlgorithmIdentifier maskGenAlgorithm,
  46. DerInteger saltLength,
  47. DerInteger trailerField)
  48. {
  49. this.hashAlgorithm = hashAlgorithm;
  50. this.maskGenAlgorithm = maskGenAlgorithm;
  51. this.saltLength = saltLength;
  52. this.trailerField = trailerField;
  53. }
  54. public RsassaPssParameters(
  55. Asn1Sequence seq)
  56. {
  57. hashAlgorithm = DefaultHashAlgorithm;
  58. maskGenAlgorithm = DefaultMaskGenFunction;
  59. saltLength = DefaultSaltLength;
  60. trailerField = DefaultTrailerField;
  61. for (int i = 0; i != seq.Count; i++)
  62. {
  63. Asn1TaggedObject o = (Asn1TaggedObject)seq[i];
  64. switch (o.TagNo)
  65. {
  66. case 0:
  67. hashAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  68. break;
  69. case 1:
  70. maskGenAlgorithm = AlgorithmIdentifier.GetInstance(o, true);
  71. break;
  72. case 2:
  73. saltLength = DerInteger.GetInstance(o, true);
  74. break;
  75. case 3:
  76. trailerField = DerInteger.GetInstance(o, true);
  77. break;
  78. default:
  79. throw new ArgumentException("unknown tag");
  80. }
  81. }
  82. }
  83. public AlgorithmIdentifier HashAlgorithm
  84. {
  85. get { return hashAlgorithm; }
  86. }
  87. public AlgorithmIdentifier MaskGenAlgorithm
  88. {
  89. get { return maskGenAlgorithm; }
  90. }
  91. public DerInteger SaltLength
  92. {
  93. get { return saltLength; }
  94. }
  95. public DerInteger TrailerField
  96. {
  97. get { return trailerField; }
  98. }
  99. /**
  100. * <pre>
  101. * RSASSA-PSS-params ::= SEQUENCE {
  102. * hashAlgorithm [0] OAEP-PSSDigestAlgorithms DEFAULT sha1,
  103. * maskGenAlgorithm [1] PKCS1MGFAlgorithms DEFAULT mgf1SHA1,
  104. * saltLength [2] INTEGER DEFAULT 20,
  105. * trailerField [3] TrailerField DEFAULT trailerFieldBC
  106. * }
  107. *
  108. * OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {
  109. * { OID id-sha1 PARAMETERS NULL }|
  110. * { OID id-sha256 PARAMETERS NULL }|
  111. * { OID id-sha384 PARAMETERS NULL }|
  112. * { OID id-sha512 PARAMETERS NULL },
  113. * ... -- Allows for future expansion --
  114. * }
  115. *
  116. * PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {
  117. * { OID id-mgf1 PARAMETERS OAEP-PSSDigestAlgorithms },
  118. * ... -- Allows for future expansion --
  119. * }
  120. *
  121. * TrailerField ::= INTEGER { trailerFieldBC(1) }
  122. * </pre>
  123. * @return the asn1 primitive representing the parameters.
  124. */
  125. public override Asn1Object ToAsn1Object()
  126. {
  127. Asn1EncodableVector v = new Asn1EncodableVector();
  128. if (!hashAlgorithm.Equals(DefaultHashAlgorithm))
  129. {
  130. v.Add(new DerTaggedObject(true, 0, hashAlgorithm));
  131. }
  132. if (!maskGenAlgorithm.Equals(DefaultMaskGenFunction))
  133. {
  134. v.Add(new DerTaggedObject(true, 1, maskGenAlgorithm));
  135. }
  136. if (!saltLength.Equals(DefaultSaltLength))
  137. {
  138. v.Add(new DerTaggedObject(true, 2, saltLength));
  139. }
  140. if (!trailerField.Equals(DefaultTrailerField))
  141. {
  142. v.Add(new DerTaggedObject(true, 3, trailerField));
  143. }
  144. return new DerSequence(v);
  145. }
  146. }
  147. }
  148. #pragma warning restore
  149. #endif