x509.h 8.5KB

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