您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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