Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

x509.c 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <ipxe/list.h>
  25. #include <ipxe/base16.h>
  26. #include <ipxe/asn1.h>
  27. #include <ipxe/crypto.h>
  28. #include <ipxe/md5.h>
  29. #include <ipxe/sha1.h>
  30. #include <ipxe/sha256.h>
  31. #include <ipxe/rsa.h>
  32. #include <ipxe/rootcert.h>
  33. #include <ipxe/certstore.h>
  34. #include <ipxe/x509.h>
  35. #include <config/crypto.h>
  36. /** @file
  37. *
  38. * X.509 certificates
  39. *
  40. * The structure of X.509v3 certificates is documented in RFC 5280
  41. * section 4.1.
  42. */
  43. /* Disambiguate the various error causes */
  44. #define ENOTSUP_ALGORITHM \
  45. __einfo_error ( EINFO_ENOTSUP_ALGORITHM )
  46. #define EINFO_ENOTSUP_ALGORITHM \
  47. __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported algorithm" )
  48. #define ENOTSUP_EXTENSION \
  49. __einfo_error ( EINFO_ENOTSUP_EXTENSION )
  50. #define EINFO_ENOTSUP_EXTENSION \
  51. __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported extension" )
  52. #define EINVAL_ALGORITHM \
  53. __einfo_error ( EINFO_EINVAL_ALGORITHM )
  54. #define EINFO_EINVAL_ALGORITHM \
  55. __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid algorithm type" )
  56. #define EINVAL_ALGORITHM_MISMATCH \
  57. __einfo_error ( EINFO_EINVAL_ALGORITHM_MISMATCH )
  58. #define EINFO_EINVAL_ALGORITHM_MISMATCH \
  59. __einfo_uniqify ( EINFO_EINVAL, 0x04, "Signature algorithm mismatch" )
  60. #define EINVAL_PATH_LEN \
  61. __einfo_error ( EINFO_EINVAL_PATH_LEN )
  62. #define EINFO_EINVAL_PATH_LEN \
  63. __einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid pathLenConstraint" )
  64. #define EINVAL_VERSION \
  65. __einfo_error ( EINFO_EINVAL_VERSION )
  66. #define EINFO_EINVAL_VERSION \
  67. __einfo_uniqify ( EINFO_EINVAL, 0x06, "Invalid version" )
  68. #define EACCES_WRONG_ISSUER \
  69. __einfo_error ( EINFO_EACCES_WRONG_ISSUER )
  70. #define EINFO_EACCES_WRONG_ISSUER \
  71. __einfo_uniqify ( EINFO_EACCES, 0x01, "Wrong issuer" )
  72. #define EACCES_NOT_CA \
  73. __einfo_error ( EINFO_EACCES_NOT_CA )
  74. #define EINFO_EACCES_NOT_CA \
  75. __einfo_uniqify ( EINFO_EACCES, 0x02, "Not a CA certificate" )
  76. #define EACCES_KEY_USAGE \
  77. __einfo_error ( EINFO_EACCES_KEY_USAGE )
  78. #define EINFO_EACCES_KEY_USAGE \
  79. __einfo_uniqify ( EINFO_EACCES, 0x03, "Incorrect key usage" )
  80. #define EACCES_EXPIRED \
  81. __einfo_error ( EINFO_EACCES_EXPIRED )
  82. #define EINFO_EACCES_EXPIRED \
  83. __einfo_uniqify ( EINFO_EACCES, 0x04, "Expired (or not yet valid)" )
  84. #define EACCES_PATH_LEN \
  85. __einfo_error ( EINFO_EACCES_PATH_LEN )
  86. #define EINFO_EACCES_PATH_LEN \
  87. __einfo_uniqify ( EINFO_EACCES, 0x05, "Maximum path length exceeded" )
  88. #define EACCES_UNTRUSTED \
  89. __einfo_error ( EINFO_EACCES_UNTRUSTED )
  90. #define EINFO_EACCES_UNTRUSTED \
  91. __einfo_uniqify ( EINFO_EACCES, 0x06, "Untrusted root certificate" )
  92. #define EACCES_OUT_OF_ORDER \
  93. __einfo_error ( EINFO_EACCES_OUT_OF_ORDER )
  94. #define EINFO_EACCES_OUT_OF_ORDER \
  95. __einfo_uniqify ( EINFO_EACCES, 0x07, "Validation out of order" )
  96. #define EACCES_EMPTY \
  97. __einfo_error ( EINFO_EACCES_EMPTY )
  98. #define EINFO_EACCES_EMPTY \
  99. __einfo_uniqify ( EINFO_EACCES, 0x08, "Empty certificate chain" )
  100. #define EACCES_OCSP_REQUIRED \
  101. __einfo_error ( EINFO_EACCES_OCSP_REQUIRED )
  102. #define EINFO_EACCES_OCSP_REQUIRED \
  103. __einfo_uniqify ( EINFO_EACCES, 0x09, "OCSP check required" )
  104. #define EACCES_WRONG_NAME \
  105. __einfo_error ( EINFO_EACCES_WRONG_NAME )
  106. #define EINFO_EACCES_WRONG_NAME \
  107. __einfo_uniqify ( EINFO_EACCES, 0x0a, "Incorrect certificate name" )
  108. #define EACCES_USELESS \
  109. __einfo_error ( EINFO_EACCES_USELESS )
  110. #define EINFO_EACCES_USELESS \
  111. __einfo_uniqify ( EINFO_EACCES, 0x0b, "No usable certificates" )
  112. /**
  113. * Get X.509 certificate name (for debugging)
  114. *
  115. * @v cert X.509 certificate
  116. * @ret name Name (for debugging)
  117. */
  118. const char * x509_name ( struct x509_certificate *cert ) {
  119. struct asn1_cursor *common_name = &cert->subject.common_name;
  120. struct digest_algorithm *digest = &sha1_algorithm;
  121. static char buf[64];
  122. uint8_t fingerprint[ digest->digestsize ];
  123. size_t len;
  124. len = common_name->len;
  125. if ( len ) {
  126. /* Certificate has a commonName: use that */
  127. if ( len > ( sizeof ( buf ) - 1 /* NUL */ ) )
  128. len = ( sizeof ( buf ) - 1 /* NUL */ );
  129. memcpy ( buf, common_name->data, len );
  130. buf[len] = '\0';
  131. } else {
  132. /* Certificate has no commonName: use SHA-1 fingerprint */
  133. x509_fingerprint ( cert, digest, fingerprint );
  134. base16_encode ( fingerprint, sizeof ( fingerprint ), buf );
  135. }
  136. return buf;
  137. }
  138. /** "commonName" object identifier */
  139. static uint8_t oid_common_name[] = { ASN1_OID_COMMON_NAME };
  140. /** "commonName" object identifier cursor */
  141. static struct asn1_cursor oid_common_name_cursor =
  142. ASN1_OID_CURSOR ( oid_common_name );
  143. /**
  144. * Parse X.509 certificate version
  145. *
  146. * @v cert X.509 certificate
  147. * @v raw ASN.1 cursor
  148. * @ret rc Return status code
  149. */
  150. static int x509_parse_version ( struct x509_certificate *cert,
  151. const struct asn1_cursor *raw ) {
  152. struct asn1_cursor cursor;
  153. int version;
  154. int rc;
  155. /* Enter version */
  156. memcpy ( &cursor, raw, sizeof ( cursor ) );
  157. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 0 ) );
  158. /* Parse integer */
  159. if ( ( rc = asn1_integer ( &cursor, &version ) ) != 0 ) {
  160. DBGC ( cert, "X509 %p cannot parse version: %s\n",
  161. cert, strerror ( rc ) );
  162. DBGC_HDA ( cert, 0, raw->data, raw->len );
  163. return rc;
  164. }
  165. /* Sanity check */
  166. if ( version < 0 ) {
  167. DBGC ( cert, "X509 %p invalid version %d\n", cert, version );
  168. DBGC_HDA ( cert, 0, raw->data, raw->len );
  169. return -EINVAL_VERSION;
  170. }
  171. /* Record version */
  172. cert->version = version;
  173. DBGC2 ( cert, "X509 %p is a version %d certificate\n",
  174. cert, ( cert->version + 1 ) );
  175. return 0;
  176. }
  177. /**
  178. * Parse X.509 certificate serial number
  179. *
  180. * @v cert X.509 certificate
  181. * @v raw ASN.1 cursor
  182. * @ret rc Return status code
  183. */
  184. static int x509_parse_serial ( struct x509_certificate *cert,
  185. const struct asn1_cursor *raw ) {
  186. struct x509_serial *serial = &cert->serial;
  187. int rc;
  188. /* Record raw serial number */
  189. memcpy ( &serial->raw, raw, sizeof ( serial->raw ) );
  190. if ( ( rc = asn1_shrink ( &serial->raw, ASN1_INTEGER ) ) != 0 ) {
  191. DBGC ( cert, "X509 %p cannot shrink serialNumber: %s\n",
  192. cert, strerror ( rc ) );
  193. return rc;
  194. }
  195. DBGC2 ( cert, "X509 %p issuer is:\n", cert );
  196. DBGC2_HDA ( cert, 0, serial->raw.data, serial->raw.len );
  197. return 0;
  198. }
  199. /**
  200. * Parse X.509 certificate issuer
  201. *
  202. * @v cert X.509 certificate
  203. * @v raw ASN.1 cursor
  204. * @ret rc Return status code
  205. */
  206. static int x509_parse_issuer ( struct x509_certificate *cert,
  207. const struct asn1_cursor *raw ) {
  208. struct x509_issuer *issuer = &cert->issuer;
  209. int rc;
  210. /* Record raw issuer */
  211. memcpy ( &issuer->raw, raw, sizeof ( issuer->raw ) );
  212. if ( ( rc = asn1_shrink ( &issuer->raw, ASN1_SEQUENCE ) ) != 0 ) {
  213. DBGC ( cert, "X509 %p cannot shrink issuer: %s\n",
  214. cert, strerror ( rc ) );
  215. return rc;
  216. }
  217. DBGC2 ( cert, "X509 %p issuer is:\n", cert );
  218. DBGC2_HDA ( cert, 0, issuer->raw.data, issuer->raw.len );
  219. return 0;
  220. }
  221. /**
  222. * Parse X.509 certificate validity
  223. *
  224. * @v cert X.509 certificate
  225. * @v raw ASN.1 cursor
  226. * @ret rc Return status code
  227. */
  228. static int x509_parse_validity ( struct x509_certificate *cert,
  229. const struct asn1_cursor *raw ) {
  230. struct x509_validity *validity = &cert->validity;
  231. struct x509_time *not_before = &validity->not_before;
  232. struct x509_time *not_after = &validity->not_after;
  233. struct asn1_cursor cursor;
  234. int rc;
  235. /* Enter validity */
  236. memcpy ( &cursor, raw, sizeof ( cursor ) );
  237. asn1_enter ( &cursor, ASN1_SEQUENCE );
  238. /* Parse notBefore */
  239. if ( ( rc = asn1_generalized_time ( &cursor,
  240. &not_before->time ) ) != 0 ) {
  241. DBGC ( cert, "X509 %p cannot parse notBefore: %s\n",
  242. cert, strerror ( rc ) );
  243. return rc;
  244. }
  245. DBGC2 ( cert, "X509 %p valid from time %lld\n",
  246. cert, not_before->time );
  247. asn1_skip_any ( &cursor );
  248. /* Parse notAfter */
  249. if ( ( rc = asn1_generalized_time ( &cursor,
  250. &not_after->time ) ) != 0 ) {
  251. DBGC ( cert, "X509 %p cannot parse notAfter: %s\n",
  252. cert, strerror ( rc ) );
  253. return rc;
  254. }
  255. DBGC2 ( cert, "X509 %p valid until time %lld\n",
  256. cert, not_after->time );
  257. return 0;
  258. }
  259. /**
  260. * Parse X.509 certificate common name
  261. *
  262. * @v cert X.509 certificate
  263. * @v raw ASN.1 cursor
  264. * @ret rc Return status code
  265. */
  266. static int x509_parse_common_name ( struct x509_certificate *cert,
  267. const struct asn1_cursor *raw ) {
  268. struct asn1_cursor cursor;
  269. struct asn1_cursor oid_cursor;
  270. struct asn1_cursor name_cursor;
  271. int rc;
  272. /* Enter name */
  273. memcpy ( &cursor, raw, sizeof ( cursor ) );
  274. asn1_enter ( &cursor, ASN1_SEQUENCE );
  275. /* Scan through name list */
  276. for ( ; cursor.len ; asn1_skip_any ( &cursor ) ) {
  277. /* Check for "commonName" OID */
  278. memcpy ( &oid_cursor, &cursor, sizeof ( oid_cursor ) );
  279. asn1_enter ( &oid_cursor, ASN1_SET );
  280. asn1_enter ( &oid_cursor, ASN1_SEQUENCE );
  281. memcpy ( &name_cursor, &oid_cursor, sizeof ( name_cursor ) );
  282. asn1_enter ( &oid_cursor, ASN1_OID );
  283. if ( asn1_compare ( &oid_common_name_cursor, &oid_cursor ) != 0)
  284. continue;
  285. asn1_skip_any ( &name_cursor );
  286. if ( ( rc = asn1_enter_any ( &name_cursor ) ) != 0 ) {
  287. DBGC ( cert, "X509 %p cannot locate name:\n", cert );
  288. DBGC_HDA ( cert, 0, raw->data, raw->len );
  289. return rc;
  290. }
  291. /* Record common name */
  292. memcpy ( &cert->subject.common_name, &name_cursor,
  293. sizeof ( cert->subject.common_name ) );
  294. return 0;
  295. }
  296. /* Certificates may not have a commonName */
  297. DBGC2 ( cert, "X509 %p no commonName found:\n", cert );
  298. return 0;
  299. }
  300. /**
  301. * Parse X.509 certificate subject
  302. *
  303. * @v cert X.509 certificate
  304. * @v raw ASN.1 cursor
  305. * @ret rc Return status code
  306. */
  307. static int x509_parse_subject ( struct x509_certificate *cert,
  308. const struct asn1_cursor *raw ) {
  309. struct x509_subject *subject = &cert->subject;
  310. int rc;
  311. /* Record raw subject */
  312. memcpy ( &subject->raw, raw, sizeof ( subject->raw ) );
  313. asn1_shrink_any ( &subject->raw );
  314. DBGC2 ( cert, "X509 %p subject is:\n", cert );
  315. DBGC2_HDA ( cert, 0, subject->raw.data, subject->raw.len );
  316. /* Parse common name */
  317. if ( ( rc = x509_parse_common_name ( cert, raw ) ) != 0 )
  318. return rc;
  319. DBGC2 ( cert, "X509 %p common name is \"%s\":\n", cert,
  320. x509_name ( cert ) );
  321. return 0;
  322. }
  323. /**
  324. * Parse X.509 certificate public key information
  325. *
  326. * @v cert X.509 certificate
  327. * @v raw ASN.1 cursor
  328. * @ret rc Return status code
  329. */
  330. static int x509_parse_public_key ( struct x509_certificate *cert,
  331. const struct asn1_cursor *raw ) {
  332. struct x509_public_key *public_key = &cert->subject.public_key;
  333. struct asn1_algorithm **algorithm = &public_key->algorithm;
  334. struct asn1_bit_string *raw_bits = &public_key->raw_bits;
  335. struct asn1_cursor cursor;
  336. int rc;
  337. /* Record raw subjectPublicKeyInfo */
  338. memcpy ( &cursor, raw, sizeof ( cursor ) );
  339. asn1_shrink_any ( &cursor );
  340. memcpy ( &public_key->raw, &cursor, sizeof ( public_key->raw ) );
  341. DBGC2 ( cert, "X509 %p public key is:\n", cert );
  342. DBGC2_HDA ( cert, 0, public_key->raw.data, public_key->raw.len );
  343. /* Enter subjectPublicKeyInfo */
  344. asn1_enter ( &cursor, ASN1_SEQUENCE );
  345. /* Parse algorithm */
  346. if ( ( rc = asn1_pubkey_algorithm ( &cursor, algorithm ) ) != 0 ) {
  347. DBGC ( cert, "X509 %p could not parse public key algorithm: "
  348. "%s\n", cert, strerror ( rc ) );
  349. return rc;
  350. }
  351. DBGC2 ( cert, "X509 %p public key algorithm is %s\n",
  352. cert, (*algorithm)->name );
  353. asn1_skip_any ( &cursor );
  354. /* Parse bit string */
  355. if ( ( rc = asn1_bit_string ( &cursor, raw_bits ) ) != 0 ) {
  356. DBGC ( cert, "X509 %p could not parse public key bits: %s\n",
  357. cert, strerror ( rc ) );
  358. return rc;
  359. }
  360. return 0;
  361. }
  362. /**
  363. * Parse X.509 certificate basic constraints
  364. *
  365. * @v cert X.509 certificate
  366. * @v raw ASN.1 cursor
  367. * @ret rc Return status code
  368. */
  369. static int x509_parse_basic_constraints ( struct x509_certificate *cert,
  370. const struct asn1_cursor *raw ) {
  371. struct x509_basic_constraints *basic = &cert->extensions.basic;
  372. struct asn1_cursor cursor;
  373. int ca = 0;
  374. int path_len;
  375. int rc;
  376. /* Enter basicConstraints */
  377. memcpy ( &cursor, raw, sizeof ( cursor ) );
  378. asn1_enter ( &cursor, ASN1_SEQUENCE );
  379. /* Parse "cA", if present */
  380. if ( asn1_type ( &cursor ) == ASN1_BOOLEAN ) {
  381. ca = asn1_boolean ( &cursor );
  382. if ( ca < 0 ) {
  383. rc = ca;
  384. DBGC ( cert, "X509 %p cannot parse cA: %s\n",
  385. cert, strerror ( rc ) );
  386. DBGC_HDA ( cert, 0, raw->data, raw->len );
  387. return rc;
  388. }
  389. asn1_skip_any ( &cursor );
  390. }
  391. basic->ca = ca;
  392. DBGC2 ( cert, "X509 %p is %sa CA certificate\n",
  393. cert, ( basic->ca ? "" : "not " ) );
  394. /* Ignore everything else unless "cA" is true */
  395. if ( ! ca )
  396. return 0;
  397. /* Parse "pathLenConstraint", if present and applicable */
  398. basic->path_len = X509_PATH_LEN_UNLIMITED;
  399. if ( asn1_type ( &cursor ) == ASN1_INTEGER ) {
  400. if ( ( rc = asn1_integer ( &cursor, &path_len ) ) != 0 ) {
  401. DBGC ( cert, "X509 %p cannot parse pathLenConstraint: "
  402. "%s\n", cert, strerror ( rc ) );
  403. DBGC_HDA ( cert, 0, raw->data, raw->len );
  404. return rc;
  405. }
  406. if ( path_len < 0 ) {
  407. DBGC ( cert, "X509 %p invalid pathLenConstraint %d\n",
  408. cert, path_len );
  409. DBGC_HDA ( cert, 0, raw->data, raw->len );
  410. return -EINVAL;
  411. }
  412. basic->path_len = path_len;
  413. DBGC2 ( cert, "X509 %p path length constraint is %u\n",
  414. cert, basic->path_len );
  415. }
  416. return 0;
  417. }
  418. /**
  419. * Parse X.509 certificate key usage
  420. *
  421. * @v cert X.509 certificate
  422. * @v raw ASN.1 cursor
  423. * @ret rc Return status code
  424. */
  425. static int x509_parse_key_usage ( struct x509_certificate *cert,
  426. const struct asn1_cursor *raw ) {
  427. struct x509_key_usage *usage = &cert->extensions.usage;
  428. struct asn1_bit_string bit_string;
  429. const uint8_t *bytes;
  430. size_t len;
  431. unsigned int i;
  432. int rc;
  433. /* Mark extension as present */
  434. usage->present = 1;
  435. /* Parse bit string */
  436. if ( ( rc = asn1_bit_string ( raw, &bit_string ) ) != 0 ) {
  437. DBGC ( cert, "X509 %p could not parse key usage: %s\n",
  438. cert, strerror ( rc ) );
  439. return rc;
  440. }
  441. /* Parse key usage bits */
  442. bytes = bit_string.data;
  443. len = bit_string.len;
  444. if ( len > sizeof ( usage->bits ) )
  445. len = sizeof ( usage->bits );
  446. for ( i = 0 ; i < len ; i++ ) {
  447. usage->bits |= ( *(bytes++) << ( 8 * i ) );
  448. }
  449. DBGC2 ( cert, "X509 %p key usage is %08x\n", cert, usage->bits );
  450. return 0;
  451. }
  452. /** "id-kp-codeSigning" object identifier */
  453. static uint8_t oid_code_signing[] = { ASN1_OID_CODESIGNING };
  454. /** "id-kp-OCSPSigning" object identifier */
  455. static uint8_t oid_ocsp_signing[] = { ASN1_OID_OCSPSIGNING };
  456. /** Supported key purposes */
  457. static struct x509_key_purpose x509_key_purposes[] = {
  458. {
  459. .name = "codeSigning",
  460. .bits = X509_CODE_SIGNING,
  461. .oid = ASN1_OID_CURSOR ( oid_code_signing ),
  462. },
  463. {
  464. .name = "ocspSigning",
  465. .bits = X509_OCSP_SIGNING,
  466. .oid = ASN1_OID_CURSOR ( oid_ocsp_signing ),
  467. },
  468. };
  469. /**
  470. * Parse X.509 certificate key purpose identifier
  471. *
  472. * @v cert X.509 certificate
  473. * @v raw ASN.1 cursor
  474. * @ret rc Return status code
  475. */
  476. static int x509_parse_key_purpose ( struct x509_certificate *cert,
  477. const struct asn1_cursor *raw ) {
  478. struct x509_extended_key_usage *ext_usage = &cert->extensions.ext_usage;
  479. struct x509_key_purpose *purpose;
  480. struct asn1_cursor cursor;
  481. unsigned int i;
  482. int rc;
  483. /* Enter keyPurposeId */
  484. memcpy ( &cursor, raw, sizeof ( cursor ) );
  485. if ( ( rc = asn1_enter ( &cursor, ASN1_OID ) ) != 0 ) {
  486. DBGC ( cert, "X509 %p invalid keyPurposeId:\n", cert );
  487. DBGC_HDA ( cert, 0, raw->data, raw->len );
  488. return rc;
  489. }
  490. /* Identify key purpose */
  491. for ( i = 0 ; i < ( sizeof ( x509_key_purposes ) /
  492. sizeof ( x509_key_purposes[0] ) ) ; i++ ) {
  493. purpose = &x509_key_purposes[i];
  494. if ( asn1_compare ( &cursor, &purpose->oid ) == 0 ) {
  495. DBGC2 ( cert, "X509 %p has key purpose %s\n",
  496. cert, purpose->name );
  497. ext_usage->bits |= purpose->bits;
  498. return 0;
  499. }
  500. }
  501. /* Ignore unrecognised key purposes */
  502. return 0;
  503. }
  504. /**
  505. * Parse X.509 certificate extended key usage
  506. *
  507. * @v cert X.509 certificate
  508. * @v raw ASN.1 cursor
  509. * @ret rc Return status code
  510. */
  511. static int x509_parse_extended_key_usage ( struct x509_certificate *cert,
  512. const struct asn1_cursor *raw ) {
  513. struct asn1_cursor cursor;
  514. int rc;
  515. /* Enter extKeyUsage */
  516. memcpy ( &cursor, raw, sizeof ( cursor ) );
  517. asn1_enter ( &cursor, ASN1_SEQUENCE );
  518. /* Parse each extended key usage in turn */
  519. while ( cursor.len ) {
  520. if ( ( rc = x509_parse_key_purpose ( cert, &cursor ) ) != 0 )
  521. return rc;
  522. asn1_skip_any ( &cursor );
  523. }
  524. return 0;
  525. }
  526. /**
  527. * Parse X.509 certificate OCSP access method
  528. *
  529. * @v cert X.509 certificate
  530. * @v raw ASN.1 cursor
  531. * @ret rc Return status code
  532. */
  533. static int x509_parse_ocsp ( struct x509_certificate *cert,
  534. const struct asn1_cursor *raw ) {
  535. struct x509_ocsp_responder *ocsp = &cert->extensions.auth_info.ocsp;
  536. struct asn1_cursor *uri = &ocsp->uri;
  537. int rc;
  538. /* Enter accessLocation */
  539. memcpy ( uri, raw, sizeof ( *uri ) );
  540. if ( ( rc = asn1_enter ( uri, X509_GENERAL_NAME_URI ) ) != 0 ) {
  541. DBGC ( cert, "X509 %p OCSP does not contain "
  542. "uniformResourceIdentifier:\n", cert );
  543. DBGC_HDA ( cert, 0, raw->data, raw->len );
  544. return rc;
  545. }
  546. DBGC2 ( cert, "X509 %p OCSP URI is:\n", cert );
  547. DBGC2_HDA ( cert, 0, uri->data, uri->len );
  548. return 0;
  549. }
  550. /** "id-ad-ocsp" object identifier */
  551. static uint8_t oid_ad_ocsp[] = { ASN1_OID_OCSP };
  552. /** Supported access methods */
  553. static struct x509_access_method x509_access_methods[] = {
  554. {
  555. .name = "OCSP",
  556. .oid = ASN1_OID_CURSOR ( oid_ad_ocsp ),
  557. .parse = x509_parse_ocsp,
  558. },
  559. };
  560. /**
  561. * Identify X.509 access method by OID
  562. *
  563. * @v oid OID
  564. * @ret method Access method, or NULL
  565. */
  566. static struct x509_access_method *
  567. x509_find_access_method ( const struct asn1_cursor *oid ) {
  568. struct x509_access_method *method;
  569. unsigned int i;
  570. for ( i = 0 ; i < ( sizeof ( x509_access_methods ) /
  571. sizeof ( x509_access_methods[0] ) ) ; i++ ) {
  572. method = &x509_access_methods[i];
  573. if ( asn1_compare ( &method->oid, oid ) == 0 )
  574. return method;
  575. }
  576. return NULL;
  577. }
  578. /**
  579. * Parse X.509 certificate access description
  580. *
  581. * @v cert X.509 certificate
  582. * @v raw ASN.1 cursor
  583. * @ret rc Return status code
  584. */
  585. static int x509_parse_access_description ( struct x509_certificate *cert,
  586. const struct asn1_cursor *raw ) {
  587. struct asn1_cursor cursor;
  588. struct asn1_cursor subcursor;
  589. struct x509_access_method *method;
  590. int rc;
  591. /* Enter keyPurposeId */
  592. memcpy ( &cursor, raw, sizeof ( cursor ) );
  593. asn1_enter ( &cursor, ASN1_SEQUENCE );
  594. /* Try to identify access method */
  595. memcpy ( &subcursor, &cursor, sizeof ( subcursor ) );
  596. asn1_enter ( &subcursor, ASN1_OID );
  597. method = x509_find_access_method ( &subcursor );
  598. asn1_skip_any ( &cursor );
  599. DBGC2 ( cert, "X509 %p found access method %s\n",
  600. cert, ( method ? method->name : "<unknown>" ) );
  601. /* Parse access location, if applicable */
  602. if ( method && ( ( rc = method->parse ( cert, &cursor ) ) != 0 ) )
  603. return rc;
  604. return 0;
  605. }
  606. /**
  607. * Parse X.509 certificate authority information access
  608. *
  609. * @v cert X.509 certificate
  610. * @v raw ASN.1 cursor
  611. * @ret rc Return status code
  612. */
  613. static int x509_parse_authority_info_access ( struct x509_certificate *cert,
  614. const struct asn1_cursor *raw ) {
  615. struct asn1_cursor cursor;
  616. int rc;
  617. /* Enter authorityInfoAccess */
  618. memcpy ( &cursor, raw, sizeof ( cursor ) );
  619. asn1_enter ( &cursor, ASN1_SEQUENCE );
  620. /* Parse each access description in turn */
  621. while ( cursor.len ) {
  622. if ( ( rc = x509_parse_access_description ( cert,
  623. &cursor ) ) != 0 )
  624. return rc;
  625. asn1_skip_any ( &cursor );
  626. }
  627. return 0;
  628. }
  629. /**
  630. * Parse X.509 certificate subject alternative name
  631. *
  632. * @v cert X.509 certificate
  633. * @v raw ASN.1 cursor
  634. * @ret rc Return status code
  635. */
  636. static int x509_parse_subject_alt_name ( struct x509_certificate *cert,
  637. const struct asn1_cursor *raw ) {
  638. struct x509_subject_alt_name *alt_name = &cert->extensions.alt_name;
  639. struct asn1_cursor *names = &alt_name->names;
  640. int rc;
  641. /* Enter subjectAltName */
  642. memcpy ( names, raw, sizeof ( *names ) );
  643. if ( ( rc = asn1_enter ( names, ASN1_SEQUENCE ) ) != 0 ) {
  644. DBGC ( cert, "X509 %p invalid subjectAltName: %s\n",
  645. cert, strerror ( rc ) );
  646. DBGC_HDA ( cert, 0, raw->data, raw->len );
  647. return rc;
  648. }
  649. DBGC2 ( cert, "X509 %p has subjectAltName:\n", cert );
  650. DBGC2_HDA ( cert, 0, names->data, names->len );
  651. return 0;
  652. }
  653. /** "id-ce-basicConstraints" object identifier */
  654. static uint8_t oid_ce_basic_constraints[] =
  655. { ASN1_OID_BASICCONSTRAINTS };
  656. /** "id-ce-keyUsage" object identifier */
  657. static uint8_t oid_ce_key_usage[] =
  658. { ASN1_OID_KEYUSAGE };
  659. /** "id-ce-extKeyUsage" object identifier */
  660. static uint8_t oid_ce_ext_key_usage[] =
  661. { ASN1_OID_EXTKEYUSAGE };
  662. /** "id-pe-authorityInfoAccess" object identifier */
  663. static uint8_t oid_pe_authority_info_access[] =
  664. { ASN1_OID_AUTHORITYINFOACCESS };
  665. /** "id-ce-subjectAltName" object identifier */
  666. static uint8_t oid_ce_subject_alt_name[] =
  667. { ASN1_OID_SUBJECTALTNAME };
  668. /** Supported certificate extensions */
  669. static struct x509_extension x509_extensions[] = {
  670. {
  671. .name = "basicConstraints",
  672. .oid = ASN1_OID_CURSOR ( oid_ce_basic_constraints ),
  673. .parse = x509_parse_basic_constraints,
  674. },
  675. {
  676. .name = "keyUsage",
  677. .oid = ASN1_OID_CURSOR ( oid_ce_key_usage ),
  678. .parse = x509_parse_key_usage,
  679. },
  680. {
  681. .name = "extKeyUsage",
  682. .oid = ASN1_OID_CURSOR ( oid_ce_ext_key_usage ),
  683. .parse = x509_parse_extended_key_usage,
  684. },
  685. {
  686. .name = "authorityInfoAccess",
  687. .oid = ASN1_OID_CURSOR ( oid_pe_authority_info_access ),
  688. .parse = x509_parse_authority_info_access,
  689. },
  690. {
  691. .name = "subjectAltName",
  692. .oid = ASN1_OID_CURSOR ( oid_ce_subject_alt_name ),
  693. .parse = x509_parse_subject_alt_name,
  694. },
  695. };
  696. /**
  697. * Identify X.509 extension by OID
  698. *
  699. * @v oid OID
  700. * @ret extension Extension, or NULL
  701. */
  702. static struct x509_extension *
  703. x509_find_extension ( const struct asn1_cursor *oid ) {
  704. struct x509_extension *extension;
  705. unsigned int i;
  706. for ( i = 0 ; i < ( sizeof ( x509_extensions ) /
  707. sizeof ( x509_extensions[0] ) ) ; i++ ) {
  708. extension = &x509_extensions[i];
  709. if ( asn1_compare ( &extension->oid, oid ) == 0 )
  710. return extension;
  711. }
  712. return NULL;
  713. }
  714. /**
  715. * Parse X.509 certificate extension
  716. *
  717. * @v cert X.509 certificate
  718. * @v raw ASN.1 cursor
  719. * @ret rc Return status code
  720. */
  721. static int x509_parse_extension ( struct x509_certificate *cert,
  722. const struct asn1_cursor *raw ) {
  723. struct asn1_cursor cursor;
  724. struct asn1_cursor subcursor;
  725. struct x509_extension *extension;
  726. int is_critical = 0;
  727. int rc;
  728. /* Enter extension */
  729. memcpy ( &cursor, raw, sizeof ( cursor ) );
  730. asn1_enter ( &cursor, ASN1_SEQUENCE );
  731. /* Try to identify extension */
  732. memcpy ( &subcursor, &cursor, sizeof ( subcursor ) );
  733. asn1_enter ( &subcursor, ASN1_OID );
  734. extension = x509_find_extension ( &subcursor );
  735. asn1_skip_any ( &cursor );
  736. DBGC2 ( cert, "X509 %p found extension %s\n",
  737. cert, ( extension ? extension->name : "<unknown>" ) );
  738. /* Identify criticality */
  739. if ( asn1_type ( &cursor ) == ASN1_BOOLEAN ) {
  740. is_critical = asn1_boolean ( &cursor );
  741. if ( is_critical < 0 ) {
  742. rc = is_critical;
  743. DBGC ( cert, "X509 %p cannot parse extension "
  744. "criticality: %s\n", cert, strerror ( rc ) );
  745. DBGC_HDA ( cert, 0, raw->data, raw->len );
  746. return rc;
  747. }
  748. asn1_skip_any ( &cursor );
  749. }
  750. /* Handle unknown extensions */
  751. if ( ! extension ) {
  752. if ( is_critical ) {
  753. /* Fail if we cannot handle a critical extension */
  754. DBGC ( cert, "X509 %p cannot handle critical "
  755. "extension:\n", cert );
  756. DBGC_HDA ( cert, 0, raw->data, raw->len );
  757. return -ENOTSUP_EXTENSION;
  758. } else {
  759. /* Ignore unknown non-critical extensions */
  760. return 0;
  761. }
  762. };
  763. /* Extract extnValue */
  764. if ( ( rc = asn1_enter ( &cursor, ASN1_OCTET_STRING ) ) != 0 ) {
  765. DBGC ( cert, "X509 %p extension missing extnValue:\n", cert );
  766. DBGC_HDA ( cert, 0, raw->data, raw->len );
  767. return rc;
  768. }
  769. /* Parse extension */
  770. if ( ( rc = extension->parse ( cert, &cursor ) ) != 0 )
  771. return rc;
  772. return 0;
  773. }
  774. /**
  775. * Parse X.509 certificate extensions, if present
  776. *
  777. * @v cert X.509 certificate
  778. * @v raw ASN.1 cursor
  779. * @ret rc Return status code
  780. */
  781. static int x509_parse_extensions ( struct x509_certificate *cert,
  782. const struct asn1_cursor *raw ) {
  783. struct asn1_cursor cursor;
  784. int rc;
  785. /* Enter extensions, if present */
  786. memcpy ( &cursor, raw, sizeof ( cursor ) );
  787. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 3 ) );
  788. asn1_enter ( &cursor, ASN1_SEQUENCE );
  789. /* Parse each extension in turn */
  790. while ( cursor.len ) {
  791. if ( ( rc = x509_parse_extension ( cert, &cursor ) ) != 0 )
  792. return rc;
  793. asn1_skip_any ( &cursor );
  794. }
  795. return 0;
  796. }
  797. /**
  798. * Parse X.509 certificate tbsCertificate
  799. *
  800. * @v cert X.509 certificate
  801. * @v raw ASN.1 cursor
  802. * @ret rc Return status code
  803. */
  804. static int x509_parse_tbscertificate ( struct x509_certificate *cert,
  805. const struct asn1_cursor *raw ) {
  806. struct asn1_algorithm **algorithm = &cert->signature_algorithm;
  807. struct asn1_cursor cursor;
  808. int rc;
  809. /* Record raw tbsCertificate */
  810. memcpy ( &cursor, raw, sizeof ( cursor ) );
  811. asn1_shrink_any ( &cursor );
  812. memcpy ( &cert->tbs, &cursor, sizeof ( cert->tbs ) );
  813. /* Enter tbsCertificate */
  814. asn1_enter ( &cursor, ASN1_SEQUENCE );
  815. /* Parse version, if present */
  816. if ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) {
  817. if ( ( rc = x509_parse_version ( cert, &cursor ) ) != 0 )
  818. return rc;
  819. asn1_skip_any ( &cursor );
  820. }
  821. /* Parse serialNumber */
  822. if ( ( rc = x509_parse_serial ( cert, &cursor ) ) != 0 )
  823. return rc;
  824. asn1_skip_any ( &cursor );
  825. /* Parse signature */
  826. if ( ( rc = asn1_signature_algorithm ( &cursor, algorithm ) ) != 0 ) {
  827. DBGC ( cert, "X509 %p could not parse signature algorithm: "
  828. "%s\n", cert, strerror ( rc ) );
  829. return rc;
  830. }
  831. DBGC2 ( cert, "X509 %p tbsCertificate signature algorithm is %s\n",
  832. cert, (*algorithm)->name );
  833. asn1_skip_any ( &cursor );
  834. /* Parse issuer */
  835. if ( ( rc = x509_parse_issuer ( cert, &cursor ) ) != 0 )
  836. return rc;
  837. asn1_skip_any ( &cursor );
  838. /* Parse validity */
  839. if ( ( rc = x509_parse_validity ( cert, &cursor ) ) != 0 )
  840. return rc;
  841. asn1_skip_any ( &cursor );
  842. /* Parse subject */
  843. if ( ( rc = x509_parse_subject ( cert, &cursor ) ) != 0 )
  844. return rc;
  845. asn1_skip_any ( &cursor );
  846. /* Parse subjectPublicKeyInfo */
  847. if ( ( rc = x509_parse_public_key ( cert, &cursor ) ) != 0 )
  848. return rc;
  849. asn1_skip_any ( &cursor );
  850. /* Parse extensions, if present */
  851. if ( ( rc = x509_parse_extensions ( cert, &cursor ) ) != 0 )
  852. return rc;
  853. return 0;
  854. }
  855. /**
  856. * Parse X.509 certificate from ASN.1 data
  857. *
  858. * @v cert X.509 certificate
  859. * @v raw ASN.1 cursor
  860. * @ret rc Return status code
  861. */
  862. int x509_parse ( struct x509_certificate *cert,
  863. const struct asn1_cursor *raw ) {
  864. struct x509_signature *signature = &cert->signature;
  865. struct asn1_algorithm **signature_algorithm = &signature->algorithm;
  866. struct asn1_bit_string *signature_value = &signature->value;
  867. struct asn1_cursor cursor;
  868. int rc;
  869. /* Record raw certificate */
  870. memcpy ( &cursor, raw, sizeof ( cursor ) );
  871. memcpy ( &cert->raw, &cursor, sizeof ( cert->raw ) );
  872. /* Enter certificate */
  873. asn1_enter ( &cursor, ASN1_SEQUENCE );
  874. /* Parse tbsCertificate */
  875. if ( ( rc = x509_parse_tbscertificate ( cert, &cursor ) ) != 0 )
  876. return rc;
  877. asn1_skip_any ( &cursor );
  878. /* Parse signatureAlgorithm */
  879. if ( ( rc = asn1_signature_algorithm ( &cursor,
  880. signature_algorithm ) ) != 0 ) {
  881. DBGC ( cert, "X509 %p could not parse signature algorithm: "
  882. "%s\n", cert, strerror ( rc ) );
  883. return rc;
  884. }
  885. DBGC2 ( cert, "X509 %p signatureAlgorithm is %s\n",
  886. cert, (*signature_algorithm)->name );
  887. asn1_skip_any ( &cursor );
  888. /* Parse signatureValue */
  889. if ( ( rc = asn1_integral_bit_string ( &cursor,
  890. signature_value ) ) != 0 ) {
  891. DBGC ( cert, "X509 %p could not parse signature value: %s\n",
  892. cert, strerror ( rc ) );
  893. return rc;
  894. }
  895. DBGC2 ( cert, "X509 %p signatureValue is:\n", cert );
  896. DBGC2_HDA ( cert, 0, signature_value->data, signature_value->len );
  897. /* Check that algorithm in tbsCertificate matches algorithm in
  898. * signature
  899. */
  900. if ( signature->algorithm != (*signature_algorithm) ) {
  901. DBGC ( cert, "X509 %p signature algorithm %s does not match "
  902. "signatureAlgorithm %s\n",
  903. cert, signature->algorithm->name,
  904. (*signature_algorithm)->name );
  905. return -EINVAL_ALGORITHM_MISMATCH;
  906. }
  907. return 0;
  908. }
  909. /**
  910. * Create X.509 certificate
  911. *
  912. * @v data Raw certificate data
  913. * @v len Length of raw data
  914. * @ret cert X.509 certificate
  915. * @ret rc Return status code
  916. *
  917. * On success, the caller holds a reference to the X.509 certificate,
  918. * and is responsible for ultimately calling x509_put().
  919. */
  920. int x509_certificate ( const void *data, size_t len,
  921. struct x509_certificate **cert ) {
  922. struct asn1_cursor cursor;
  923. void *raw;
  924. int rc;
  925. /* Initialise cursor */
  926. cursor.data = data;
  927. cursor.len = len;
  928. asn1_shrink_any ( &cursor );
  929. /* Return stored certificate, if present */
  930. if ( ( *cert = certstore_find ( &cursor ) ) != NULL ) {
  931. /* Add caller's reference */
  932. x509_get ( *cert );
  933. return 0;
  934. }
  935. /* Allocate and initialise certificate */
  936. *cert = zalloc ( sizeof ( **cert ) + cursor.len );
  937. if ( ! *cert )
  938. return -ENOMEM;
  939. ref_init ( &(*cert)->refcnt, NULL );
  940. raw = ( *cert + 1 );
  941. /* Copy raw data */
  942. memcpy ( raw, cursor.data, cursor.len );
  943. cursor.data = raw;
  944. /* Parse certificate */
  945. if ( ( rc = x509_parse ( *cert, &cursor ) ) != 0 ) {
  946. x509_put ( *cert );
  947. *cert = NULL;
  948. return rc;
  949. }
  950. /* Add certificate to store */
  951. certstore_add ( *cert );
  952. return 0;
  953. }
  954. /**
  955. * Check X.509 certificate signature
  956. *
  957. * @v cert X.509 certificate
  958. * @v public_key X.509 public key
  959. * @ret rc Return status code
  960. */
  961. static int x509_check_signature ( struct x509_certificate *cert,
  962. struct x509_public_key *public_key ) {
  963. struct x509_signature *signature = &cert->signature;
  964. struct asn1_algorithm *algorithm = signature->algorithm;
  965. struct digest_algorithm *digest = algorithm->digest;
  966. struct pubkey_algorithm *pubkey = algorithm->pubkey;
  967. uint8_t digest_ctx[ digest->ctxsize ];
  968. uint8_t digest_out[ digest->digestsize ];
  969. uint8_t pubkey_ctx[ pubkey->ctxsize ];
  970. int rc;
  971. /* Sanity check */
  972. assert ( cert->signature_algorithm == cert->signature.algorithm );
  973. /* Calculate certificate digest */
  974. digest_init ( digest, digest_ctx );
  975. digest_update ( digest, digest_ctx, cert->tbs.data, cert->tbs.len );
  976. digest_final ( digest, digest_ctx, digest_out );
  977. DBGC2 ( cert, "X509 %p \"%s\" digest:\n", cert, x509_name ( cert ) );
  978. DBGC2_HDA ( cert, 0, digest_out, sizeof ( digest_out ) );
  979. /* Check that signature public key algorithm matches signer */
  980. if ( public_key->algorithm->pubkey != pubkey ) {
  981. DBGC ( cert, "X509 %p \"%s\" signature algorithm %s does not "
  982. "match signer's algorithm %s\n",
  983. cert, x509_name ( cert ), algorithm->name,
  984. public_key->algorithm->name );
  985. rc = -EINVAL_ALGORITHM_MISMATCH;
  986. goto err_mismatch;
  987. }
  988. /* Verify signature using signer's public key */
  989. if ( ( rc = pubkey_init ( pubkey, pubkey_ctx, public_key->raw.data,
  990. public_key->raw.len ) ) != 0 ) {
  991. DBGC ( cert, "X509 %p \"%s\" cannot initialise public key: "
  992. "%s\n", cert, x509_name ( cert ), strerror ( rc ) );
  993. goto err_pubkey_init;
  994. }
  995. if ( ( rc = pubkey_verify ( pubkey, pubkey_ctx, digest, digest_out,
  996. signature->value.data,
  997. signature->value.len ) ) != 0 ) {
  998. DBGC ( cert, "X509 %p \"%s\" signature verification failed: "
  999. "%s\n", cert, x509_name ( cert ), strerror ( rc ) );
  1000. goto err_pubkey_verify;
  1001. }
  1002. /* Success */
  1003. rc = 0;
  1004. err_pubkey_verify:
  1005. pubkey_final ( pubkey, pubkey_ctx );
  1006. err_pubkey_init:
  1007. err_mismatch:
  1008. return rc;
  1009. }
  1010. /**
  1011. * Check X.509 certificate against issuer certificate
  1012. *
  1013. * @v cert X.509 certificate
  1014. * @v issuer X.509 issuer certificate
  1015. * @ret rc Return status code
  1016. */
  1017. int x509_check_issuer ( struct x509_certificate *cert,
  1018. struct x509_certificate *issuer ) {
  1019. struct x509_public_key *public_key = &issuer->subject.public_key;
  1020. int rc;
  1021. /* Check issuer. In theory, this should be a full X.500 DN
  1022. * comparison, which would require support for a plethora of
  1023. * abominations such as TeletexString (which allows the
  1024. * character set to be changed mid-string using escape codes).
  1025. * In practice, we assume that anyone who deliberately changes
  1026. * the encoding of the issuer DN is probably a masochist who
  1027. * will rather enjoy the process of figuring out exactly why
  1028. * their certificate doesn't work.
  1029. *
  1030. * See http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt
  1031. * for some enjoyable ranting on this subject.
  1032. */
  1033. if ( asn1_compare ( &cert->issuer.raw, &issuer->subject.raw ) != 0 ) {
  1034. DBGC ( cert, "X509 %p \"%s\" issuer does not match ",
  1035. cert, x509_name ( cert ) );
  1036. DBGC ( cert, "X509 %p \"%s\" subject\n",
  1037. issuer, x509_name ( issuer ) );
  1038. DBGC_HDA ( cert, 0, cert->issuer.raw.data,
  1039. cert->issuer.raw.len );
  1040. DBGC_HDA ( issuer, 0, issuer->subject.raw.data,
  1041. issuer->subject.raw.len );
  1042. return -EACCES_WRONG_ISSUER;
  1043. }
  1044. /* Check that issuer is allowed to sign certificates */
  1045. if ( ! issuer->extensions.basic.ca ) {
  1046. DBGC ( issuer, "X509 %p \"%s\" cannot sign ",
  1047. issuer, x509_name ( issuer ) );
  1048. DBGC ( issuer, "X509 %p \"%s\": not a CA certificate\n",
  1049. cert, x509_name ( cert ) );
  1050. return -EACCES_NOT_CA;
  1051. }
  1052. if ( issuer->extensions.usage.present &&
  1053. ( ! ( issuer->extensions.usage.bits & X509_KEY_CERT_SIGN ) ) ) {
  1054. DBGC ( issuer, "X509 %p \"%s\" cannot sign ",
  1055. issuer, x509_name ( issuer ) );
  1056. DBGC ( issuer, "X509 %p \"%s\": no keyCertSign usage\n",
  1057. cert, x509_name ( cert ) );
  1058. return -EACCES_KEY_USAGE;
  1059. }
  1060. /* Check signature */
  1061. if ( ( rc = x509_check_signature ( cert, public_key ) ) != 0 )
  1062. return rc;
  1063. return 0;
  1064. }
  1065. /**
  1066. * Calculate X.509 certificate fingerprint
  1067. *
  1068. * @v cert X.509 certificate
  1069. * @v digest Digest algorithm
  1070. * @v fingerprint Fingerprint buffer
  1071. */
  1072. void x509_fingerprint ( struct x509_certificate *cert,
  1073. struct digest_algorithm *digest,
  1074. void *fingerprint ) {
  1075. uint8_t ctx[ digest->ctxsize ];
  1076. /* Calculate fingerprint */
  1077. digest_init ( digest, ctx );
  1078. digest_update ( digest, ctx, cert->raw.data, cert->raw.len );
  1079. digest_final ( digest, ctx, fingerprint );
  1080. }
  1081. /**
  1082. * Check X.509 root certificate
  1083. *
  1084. * @v cert X.509 certificate
  1085. * @v root X.509 root certificate list
  1086. * @ret rc Return status code
  1087. */
  1088. int x509_check_root ( struct x509_certificate *cert, struct x509_root *root ) {
  1089. struct digest_algorithm *digest = root->digest;
  1090. uint8_t fingerprint[ digest->digestsize ];
  1091. const uint8_t *root_fingerprint = root->fingerprints;
  1092. unsigned int i;
  1093. /* Calculate certificate fingerprint */
  1094. x509_fingerprint ( cert, digest, fingerprint );
  1095. /* Check fingerprint against all root certificates */
  1096. for ( i = 0 ; i < root->count ; i++ ) {
  1097. if ( memcmp ( fingerprint, root_fingerprint,
  1098. sizeof ( fingerprint ) ) == 0 ) {
  1099. DBGC ( cert, "X509 %p \"%s\" is a root certificate\n",
  1100. cert, x509_name ( cert ) );
  1101. return 0;
  1102. }
  1103. root_fingerprint += sizeof ( fingerprint );
  1104. }
  1105. DBGC2 ( cert, "X509 %p \"%s\" is not a root certificate\n",
  1106. cert, x509_name ( cert ) );
  1107. return -ENOENT;
  1108. }
  1109. /**
  1110. * Check X.509 certificate validity period
  1111. *
  1112. * @v cert X.509 certificate
  1113. * @v time Time at which to check certificate
  1114. * @ret rc Return status code
  1115. */
  1116. int x509_check_time ( struct x509_certificate *cert, time_t time ) {
  1117. struct x509_validity *validity = &cert->validity;
  1118. /* Check validity period */
  1119. if ( validity->not_before.time > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
  1120. DBGC ( cert, "X509 %p \"%s\" is not yet valid (at time %lld)\n",
  1121. cert, x509_name ( cert ), time );
  1122. return -EACCES_EXPIRED;
  1123. }
  1124. if ( validity->not_after.time < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
  1125. DBGC ( cert, "X509 %p \"%s\" has expired (at time %lld)\n",
  1126. cert, x509_name ( cert ), time );
  1127. return -EACCES_EXPIRED;
  1128. }
  1129. DBGC2 ( cert, "X509 %p \"%s\" is valid (at time %lld)\n",
  1130. cert, x509_name ( cert ), time );
  1131. return 0;
  1132. }
  1133. /**
  1134. * Validate X.509 certificate
  1135. *
  1136. * @v cert X.509 certificate
  1137. * @v issuer Issuing X.509 certificate (or NULL)
  1138. * @v time Time at which to validate certificate
  1139. * @v root Root certificate list, or NULL to use default
  1140. * @ret rc Return status code
  1141. *
  1142. * The issuing certificate must have already been validated.
  1143. *
  1144. * Validation results are cached: if a certificate has already been
  1145. * successfully validated then @c issuer, @c time, and @c root will be
  1146. * ignored.
  1147. */
  1148. int x509_validate ( struct x509_certificate *cert,
  1149. struct x509_certificate *issuer,
  1150. time_t time, struct x509_root *root ) {
  1151. unsigned int max_path_remaining;
  1152. int rc;
  1153. /* Use default root certificate store if none specified */
  1154. if ( ! root )
  1155. root = &root_certificates;
  1156. /* Return success if certificate has already been validated */
  1157. if ( cert->valid )
  1158. return 0;
  1159. /* Fail if certificate is invalid at specified time */
  1160. if ( ( rc = x509_check_time ( cert, time ) ) != 0 )
  1161. return rc;
  1162. /* Succeed if certificate is a trusted root certificate */
  1163. if ( x509_check_root ( cert, root ) == 0 ) {
  1164. cert->valid = 1;
  1165. cert->path_remaining = ( cert->extensions.basic.path_len + 1 );
  1166. return 0;
  1167. }
  1168. /* Fail unless we have an issuer */
  1169. if ( ! issuer ) {
  1170. DBGC2 ( cert, "X509 %p \"%s\" has no issuer\n",
  1171. cert, x509_name ( cert ) );
  1172. return -EACCES_UNTRUSTED;
  1173. }
  1174. /* Fail unless issuer has already been validated */
  1175. if ( ! issuer->valid ) {
  1176. DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
  1177. DBGC ( cert, "issuer %p \"%s\" has not yet been validated\n",
  1178. issuer, x509_name ( issuer ) );
  1179. return -EACCES_OUT_OF_ORDER;
  1180. }
  1181. /* Fail if issuing certificate cannot validate this certificate */
  1182. if ( ( rc = x509_check_issuer ( cert, issuer ) ) != 0 )
  1183. return rc;
  1184. /* Fail if path length constraint is violated */
  1185. if ( issuer->path_remaining == 0 ) {
  1186. DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
  1187. DBGC ( cert, "issuer %p \"%s\" path length exceeded\n",
  1188. issuer, x509_name ( issuer ) );
  1189. return -EACCES_PATH_LEN;
  1190. }
  1191. /* Fail if OCSP is required */
  1192. if ( cert->extensions.auth_info.ocsp.uri.len &&
  1193. ( ! cert->extensions.auth_info.ocsp.good ) ) {
  1194. DBGC ( cert, "X509 %p \"%s\" requires an OCSP check\n",
  1195. cert, x509_name ( cert ) );
  1196. return -EACCES_OCSP_REQUIRED;
  1197. }
  1198. /* Calculate effective path length */
  1199. cert->path_remaining = ( issuer->path_remaining - 1 );
  1200. max_path_remaining = ( cert->extensions.basic.path_len + 1 );
  1201. if ( cert->path_remaining > max_path_remaining )
  1202. cert->path_remaining = max_path_remaining;
  1203. /* Mark certificate as valid */
  1204. cert->valid = 1;
  1205. DBGC ( cert, "X509 %p \"%s\" successfully validated using ",
  1206. cert, x509_name ( cert ) );
  1207. DBGC ( cert, "issuer %p \"%s\"\n", issuer, x509_name ( issuer ) );
  1208. return 0;
  1209. }
  1210. /**
  1211. * Check X.509 certificate alternative dNSName
  1212. *
  1213. * @v cert X.509 certificate
  1214. * @v raw ASN.1 cursor
  1215. * @v name Name
  1216. * @ret rc Return status code
  1217. */
  1218. static int x509_check_dnsname ( struct x509_certificate *cert,
  1219. const struct asn1_cursor *raw,
  1220. const char *name ) {
  1221. const char *fullname = name;
  1222. const char *dnsname = raw->data;
  1223. size_t len = raw->len;
  1224. /* Check for wildcards */
  1225. if ( ( len >= 2 ) && ( dnsname[0] == '*' ) && ( dnsname[1] == '.' ) ) {
  1226. /* Skip initial "*." */
  1227. dnsname += 2;
  1228. len -= 2;
  1229. /* Skip initial portion of name to be tested */
  1230. name = strchr ( name, '.' );
  1231. if ( ! name )
  1232. return -ENOENT;
  1233. name++;
  1234. }
  1235. /* Compare names */
  1236. if ( ! ( ( strlen ( name ) == len ) &&
  1237. ( memcmp ( name, dnsname, len ) == 0 ) ) )
  1238. return -ENOENT;
  1239. if ( name != fullname ) {
  1240. DBGC2 ( cert, "X509 %p \"%s\" found wildcard match for "
  1241. "\"*.%s\"\n", cert, x509_name ( cert ), name );
  1242. }
  1243. return 0;
  1244. }
  1245. /**
  1246. * Check X.509 certificate alternative name
  1247. *
  1248. * @v cert X.509 certificate
  1249. * @v raw ASN.1 cursor
  1250. * @v name Name
  1251. * @ret rc Return status code
  1252. */
  1253. static int x509_check_alt_name ( struct x509_certificate *cert,
  1254. const struct asn1_cursor *raw,
  1255. const char *name ) {
  1256. struct asn1_cursor alt_name;
  1257. unsigned int type;
  1258. /* Enter generalName */
  1259. memcpy ( &alt_name, raw, sizeof ( alt_name ) );
  1260. type = asn1_type ( &alt_name );
  1261. asn1_enter_any ( &alt_name );
  1262. /* Check this name */
  1263. switch ( type ) {
  1264. case X509_GENERAL_NAME_DNS :
  1265. return x509_check_dnsname ( cert, &alt_name, name );
  1266. default:
  1267. DBGC2 ( cert, "X509 %p \"%s\" unknown name of type %#02x:\n",
  1268. cert, x509_name ( cert ), type );
  1269. DBGC2_HDA ( cert, 0, alt_name.data, alt_name.len );
  1270. return -ENOTSUP;
  1271. }
  1272. }
  1273. /**
  1274. * Check X.509 certificate name
  1275. *
  1276. * @v cert X.509 certificate
  1277. * @v name Name
  1278. * @ret rc Return status code
  1279. */
  1280. int x509_check_name ( struct x509_certificate *cert, const char *name ) {
  1281. struct asn1_cursor *common_name = &cert->subject.common_name;
  1282. struct asn1_cursor alt_name;
  1283. int rc;
  1284. /* Check commonName */
  1285. if ( x509_check_dnsname ( cert, common_name, name ) == 0 ) {
  1286. DBGC2 ( cert, "X509 %p \"%s\" commonName matches \"%s\"\n",
  1287. cert, x509_name ( cert ), name );
  1288. return 0;
  1289. }
  1290. /* Check any subjectAlternativeNames */
  1291. memcpy ( &alt_name, &cert->extensions.alt_name.names,
  1292. sizeof ( alt_name ) );
  1293. for ( ; alt_name.len ; asn1_skip_any ( &alt_name ) ) {
  1294. if ( ( rc = x509_check_alt_name ( cert, &alt_name,
  1295. name ) ) == 0 ) {
  1296. DBGC2 ( cert, "X509 %p \"%s\" subjectAltName matches "
  1297. "\"%s\"\n", cert, x509_name ( cert ), name );
  1298. return 0;
  1299. }
  1300. }
  1301. DBGC ( cert, "X509 %p \"%s\" does not match name \"%s\"\n",
  1302. cert, x509_name ( cert ), name );
  1303. return -EACCES_WRONG_NAME;
  1304. }
  1305. /**
  1306. * Free X.509 certificate chain
  1307. *
  1308. * @v refcnt Reference count
  1309. */
  1310. static void x509_free_chain ( struct refcnt *refcnt ) {
  1311. struct x509_chain *chain =
  1312. container_of ( refcnt, struct x509_chain, refcnt );
  1313. struct x509_link *link;
  1314. struct x509_link *tmp;
  1315. DBGC2 ( chain, "X509 chain %p freed\n", chain );
  1316. /* Free each link in the chain */
  1317. list_for_each_entry_safe ( link, tmp, &chain->links, list ) {
  1318. x509_put ( link->cert );
  1319. list_del ( &link->list );
  1320. free ( link );
  1321. }
  1322. /* Free chain */
  1323. free ( chain );
  1324. }
  1325. /**
  1326. * Allocate X.509 certificate chain
  1327. *
  1328. * @ret chain X.509 certificate chain, or NULL
  1329. */
  1330. struct x509_chain * x509_alloc_chain ( void ) {
  1331. struct x509_chain *chain;
  1332. /* Allocate chain */
  1333. chain = zalloc ( sizeof ( *chain ) );
  1334. if ( ! chain )
  1335. return NULL;
  1336. /* Initialise chain */
  1337. ref_init ( &chain->refcnt, x509_free_chain );
  1338. INIT_LIST_HEAD ( &chain->links );
  1339. DBGC2 ( chain, "X509 chain %p allocated\n", chain );
  1340. return chain;
  1341. }
  1342. /**
  1343. * Append X.509 certificate to X.509 certificate chain
  1344. *
  1345. * @v chain X.509 certificate chain
  1346. * @v cert X.509 certificate
  1347. * @ret rc Return status code
  1348. */
  1349. int x509_append ( struct x509_chain *chain, struct x509_certificate *cert ) {
  1350. struct x509_link *link;
  1351. /* Allocate link */
  1352. link = zalloc ( sizeof ( *link ) );
  1353. if ( ! link )
  1354. return -ENOMEM;
  1355. /* Add link to chain */
  1356. link->cert = x509_get ( cert );
  1357. list_add_tail ( &link->list, &chain->links );
  1358. DBGC ( chain, "X509 chain %p added X509 %p \"%s\"\n",
  1359. chain, cert, x509_name ( cert ) );
  1360. return 0;
  1361. }
  1362. /**
  1363. * Append X.509 certificate to X.509 certificate chain
  1364. *
  1365. * @v chain X.509 certificate chain
  1366. * @v data Raw certificate data
  1367. * @v len Length of raw data
  1368. * @ret rc Return status code
  1369. */
  1370. int x509_append_raw ( struct x509_chain *chain, const void *data,
  1371. size_t len ) {
  1372. struct x509_certificate *cert;
  1373. int rc;
  1374. /* Parse certificate */
  1375. if ( ( rc = x509_certificate ( data, len, &cert ) ) != 0 )
  1376. goto err_parse;
  1377. /* Append certificate to chain */
  1378. if ( ( rc = x509_append ( chain, cert ) ) != 0 )
  1379. goto err_append;
  1380. /* Drop reference to certificate */
  1381. x509_put ( cert );
  1382. return 0;
  1383. err_append:
  1384. x509_put ( cert );
  1385. err_parse:
  1386. return rc;
  1387. }
  1388. /**
  1389. * Identify X.509 certificate by subject
  1390. *
  1391. * @v certs X.509 certificate list
  1392. * @v subject Subject
  1393. * @ret cert X.509 certificate, or NULL if not found
  1394. */
  1395. static struct x509_certificate *
  1396. x509_find_subject ( struct x509_chain *certs,
  1397. const struct asn1_cursor *subject ) {
  1398. struct x509_link *link;
  1399. struct x509_certificate *cert;
  1400. /* Scan through certificate list */
  1401. list_for_each_entry ( link, &certs->links, list ) {
  1402. /* Check subject */
  1403. cert = link->cert;
  1404. if ( asn1_compare ( subject, &cert->subject.raw ) == 0 )
  1405. return cert;
  1406. }
  1407. return NULL;
  1408. }
  1409. /**
  1410. * Append X.509 certificates to X.509 certificate chain
  1411. *
  1412. * @v chain X.509 certificate chain
  1413. * @v certs X.509 certificate list
  1414. * @ret rc Return status code
  1415. *
  1416. * Certificates will be automatically appended to the chain based upon
  1417. * the subject and issuer names.
  1418. */
  1419. int x509_auto_append ( struct x509_chain *chain, struct x509_chain *certs ) {
  1420. struct x509_certificate *cert;
  1421. struct x509_certificate *previous;
  1422. int rc;
  1423. /* Get current certificate */
  1424. cert = x509_last ( chain );
  1425. if ( ! cert ) {
  1426. DBGC ( chain, "X509 chain %p has no certificates\n", chain );
  1427. return -EACCES_EMPTY;
  1428. }
  1429. /* Append certificates, in order */
  1430. while ( 1 ) {
  1431. /* Find issuing certificate */
  1432. previous = cert;
  1433. cert = x509_find_subject ( certs, &cert->issuer.raw );
  1434. if ( ! cert )
  1435. break;
  1436. if ( cert == previous )
  1437. break;
  1438. /* Append certificate to chain */
  1439. if ( ( rc = x509_append ( chain, cert ) ) != 0 )
  1440. return rc;
  1441. }
  1442. return 0;
  1443. }
  1444. /**
  1445. * Validate X.509 certificate chain
  1446. *
  1447. * @v chain X.509 certificate chain
  1448. * @v time Time at which to validate certificates
  1449. * @v store Certificate store, or NULL to use default
  1450. * @v root Root certificate list, or NULL to use default
  1451. * @ret rc Return status code
  1452. */
  1453. int x509_validate_chain ( struct x509_chain *chain, time_t time,
  1454. struct x509_chain *store, struct x509_root *root ) {
  1455. struct x509_certificate *issuer = NULL;
  1456. struct x509_link *link;
  1457. int rc;
  1458. /* Use default certificate store if none specified */
  1459. if ( ! store )
  1460. store = &certstore;
  1461. /* Append any applicable certificates from the certificate store */
  1462. if ( ( rc = x509_auto_append ( chain, store ) ) != 0 )
  1463. return rc;
  1464. /* Find first certificate that can be validated as a
  1465. * standalone (i.e. is already valid, or can be validated as
  1466. * a trusted root certificate).
  1467. */
  1468. list_for_each_entry ( link, &chain->links, list ) {
  1469. /* Try validating this certificate as a standalone */
  1470. if ( ( rc = x509_validate ( link->cert, NULL, time,
  1471. root ) ) != 0 )
  1472. continue;
  1473. /* Work back up to start of chain, performing pairwise
  1474. * validation.
  1475. */
  1476. issuer = link->cert;
  1477. list_for_each_entry_continue_reverse ( link, &chain->links,
  1478. list ) {
  1479. /* Validate this certificate against its issuer */
  1480. if ( ( rc = x509_validate ( link->cert, issuer, time,
  1481. root ) ) != 0 )
  1482. return rc;
  1483. issuer = link->cert;
  1484. }
  1485. return 0;
  1486. }
  1487. DBGC ( chain, "X509 chain %p found no usable certificates\n", chain );
  1488. return -EACCES_USELESS;
  1489. }
  1490. /* Drag in certificate store */
  1491. REQUIRE_OBJECT ( certstore );