Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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