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.

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