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.h 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #ifndef _IPXE_X509_H
  2. #define _IPXE_X509_H
  3. /** @file
  4. *
  5. * X.509 certificates
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <stddef.h>
  11. #include <time.h>
  12. #include <ipxe/asn1.h>
  13. #include <ipxe/refcnt.h>
  14. #include <ipxe/list.h>
  15. struct image;
  16. /** An X.509 serial number */
  17. struct x509_serial {
  18. /** Raw serial number */
  19. struct asn1_cursor raw;
  20. };
  21. /** An X.509 issuer */
  22. struct x509_issuer {
  23. /** Raw issuer */
  24. struct asn1_cursor raw;
  25. };
  26. /** An X.509 time */
  27. struct x509_time {
  28. /** Seconds since the Epoch */
  29. time_t time;
  30. };
  31. /** An X.509 certificate validity period */
  32. struct x509_validity {
  33. /** Not valid before */
  34. struct x509_time not_before;
  35. /** Not valid after */
  36. struct x509_time not_after;
  37. };
  38. /** An X.509 certificate public key */
  39. struct x509_public_key {
  40. /** Raw public key information */
  41. struct asn1_cursor raw;
  42. /** Public key algorithm */
  43. struct asn1_algorithm *algorithm;
  44. /** Raw public key bit string */
  45. struct asn1_bit_string raw_bits;
  46. };
  47. /** An X.509 certificate subject */
  48. struct x509_subject {
  49. /** Raw subject */
  50. struct asn1_cursor raw;
  51. /** Common name */
  52. struct asn1_cursor common_name;
  53. /** Public key information */
  54. struct x509_public_key public_key;
  55. };
  56. /** An X.509 certificate signature */
  57. struct x509_signature {
  58. /** Signature algorithm */
  59. struct asn1_algorithm *algorithm;
  60. /** Signature value */
  61. struct asn1_bit_string value;
  62. };
  63. /** An X.509 certificate basic constraints set */
  64. struct x509_basic_constraints {
  65. /** Subject is a CA */
  66. int ca;
  67. /** Path length */
  68. unsigned int path_len;
  69. };
  70. /** Unlimited path length
  71. *
  72. * We use -2U, since this quantity represents one *fewer* than the
  73. * maximum number of remaining certificates in a chain.
  74. */
  75. #define X509_PATH_LEN_UNLIMITED -2U
  76. /** An X.509 certificate key usage */
  77. struct x509_key_usage {
  78. /** Key usage extension is present */
  79. int present;
  80. /** Usage bits */
  81. unsigned int bits;
  82. };
  83. /** X.509 certificate key usage bits */
  84. enum x509_key_usage_bits {
  85. X509_DIGITAL_SIGNATURE = 0x0080,
  86. X509_NON_REPUDIATION = 0x0040,
  87. X509_KEY_ENCIPHERMENT = 0x0020,
  88. X509_DATA_ENCIPHERMENT = 0x0010,
  89. X509_KEY_AGREEMENT = 0x0008,
  90. X509_KEY_CERT_SIGN = 0x0004,
  91. X509_CRL_SIGN = 0x0002,
  92. X509_ENCIPHER_ONLY = 0x0001,
  93. X509_DECIPHER_ONLY = 0x8000,
  94. };
  95. /** An X.509 certificate extended key usage */
  96. struct x509_extended_key_usage {
  97. /** Usage bits */
  98. unsigned int bits;
  99. };
  100. /** X.509 certificate extended key usage bits
  101. *
  102. * Extended key usages are identified by OID; these bits are purely an
  103. * internal definition.
  104. */
  105. enum x509_extended_key_usage_bits {
  106. X509_CODE_SIGNING = 0x0001,
  107. X509_OCSP_SIGNING = 0x0002,
  108. };
  109. /** X.509 certificate OCSP responder */
  110. struct x509_ocsp_responder {
  111. /** URI */
  112. struct asn1_cursor uri;
  113. /** OCSP status is good */
  114. int good;
  115. };
  116. /** X.509 certificate authority information access */
  117. struct x509_authority_info_access {
  118. /** OCSP responder */
  119. struct x509_ocsp_responder ocsp;
  120. };
  121. /** X.509 certificate subject alternative name */
  122. struct x509_subject_alt_name {
  123. /** Names */
  124. struct asn1_cursor names;
  125. };
  126. /** X.509 certificate general name types */
  127. enum x509_general_name_types {
  128. X509_GENERAL_NAME_DNS = ASN1_IMPLICIT_TAG ( 2 ),
  129. X509_GENERAL_NAME_URI = ASN1_IMPLICIT_TAG ( 6 ),
  130. X509_GENERAL_NAME_IP = ASN1_IMPLICIT_TAG ( 7 ),
  131. };
  132. /** An X.509 certificate extensions set */
  133. struct x509_extensions {
  134. /** Basic constraints */
  135. struct x509_basic_constraints basic;
  136. /** Key usage */
  137. struct x509_key_usage usage;
  138. /** Extended key usage */
  139. struct x509_extended_key_usage ext_usage;
  140. /** Authority information access */
  141. struct x509_authority_info_access auth_info;
  142. /** Subject alternative name */
  143. struct x509_subject_alt_name alt_name;
  144. };
  145. /** A link in an X.509 certificate chain */
  146. struct x509_link {
  147. /** List of links */
  148. struct list_head list;
  149. /** Certificate */
  150. struct x509_certificate *cert;
  151. };
  152. /** An X.509 certificate chain */
  153. struct x509_chain {
  154. /** Reference count */
  155. struct refcnt refcnt;
  156. /** List of links */
  157. struct list_head links;
  158. };
  159. /** An X.509 certificate */
  160. struct x509_certificate {
  161. /** Reference count */
  162. struct refcnt refcnt;
  163. /** Link in certificate store */
  164. struct x509_link store;
  165. /** Flags */
  166. unsigned int flags;
  167. /** Maximum number of subsequent certificates in chain */
  168. unsigned int path_remaining;
  169. /** Raw certificate */
  170. struct asn1_cursor raw;
  171. /** Version */
  172. unsigned int version;
  173. /** Serial number */
  174. struct x509_serial serial;
  175. /** Raw tbsCertificate */
  176. struct asn1_cursor tbs;
  177. /** Signature algorithm */
  178. struct asn1_algorithm *signature_algorithm;
  179. /** Issuer */
  180. struct x509_issuer issuer;
  181. /** Validity */
  182. struct x509_validity validity;
  183. /** Subject */
  184. struct x509_subject subject;
  185. /** Signature */
  186. struct x509_signature signature;
  187. /** Extensions */
  188. struct x509_extensions extensions;
  189. };
  190. /** X.509 certificate flags */
  191. enum x509_flags {
  192. /** Certificate has been validated */
  193. X509_FL_VALIDATED = 0x0001,
  194. };
  195. /**
  196. * Get reference to X.509 certificate
  197. *
  198. * @v cert X.509 certificate
  199. * @ret cert X.509 certificate
  200. */
  201. static inline __attribute__ (( always_inline )) struct x509_certificate *
  202. x509_get ( struct x509_certificate *cert ) {
  203. ref_get ( &cert->refcnt );
  204. return cert;
  205. }
  206. /**
  207. * Drop reference to X.509 certificate
  208. *
  209. * @v cert X.509 certificate
  210. */
  211. static inline __attribute__ (( always_inline )) void
  212. x509_put ( struct x509_certificate *cert ) {
  213. ref_put ( &cert->refcnt );
  214. }
  215. /**
  216. * Get reference to X.509 certificate chain
  217. *
  218. * @v chain X.509 certificate chain
  219. * @ret chain X.509 certificate chain
  220. */
  221. static inline __attribute__ (( always_inline )) struct x509_chain *
  222. x509_chain_get ( struct x509_chain *chain ) {
  223. ref_get ( &chain->refcnt );
  224. return chain;
  225. }
  226. /**
  227. * Drop reference to X.509 certificate chain
  228. *
  229. * @v chain X.509 certificate chain
  230. */
  231. static inline __attribute__ (( always_inline )) void
  232. x509_chain_put ( struct x509_chain *chain ) {
  233. ref_put ( &chain->refcnt );
  234. }
  235. /**
  236. * Get first certificate in X.509 certificate chain
  237. *
  238. * @v chain X.509 certificate chain
  239. * @ret cert X.509 certificate, or NULL
  240. */
  241. static inline __attribute__ (( always_inline )) struct x509_certificate *
  242. x509_first ( struct x509_chain *chain ) {
  243. struct x509_link *link;
  244. link = list_first_entry ( &chain->links, struct x509_link, list );
  245. return ( link ? link->cert : NULL );
  246. }
  247. /**
  248. * Get last certificate in X.509 certificate chain
  249. *
  250. * @v chain X.509 certificate chain
  251. * @ret cert X.509 certificate, or NULL
  252. */
  253. static inline __attribute__ (( always_inline )) struct x509_certificate *
  254. x509_last ( struct x509_chain *chain ) {
  255. struct x509_link *link;
  256. link = list_last_entry ( &chain->links, struct x509_link, list );
  257. return ( link ? link->cert : NULL );
  258. }
  259. /** An X.509 extension */
  260. struct x509_extension {
  261. /** Name */
  262. const char *name;
  263. /** Object identifier */
  264. struct asn1_cursor oid;
  265. /** Parse extension
  266. *
  267. * @v cert X.509 certificate
  268. * @v raw ASN.1 cursor
  269. * @ret rc Return status code
  270. */
  271. int ( * parse ) ( struct x509_certificate *cert,
  272. const struct asn1_cursor *raw );
  273. };
  274. /** An X.509 key purpose */
  275. struct x509_key_purpose {
  276. /** Name */
  277. const char *name;
  278. /** Object identifier */
  279. struct asn1_cursor oid;
  280. /** Extended key usage bits */
  281. unsigned int bits;
  282. };
  283. /** An X.509 access method */
  284. struct x509_access_method {
  285. /** Name */
  286. const char *name;
  287. /** Object identifier */
  288. struct asn1_cursor oid;
  289. /** Parse access method
  290. *
  291. * @v cert X.509 certificate
  292. * @v raw ASN.1 cursor
  293. * @ret rc Return status code
  294. */
  295. int ( * parse ) ( struct x509_certificate *cert,
  296. const struct asn1_cursor *raw );
  297. };
  298. /** An X.509 root certificate store */
  299. struct x509_root {
  300. /** Fingerprint digest algorithm */
  301. struct digest_algorithm *digest;
  302. /** Number of certificates */
  303. unsigned int count;
  304. /** Certificate fingerprints */
  305. const void *fingerprints;
  306. };
  307. extern const char * x509_name ( struct x509_certificate *cert );
  308. extern int x509_parse ( struct x509_certificate *cert,
  309. const struct asn1_cursor *raw );
  310. extern int x509_certificate ( const void *data, size_t len,
  311. struct x509_certificate **cert );
  312. extern int x509_validate ( struct x509_certificate *cert,
  313. struct x509_certificate *issuer,
  314. time_t time, struct x509_root *root );
  315. extern int x509_check_name ( struct x509_certificate *cert, const char *name );
  316. extern struct x509_chain * x509_alloc_chain ( void );
  317. extern int x509_append ( struct x509_chain *chain,
  318. struct x509_certificate *cert );
  319. extern int x509_append_raw ( struct x509_chain *chain, const void *data,
  320. size_t len );
  321. extern int x509_auto_append ( struct x509_chain *chain,
  322. struct x509_chain *certs );
  323. extern int x509_validate_chain ( struct x509_chain *chain, time_t time,
  324. struct x509_chain *store,
  325. struct x509_root *root );
  326. extern int image_x509 ( struct image *image, size_t offset,
  327. struct x509_certificate **cert );
  328. /* Functions exposed only for unit testing */
  329. extern int x509_check_issuer ( struct x509_certificate *cert,
  330. struct x509_certificate *issuer );
  331. extern void x509_fingerprint ( struct x509_certificate *cert,
  332. struct digest_algorithm *digest,
  333. void *fingerprint );
  334. extern int x509_check_root ( struct x509_certificate *cert,
  335. struct x509_root *root );
  336. extern int x509_check_time ( struct x509_certificate *cert, time_t time );
  337. /**
  338. * Check if X.509 certificate is valid
  339. *
  340. * @v cert X.509 certificate
  341. */
  342. static inline int x509_is_valid ( struct x509_certificate *cert ) {
  343. return ( cert->flags & X509_FL_VALIDATED );
  344. }
  345. /**
  346. * Invalidate X.509 certificate
  347. *
  348. * @v cert X.509 certificate
  349. */
  350. static inline void x509_invalidate ( struct x509_certificate *cert ) {
  351. cert->flags &= ~X509_FL_VALIDATED;
  352. cert->path_remaining = 0;
  353. }
  354. /**
  355. * Invalidate X.509 certificate chain
  356. *
  357. * @v chain X.509 certificate chain
  358. */
  359. static inline void x509_invalidate_chain ( struct x509_chain *chain ) {
  360. struct x509_link *link;
  361. list_for_each_entry ( link, &chain->links, list )
  362. x509_invalidate ( link->cert );
  363. }
  364. #endif /* _IPXE_X509_H */