Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

632 lines
24 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 2001 - 2001
  5. //
  6. // File: vercert.cpp
  7. //
  8. // Contents: Minimal Cryptographic functions to verify ASN.1 encoded
  9. // X.509 certificates
  10. //
  11. //
  12. // Functions: MinCryptVerifyCertificate
  13. //
  14. // History: 17-Jan-01 philh created
  15. //--------------------------------------------------------------------------
  16. #include "global.hxx"
  17. #define MAX_CHAIN_DEPTH 10
  18. //+=========================================================================
  19. // Microsoft Roots
  20. //-=========================================================================
  21. // Name:: <CN=Microsoft Authenticode(tm) Root Authority, O=MSFT, C=US>
  22. const BYTE rgbMicrosoftRoot0_Name[] = {
  23. 0x30, 0x50, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
  24. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  25. 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0A,
  26. 0x13, 0x04, 0x4D, 0x53, 0x46, 0x54, 0x31, 0x32,
  27. 0x30, 0x30, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
  28. 0x29, 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F,
  29. 0x66, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68, 0x65,
  30. 0x6E, 0x74, 0x69, 0x63, 0x6F, 0x64, 0x65, 0x28,
  31. 0x74, 0x6D, 0x29, 0x20, 0x52, 0x6F, 0x6F, 0x74,
  32. 0x20, 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x69,
  33. 0x74, 0x79
  34. };
  35. const BYTE rgbMicrosoftRoot0_PubKeyInfo[]= {
  36. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09,
  37. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
  38. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00,
  39. 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01,
  40. 0x00, 0xDF, 0x08, 0xBA, 0xE3, 0x3F, 0x6E, 0x64,
  41. 0x9B, 0xF5, 0x89, 0xAF, 0x28, 0x96, 0x4A, 0x07,
  42. 0x8F, 0x1B, 0x2E, 0x8B, 0x3E, 0x1D, 0xFC, 0xB8,
  43. 0x80, 0x69, 0xA3, 0xA1, 0xCE, 0xDB, 0xDF, 0xB0,
  44. 0x8E, 0x6C, 0x89, 0x76, 0x29, 0x4F, 0xCA, 0x60,
  45. 0x35, 0x39, 0xAD, 0x72, 0x32, 0xE0, 0x0B, 0xAE,
  46. 0x29, 0x3D, 0x4C, 0x16, 0xD9, 0x4B, 0x3C, 0x9D,
  47. 0xDA, 0xC5, 0xD3, 0xD1, 0x09, 0xC9, 0x2C, 0x6F,
  48. 0xA6, 0xC2, 0x60, 0x53, 0x45, 0xDD, 0x4B, 0xD1,
  49. 0x55, 0xCD, 0x03, 0x1C, 0xD2, 0x59, 0x56, 0x24,
  50. 0xF3, 0xE5, 0x78, 0xD8, 0x07, 0xCC, 0xD8, 0xB3,
  51. 0x1F, 0x90, 0x3F, 0xC0, 0x1A, 0x71, 0x50, 0x1D,
  52. 0x2D, 0xA7, 0x12, 0x08, 0x6D, 0x7C, 0xB0, 0x86,
  53. 0x6C, 0xC7, 0xBA, 0x85, 0x32, 0x07, 0xE1, 0x61,
  54. 0x6F, 0xAF, 0x03, 0xC5, 0x6D, 0xE5, 0xD6, 0xA1,
  55. 0x8F, 0x36, 0xF6, 0xC1, 0x0B, 0xD1, 0x3E, 0x69,
  56. 0x97, 0x48, 0x72, 0xC9, 0x7F, 0xA4, 0xC8, 0xC2,
  57. 0x4A, 0x4C, 0x7E, 0xA1, 0xD1, 0x94, 0xA6, 0xD7,
  58. 0xDC, 0xEB, 0x05, 0x46, 0x2E, 0xB8, 0x18, 0xB4,
  59. 0x57, 0x1D, 0x86, 0x49, 0xDB, 0x69, 0x4A, 0x2C,
  60. 0x21, 0xF5, 0x5E, 0x0F, 0x54, 0x2D, 0x5A, 0x43,
  61. 0xA9, 0x7A, 0x7E, 0x6A, 0x8E, 0x50, 0x4D, 0x25,
  62. 0x57, 0xA1, 0xBF, 0x1B, 0x15, 0x05, 0x43, 0x7B,
  63. 0x2C, 0x05, 0x8D, 0xBD, 0x3D, 0x03, 0x8C, 0x93,
  64. 0x22, 0x7D, 0x63, 0xEA, 0x0A, 0x57, 0x05, 0x06,
  65. 0x0A, 0xDB, 0x61, 0x98, 0x65, 0x2D, 0x47, 0x49,
  66. 0xA8, 0xE7, 0xE6, 0x56, 0x75, 0x5C, 0xB8, 0x64,
  67. 0x08, 0x63, 0xA9, 0x30, 0x40, 0x66, 0xB2, 0xF9,
  68. 0xB6, 0xE3, 0x34, 0xE8, 0x67, 0x30, 0xE1, 0x43,
  69. 0x0B, 0x87, 0xFF, 0xC9, 0xBE, 0x72, 0x10, 0x5E,
  70. 0x23, 0xF0, 0x9B, 0xA7, 0x48, 0x65, 0xBF, 0x09,
  71. 0x88, 0x7B, 0xCD, 0x72, 0xBC, 0x2E, 0x79, 0x9B,
  72. 0x7B, 0x02, 0x03, 0x01, 0x00, 0x01
  73. };
  74. // Name:: <CN=Microsoft Root Authority, OU=Microsoft Corporation,
  75. // OU=Copyright (c) 1997 Microsoft Corp.>
  76. const BYTE rgbMicrosoftRoot1_Name[]= {
  77. 0x30, 0x70, 0x31, 0x2B, 0x30, 0x29, 0x06, 0x03,
  78. 0x55, 0x04, 0x0B, 0x13, 0x22, 0x43, 0x6F, 0x70,
  79. 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x28,
  80. 0x63, 0x29, 0x20, 0x31, 0x39, 0x39, 0x37, 0x20,
  81. 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66,
  82. 0x74, 0x20, 0x43, 0x6F, 0x72, 0x70, 0x2E, 0x31,
  83. 0x1E, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x04, 0x0B,
  84. 0x13, 0x15, 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73,
  85. 0x6F, 0x66, 0x74, 0x20, 0x43, 0x6F, 0x72, 0x70,
  86. 0x6F, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x31,
  87. 0x21, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x04, 0x03,
  88. 0x13, 0x18, 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73,
  89. 0x6F, 0x66, 0x74, 0x20, 0x52, 0x6F, 0x6F, 0x74,
  90. 0x20, 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x69,
  91. 0x74, 0x79
  92. };
  93. const BYTE rgbMicrosoftRoot1_PubKeyInfo[]= {
  94. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09,
  95. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
  96. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00,
  97. 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01,
  98. 0x00, 0xA9, 0x02, 0xBD, 0xC1, 0x70, 0xE6, 0x3B,
  99. 0xF2, 0x4E, 0x1B, 0x28, 0x9F, 0x97, 0x78, 0x5E,
  100. 0x30, 0xEA, 0xA2, 0xA9, 0x8D, 0x25, 0x5F, 0xF8,
  101. 0xFE, 0x95, 0x4C, 0xA3, 0xB7, 0xFE, 0x9D, 0xA2,
  102. 0x20, 0x3E, 0x7C, 0x51, 0xA2, 0x9B, 0xA2, 0x8F,
  103. 0x60, 0x32, 0x6B, 0xD1, 0x42, 0x64, 0x79, 0xEE,
  104. 0xAC, 0x76, 0xC9, 0x54, 0xDA, 0xF2, 0xEB, 0x9C,
  105. 0x86, 0x1C, 0x8F, 0x9F, 0x84, 0x66, 0xB3, 0xC5,
  106. 0x6B, 0x7A, 0x62, 0x23, 0xD6, 0x1D, 0x3C, 0xDE,
  107. 0x0F, 0x01, 0x92, 0xE8, 0x96, 0xC4, 0xBF, 0x2D,
  108. 0x66, 0x9A, 0x9A, 0x68, 0x26, 0x99, 0xD0, 0x3A,
  109. 0x2C, 0xBF, 0x0C, 0xB5, 0x58, 0x26, 0xC1, 0x46,
  110. 0xE7, 0x0A, 0x3E, 0x38, 0x96, 0x2C, 0xA9, 0x28,
  111. 0x39, 0xA8, 0xEC, 0x49, 0x83, 0x42, 0xE3, 0x84,
  112. 0x0F, 0xBB, 0x9A, 0x6C, 0x55, 0x61, 0xAC, 0x82,
  113. 0x7C, 0xA1, 0x60, 0x2D, 0x77, 0x4C, 0xE9, 0x99,
  114. 0xB4, 0x64, 0x3B, 0x9A, 0x50, 0x1C, 0x31, 0x08,
  115. 0x24, 0x14, 0x9F, 0xA9, 0xE7, 0x91, 0x2B, 0x18,
  116. 0xE6, 0x3D, 0x98, 0x63, 0x14, 0x60, 0x58, 0x05,
  117. 0x65, 0x9F, 0x1D, 0x37, 0x52, 0x87, 0xF7, 0xA7,
  118. 0xEF, 0x94, 0x02, 0xC6, 0x1B, 0xD3, 0xBF, 0x55,
  119. 0x45, 0xB3, 0x89, 0x80, 0xBF, 0x3A, 0xEC, 0x54,
  120. 0x94, 0x4E, 0xAE, 0xFD, 0xA7, 0x7A, 0x6D, 0x74,
  121. 0x4E, 0xAF, 0x18, 0xCC, 0x96, 0x09, 0x28, 0x21,
  122. 0x00, 0x57, 0x90, 0x60, 0x69, 0x37, 0xBB, 0x4B,
  123. 0x12, 0x07, 0x3C, 0x56, 0xFF, 0x5B, 0xFB, 0xA4,
  124. 0x66, 0x0A, 0x08, 0xA6, 0xD2, 0x81, 0x56, 0x57,
  125. 0xEF, 0xB6, 0x3B, 0x5E, 0x16, 0x81, 0x77, 0x04,
  126. 0xDA, 0xF6, 0xBE, 0xAE, 0x80, 0x95, 0xFE, 0xB0,
  127. 0xCD, 0x7F, 0xD6, 0xA7, 0x1A, 0x72, 0x5C, 0x3C,
  128. 0xCA, 0xBC, 0xF0, 0x08, 0xA3, 0x22, 0x30, 0xB3,
  129. 0x06, 0x85, 0xC9, 0xB3, 0x20, 0x77, 0x13, 0x85,
  130. 0xDF, 0x02, 0x03, 0x01, 0x00, 0x01
  131. };
  132. // 4096 bit key generated in 2001
  133. //
  134. // Name:: <CN=Microsoft Root Certificate Authority, DC=microsoft, DC=com>
  135. const BYTE rgbMicrosoftRoot2_Name[]= {
  136. 0x30, 0x5F, 0x31, 0x13, 0x30, 0x11, 0x06, 0x0A,
  137. 0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64,
  138. 0x01, 0x19, 0x16, 0x03, 0x63, 0x6F, 0x6D, 0x31,
  139. 0x19, 0x30, 0x17, 0x06, 0x0A, 0x09, 0x92, 0x26,
  140. 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x19, 0x16,
  141. 0x09, 0x6D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F,
  142. 0x66, 0x74, 0x31, 0x2D, 0x30, 0x2B, 0x06, 0x03,
  143. 0x55, 0x04, 0x03, 0x13, 0x24, 0x4D, 0x69, 0x63,
  144. 0x72, 0x6F, 0x73, 0x6F, 0x66, 0x74, 0x20, 0x52,
  145. 0x6F, 0x6F, 0x74, 0x20, 0x43, 0x65, 0x72, 0x74,
  146. 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20,
  147. 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x69, 0x74,
  148. 0x79
  149. };
  150. const BYTE rgbMicrosoftRoot2_PubKeyInfo[]= {
  151. 0x30, 0x82, 0x02, 0x22, 0x30, 0x0D, 0x06, 0x09,
  152. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01,
  153. 0x01, 0x05, 0x00, 0x03, 0x82, 0x02, 0x0F, 0x00,
  154. 0x30, 0x82, 0x02, 0x0A, 0x02, 0x82, 0x02, 0x01,
  155. 0x00, 0xF3, 0x5D, 0xFA, 0x80, 0x67, 0xD4, 0x5A,
  156. 0xA7, 0xA9, 0x0C, 0x2C, 0x90, 0x20, 0xD0, 0x35,
  157. 0x08, 0x3C, 0x75, 0x84, 0xCD, 0xB7, 0x07, 0x89,
  158. 0x9C, 0x89, 0xDA, 0xDE, 0xCE, 0xC3, 0x60, 0xFA,
  159. 0x91, 0x68, 0x5A, 0x9E, 0x94, 0x71, 0x29, 0x18,
  160. 0x76, 0x7C, 0xC2, 0xE0, 0xC8, 0x25, 0x76, 0x94,
  161. 0x0E, 0x58, 0xFA, 0x04, 0x34, 0x36, 0xE6, 0xDF,
  162. 0xAF, 0xF7, 0x80, 0xBA, 0xE9, 0x58, 0x0B, 0x2B,
  163. 0x93, 0xE5, 0x9D, 0x05, 0xE3, 0x77, 0x22, 0x91,
  164. 0xF7, 0x34, 0x64, 0x3C, 0x22, 0x91, 0x1D, 0x5E,
  165. 0xE1, 0x09, 0x90, 0xBC, 0x14, 0xFE, 0xFC, 0x75,
  166. 0x58, 0x19, 0xE1, 0x79, 0xB7, 0x07, 0x92, 0xA3,
  167. 0xAE, 0x88, 0x59, 0x08, 0xD8, 0x9F, 0x07, 0xCA,
  168. 0x03, 0x58, 0xFC, 0x68, 0x29, 0x6D, 0x32, 0xD7,
  169. 0xD2, 0xA8, 0xCB, 0x4B, 0xFC, 0xE1, 0x0B, 0x48,
  170. 0x32, 0x4F, 0xE6, 0xEB, 0xB8, 0xAD, 0x4F, 0xE4,
  171. 0x5C, 0x6F, 0x13, 0x94, 0x99, 0xDB, 0x95, 0xD5,
  172. 0x75, 0xDB, 0xA8, 0x1A, 0xB7, 0x94, 0x91, 0xB4,
  173. 0x77, 0x5B, 0xF5, 0x48, 0x0C, 0x8F, 0x6A, 0x79,
  174. 0x7D, 0x14, 0x70, 0x04, 0x7D, 0x6D, 0xAF, 0x90,
  175. 0xF5, 0xDA, 0x70, 0xD8, 0x47, 0xB7, 0xBF, 0x9B,
  176. 0x2F, 0x6C, 0xE7, 0x05, 0xB7, 0xE1, 0x11, 0x60,
  177. 0xAC, 0x79, 0x91, 0x14, 0x7C, 0xC5, 0xD6, 0xA6,
  178. 0xE4, 0xE1, 0x7E, 0xD5, 0xC3, 0x7E, 0xE5, 0x92,
  179. 0xD2, 0x3C, 0x00, 0xB5, 0x36, 0x82, 0xDE, 0x79,
  180. 0xE1, 0x6D, 0xF3, 0xB5, 0x6E, 0xF8, 0x9F, 0x33,
  181. 0xC9, 0xCB, 0x52, 0x7D, 0x73, 0x98, 0x36, 0xDB,
  182. 0x8B, 0xA1, 0x6B, 0xA2, 0x95, 0x97, 0x9B, 0xA3,
  183. 0xDE, 0xC2, 0x4D, 0x26, 0xFF, 0x06, 0x96, 0x67,
  184. 0x25, 0x06, 0xC8, 0xE7, 0xAC, 0xE4, 0xEE, 0x12,
  185. 0x33, 0x95, 0x31, 0x99, 0xC8, 0x35, 0x08, 0x4E,
  186. 0x34, 0xCA, 0x79, 0x53, 0xD5, 0xB5, 0xBE, 0x63,
  187. 0x32, 0x59, 0x40, 0x36, 0xC0, 0xA5, 0x4E, 0x04,
  188. 0x4D, 0x3D, 0xDB, 0x5B, 0x07, 0x33, 0xE4, 0x58,
  189. 0xBF, 0xEF, 0x3F, 0x53, 0x64, 0xD8, 0x42, 0x59,
  190. 0x35, 0x57, 0xFD, 0x0F, 0x45, 0x7C, 0x24, 0x04,
  191. 0x4D, 0x9E, 0xD6, 0x38, 0x74, 0x11, 0x97, 0x22,
  192. 0x90, 0xCE, 0x68, 0x44, 0x74, 0x92, 0x6F, 0xD5,
  193. 0x4B, 0x6F, 0xB0, 0x86, 0xE3, 0xC7, 0x36, 0x42,
  194. 0xA0, 0xD0, 0xFC, 0xC1, 0xC0, 0x5A, 0xF9, 0xA3,
  195. 0x61, 0xB9, 0x30, 0x47, 0x71, 0x96, 0x0A, 0x16,
  196. 0xB0, 0x91, 0xC0, 0x42, 0x95, 0xEF, 0x10, 0x7F,
  197. 0x28, 0x6A, 0xE3, 0x2A, 0x1F, 0xB1, 0xE4, 0xCD,
  198. 0x03, 0x3F, 0x77, 0x71, 0x04, 0xC7, 0x20, 0xFC,
  199. 0x49, 0x0F, 0x1D, 0x45, 0x88, 0xA4, 0xD7, 0xCB,
  200. 0x7E, 0x88, 0xAD, 0x8E, 0x2D, 0xEC, 0x45, 0xDB,
  201. 0xC4, 0x51, 0x04, 0xC9, 0x2A, 0xFC, 0xEC, 0x86,
  202. 0x9E, 0x9A, 0x11, 0x97, 0x5B, 0xDE, 0xCE, 0x53,
  203. 0x88, 0xE6, 0xE2, 0xB7, 0xFD, 0xAC, 0x95, 0xC2,
  204. 0x28, 0x40, 0xDB, 0xEF, 0x04, 0x90, 0xDF, 0x81,
  205. 0x33, 0x39, 0xD9, 0xB2, 0x45, 0xA5, 0x23, 0x87,
  206. 0x06, 0xA5, 0x55, 0x89, 0x31, 0xBB, 0x06, 0x2D,
  207. 0x60, 0x0E, 0x41, 0x18, 0x7D, 0x1F, 0x2E, 0xB5,
  208. 0x97, 0xCB, 0x11, 0xEB, 0x15, 0xD5, 0x24, 0xA5,
  209. 0x94, 0xEF, 0x15, 0x14, 0x89, 0xFD, 0x4B, 0x73,
  210. 0xFA, 0x32, 0x5B, 0xFC, 0xD1, 0x33, 0x00, 0xF9,
  211. 0x59, 0x62, 0x70, 0x07, 0x32, 0xEA, 0x2E, 0xAB,
  212. 0x40, 0x2D, 0x7B, 0xCA, 0xDD, 0x21, 0x67, 0x1B,
  213. 0x30, 0x99, 0x8F, 0x16, 0xAA, 0x23, 0xA8, 0x41,
  214. 0xD1, 0xB0, 0x6E, 0x11, 0x9B, 0x36, 0xC4, 0xDE,
  215. 0x40, 0x74, 0x9C, 0xE1, 0x58, 0x65, 0xC1, 0x60,
  216. 0x1E, 0x7A, 0x5B, 0x38, 0xC8, 0x8F, 0xBB, 0x04,
  217. 0x26, 0x7C, 0xD4, 0x16, 0x40, 0xE5, 0xB6, 0x6B,
  218. 0x6C, 0xAA, 0x86, 0xFD, 0x00, 0xBF, 0xCE, 0xC1,
  219. 0x35, 0x02, 0x03, 0x01, 0x00, 0x01
  220. };
  221. //+=========================================================================
  222. // Test Roots
  223. //-=========================================================================
  224. // Name:: <CN=Microsoft Test Root Authority, OU=Microsoft Corporation, OU=Copyright (c) 1999 Microsoft Corp.>
  225. const BYTE rgbTestRoot0_Name[] = {
  226. 0x30, 0x75, 0x31, 0x2B, 0x30, 0x29, 0x06, 0x03,
  227. 0x55, 0x04, 0x0B, 0x13, 0x22, 0x43, 0x6F, 0x70,
  228. 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x20, 0x28,
  229. 0x63, 0x29, 0x20, 0x31, 0x39, 0x39, 0x39, 0x20,
  230. 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73, 0x6F, 0x66,
  231. 0x74, 0x20, 0x43, 0x6F, 0x72, 0x70, 0x2E, 0x31,
  232. 0x1E, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x04, 0x0B,
  233. 0x13, 0x15, 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73,
  234. 0x6F, 0x66, 0x74, 0x20, 0x43, 0x6F, 0x72, 0x70,
  235. 0x6F, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x31,
  236. 0x26, 0x30, 0x24, 0x06, 0x03, 0x55, 0x04, 0x03,
  237. 0x13, 0x1D, 0x4D, 0x69, 0x63, 0x72, 0x6F, 0x73,
  238. 0x6F, 0x66, 0x74, 0x20, 0x54, 0x65, 0x73, 0x74,
  239. 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x20, 0x41, 0x75,
  240. 0x74, 0x68, 0x6F, 0x72, 0x69, 0x74, 0x79
  241. };
  242. const BYTE rgbTestRoot0_PubKeyInfo[]= {
  243. 0x30, 0x81, 0xDF, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  244. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
  245. 0x05, 0x00, 0x03, 0x81, 0xCD, 0x00, 0x30, 0x81,
  246. 0xC9, 0x02, 0x81, 0xC1, 0x00, 0xA9, 0xAA, 0x83,
  247. 0x58, 0x6D, 0xB5, 0xD3, 0x0C, 0x4B, 0x5B, 0x80,
  248. 0x90, 0xE5, 0xC3, 0x0F, 0x28, 0x0C, 0x7E, 0x3D,
  249. 0x3C, 0x24, 0xC5, 0x29, 0x56, 0x63, 0x8C, 0xEE,
  250. 0xC7, 0x83, 0x4A, 0xD8, 0x8C, 0x25, 0xD3, 0x0E,
  251. 0xD3, 0x12, 0xB7, 0xE1, 0x86, 0x72, 0x74, 0xA7,
  252. 0x8B, 0xFB, 0x0F, 0x05, 0xE9, 0x65, 0xC1, 0x9B,
  253. 0xD8, 0x56, 0xC2, 0x93, 0xF0, 0xFB, 0xE9, 0x5A,
  254. 0x48, 0x85, 0x7D, 0x95, 0xAA, 0xDF, 0x01, 0x86,
  255. 0xB7, 0x33, 0x33, 0x46, 0x56, 0xCB, 0x5B, 0x7A,
  256. 0xC4, 0xAF, 0xA0, 0x96, 0x53, 0x3A, 0xE9, 0xFB,
  257. 0x3B, 0x78, 0xC1, 0x43, 0x0C, 0xC7, 0x6E, 0x1C,
  258. 0x2F, 0xD1, 0x55, 0xF1, 0x19, 0xB2, 0x3F, 0xF8,
  259. 0xD6, 0xA0, 0xC7, 0x24, 0x95, 0x3B, 0xC8, 0x45,
  260. 0x25, 0x6F, 0x45, 0x3A, 0x46, 0x4F, 0xD2, 0x27,
  261. 0x8B, 0xC7, 0x50, 0x75, 0xC6, 0x80, 0x5E, 0x0D,
  262. 0x99, 0x78, 0x61, 0x77, 0x39, 0xC1, 0xB3, 0x0F,
  263. 0x9D, 0x12, 0x9C, 0xC4, 0xBB, 0x32, 0x7B, 0xB2,
  264. 0x4B, 0x26, 0xAA, 0x4E, 0xC0, 0x32, 0xB0, 0x2A,
  265. 0x13, 0x21, 0xBE, 0xED, 0x24, 0xF4, 0x7D, 0x0D,
  266. 0xEA, 0xAA, 0x8A, 0x7A, 0xD2, 0x8B, 0x4D, 0x97,
  267. 0xB5, 0x4D, 0x64, 0xBA, 0xFB, 0x46, 0xDD, 0x69,
  268. 0x6F, 0x9A, 0x0E, 0xCC, 0x53, 0x77, 0xAA, 0x6E,
  269. 0xAE, 0x20, 0xD6, 0x21, 0x98, 0x69, 0xD9, 0x46,
  270. 0xB9, 0x64, 0x32, 0xD4, 0x17, 0x02, 0x03, 0x01,
  271. 0x00, 0x01
  272. };
  273. typedef struct _ROOT_INFO {
  274. CRYPT_DER_BLOB EncodedName;
  275. CRYPT_DER_BLOB EncodedPubKeyInfo;
  276. BOOL fTestRoot;
  277. } ROOT_INFO, *PROOT_INFO;
  278. const ROOT_INFO RootTable[] = {
  279. sizeof(rgbMicrosoftRoot0_Name), (BYTE *) rgbMicrosoftRoot0_Name,
  280. sizeof(rgbMicrosoftRoot0_PubKeyInfo), (BYTE *) rgbMicrosoftRoot0_PubKeyInfo,
  281. FALSE,
  282. sizeof(rgbMicrosoftRoot1_Name), (BYTE *) rgbMicrosoftRoot1_Name,
  283. sizeof(rgbMicrosoftRoot1_PubKeyInfo), (BYTE *) rgbMicrosoftRoot1_PubKeyInfo,
  284. FALSE,
  285. sizeof(rgbMicrosoftRoot2_Name), (BYTE *) rgbMicrosoftRoot2_Name,
  286. sizeof(rgbMicrosoftRoot2_PubKeyInfo), (BYTE *) rgbMicrosoftRoot2_PubKeyInfo,
  287. FALSE,
  288. sizeof(rgbTestRoot0_Name), (BYTE *) rgbTestRoot0_Name,
  289. sizeof(rgbTestRoot0_PubKeyInfo), (BYTE *) rgbTestRoot0_PubKeyInfo,
  290. TRUE
  291. };
  292. #define ROOT_CNT (sizeof(RootTable) / sizeof(RootTable[0]))
  293. #define wszSETUP_REG \
  294. L"System\\Setup"
  295. #define wszSYSTEM_SETUP_REG_VALUE \
  296. L"SystemSetupInProgress"
  297. #define wszTEST_ROOT_REG \
  298. L"SOFTWARE\\Microsoft\\SystemCertificates\\Root\\Certificates\\2BD63D28D7BCD0E251195AEB519243C13142EBC3"
  299. // Check the CERT_STORE_PROV_SYSTEM_REGISTRY store for the Test Root.
  300. //
  301. // If the Test Root is found, return ERROR_SUCCESS.
  302. // If the Test Root is not found but we're in NT GUI-mode setup,
  303. // return ERROR_SUCCESS.
  304. // If the Test Root is not found and we're not in setup,
  305. // return CERT_E_UNTRUSTEDROOT.
  306. // For any other error, return the provided error code.
  307. DWORD
  308. WINAPI
  309. I_CheckIsTestRootAllowed(void)
  310. {
  311. DWORD dwReturn;
  312. HKEY hTestRootKey = 0;
  313. HKEY hSetupKey = 0;
  314. DWORD cb = 0;
  315. BOOL fSystemSetupInProgress = FALSE;
  316. dwReturn = RegOpenKeyExW(
  317. HKEY_LOCAL_MACHINE,
  318. wszTEST_ROOT_REG,
  319. 0, KEY_READ, &hTestRootKey);
  320. if (ERROR_SUCCESS != dwReturn)
  321. {
  322. //
  323. // We've determined that the Test
  324. // Root isn't installed. Check if we're in NT GUI-mode
  325. // setup right now.
  326. //
  327. // If we're in setup, we need to allow the Test Root
  328. // (but *only* for an otherwise valid signature)
  329. // since we may have been called before
  330. // setup has had a chance to install the Test Root.
  331. //
  332. dwReturn = RegOpenKeyExW(
  333. HKEY_LOCAL_MACHINE,
  334. wszSETUP_REG,
  335. 0, KEY_READ, &hSetupKey);
  336. if (ERROR_SUCCESS != dwReturn)
  337. goto Ret;
  338. cb = sizeof(fSystemSetupInProgress);
  339. dwReturn = RegQueryValueExW(
  340. hSetupKey,
  341. wszSYSTEM_SETUP_REG_VALUE,
  342. NULL,
  343. NULL,
  344. (PBYTE) &fSystemSetupInProgress,
  345. &cb);
  346. if (ERROR_SUCCESS != dwReturn)
  347. goto Ret;
  348. if (FALSE == fSystemSetupInProgress)
  349. dwReturn = CERT_E_UNTRUSTEDROOT;
  350. }
  351. Ret:
  352. if (hTestRootKey)
  353. RegCloseKey(hTestRootKey);
  354. if (hSetupKey)
  355. RegCloseKey(hSetupKey);
  356. return dwReturn;
  357. }
  358. // If found, returns ERROR_SUCCESS and sets *ppRootBlob to the PubKeyInfo blob.
  359. // Otherwise, returns appropriate error code and set *ppRootBlob to NULL.
  360. DWORD
  361. WINAPI
  362. I_MinCryptFindRootByName(
  363. IN PCRYPT_DER_BLOB pIssuerNameValueBlob,
  364. OUT PCRYPT_DER_BLOB *ppRootBlob
  365. )
  366. {
  367. DWORD i;
  368. DWORD dwStatus;
  369. BOOL fInGuiModeSetup = FALSE;
  370. *ppRootBlob = NULL;
  371. for (i = 0; i < ROOT_CNT; i++) {
  372. if (pIssuerNameValueBlob->cbData == RootTable[i].EncodedName.cbData &&
  373. 0 == memcmp(pIssuerNameValueBlob->pbData,
  374. RootTable[i].EncodedName.pbData,
  375. pIssuerNameValueBlob->cbData))
  376. {
  377. if (RootTable[i].fTestRoot)
  378. {
  379. dwStatus = I_CheckIsTestRootAllowed();
  380. if (ERROR_SUCCESS != dwStatus)
  381. return dwStatus;
  382. }
  383. *ppRootBlob = (PCRYPT_DER_BLOB) &RootTable[i].EncodedPubKeyInfo;
  384. return ERROR_SUCCESS;
  385. }
  386. }
  387. return CERT_E_UNTRUSTEDROOT;
  388. }
  389. // If found, returns ERROR_SUCCESS and sets *ppRootBlob to the PubKeyInfo blob.
  390. // Otherwise, returns appropriate error code and set *ppRootBlob to NULL.
  391. DWORD
  392. WINAPI
  393. I_MinCryptFindRootByKey(
  394. IN PCRYPT_DER_BLOB pSubjectPubKeyInfoBlob,
  395. OUT PCRYPT_DER_BLOB *ppRootBlob
  396. )
  397. {
  398. DWORD i;
  399. DWORD dwStatus;
  400. *ppRootBlob = NULL;
  401. for (i = 0; i < ROOT_CNT; i++) {
  402. if (pSubjectPubKeyInfoBlob->cbData ==
  403. RootTable[i].EncodedPubKeyInfo.cbData
  404. &&
  405. 0 == memcmp(pSubjectPubKeyInfoBlob->pbData,
  406. RootTable[i].EncodedPubKeyInfo.pbData,
  407. pSubjectPubKeyInfoBlob->cbData))
  408. {
  409. if (RootTable[i].fTestRoot)
  410. {
  411. dwStatus = I_CheckIsTestRootAllowed();
  412. if (ERROR_SUCCESS != dwStatus)
  413. return dwStatus;
  414. }
  415. *ppRootBlob = (PCRYPT_DER_BLOB) &RootTable[i].EncodedPubKeyInfo;
  416. return ERROR_SUCCESS;
  417. }
  418. }
  419. return CERT_E_UNTRUSTEDROOT;
  420. }
  421. // If found, returns pointer to rgCertBlob[MINASN1_CERT_BLOB_CNT].
  422. // Otherwise, returns NULL.
  423. PCRYPT_DER_BLOB
  424. WINAPI
  425. I_MinCryptFindIssuerCertificateByName(
  426. IN PCRYPT_DER_BLOB pIssuerNameValueBlob,
  427. IN DWORD cCert,
  428. IN CRYPT_DER_BLOB rgrgCertBlob[][MINASN1_CERT_BLOB_CNT]
  429. )
  430. {
  431. DWORD i;
  432. DWORD cbName = pIssuerNameValueBlob->cbData;
  433. const BYTE *pbName = pIssuerNameValueBlob->pbData;
  434. if (0 == cbName)
  435. return NULL;
  436. for (i = 0; i < cCert; i++) {
  437. if (cbName == rgrgCertBlob[i][MINASN1_CERT_SUBJECT_IDX].cbData &&
  438. 0 == memcmp(pbName,
  439. rgrgCertBlob[i][MINASN1_CERT_SUBJECT_IDX].pbData,
  440. cbName))
  441. return rgrgCertBlob[i];
  442. }
  443. return NULL;
  444. }
  445. //+-------------------------------------------------------------------------
  446. // Verifies a previously parsed X.509 Certificate.
  447. //
  448. // Assumes the ASN.1 encoded X.509 certificate was parsed via
  449. // MinAsn1ParseCertificate() and the set of potential issuer certificates
  450. // were parsed via one or more of:
  451. // - MinAsn1ParseCertificate()
  452. // - MinAsn1ParseSignedDataCertificates()
  453. // - MinAsn1ExtractParsedCertificatesFromSignedData()
  454. //
  455. // Iteratively finds the issuer certificate via its encoded name. The
  456. // public key in the issuer certificate is used to verify the subject
  457. // certificate's signature. This is repeated until finding a self signed
  458. // certificate or a baked in root identified by its encoded name.
  459. // For a self signed certificate, compares against the baked in root
  460. // public keys.
  461. //
  462. // If the certificate and its issuers were successfully verified to a
  463. // baked in root, ERROR_SUCCESS is returned. Otherwise, a nonzero error
  464. // code is returned.
  465. //--------------------------------------------------------------------------
  466. LONG
  467. WINAPI
  468. MinCryptVerifyCertificate(
  469. IN CRYPT_DER_BLOB rgSubjectCertBlob[MINASN1_CERT_BLOB_CNT],
  470. IN DWORD cIssuerCert,
  471. IN CRYPT_DER_BLOB rgrgIssuerCertBlob[][MINASN1_CERT_BLOB_CNT]
  472. )
  473. {
  474. LONG lErr;
  475. DWORD dwChainDepth = 0;
  476. PCRYPT_DER_BLOB rgSubject;
  477. BOOL fRoot = FALSE;
  478. rgSubject = rgSubjectCertBlob;
  479. while (!fRoot) {
  480. ALG_ID HashAlgId;
  481. BYTE rgbHash[MINCRYPT_MAX_HASH_LEN];
  482. DWORD cbHash;
  483. PCRYPT_DER_BLOB rgIssuer = NULL;
  484. PCRYPT_DER_BLOB pIssuerPubKeyInfo = NULL;
  485. // Hash the Subject's ToBeSigned bytes
  486. HashAlgId = MinCryptDecodeHashAlgorithmIdentifier(
  487. &rgSubject[MINASN1_CERT_SIGN_ALGID_IDX]);
  488. if (0 == HashAlgId)
  489. goto UnknownHashAlgId;
  490. lErr = MinCryptHashMemory(
  491. HashAlgId,
  492. 1, // cBlob,
  493. &rgSubject[MINASN1_CERT_TO_BE_SIGNED_IDX],
  494. rgbHash,
  495. &cbHash
  496. );
  497. if (ERROR_SUCCESS != lErr)
  498. goto ErrorReturn;
  499. // Get the public key to decrypt the signature
  500. // Check if SelfSigned
  501. if (rgSubject[MINASN1_CERT_ISSUER_IDX].cbData ==
  502. rgSubject[MINASN1_CERT_SUBJECT_IDX].cbData
  503. &&
  504. 0 == memcmp(rgSubject[MINASN1_CERT_ISSUER_IDX].pbData,
  505. rgSubject[MINASN1_CERT_SUBJECT_IDX].pbData,
  506. rgSubject[MINASN1_CERT_ISSUER_IDX].cbData)) {
  507. lErr = I_MinCryptFindRootByKey(
  508. &rgSubject[MINASN1_CERT_PUBKEY_INFO_IDX],
  509. &pIssuerPubKeyInfo);
  510. if (NULL == pIssuerPubKeyInfo)
  511. goto ErrorReturn;
  512. fRoot = TRUE;
  513. } else {
  514. // Check if the issuer is a root
  515. lErr = I_MinCryptFindRootByName(
  516. &rgSubject[MINASN1_CERT_ISSUER_IDX],
  517. &pIssuerPubKeyInfo);
  518. if (pIssuerPubKeyInfo)
  519. fRoot = TRUE;
  520. else {
  521. // If some other error code is set, then some
  522. // sort of unexpected system error occurred
  523. // and we should bail.
  524. if (CERT_E_UNTRUSTEDROOT != lErr)
  525. goto ErrorReturn;
  526. // Try to find the issuer from the input set of
  527. // certificates
  528. rgIssuer = I_MinCryptFindIssuerCertificateByName(
  529. &rgSubject[MINASN1_CERT_ISSUER_IDX],
  530. cIssuerCert,
  531. rgrgIssuerCertBlob
  532. );
  533. if (NULL == rgIssuer)
  534. goto PartialChain;
  535. pIssuerPubKeyInfo = &rgIssuer[MINASN1_CERT_PUBKEY_INFO_IDX];
  536. }
  537. }
  538. // Use the issuer or root's public key to decrypt and verify
  539. // the signature.
  540. lErr = MinCryptVerifySignedHash(
  541. HashAlgId,
  542. rgbHash,
  543. cbHash,
  544. &rgSubject[MINASN1_CERT_SIGNATURE_IDX],
  545. pIssuerPubKeyInfo
  546. );
  547. if (ERROR_SUCCESS != lErr)
  548. goto ErrorReturn;
  549. if (!fRoot) {
  550. assert(rgIssuer);
  551. dwChainDepth++;
  552. if (MAX_CHAIN_DEPTH < dwChainDepth)
  553. goto CyclicChain;
  554. rgSubject = rgIssuer;
  555. }
  556. }
  557. lErr = ERROR_SUCCESS;
  558. ErrorReturn:
  559. CommonReturn:
  560. return lErr;
  561. UnknownHashAlgId:
  562. lErr = CRYPT_E_UNKNOWN_ALGO;
  563. goto CommonReturn;
  564. CyclicChain:
  565. PartialChain:
  566. lErr = CERT_E_CHAINING;
  567. goto CommonReturn;
  568. }