X509Builder.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // X509Builder.cs: Abstract builder class for X509 objects
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // (C) 2004 Novell (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Globalization;
  32. using System.Security.Cryptography;
  33. namespace MediaBrowser.Server.Mono.Security {
  34. public abstract class X509Builder {
  35. private const string defaultHash = "SHA1";
  36. private string hashName;
  37. protected X509Builder ()
  38. {
  39. hashName = defaultHash;
  40. }
  41. protected abstract ASN1 ToBeSigned (string hashName);
  42. // move to PKCS1
  43. protected string GetOid (string hashName)
  44. {
  45. switch (hashName.ToLower (CultureInfo.InvariantCulture)) {
  46. case "md2":
  47. // md2withRSAEncryption (1 2 840 113549 1 1 2)
  48. return "1.2.840.113549.1.1.2";
  49. case "md4":
  50. // md4withRSAEncryption (1 2 840 113549 1 1 3)
  51. return "1.2.840.113549.1.1.3";
  52. case "md5":
  53. // md5withRSAEncryption (1 2 840 113549 1 1 4)
  54. return "1.2.840.113549.1.1.4";
  55. case "sha1":
  56. // sha1withRSAEncryption (1 2 840 113549 1 1 5)
  57. return "1.2.840.113549.1.1.5";
  58. case "sha256":
  59. // sha256WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 11 }
  60. return "1.2.840.113549.1.1.11";
  61. case "sha384":
  62. // sha384WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 12 }
  63. return "1.2.840.113549.1.1.12";
  64. case "sha512":
  65. // sha512WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 13 }
  66. return "1.2.840.113549.1.1.13";
  67. default:
  68. throw new NotSupportedException ("Unknown hash algorithm " + hashName);
  69. }
  70. }
  71. public string Hash {
  72. get { return hashName; }
  73. set {
  74. if (hashName == null)
  75. hashName = defaultHash;
  76. else
  77. hashName = value;
  78. }
  79. }
  80. public virtual byte[] Sign (AsymmetricAlgorithm aa)
  81. {
  82. if (aa is RSA)
  83. return Sign (aa as RSA);
  84. else if (aa is DSA)
  85. return Sign (aa as DSA);
  86. else
  87. throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString());
  88. }
  89. private byte[] Build (ASN1 tbs, string hashoid, byte[] signature)
  90. {
  91. ASN1 builder = new ASN1 (0x30);
  92. builder.Add (tbs);
  93. builder.Add (PKCS7.AlgorithmIdentifier (hashoid));
  94. // first byte of BITSTRING is the number of unused bits in the first byte
  95. byte[] bitstring = new byte [signature.Length + 1];
  96. Buffer.BlockCopy (signature, 0, bitstring, 1, signature.Length);
  97. builder.Add (new ASN1 (0x03, bitstring));
  98. return builder.GetBytes ();
  99. }
  100. public virtual byte[] Sign (RSA key)
  101. {
  102. string oid = GetOid (hashName);
  103. ASN1 tbs = ToBeSigned (oid);
  104. HashAlgorithm ha = HashAlgorithm.Create (hashName);
  105. byte[] hash = ha.ComputeHash (tbs.GetBytes ());
  106. RSAPKCS1SignatureFormatter pkcs1 = new RSAPKCS1SignatureFormatter (key);
  107. pkcs1.SetHashAlgorithm (hashName);
  108. byte[] signature = pkcs1.CreateSignature (hash);
  109. return Build (tbs, oid, signature);
  110. }
  111. public virtual byte[] Sign (DSA key)
  112. {
  113. string oid = "1.2.840.10040.4.3";
  114. ASN1 tbs = ToBeSigned (oid);
  115. HashAlgorithm ha = HashAlgorithm.Create (hashName);
  116. if (!(ha is SHA1))
  117. throw new NotSupportedException ("Only SHA-1 is supported for DSA");
  118. byte[] hash = ha.ComputeHash (tbs.GetBytes ());
  119. DSASignatureFormatter dsa = new DSASignatureFormatter (key);
  120. dsa.SetHashAlgorithm (hashName);
  121. byte[] rs = dsa.CreateSignature (hash);
  122. // split R and S
  123. byte[] r = new byte [20];
  124. Buffer.BlockCopy (rs, 0, r, 0, 20);
  125. byte[] s = new byte [20];
  126. Buffer.BlockCopy (rs, 20, s, 0, 20);
  127. ASN1 signature = new ASN1 (0x30);
  128. signature.Add (new ASN1 (0x02, r));
  129. signature.Add (new ASN1 (0x02, s));
  130. // dsaWithSha1 (1 2 840 10040 4 3)
  131. return Build (tbs, oid, signature.GetBytes ());
  132. }
  133. }
  134. }