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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 );
  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. };
  130. /** An X.509 certificate extensions set */
  131. struct x509_extensions {
  132. /** Basic constraints */
  133. struct x509_basic_constraints basic;
  134. /** Key usage */
  135. struct x509_key_usage usage;
  136. /** Extended key usage */
  137. struct x509_extended_key_usage ext_usage;
  138. /** Authority information access */
  139. struct x509_authority_info_access auth_info;
  140. /** Subject alternative name */
  141. struct x509_subject_alt_name alt_name;
  142. };
  143. /** A link in an X.509 certificate chain */
  144. struct x509_link {
  145. /** List of links */
  146. struct list_head list;
  147. /** Certificate */
  148. struct x509_certificate *cert;
  149. };
  150. /** An X.509 certificate chain */
  151. struct x509_chain {
  152. /** Reference count */
  153. struct refcnt refcnt;
  154. /** List of links */
  155. struct list_head links;
  156. };
  157. /** An X.509 certificate */
  158. struct x509_certificate {
  159. /** Reference count */
  160. struct refcnt refcnt;
  161. /** Link in certificate store */
  162. struct x509_link store;
  163. /** Certificate has been validated */
  164. int valid;
  165. /** Maximum number of subsequent certificates in chain */
  166. unsigned int path_remaining;
  167. /** Raw certificate */
  168. struct asn1_cursor raw;
  169. /** Version */
  170. unsigned int version;
  171. /** Serial number */
  172. struct x509_serial serial;
  173. /** Raw tbsCertificate */
  174. struct asn1_cursor tbs;
  175. /** Signature algorithm */
  176. struct asn1_algorithm *signature_algorithm;
  177. /** Issuer */
  178. struct x509_issuer issuer;
  179. /** Validity */
  180. struct x509_validity validity;
  181. /** Subject */
  182. struct x509_subject subject;
  183. /** Signature */
  184. struct x509_signature signature;
  185. /** Extensions */
  186. struct x509_extensions extensions;
  187. };
  188. /**
  189. * Get reference to X.509 certificate
  190. *
  191. * @v cert X.509 certificate
  192. * @ret cert X.509 certificate
  193. */
  194. static inline __attribute__ (( always_inline )) struct x509_certificate *
  195. x509_get ( struct x509_certificate *cert ) {
  196. ref_get ( &cert->refcnt );
  197. return cert;
  198. }
  199. /**
  200. * Drop reference to X.509 certificate
  201. *
  202. * @v cert X.509 certificate
  203. */
  204. static inline __attribute__ (( always_inline )) void
  205. x509_put ( struct x509_certificate *cert ) {
  206. ref_put ( &cert->refcnt );
  207. }
  208. /**
  209. * Get reference to X.509 certificate chain
  210. *
  211. * @v chain X.509 certificate chain
  212. * @ret chain X.509 certificate chain
  213. */
  214. static inline __attribute__ (( always_inline )) struct x509_chain *
  215. x509_chain_get ( struct x509_chain *chain ) {
  216. ref_get ( &chain->refcnt );
  217. return chain;
  218. }
  219. /**
  220. * Drop reference to X.509 certificate chain
  221. *
  222. * @v chain X.509 certificate chain
  223. */
  224. static inline __attribute__ (( always_inline )) void
  225. x509_chain_put ( struct x509_chain *chain ) {
  226. ref_put ( &chain->refcnt );
  227. }
  228. /**
  229. * Get first certificate in X.509 certificate chain
  230. *
  231. * @v chain X.509 certificate chain
  232. * @ret cert X.509 certificate, or NULL
  233. */
  234. static inline __attribute__ (( always_inline )) struct x509_certificate *
  235. x509_first ( struct x509_chain *chain ) {
  236. struct x509_link *link;
  237. link = list_first_entry ( &chain->links, struct x509_link, list );
  238. return ( link ? link->cert : NULL );
  239. }
  240. /**
  241. * Get last certificate in X.509 certificate chain
  242. *
  243. * @v chain X.509 certificate chain
  244. * @ret cert X.509 certificate, or NULL
  245. */
  246. static inline __attribute__ (( always_inline )) struct x509_certificate *
  247. x509_last ( struct x509_chain *chain ) {
  248. struct x509_link *link;
  249. link = list_last_entry ( &chain->links, struct x509_link, list );
  250. return ( link ? link->cert : NULL );
  251. }
  252. /** An X.509 extension */
  253. struct x509_extension {
  254. /** Name */
  255. const char *name;
  256. /** Object identifier */
  257. struct asn1_cursor oid;
  258. /** Parse extension
  259. *
  260. * @v cert X.509 certificate
  261. * @v raw ASN.1 cursor
  262. * @ret rc Return status code
  263. */
  264. int ( * parse ) ( struct x509_certificate *cert,
  265. const struct asn1_cursor *raw );
  266. };
  267. /** An X.509 key purpose */
  268. struct x509_key_purpose {
  269. /** Name */
  270. const char *name;
  271. /** Object identifier */
  272. struct asn1_cursor oid;
  273. /** Extended key usage bits */
  274. unsigned int bits;
  275. };
  276. /** An X.509 access method */
  277. struct x509_access_method {
  278. /** Name */
  279. const char *name;
  280. /** Object identifier */
  281. struct asn1_cursor oid;
  282. /** Parse access method
  283. *
  284. * @v cert X.509 certificate
  285. * @v raw ASN.1 cursor
  286. * @ret rc Return status code
  287. */
  288. int ( * parse ) ( struct x509_certificate *cert,
  289. const struct asn1_cursor *raw );
  290. };
  291. /** An X.509 root certificate store */
  292. struct x509_root {
  293. /** Fingerprint digest algorithm */
  294. struct digest_algorithm *digest;
  295. /** Number of certificates */
  296. unsigned int count;
  297. /** Certificate fingerprints */
  298. const void *fingerprints;
  299. };
  300. extern const char * x509_name ( struct x509_certificate *cert );
  301. extern int x509_parse ( struct x509_certificate *cert,
  302. const struct asn1_cursor *raw );
  303. extern int x509_certificate ( const void *data, size_t len,
  304. struct x509_certificate **cert );
  305. extern int x509_validate ( struct x509_certificate *cert,
  306. struct x509_certificate *issuer,
  307. time_t time, struct x509_root *root );
  308. extern int x509_check_name ( struct x509_certificate *cert, const char *name );
  309. extern struct x509_chain * x509_alloc_chain ( void );
  310. extern int x509_append ( struct x509_chain *chain,
  311. struct x509_certificate *cert );
  312. extern int x509_append_raw ( struct x509_chain *chain, const void *data,
  313. size_t len );
  314. extern int x509_auto_append ( struct x509_chain *chain,
  315. struct x509_chain *certs );
  316. extern int x509_validate_chain ( struct x509_chain *chain, time_t time,
  317. struct x509_chain *store,
  318. struct x509_root *root );
  319. /* Functions exposed only for unit testing */
  320. extern int x509_check_issuer ( struct x509_certificate *cert,
  321. struct x509_certificate *issuer );
  322. extern void x509_fingerprint ( struct x509_certificate *cert,
  323. struct digest_algorithm *digest,
  324. void *fingerprint );
  325. extern int x509_check_root ( struct x509_certificate *cert,
  326. struct x509_root *root );
  327. extern int x509_check_time ( struct x509_certificate *cert, time_t time );
  328. /**
  329. * Invalidate X.509 certificate
  330. *
  331. * @v cert X.509 certificate
  332. */
  333. static inline void x509_invalidate ( struct x509_certificate *cert ) {
  334. cert->valid = 0;
  335. cert->path_remaining = 0;
  336. }
  337. /**
  338. * Invalidate X.509 certificate chain
  339. *
  340. * @v chain X.509 certificate chain
  341. */
  342. static inline void x509_invalidate_chain ( struct x509_chain *chain ) {
  343. struct x509_link *link;
  344. list_for_each_entry ( link, &chain->links, list )
  345. x509_invalidate ( link->cert );
  346. }
  347. #endif /* _IPXE_X509_H */