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 48KB

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