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

x509.c 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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, ASN1_IMPLICIT_TAG ( 6 ) ) ) != 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. /** "id-ce-basicConstraints" object identifier */
  630. static uint8_t oid_ce_basic_constraints[] =
  631. { ASN1_OID_BASICCONSTRAINTS };
  632. /** "id-ce-keyUsage" object identifier */
  633. static uint8_t oid_ce_key_usage[] =
  634. { ASN1_OID_KEYUSAGE };
  635. /** "id-ce-extKeyUsage" object identifier */
  636. static uint8_t oid_ce_ext_key_usage[] =
  637. { ASN1_OID_EXTKEYUSAGE };
  638. /** "id-pe-authorityInfoAccess" object identifier */
  639. static uint8_t oid_pe_authority_info_access[] =
  640. { ASN1_OID_AUTHORITYINFOACCESS };
  641. /** Supported certificate extensions */
  642. static struct x509_extension x509_extensions[] = {
  643. {
  644. .name = "basicConstraints",
  645. .oid = ASN1_OID_CURSOR ( oid_ce_basic_constraints ),
  646. .parse = x509_parse_basic_constraints,
  647. },
  648. {
  649. .name = "keyUsage",
  650. .oid = ASN1_OID_CURSOR ( oid_ce_key_usage ),
  651. .parse = x509_parse_key_usage,
  652. },
  653. {
  654. .name = "extKeyUsage",
  655. .oid = ASN1_OID_CURSOR ( oid_ce_ext_key_usage ),
  656. .parse = x509_parse_extended_key_usage,
  657. },
  658. {
  659. .name = "authorityInfoAccess",
  660. .oid = ASN1_OID_CURSOR ( oid_pe_authority_info_access ),
  661. .parse = x509_parse_authority_info_access,
  662. },
  663. };
  664. /**
  665. * Identify X.509 extension by OID
  666. *
  667. * @v oid OID
  668. * @ret extension Extension, or NULL
  669. */
  670. static struct x509_extension *
  671. x509_find_extension ( const struct asn1_cursor *oid ) {
  672. struct x509_extension *extension;
  673. unsigned int i;
  674. for ( i = 0 ; i < ( sizeof ( x509_extensions ) /
  675. sizeof ( x509_extensions[0] ) ) ; i++ ) {
  676. extension = &x509_extensions[i];
  677. if ( asn1_compare ( &extension->oid, oid ) == 0 )
  678. return extension;
  679. }
  680. return NULL;
  681. }
  682. /**
  683. * Parse X.509 certificate extension
  684. *
  685. * @v cert X.509 certificate
  686. * @v raw ASN.1 cursor
  687. * @ret rc Return status code
  688. */
  689. static int x509_parse_extension ( struct x509_certificate *cert,
  690. const struct asn1_cursor *raw ) {
  691. struct asn1_cursor cursor;
  692. struct asn1_cursor subcursor;
  693. struct x509_extension *extension;
  694. int is_critical = 0;
  695. int rc;
  696. /* Enter extension */
  697. memcpy ( &cursor, raw, sizeof ( cursor ) );
  698. asn1_enter ( &cursor, ASN1_SEQUENCE );
  699. /* Try to identify extension */
  700. memcpy ( &subcursor, &cursor, sizeof ( subcursor ) );
  701. asn1_enter ( &subcursor, ASN1_OID );
  702. extension = x509_find_extension ( &subcursor );
  703. asn1_skip_any ( &cursor );
  704. DBGC2 ( cert, "X509 %p found extension %s\n",
  705. cert, ( extension ? extension->name : "<unknown>" ) );
  706. /* Identify criticality */
  707. if ( asn1_type ( &cursor ) == ASN1_BOOLEAN ) {
  708. is_critical = asn1_boolean ( &cursor );
  709. if ( is_critical < 0 ) {
  710. rc = is_critical;
  711. DBGC ( cert, "X509 %p cannot parse extension "
  712. "criticality: %s\n", cert, strerror ( rc ) );
  713. DBGC_HDA ( cert, 0, raw->data, raw->len );
  714. return rc;
  715. }
  716. asn1_skip_any ( &cursor );
  717. }
  718. /* Handle unknown extensions */
  719. if ( ! extension ) {
  720. if ( is_critical ) {
  721. /* Fail if we cannot handle a critical extension */
  722. DBGC ( cert, "X509 %p cannot handle critical "
  723. "extension:\n", cert );
  724. DBGC_HDA ( cert, 0, raw->data, raw->len );
  725. return -ENOTSUP_EXTENSION;
  726. } else {
  727. /* Ignore unknown non-critical extensions */
  728. return 0;
  729. }
  730. };
  731. /* Extract extnValue */
  732. if ( ( rc = asn1_enter ( &cursor, ASN1_OCTET_STRING ) ) != 0 ) {
  733. DBGC ( cert, "X509 %p extension missing extnValue:\n", cert );
  734. DBGC_HDA ( cert, 0, raw->data, raw->len );
  735. return rc;
  736. }
  737. /* Parse extension */
  738. if ( ( rc = extension->parse ( cert, &cursor ) ) != 0 )
  739. return rc;
  740. return 0;
  741. }
  742. /**
  743. * Parse X.509 certificate extensions, if present
  744. *
  745. * @v cert X.509 certificate
  746. * @v raw ASN.1 cursor
  747. * @ret rc Return status code
  748. */
  749. static int x509_parse_extensions ( struct x509_certificate *cert,
  750. const struct asn1_cursor *raw ) {
  751. struct asn1_cursor cursor;
  752. int rc;
  753. /* Enter extensions, if present */
  754. memcpy ( &cursor, raw, sizeof ( cursor ) );
  755. asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 3 ) );
  756. asn1_enter ( &cursor, ASN1_SEQUENCE );
  757. /* Parse each extension in turn */
  758. while ( cursor.len ) {
  759. if ( ( rc = x509_parse_extension ( cert, &cursor ) ) != 0 )
  760. return rc;
  761. asn1_skip_any ( &cursor );
  762. }
  763. return 0;
  764. }
  765. /**
  766. * Parse X.509 certificate tbsCertificate
  767. *
  768. * @v cert X.509 certificate
  769. * @v raw ASN.1 cursor
  770. * @ret rc Return status code
  771. */
  772. static int x509_parse_tbscertificate ( struct x509_certificate *cert,
  773. const struct asn1_cursor *raw ) {
  774. struct asn1_algorithm **algorithm = &cert->signature_algorithm;
  775. struct asn1_cursor cursor;
  776. int rc;
  777. /* Record raw tbsCertificate */
  778. memcpy ( &cursor, raw, sizeof ( cursor ) );
  779. asn1_shrink_any ( &cursor );
  780. memcpy ( &cert->tbs, &cursor, sizeof ( cert->tbs ) );
  781. /* Enter tbsCertificate */
  782. asn1_enter ( &cursor, ASN1_SEQUENCE );
  783. /* Parse version, if present */
  784. if ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) {
  785. if ( ( rc = x509_parse_version ( cert, &cursor ) ) != 0 )
  786. return rc;
  787. asn1_skip_any ( &cursor );
  788. }
  789. /* Parse serialNumber */
  790. if ( ( rc = x509_parse_serial ( cert, &cursor ) ) != 0 )
  791. return rc;
  792. asn1_skip_any ( &cursor );
  793. /* Parse signature */
  794. if ( ( rc = asn1_signature_algorithm ( &cursor, algorithm ) ) != 0 ) {
  795. DBGC ( cert, "X509 %p could not parse signature algorithm: "
  796. "%s\n", cert, strerror ( rc ) );
  797. return rc;
  798. }
  799. DBGC2 ( cert, "X509 %p tbsCertificate signature algorithm is %s\n",
  800. cert, (*algorithm)->name );
  801. asn1_skip_any ( &cursor );
  802. /* Parse issuer */
  803. if ( ( rc = x509_parse_issuer ( cert, &cursor ) ) != 0 )
  804. return rc;
  805. asn1_skip_any ( &cursor );
  806. /* Parse validity */
  807. if ( ( rc = x509_parse_validity ( cert, &cursor ) ) != 0 )
  808. return rc;
  809. asn1_skip_any ( &cursor );
  810. /* Parse subject */
  811. if ( ( rc = x509_parse_subject ( cert, &cursor ) ) != 0 )
  812. return rc;
  813. asn1_skip_any ( &cursor );
  814. /* Parse subjectPublicKeyInfo */
  815. if ( ( rc = x509_parse_public_key ( cert, &cursor ) ) != 0 )
  816. return rc;
  817. asn1_skip_any ( &cursor );
  818. /* Parse extensions, if present */
  819. if ( ( rc = x509_parse_extensions ( cert, &cursor ) ) != 0 )
  820. return rc;
  821. return 0;
  822. }
  823. /**
  824. * Parse X.509 certificate from ASN.1 data
  825. *
  826. * @v cert X.509 certificate
  827. * @v raw ASN.1 cursor
  828. * @ret rc Return status code
  829. */
  830. int x509_parse ( struct x509_certificate *cert,
  831. const struct asn1_cursor *raw ) {
  832. struct x509_signature *signature = &cert->signature;
  833. struct asn1_algorithm **signature_algorithm = &signature->algorithm;
  834. struct asn1_bit_string *signature_value = &signature->value;
  835. struct asn1_cursor cursor;
  836. int rc;
  837. /* Record raw certificate */
  838. memcpy ( &cursor, raw, sizeof ( cursor ) );
  839. memcpy ( &cert->raw, &cursor, sizeof ( cert->raw ) );
  840. /* Enter certificate */
  841. asn1_enter ( &cursor, ASN1_SEQUENCE );
  842. /* Parse tbsCertificate */
  843. if ( ( rc = x509_parse_tbscertificate ( cert, &cursor ) ) != 0 )
  844. return rc;
  845. asn1_skip_any ( &cursor );
  846. /* Parse signatureAlgorithm */
  847. if ( ( rc = asn1_signature_algorithm ( &cursor,
  848. signature_algorithm ) ) != 0 ) {
  849. DBGC ( cert, "X509 %p could not parse signature algorithm: "
  850. "%s\n", cert, strerror ( rc ) );
  851. return rc;
  852. }
  853. DBGC2 ( cert, "X509 %p signatureAlgorithm is %s\n",
  854. cert, (*signature_algorithm)->name );
  855. asn1_skip_any ( &cursor );
  856. /* Parse signatureValue */
  857. if ( ( rc = asn1_integral_bit_string ( &cursor,
  858. signature_value ) ) != 0 ) {
  859. DBGC ( cert, "X509 %p could not parse signature value: %s\n",
  860. cert, strerror ( rc ) );
  861. return rc;
  862. }
  863. DBGC2 ( cert, "X509 %p signatureValue is:\n", cert );
  864. DBGC2_HDA ( cert, 0, signature_value->data, signature_value->len );
  865. /* Check that algorithm in tbsCertificate matches algorithm in
  866. * signature
  867. */
  868. if ( signature->algorithm != (*signature_algorithm) ) {
  869. DBGC ( cert, "X509 %p signature algorithm %s does not match "
  870. "signatureAlgorithm %s\n",
  871. cert, signature->algorithm->name,
  872. (*signature_algorithm)->name );
  873. return -EINVAL_ALGORITHM_MISMATCH;
  874. }
  875. return 0;
  876. }
  877. /**
  878. * Create X.509 certificate
  879. *
  880. * @v data Raw certificate data
  881. * @v len Length of raw data
  882. * @ret cert X.509 certificate
  883. * @ret rc Return status code
  884. *
  885. * On success, the caller holds a reference to the X.509 certificate,
  886. * and is responsible for ultimately calling x509_put().
  887. */
  888. int x509_certificate ( const void *data, size_t len,
  889. struct x509_certificate **cert ) {
  890. struct asn1_cursor cursor;
  891. void *raw;
  892. int rc;
  893. /* Initialise cursor */
  894. cursor.data = data;
  895. cursor.len = len;
  896. asn1_shrink_any ( &cursor );
  897. /* Return stored certificate, if present */
  898. if ( ( *cert = certstore_find ( &cursor ) ) != NULL ) {
  899. /* Add caller's reference */
  900. x509_get ( *cert );
  901. return 0;
  902. }
  903. /* Allocate and initialise certificate */
  904. *cert = zalloc ( sizeof ( **cert ) + cursor.len );
  905. if ( ! *cert )
  906. return -ENOMEM;
  907. ref_init ( &(*cert)->refcnt, NULL );
  908. raw = ( *cert + 1 );
  909. /* Copy raw data */
  910. memcpy ( raw, cursor.data, cursor.len );
  911. cursor.data = raw;
  912. /* Parse certificate */
  913. if ( ( rc = x509_parse ( *cert, &cursor ) ) != 0 ) {
  914. x509_put ( *cert );
  915. *cert = NULL;
  916. return rc;
  917. }
  918. /* Add certificate to store */
  919. certstore_add ( *cert );
  920. return 0;
  921. }
  922. /**
  923. * Check X.509 certificate signature
  924. *
  925. * @v cert X.509 certificate
  926. * @v public_key X.509 public key
  927. * @ret rc Return status code
  928. */
  929. static int x509_check_signature ( struct x509_certificate *cert,
  930. struct x509_public_key *public_key ) {
  931. struct x509_signature *signature = &cert->signature;
  932. struct asn1_algorithm *algorithm = signature->algorithm;
  933. struct digest_algorithm *digest = algorithm->digest;
  934. struct pubkey_algorithm *pubkey = algorithm->pubkey;
  935. uint8_t digest_ctx[ digest->ctxsize ];
  936. uint8_t digest_out[ digest->digestsize ];
  937. uint8_t pubkey_ctx[ pubkey->ctxsize ];
  938. int rc;
  939. /* Sanity check */
  940. assert ( cert->signature_algorithm == cert->signature.algorithm );
  941. /* Calculate certificate digest */
  942. digest_init ( digest, digest_ctx );
  943. digest_update ( digest, digest_ctx, cert->tbs.data, cert->tbs.len );
  944. digest_final ( digest, digest_ctx, digest_out );
  945. DBGC2 ( cert, "X509 %p \"%s\" digest:\n", cert, x509_name ( cert ) );
  946. DBGC2_HDA ( cert, 0, digest_out, sizeof ( digest_out ) );
  947. /* Check that signature public key algorithm matches signer */
  948. if ( public_key->algorithm->pubkey != pubkey ) {
  949. DBGC ( cert, "X509 %p \"%s\" signature algorithm %s does not "
  950. "match signer's algorithm %s\n",
  951. cert, x509_name ( cert ), algorithm->name,
  952. public_key->algorithm->name );
  953. rc = -EINVAL_ALGORITHM_MISMATCH;
  954. goto err_mismatch;
  955. }
  956. /* Verify signature using signer's public key */
  957. if ( ( rc = pubkey_init ( pubkey, pubkey_ctx, public_key->raw.data,
  958. public_key->raw.len ) ) != 0 ) {
  959. DBGC ( cert, "X509 %p \"%s\" cannot initialise public key: "
  960. "%s\n", cert, x509_name ( cert ), strerror ( rc ) );
  961. goto err_pubkey_init;
  962. }
  963. if ( ( rc = pubkey_verify ( pubkey, pubkey_ctx, digest, digest_out,
  964. signature->value.data,
  965. signature->value.len ) ) != 0 ) {
  966. DBGC ( cert, "X509 %p \"%s\" signature verification failed: "
  967. "%s\n", cert, x509_name ( cert ), strerror ( rc ) );
  968. goto err_pubkey_verify;
  969. }
  970. /* Success */
  971. rc = 0;
  972. err_pubkey_verify:
  973. pubkey_final ( pubkey, pubkey_ctx );
  974. err_pubkey_init:
  975. err_mismatch:
  976. return rc;
  977. }
  978. /**
  979. * Check X.509 certificate against issuer certificate
  980. *
  981. * @v cert X.509 certificate
  982. * @v issuer X.509 issuer certificate
  983. * @ret rc Return status code
  984. */
  985. int x509_check_issuer ( struct x509_certificate *cert,
  986. struct x509_certificate *issuer ) {
  987. struct x509_public_key *public_key = &issuer->subject.public_key;
  988. int rc;
  989. /* Check issuer. In theory, this should be a full X.500 DN
  990. * comparison, which would require support for a plethora of
  991. * abominations such as TeletexString (which allows the
  992. * character set to be changed mid-string using escape codes).
  993. * In practice, we assume that anyone who deliberately changes
  994. * the encoding of the issuer DN is probably a masochist who
  995. * will rather enjoy the process of figuring out exactly why
  996. * their certificate doesn't work.
  997. *
  998. * See http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt
  999. * for some enjoyable ranting on this subject.
  1000. */
  1001. if ( asn1_compare ( &cert->issuer.raw, &issuer->subject.raw ) != 0 ) {
  1002. DBGC ( cert, "X509 %p \"%s\" issuer does not match ",
  1003. cert, x509_name ( cert ) );
  1004. DBGC ( cert, "X509 %p \"%s\" subject\n",
  1005. issuer, x509_name ( issuer ) );
  1006. DBGC_HDA ( cert, 0, cert->issuer.raw.data,
  1007. cert->issuer.raw.len );
  1008. DBGC_HDA ( issuer, 0, issuer->subject.raw.data,
  1009. issuer->subject.raw.len );
  1010. return -EACCES_WRONG_ISSUER;
  1011. }
  1012. /* Check that issuer is allowed to sign certificates */
  1013. if ( ! issuer->extensions.basic.ca ) {
  1014. DBGC ( issuer, "X509 %p \"%s\" cannot sign ",
  1015. issuer, x509_name ( issuer ) );
  1016. DBGC ( issuer, "X509 %p \"%s\": not a CA certificate\n",
  1017. cert, x509_name ( cert ) );
  1018. return -EACCES_NOT_CA;
  1019. }
  1020. if ( issuer->extensions.usage.present &&
  1021. ( ! ( issuer->extensions.usage.bits & X509_KEY_CERT_SIGN ) ) ) {
  1022. DBGC ( issuer, "X509 %p \"%s\" cannot sign ",
  1023. issuer, x509_name ( issuer ) );
  1024. DBGC ( issuer, "X509 %p \"%s\": no keyCertSign usage\n",
  1025. cert, x509_name ( cert ) );
  1026. return -EACCES_KEY_USAGE;
  1027. }
  1028. /* Check signature */
  1029. if ( ( rc = x509_check_signature ( cert, public_key ) ) != 0 )
  1030. return rc;
  1031. return 0;
  1032. }
  1033. /**
  1034. * Calculate X.509 certificate fingerprint
  1035. *
  1036. * @v cert X.509 certificate
  1037. * @v digest Digest algorithm
  1038. * @v fingerprint Fingerprint buffer
  1039. */
  1040. void x509_fingerprint ( struct x509_certificate *cert,
  1041. struct digest_algorithm *digest,
  1042. void *fingerprint ) {
  1043. uint8_t ctx[ digest->ctxsize ];
  1044. /* Calculate fingerprint */
  1045. digest_init ( digest, ctx );
  1046. digest_update ( digest, ctx, cert->raw.data, cert->raw.len );
  1047. digest_final ( digest, ctx, fingerprint );
  1048. }
  1049. /**
  1050. * Check X.509 root certificate
  1051. *
  1052. * @v cert X.509 certificate
  1053. * @v root X.509 root certificate list
  1054. * @ret rc Return status code
  1055. */
  1056. int x509_check_root ( struct x509_certificate *cert, struct x509_root *root ) {
  1057. struct digest_algorithm *digest = root->digest;
  1058. uint8_t fingerprint[ digest->digestsize ];
  1059. const uint8_t *root_fingerprint = root->fingerprints;
  1060. unsigned int i;
  1061. /* Calculate certificate fingerprint */
  1062. x509_fingerprint ( cert, digest, fingerprint );
  1063. /* Check fingerprint against all root certificates */
  1064. for ( i = 0 ; i < root->count ; i++ ) {
  1065. if ( memcmp ( fingerprint, root_fingerprint,
  1066. sizeof ( fingerprint ) ) == 0 ) {
  1067. DBGC ( cert, "X509 %p \"%s\" is a root certificate\n",
  1068. cert, x509_name ( cert ) );
  1069. return 0;
  1070. }
  1071. root_fingerprint += sizeof ( fingerprint );
  1072. }
  1073. DBGC2 ( cert, "X509 %p \"%s\" is not a root certificate\n",
  1074. cert, x509_name ( cert ) );
  1075. return -ENOENT;
  1076. }
  1077. /**
  1078. * Check X.509 certificate validity period
  1079. *
  1080. * @v cert X.509 certificate
  1081. * @v time Time at which to check certificate
  1082. * @ret rc Return status code
  1083. */
  1084. int x509_check_time ( struct x509_certificate *cert, time_t time ) {
  1085. struct x509_validity *validity = &cert->validity;
  1086. /* Check validity period */
  1087. if ( validity->not_before.time > ( time + TIMESTAMP_ERROR_MARGIN ) ) {
  1088. DBGC ( cert, "X509 %p \"%s\" is not yet valid (at time %lld)\n",
  1089. cert, x509_name ( cert ), time );
  1090. return -EACCES_EXPIRED;
  1091. }
  1092. if ( validity->not_after.time < ( time - TIMESTAMP_ERROR_MARGIN ) ) {
  1093. DBGC ( cert, "X509 %p \"%s\" has expired (at time %lld)\n",
  1094. cert, x509_name ( cert ), time );
  1095. return -EACCES_EXPIRED;
  1096. }
  1097. DBGC2 ( cert, "X509 %p \"%s\" is valid (at time %lld)\n",
  1098. cert, x509_name ( cert ), time );
  1099. return 0;
  1100. }
  1101. /**
  1102. * Validate X.509 certificate
  1103. *
  1104. * @v cert X.509 certificate
  1105. * @v issuer Issuing X.509 certificate (or NULL)
  1106. * @v time Time at which to validate certificate
  1107. * @v root Root certificate list, or NULL to use default
  1108. * @ret rc Return status code
  1109. *
  1110. * The issuing certificate must have already been validated.
  1111. *
  1112. * Validation results are cached: if a certificate has already been
  1113. * successfully validated then @c issuer, @c time, and @c root will be
  1114. * ignored.
  1115. */
  1116. int x509_validate ( struct x509_certificate *cert,
  1117. struct x509_certificate *issuer,
  1118. time_t time, struct x509_root *root ) {
  1119. unsigned int max_path_remaining;
  1120. int rc;
  1121. /* Use default root certificate store if none specified */
  1122. if ( ! root )
  1123. root = &root_certificates;
  1124. /* Return success if certificate has already been validated */
  1125. if ( cert->valid )
  1126. return 0;
  1127. /* Fail if certificate is invalid at specified time */
  1128. if ( ( rc = x509_check_time ( cert, time ) ) != 0 )
  1129. return rc;
  1130. /* Succeed if certificate is a trusted root certificate */
  1131. if ( x509_check_root ( cert, root ) == 0 ) {
  1132. cert->valid = 1;
  1133. cert->path_remaining = ( cert->extensions.basic.path_len + 1 );
  1134. return 0;
  1135. }
  1136. /* Fail unless we have an issuer */
  1137. if ( ! issuer ) {
  1138. DBGC2 ( cert, "X509 %p \"%s\" has no issuer\n",
  1139. cert, x509_name ( cert ) );
  1140. return -EACCES_UNTRUSTED;
  1141. }
  1142. /* Fail unless issuer has already been validated */
  1143. if ( ! issuer->valid ) {
  1144. DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
  1145. DBGC ( cert, "issuer %p \"%s\" has not yet been validated\n",
  1146. issuer, x509_name ( issuer ) );
  1147. return -EACCES_OUT_OF_ORDER;
  1148. }
  1149. /* Fail if issuing certificate cannot validate this certificate */
  1150. if ( ( rc = x509_check_issuer ( cert, issuer ) ) != 0 )
  1151. return rc;
  1152. /* Fail if path length constraint is violated */
  1153. if ( issuer->path_remaining == 0 ) {
  1154. DBGC ( cert, "X509 %p \"%s\" ", cert, x509_name ( cert ) );
  1155. DBGC ( cert, "issuer %p \"%s\" path length exceeded\n",
  1156. issuer, x509_name ( issuer ) );
  1157. return -EACCES_PATH_LEN;
  1158. }
  1159. /* Fail if OCSP is required */
  1160. if ( cert->extensions.auth_info.ocsp.uri.len &&
  1161. ( ! cert->extensions.auth_info.ocsp.good ) ) {
  1162. DBGC ( cert, "X509 %p \"%s\" requires an OCSP check\n",
  1163. cert, x509_name ( cert ) );
  1164. return -EACCES_OCSP_REQUIRED;
  1165. }
  1166. /* Calculate effective path length */
  1167. cert->path_remaining = ( issuer->path_remaining - 1 );
  1168. max_path_remaining = ( cert->extensions.basic.path_len + 1 );
  1169. if ( cert->path_remaining > max_path_remaining )
  1170. cert->path_remaining = max_path_remaining;
  1171. /* Mark certificate as valid */
  1172. cert->valid = 1;
  1173. DBGC ( cert, "X509 %p \"%s\" successfully validated using ",
  1174. cert, x509_name ( cert ) );
  1175. DBGC ( cert, "issuer %p \"%s\"\n", issuer, x509_name ( issuer ) );
  1176. return 0;
  1177. }
  1178. /**
  1179. * Check X.509 certificate name
  1180. *
  1181. * @v cert X.509 certificate
  1182. * @v name Name
  1183. * @ret rc Return status code
  1184. */
  1185. int x509_check_name ( struct x509_certificate *cert, const char *name ) {
  1186. struct asn1_cursor *common_name = &cert->subject.common_name;
  1187. size_t len = strlen ( name );
  1188. /* Check commonName */
  1189. if ( ! ( ( len == common_name->len ) &&
  1190. ( memcmp ( name, common_name->data, len ) == 0 ) ) ) {
  1191. DBGC ( cert, "X509 %p \"%s\" does not match name \"%s\"\n",
  1192. cert, x509_name ( cert ), name );
  1193. return -EACCES_WRONG_NAME;
  1194. }
  1195. return 0;
  1196. }
  1197. /**
  1198. * Free X.509 certificate chain
  1199. *
  1200. * @v refcnt Reference count
  1201. */
  1202. static void x509_free_chain ( struct refcnt *refcnt ) {
  1203. struct x509_chain *chain =
  1204. container_of ( refcnt, struct x509_chain, refcnt );
  1205. struct x509_link *link;
  1206. struct x509_link *tmp;
  1207. DBGC2 ( chain, "X509 chain %p freed\n", chain );
  1208. /* Free each link in the chain */
  1209. list_for_each_entry_safe ( link, tmp, &chain->links, list ) {
  1210. x509_put ( link->cert );
  1211. list_del ( &link->list );
  1212. free ( link );
  1213. }
  1214. /* Free chain */
  1215. free ( chain );
  1216. }
  1217. /**
  1218. * Allocate X.509 certificate chain
  1219. *
  1220. * @ret chain X.509 certificate chain, or NULL
  1221. */
  1222. struct x509_chain * x509_alloc_chain ( void ) {
  1223. struct x509_chain *chain;
  1224. /* Allocate chain */
  1225. chain = zalloc ( sizeof ( *chain ) );
  1226. if ( ! chain )
  1227. return NULL;
  1228. /* Initialise chain */
  1229. ref_init ( &chain->refcnt, x509_free_chain );
  1230. INIT_LIST_HEAD ( &chain->links );
  1231. DBGC2 ( chain, "X509 chain %p allocated\n", chain );
  1232. return chain;
  1233. }
  1234. /**
  1235. * Append X.509 certificate to X.509 certificate chain
  1236. *
  1237. * @v chain X.509 certificate chain
  1238. * @v cert X.509 certificate
  1239. * @ret rc Return status code
  1240. */
  1241. int x509_append ( struct x509_chain *chain, struct x509_certificate *cert ) {
  1242. struct x509_link *link;
  1243. /* Allocate link */
  1244. link = zalloc ( sizeof ( *link ) );
  1245. if ( ! link )
  1246. return -ENOMEM;
  1247. /* Add link to chain */
  1248. link->cert = x509_get ( cert );
  1249. list_add_tail ( &link->list, &chain->links );
  1250. DBGC ( chain, "X509 chain %p added X509 %p \"%s\"\n",
  1251. chain, cert, x509_name ( cert ) );
  1252. return 0;
  1253. }
  1254. /**
  1255. * Append X.509 certificate to X.509 certificate chain
  1256. *
  1257. * @v chain X.509 certificate chain
  1258. * @v data Raw certificate data
  1259. * @v len Length of raw data
  1260. * @ret rc Return status code
  1261. */
  1262. int x509_append_raw ( struct x509_chain *chain, const void *data,
  1263. size_t len ) {
  1264. struct x509_certificate *cert;
  1265. int rc;
  1266. /* Parse certificate */
  1267. if ( ( rc = x509_certificate ( data, len, &cert ) ) != 0 )
  1268. goto err_parse;
  1269. /* Append certificate to chain */
  1270. if ( ( rc = x509_append ( chain, cert ) ) != 0 )
  1271. goto err_append;
  1272. /* Drop reference to certificate */
  1273. x509_put ( cert );
  1274. return 0;
  1275. err_append:
  1276. x509_put ( cert );
  1277. err_parse:
  1278. return rc;
  1279. }
  1280. /**
  1281. * Identify X.509 certificate by subject
  1282. *
  1283. * @v certs X.509 certificate list
  1284. * @v subject Subject
  1285. * @ret cert X.509 certificate, or NULL if not found
  1286. */
  1287. static struct x509_certificate *
  1288. x509_find_subject ( struct x509_chain *certs,
  1289. const struct asn1_cursor *subject ) {
  1290. struct x509_link *link;
  1291. struct x509_certificate *cert;
  1292. /* Scan through certificate list */
  1293. list_for_each_entry ( link, &certs->links, list ) {
  1294. /* Check subject */
  1295. cert = link->cert;
  1296. if ( asn1_compare ( subject, &cert->subject.raw ) == 0 )
  1297. return cert;
  1298. }
  1299. return NULL;
  1300. }
  1301. /**
  1302. * Append X.509 certificates to X.509 certificate chain
  1303. *
  1304. * @v chain X.509 certificate chain
  1305. * @v certs X.509 certificate list
  1306. * @ret rc Return status code
  1307. *
  1308. * Certificates will be automatically appended to the chain based upon
  1309. * the subject and issuer names.
  1310. */
  1311. int x509_auto_append ( struct x509_chain *chain, struct x509_chain *certs ) {
  1312. struct x509_certificate *cert;
  1313. struct x509_certificate *previous;
  1314. int rc;
  1315. /* Get current certificate */
  1316. cert = x509_last ( chain );
  1317. if ( ! cert ) {
  1318. DBGC ( chain, "X509 chain %p has no certificates\n", chain );
  1319. return -EACCES_EMPTY;
  1320. }
  1321. /* Append certificates, in order */
  1322. while ( 1 ) {
  1323. /* Find issuing certificate */
  1324. previous = cert;
  1325. cert = x509_find_subject ( certs, &cert->issuer.raw );
  1326. if ( ! cert )
  1327. break;
  1328. if ( cert == previous )
  1329. break;
  1330. /* Append certificate to chain */
  1331. if ( ( rc = x509_append ( chain, cert ) ) != 0 )
  1332. return rc;
  1333. }
  1334. return 0;
  1335. }
  1336. /**
  1337. * Validate X.509 certificate chain
  1338. *
  1339. * @v chain X.509 certificate chain
  1340. * @v time Time at which to validate certificates
  1341. * @v store Certificate store, or NULL to use default
  1342. * @v root Root certificate list, or NULL to use default
  1343. * @ret rc Return status code
  1344. */
  1345. int x509_validate_chain ( struct x509_chain *chain, time_t time,
  1346. struct x509_chain *store, struct x509_root *root ) {
  1347. struct x509_certificate *issuer = NULL;
  1348. struct x509_link *link;
  1349. int rc;
  1350. /* Use default certificate store if none specified */
  1351. if ( ! store )
  1352. store = &certstore;
  1353. /* Append any applicable certificates from the certificate store */
  1354. if ( ( rc = x509_auto_append ( chain, store ) ) != 0 )
  1355. return rc;
  1356. /* Find first certificate that can be validated as a
  1357. * standalone (i.e. is already valid, or can be validated as
  1358. * a trusted root certificate).
  1359. */
  1360. list_for_each_entry ( link, &chain->links, list ) {
  1361. /* Try validating this certificate as a standalone */
  1362. if ( ( rc = x509_validate ( link->cert, NULL, time,
  1363. root ) ) != 0 )
  1364. continue;
  1365. /* Work back up to start of chain, performing pairwise
  1366. * validation.
  1367. */
  1368. issuer = link->cert;
  1369. list_for_each_entry_continue_reverse ( link, &chain->links,
  1370. list ) {
  1371. /* Validate this certificate against its issuer */
  1372. if ( ( rc = x509_validate ( link->cert, issuer, time,
  1373. root ) ) != 0 )
  1374. return rc;
  1375. issuer = link->cert;
  1376. }
  1377. return 0;
  1378. }
  1379. DBGC ( chain, "X509 chain %p found no usable certificates\n", chain );
  1380. return -EACCES_USELESS;
  1381. }
  1382. /* Drag in certificate store */
  1383. REQUIRE_OBJECT ( certstore );