ASN1Convert.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // ASN1Convert.cs: Abstract Syntax Notation 1 convertion routines
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. // Jesper Pedersen <jep@itplus.dk>
  7. //
  8. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  9. // (C) 2004 IT+ A/S (http://www.itplus.dk)
  10. // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Globalization;
  33. using System.Security.Cryptography;
  34. using System.Text;
  35. namespace Emby.Server.Core.Cryptography
  36. {
  37. // References:
  38. // a. ITU ASN.1 standards (free download)
  39. // http://www.itu.int/ITU-T/studygroups/com17/languages/
  40. public static class ASN1Convert {
  41. // RFC3280, section 4.2.1.5
  42. // CAs conforming to this profile MUST always encode certificate
  43. // validity dates through the year 2049 as UTCTime; certificate validity
  44. // dates in 2050 or later MUST be encoded as GeneralizedTime.
  45. // Under 1.x this API requires a Local datetime to be provided
  46. // Under 2.0 it will also accept a Utc datetime
  47. static public ASN1 FromDateTime (DateTime dt)
  48. {
  49. if (dt.Year < 2050) {
  50. // UTCTIME
  51. return new ASN1 (0x17, Encoding.ASCII.GetBytes (
  52. dt.ToUniversalTime ().ToString ("yyMMddHHmmss",
  53. CultureInfo.InvariantCulture) + "Z"));
  54. }
  55. else {
  56. // GENERALIZEDTIME
  57. return new ASN1 (0x18, Encoding.ASCII.GetBytes (
  58. dt.ToUniversalTime ().ToString ("yyyyMMddHHmmss",
  59. CultureInfo.InvariantCulture) + "Z"));
  60. }
  61. }
  62. static public ASN1 FromInt32 (Int32 value)
  63. {
  64. byte[] integer = BitConverterLE.GetBytes (value);
  65. Array.Reverse (integer);
  66. int x = 0;
  67. while ((x < integer.Length) && (integer [x] == 0x00))
  68. x++;
  69. ASN1 asn1 = new ASN1 (0x02);
  70. switch (x) {
  71. case 0:
  72. asn1.Value = integer;
  73. break;
  74. case 4:
  75. asn1.Value = new byte [1];
  76. break;
  77. default:
  78. byte[] smallerInt = new byte [4 - x];
  79. Buffer.BlockCopy (integer, x, smallerInt, 0, smallerInt.Length);
  80. asn1.Value = smallerInt;
  81. break;
  82. }
  83. return asn1;
  84. }
  85. static public ASN1 FromOid (string oid)
  86. {
  87. if (oid == null)
  88. throw new ArgumentNullException ("oid");
  89. return new ASN1 (CryptoConfig.EncodeOID (oid));
  90. }
  91. static public ASN1 FromUnsignedBigInteger (byte[] big)
  92. {
  93. if (big == null)
  94. throw new ArgumentNullException ("big");
  95. // check for numbers that could be interpreted as negative (first bit)
  96. if (big [0] >= 0x80) {
  97. // in thie cas we add a new, empty, byte (position 0) so we're
  98. // sure this will always be interpreted an unsigned integer.
  99. // However we can't feed it into RSAParameters or DSAParameters
  100. int length = big.Length + 1;
  101. byte[] uinteger = new byte [length];
  102. Buffer.BlockCopy (big, 0, uinteger, 1, length - 1);
  103. big = uinteger;
  104. }
  105. return new ASN1 (0x02, big);
  106. }
  107. static public int ToInt32 (ASN1 asn1)
  108. {
  109. if (asn1 == null)
  110. throw new ArgumentNullException ("asn1");
  111. if (asn1.Tag != 0x02)
  112. throw new FormatException ("Only integer can be converted");
  113. int x = 0;
  114. for (int i=0; i < asn1.Value.Length; i++)
  115. x = (x << 8) + asn1.Value [i];
  116. return x;
  117. }
  118. // Convert a binary encoded OID to human readable string representation of
  119. // an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann.
  120. static public string ToOid (ASN1 asn1)
  121. {
  122. if (asn1 == null)
  123. throw new ArgumentNullException ("asn1");
  124. byte[] aOID = asn1.Value;
  125. StringBuilder sb = new StringBuilder ();
  126. // Pick apart the OID
  127. byte x = (byte) (aOID[0] / 40);
  128. byte y = (byte) (aOID[0] % 40);
  129. if (x > 2) {
  130. // Handle special case for large y if x = 2
  131. y += (byte) ((x - 2) * 40);
  132. x = 2;
  133. }
  134. sb.Append (x.ToString (CultureInfo.InvariantCulture));
  135. sb.Append (".");
  136. sb.Append (y.ToString (CultureInfo.InvariantCulture));
  137. ulong val = 0;
  138. for (x = 1; x < aOID.Length; x++) {
  139. val = ((val << 7) | ((byte) (aOID [x] & 0x7F)));
  140. if ( !((aOID [x] & 0x80) == 0x80)) {
  141. sb.Append (".");
  142. sb.Append (val.ToString (CultureInfo.InvariantCulture));
  143. val = 0;
  144. }
  145. }
  146. return sb.ToString ();
  147. }
  148. static public DateTime ToDateTime (ASN1 time)
  149. {
  150. if (time == null)
  151. throw new ArgumentNullException ("time");
  152. string t = Encoding.ASCII.GetString (time.Value);
  153. // to support both UTCTime and GeneralizedTime (and not so common format)
  154. string mask = null;
  155. int year;
  156. switch (t.Length) {
  157. case 11:
  158. // illegal format, still it's supported for compatibility
  159. mask = "yyMMddHHmmZ";
  160. break;
  161. case 13:
  162. // RFC3280: 4.1.2.5.1 UTCTime
  163. year = Convert.ToInt16 (t.Substring (0, 2), CultureInfo.InvariantCulture);
  164. // Where YY is greater than or equal to 50, the
  165. // year SHALL be interpreted as 19YY; and
  166. // Where YY is less than 50, the year SHALL be
  167. // interpreted as 20YY.
  168. if (year >= 50)
  169. t = "19" + t;
  170. else
  171. t = "20" + t;
  172. mask = "yyyyMMddHHmmssZ";
  173. break;
  174. case 15:
  175. mask = "yyyyMMddHHmmssZ"; // GeneralizedTime
  176. break;
  177. case 17:
  178. // another illegal format (990630000000+1000), again supported for compatibility
  179. year = Convert.ToInt16 (t.Substring (0, 2), CultureInfo.InvariantCulture);
  180. string century = (year >= 50) ? "19" : "20";
  181. // ASN.1 (see ITU X.680 section 43.3) deals with offset differently than .NET
  182. char sign = (t[12] == '+') ? '-' : '+';
  183. t = String.Format ("{0}{1}{2}{3}{4}:{5}{6}", century, t.Substring (0, 12), sign,
  184. t[13], t[14], t[15], t[16]);
  185. mask = "yyyyMMddHHmmsszzz";
  186. break;
  187. }
  188. return DateTime.ParseExact (t, mask, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
  189. }
  190. }
  191. }