Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

x509.h 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. /** Certificate was added at build time */
  195. X509_FL_PERMANENT = 0x0002,
  196. /** Certificate was added explicitly at run time */
  197. X509_FL_EXPLICIT = 0x0004,
  198. };
  199. /**
  200. * Get reference to X.509 certificate
  201. *
  202. * @v cert X.509 certificate
  203. * @ret cert X.509 certificate
  204. */
  205. static inline __attribute__ (( always_inline )) struct x509_certificate *
  206. x509_get ( struct x509_certificate *cert ) {
  207. ref_get ( &cert->refcnt );
  208. return cert;
  209. }
  210. /**
  211. * Drop reference to X.509 certificate
  212. *
  213. * @v cert X.509 certificate
  214. */
  215. static inline __attribute__ (( always_inline )) void
  216. x509_put ( struct x509_certificate *cert ) {
  217. ref_put ( &cert->refcnt );
  218. }
  219. /**
  220. * Get reference to X.509 certificate chain
  221. *
  222. * @v chain X.509 certificate chain
  223. * @ret chain X.509 certificate chain
  224. */
  225. static inline __attribute__ (( always_inline )) struct x509_chain *
  226. x509_chain_get ( struct x509_chain *chain ) {
  227. ref_get ( &chain->refcnt );
  228. return chain;
  229. }
  230. /**
  231. * Drop reference to X.509 certificate chain
  232. *
  233. * @v chain X.509 certificate chain
  234. */
  235. static inline __attribute__ (( always_inline )) void
  236. x509_chain_put ( struct x509_chain *chain ) {
  237. ref_put ( &chain->refcnt );
  238. }
  239. /**
  240. * Get first certificate in X.509 certificate chain
  241. *
  242. * @v chain X.509 certificate chain
  243. * @ret cert X.509 certificate, or NULL
  244. */
  245. static inline __attribute__ (( always_inline )) struct x509_certificate *
  246. x509_first ( struct x509_chain *chain ) {
  247. struct x509_link *link;
  248. link = list_first_entry ( &chain->links, struct x509_link, list );
  249. return ( link ? link->cert : NULL );
  250. }
  251. /**
  252. * Get last certificate in X.509 certificate chain
  253. *
  254. * @v chain X.509 certificate chain
  255. * @ret cert X.509 certificate, or NULL
  256. */
  257. static inline __attribute__ (( always_inline )) struct x509_certificate *
  258. x509_last ( struct x509_chain *chain ) {
  259. struct x509_link *link;
  260. link = list_last_entry ( &chain->links, struct x509_link, list );
  261. return ( link ? link->cert : NULL );
  262. }
  263. /** An X.509 extension */
  264. struct x509_extension {
  265. /** Name */
  266. const char *name;
  267. /** Object identifier */
  268. struct asn1_cursor oid;
  269. /** Parse extension
  270. *
  271. * @v cert X.509 certificate
  272. * @v raw ASN.1 cursor
  273. * @ret rc Return status code
  274. */
  275. int ( * parse ) ( struct x509_certificate *cert,
  276. const struct asn1_cursor *raw );
  277. };
  278. /** An X.509 key purpose */
  279. struct x509_key_purpose {
  280. /** Name */
  281. const char *name;
  282. /** Object identifier */
  283. struct asn1_cursor oid;
  284. /** Extended key usage bits */
  285. unsigned int bits;
  286. };
  287. /** An X.509 access method */
  288. struct x509_access_method {
  289. /** Name */
  290. const char *name;
  291. /** Object identifier */
  292. struct asn1_cursor oid;
  293. /** Parse access method
  294. *
  295. * @v cert X.509 certificate
  296. * @v raw ASN.1 cursor
  297. * @ret rc Return status code
  298. */
  299. int ( * parse ) ( struct x509_certificate *cert,
  300. const struct asn1_cursor *raw );
  301. };
  302. /** An X.509 root certificate store */
  303. struct x509_root {
  304. /** Fingerprint digest algorithm */
  305. struct digest_algorithm *digest;
  306. /** Number of certificates */
  307. unsigned int count;
  308. /** Certificate fingerprints */
  309. const void *fingerprints;
  310. };
  311. extern const char * x509_name ( struct x509_certificate *cert );
  312. extern int x509_parse ( struct x509_certificate *cert,
  313. const struct asn1_cursor *raw );
  314. extern int x509_certificate ( const void *data, size_t len,
  315. struct x509_certificate **cert );
  316. extern int x509_validate ( struct x509_certificate *cert,
  317. struct x509_certificate *issuer,
  318. time_t time, struct x509_root *root );
  319. extern int x509_check_name ( struct x509_certificate *cert, const char *name );
  320. extern struct x509_chain * x509_alloc_chain ( void );
  321. extern int x509_append ( struct x509_chain *chain,
  322. struct x509_certificate *cert );
  323. extern int x509_append_raw ( struct x509_chain *chain, const void *data,
  324. size_t len );
  325. extern int x509_auto_append ( struct x509_chain *chain,
  326. struct x509_chain *certs );
  327. extern int x509_validate_chain ( struct x509_chain *chain, time_t time,
  328. struct x509_chain *store,
  329. struct x509_root *root );
  330. extern int image_x509 ( struct image *image, size_t offset,
  331. struct x509_certificate **cert );
  332. /* Functions exposed only for unit testing */
  333. extern int x509_check_issuer ( struct x509_certificate *cert,
  334. struct x509_certificate *issuer );
  335. extern void x509_fingerprint ( struct x509_certificate *cert,
  336. struct digest_algorithm *digest,
  337. void *fingerprint );
  338. extern int x509_check_root ( struct x509_certificate *cert,
  339. struct x509_root *root );
  340. extern int x509_check_time ( struct x509_certificate *cert, time_t time );
  341. /**
  342. * Check if X.509 certificate is valid
  343. *
  344. * @v cert X.509 certificate
  345. */
  346. static inline int x509_is_valid ( struct x509_certificate *cert ) {
  347. return ( cert->flags & X509_FL_VALIDATED );
  348. }
  349. /**
  350. * Invalidate X.509 certificate
  351. *
  352. * @v cert X.509 certificate
  353. */
  354. static inline void x509_invalidate ( struct x509_certificate *cert ) {
  355. cert->flags &= ~X509_FL_VALIDATED;
  356. cert->path_remaining = 0;
  357. }
  358. /**
  359. * Invalidate X.509 certificate chain
  360. *
  361. * @v chain X.509 certificate chain
  362. */
  363. static inline void x509_invalidate_chain ( struct x509_chain *chain ) {
  364. struct x509_link *link;
  365. list_for_each_entry ( link, &chain->links, list )
  366. x509_invalidate ( link->cert );
  367. }
  368. #endif /* _IPXE_X509_H */