RSASSAPSSparams.cs 4.4 KB

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