X509Builder.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Emby.Server.Core.Cryptography
  34. {
  35. public abstract class X509Builder {
  36. private const string defaultHash = "SHA1";
  37. private string hashName;
  38. protected X509Builder ()
  39. {
  40. hashName = defaultHash;
  41. }
  42. protected abstract ASN1 ToBeSigned (string hashName);
  43. // move to PKCS1
  44. protected string GetOid (string hashName)
  45. {
  46. switch (hashName.ToLower (CultureInfo.InvariantCulture)) {
  47. case "md2":
  48. // md2withRSAEncryption (1 2 840 113549 1 1 2)
  49. return "1.2.840.113549.1.1.2";
  50. case "md4":
  51. // md4withRSAEncryption (1 2 840 113549 1 1 3)
  52. return "1.2.840.113549.1.1.3";
  53. case "md5":
  54. // md5withRSAEncryption (1 2 840 113549 1 1 4)
  55. return "1.2.840.113549.1.1.4";
  56. case "sha1":
  57. // sha1withRSAEncryption (1 2 840 113549 1 1 5)
  58. return "1.2.840.113549.1.1.5";
  59. case "sha256":
  60. // sha256WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 11 }
  61. return "1.2.840.113549.1.1.11";
  62. case "sha384":
  63. // sha384WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 12 }
  64. return "1.2.840.113549.1.1.12";
  65. case "sha512":
  66. // sha512WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 13 }
  67. return "1.2.840.113549.1.1.13";
  68. default:
  69. throw new NotSupportedException ("Unknown hash algorithm " + hashName);
  70. }
  71. }
  72. public string Hash {
  73. get { return hashName; }
  74. set {
  75. if (hashName == null)
  76. hashName = defaultHash;
  77. else
  78. hashName = value;
  79. }
  80. }
  81. public virtual byte[] Sign (AsymmetricAlgorithm aa)
  82. {
  83. if (aa is RSA)
  84. return Sign (aa as RSA);
  85. else if (aa is DSA)
  86. return Sign (aa as DSA);
  87. else
  88. throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString());
  89. }
  90. private byte[] Build (ASN1 tbs, string hashoid, byte[] signature)
  91. {
  92. ASN1 builder = new ASN1 (0x30);
  93. builder.Add (tbs);
  94. builder.Add (PKCS7.AlgorithmIdentifier (hashoid));
  95. // first byte of BITSTRING is the number of unused bits in the first byte
  96. byte[] bitstring = new byte [signature.Length + 1];
  97. Buffer.BlockCopy (signature, 0, bitstring, 1, signature.Length);
  98. builder.Add (new ASN1 (0x03, bitstring));
  99. return builder.GetBytes ();
  100. }
  101. public virtual byte[] Sign (RSA key)
  102. {
  103. string oid = GetOid (hashName);
  104. ASN1 tbs = ToBeSigned (oid);
  105. HashAlgorithm ha = HashAlgorithm.Create (hashName);
  106. byte[] hash = ha.ComputeHash (tbs.GetBytes ());
  107. RSAPKCS1SignatureFormatter pkcs1 = new RSAPKCS1SignatureFormatter (key);
  108. pkcs1.SetHashAlgorithm (hashName);
  109. byte[] signature = pkcs1.CreateSignature (hash);
  110. return Build (tbs, oid, signature);
  111. }
  112. public virtual byte[] Sign (DSA key)
  113. {
  114. string oid = "1.2.840.10040.4.3";
  115. ASN1 tbs = ToBeSigned (oid);
  116. HashAlgorithm ha = HashAlgorithm.Create (hashName);
  117. if (!(ha is SHA1))
  118. throw new NotSupportedException ("Only SHA-1 is supported for DSA");
  119. byte[] hash = ha.ComputeHash (tbs.GetBytes ());
  120. DSASignatureFormatter dsa = new DSASignatureFormatter (key);
  121. dsa.SetHashAlgorithm (hashName);
  122. byte[] rs = dsa.CreateSignature (hash);
  123. // split R and S
  124. byte[] r = new byte [20];
  125. Buffer.BlockCopy (rs, 0, r, 0, 20);
  126. byte[] s = new byte [20];
  127. Buffer.BlockCopy (rs, 20, s, 0, 20);
  128. ASN1 signature = new ASN1 (0x30);
  129. signature.Add (new ASN1 (0x02, r));
  130. signature.Add (new ASN1 (0x02, s));
  131. // dsaWithSha1 (1 2 840 10040 4 3)
  132. return Build (tbs, oid, signature.GetBytes ());
  133. }
  134. }
  135. }