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.4KB

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