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

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