x509.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. INCLUDES = """
  6. #include <openssl/ssl.h>
  7. /*
  8. * This is part of a work-around for the difficulty cffi has in dealing with
  9. * `STACK_OF(foo)` as the name of a type. We invent a new, simpler name that
  10. * will be an alias for this type and use the alias throughout. This works
  11. * together with another opaque typedef for the same name in the TYPES section.
  12. * Note that the result is an opaque type.
  13. */
  14. typedef STACK_OF(X509) Cryptography_STACK_OF_X509;
  15. typedef STACK_OF(X509_CRL) Cryptography_STACK_OF_X509_CRL;
  16. typedef STACK_OF(X509_REVOKED) Cryptography_STACK_OF_X509_REVOKED;
  17. """
  18. TYPES = """
  19. typedef ... Cryptography_STACK_OF_X509;
  20. typedef ... Cryptography_STACK_OF_X509_CRL;
  21. typedef ... Cryptography_STACK_OF_X509_REVOKED;
  22. typedef struct {
  23. ASN1_OBJECT *algorithm;
  24. ...;
  25. } X509_ALGOR;
  26. typedef ... X509_ATTRIBUTE;
  27. typedef ... X509_EXTENSION;
  28. typedef ... X509_EXTENSIONS;
  29. typedef ... X509_REQ;
  30. typedef ... X509_REVOKED;
  31. typedef ... X509_CRL;
  32. typedef ... X509;
  33. typedef ... NETSCAPE_SPKI;
  34. typedef ... PKCS8_PRIV_KEY_INFO;
  35. typedef void (*sk_X509_EXTENSION_freefunc)(X509_EXTENSION *);
  36. """
  37. FUNCTIONS = """
  38. X509 *X509_new(void);
  39. void X509_free(X509 *);
  40. X509 *X509_dup(X509 *);
  41. int X509_cmp(const X509 *, const X509 *);
  42. int X509_up_ref(X509 *);
  43. int X509_print_ex(BIO *, X509 *, unsigned long, unsigned long);
  44. int X509_set_version(X509 *, long);
  45. EVP_PKEY *X509_get_pubkey(X509 *);
  46. int X509_set_pubkey(X509 *, EVP_PKEY *);
  47. unsigned char *X509_alias_get0(X509 *, int *);
  48. int X509_sign(X509 *, EVP_PKEY *, const EVP_MD *);
  49. int X509_digest(const X509 *, const EVP_MD *, unsigned char *, unsigned int *);
  50. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *, long);
  51. unsigned long X509_subject_name_hash(X509 *);
  52. int X509_set_subject_name(X509 *, X509_NAME *);
  53. int X509_set_issuer_name(X509 *, X509_NAME *);
  54. int X509_add_ext(X509 *, X509_EXTENSION *, int);
  55. X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *);
  56. ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *);
  57. void X509_EXTENSION_free(X509_EXTENSION *);
  58. int X509_REQ_set_version(X509_REQ *, long);
  59. X509_REQ *X509_REQ_new(void);
  60. void X509_REQ_free(X509_REQ *);
  61. int X509_REQ_set_pubkey(X509_REQ *, EVP_PKEY *);
  62. int X509_REQ_set_subject_name(X509_REQ *, X509_NAME *);
  63. int X509_REQ_sign(X509_REQ *, EVP_PKEY *, const EVP_MD *);
  64. int X509_REQ_verify(X509_REQ *, EVP_PKEY *);
  65. EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *);
  66. int X509_REQ_print_ex(BIO *, X509_REQ *, unsigned long, unsigned long);
  67. int X509_REQ_add_extensions(X509_REQ *, X509_EXTENSIONS *);
  68. X509_EXTENSIONS *X509_REQ_get_extensions(X509_REQ *);
  69. X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *, int);
  70. int X509_REQ_get_attr_by_OBJ(const X509_REQ *, const ASN1_OBJECT *, int);
  71. void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *, int, int, void *);
  72. ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *, int);
  73. int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *);
  74. int X509_REQ_add1_attr_by_OBJ(X509_REQ *, const ASN1_OBJECT *,
  75. int, const unsigned char *, int);
  76. int X509V3_EXT_print(BIO *, X509_EXTENSION *, unsigned long, int);
  77. ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *);
  78. X509_REVOKED *X509_REVOKED_new(void);
  79. void X509_REVOKED_free(X509_REVOKED *);
  80. int X509_REVOKED_set_serialNumber(X509_REVOKED *, ASN1_INTEGER *);
  81. int X509_REVOKED_add_ext(X509_REVOKED *, X509_EXTENSION*, int);
  82. int X509_REVOKED_add1_ext_i2d(X509_REVOKED *, int, void *, int, unsigned long);
  83. X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *, int);
  84. int X509_REVOKED_set_revocationDate(X509_REVOKED *, ASN1_TIME *);
  85. X509_CRL *X509_CRL_new(void);
  86. X509_CRL *X509_CRL_dup(X509_CRL *);
  87. X509_CRL *d2i_X509_CRL_bio(BIO *, X509_CRL **);
  88. int X509_CRL_add0_revoked(X509_CRL *, X509_REVOKED *);
  89. int X509_CRL_add_ext(X509_CRL *, X509_EXTENSION *, int);
  90. int X509_CRL_cmp(const X509_CRL *, const X509_CRL *);
  91. int X509_CRL_print(BIO *, X509_CRL *);
  92. int X509_CRL_set_issuer_name(X509_CRL *, X509_NAME *);
  93. int X509_CRL_set_version(X509_CRL *, long);
  94. int X509_CRL_sign(X509_CRL *, EVP_PKEY *, const EVP_MD *);
  95. int X509_CRL_sort(X509_CRL *);
  96. int X509_CRL_verify(X509_CRL *, EVP_PKEY *);
  97. int i2d_X509_CRL_bio(BIO *, X509_CRL *);
  98. void X509_CRL_free(X509_CRL *);
  99. int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *, EVP_PKEY *);
  100. int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *, EVP_PKEY *, const EVP_MD *);
  101. char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *);
  102. NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *, int);
  103. EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *);
  104. int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *, EVP_PKEY *);
  105. NETSCAPE_SPKI *NETSCAPE_SPKI_new(void);
  106. void NETSCAPE_SPKI_free(NETSCAPE_SPKI *);
  107. /* ASN1 serialization */
  108. int i2d_X509_bio(BIO *, X509 *);
  109. X509 *d2i_X509_bio(BIO *, X509 **);
  110. int i2d_X509_REQ_bio(BIO *, X509_REQ *);
  111. X509_REQ *d2i_X509_REQ_bio(BIO *, X509_REQ **);
  112. int i2d_PrivateKey_bio(BIO *, EVP_PKEY *);
  113. EVP_PKEY *d2i_PrivateKey_bio(BIO *, EVP_PKEY **);
  114. int i2d_PUBKEY_bio(BIO *, EVP_PKEY *);
  115. EVP_PKEY *d2i_PUBKEY_bio(BIO *, EVP_PKEY **);
  116. ASN1_INTEGER *X509_get_serialNumber(X509 *);
  117. int X509_set_serialNumber(X509 *, ASN1_INTEGER *);
  118. const char *X509_verify_cert_error_string(long);
  119. const char *X509_get_default_cert_dir(void);
  120. const char *X509_get_default_cert_file(void);
  121. const char *X509_get_default_cert_dir_env(void);
  122. const char *X509_get_default_cert_file_env(void);
  123. int i2d_RSAPrivateKey_bio(BIO *, RSA *);
  124. RSA *d2i_RSAPublicKey_bio(BIO *, RSA **);
  125. int i2d_RSAPublicKey_bio(BIO *, RSA *);
  126. int i2d_DSAPrivateKey_bio(BIO *, DSA *);
  127. /* These became const X509 in 1.1.0 */
  128. int X509_get_ext_count(X509 *);
  129. X509_EXTENSION *X509_get_ext(X509 *, int);
  130. X509_NAME *X509_get_subject_name(X509 *);
  131. X509_NAME *X509_get_issuer_name(X509 *);
  132. /* This became const ASN1_OBJECT * in 1.1.0 */
  133. X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **,
  134. ASN1_OBJECT *, int,
  135. ASN1_OCTET_STRING *);
  136. /* This became const X509_EXTENSION * in 1.1.0 */
  137. int X509_EXTENSION_get_critical(X509_EXTENSION *);
  138. /* This became const X509_REVOKED * in 1.1.0 */
  139. int X509_REVOKED_get_ext_count(X509_REVOKED *);
  140. X509_EXTENSION *X509_REVOKED_get_ext(X509_REVOKED *, int);
  141. /* This became const X509_CRL * in 1.1.0 */
  142. X509_EXTENSION *X509_CRL_get_ext(X509_CRL *, int);
  143. int X509_CRL_get_ext_count(X509_CRL *);
  144. int X509_CRL_get0_by_serial(X509_CRL *, X509_REVOKED **, ASN1_INTEGER *);
  145. X509_REVOKED *X509_REVOKED_dup(X509_REVOKED *);
  146. X509_REVOKED *Cryptography_X509_REVOKED_dup(X509_REVOKED *);
  147. /* new in 1.0.2 */
  148. int i2d_re_X509_tbs(X509 *, unsigned char **);
  149. int X509_get_signature_nid(const X509 *);
  150. const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *);
  151. void X509_get0_signature(const ASN1_BIT_STRING **,
  152. const X509_ALGOR **, const X509 *);
  153. long X509_get_version(X509 *);
  154. ASN1_TIME *X509_get_notBefore(X509 *);
  155. ASN1_TIME *X509_get_notAfter(X509 *);
  156. ASN1_TIME *X509_getm_notBefore(X509 *);
  157. ASN1_TIME *X509_getm_notAfter(X509 *);
  158. long X509_REQ_get_version(X509_REQ *);
  159. X509_NAME *X509_REQ_get_subject_name(X509_REQ *);
  160. Cryptography_STACK_OF_X509 *sk_X509_new_null(void);
  161. void sk_X509_free(Cryptography_STACK_OF_X509 *);
  162. int sk_X509_num(Cryptography_STACK_OF_X509 *);
  163. int sk_X509_push(Cryptography_STACK_OF_X509 *, X509 *);
  164. X509 *sk_X509_value(Cryptography_STACK_OF_X509 *, int);
  165. X509_EXTENSIONS *sk_X509_EXTENSION_new_null(void);
  166. int sk_X509_EXTENSION_num(X509_EXTENSIONS *);
  167. X509_EXTENSION *sk_X509_EXTENSION_value(X509_EXTENSIONS *, int);
  168. int sk_X509_EXTENSION_push(X509_EXTENSIONS *, X509_EXTENSION *);
  169. int sk_X509_EXTENSION_insert(X509_EXTENSIONS *, X509_EXTENSION *, int);
  170. X509_EXTENSION *sk_X509_EXTENSION_delete(X509_EXTENSIONS *, int);
  171. void sk_X509_EXTENSION_free(X509_EXTENSIONS *);
  172. void sk_X509_EXTENSION_pop_free(X509_EXTENSIONS *, sk_X509_EXTENSION_freefunc);
  173. int sk_X509_REVOKED_num(Cryptography_STACK_OF_X509_REVOKED *);
  174. X509_REVOKED *sk_X509_REVOKED_value(Cryptography_STACK_OF_X509_REVOKED *, int);
  175. Cryptography_STACK_OF_X509_CRL *sk_X509_CRL_new_null(void);
  176. void sk_X509_CRL_free(Cryptography_STACK_OF_X509_CRL *);
  177. int sk_X509_CRL_num(Cryptography_STACK_OF_X509_CRL *);
  178. int sk_X509_CRL_push(Cryptography_STACK_OF_X509_CRL *, X509_CRL *);
  179. X509_CRL *sk_X509_CRL_value(Cryptography_STACK_OF_X509_CRL *, int);
  180. long X509_CRL_get_version(X509_CRL *);
  181. ASN1_TIME *X509_CRL_get_lastUpdate(X509_CRL *);
  182. ASN1_TIME *X509_CRL_get_nextUpdate(X509_CRL *);
  183. X509_NAME *X509_CRL_get_issuer(X509_CRL *);
  184. Cryptography_STACK_OF_X509_REVOKED *X509_CRL_get_REVOKED(X509_CRL *);
  185. /* These aren't macros these arguments are all const X on openssl > 1.0.x */
  186. int X509_CRL_set_lastUpdate(X509_CRL *, ASN1_TIME *);
  187. int X509_CRL_set_nextUpdate(X509_CRL *, ASN1_TIME *);
  188. int X509_set_notBefore(X509 *, ASN1_TIME *);
  189. int X509_set_notAfter(X509 *, ASN1_TIME *);
  190. int X509_set1_notBefore(X509 *, ASN1_TIME *);
  191. int X509_set1_notAfter(X509 *, ASN1_TIME *);
  192. EC_KEY *d2i_EC_PUBKEY_bio(BIO *, EC_KEY **);
  193. int i2d_EC_PUBKEY_bio(BIO *, EC_KEY *);
  194. EC_KEY *d2i_ECPrivateKey_bio(BIO *, EC_KEY **);
  195. int i2d_ECPrivateKey_bio(BIO *, EC_KEY *);
  196. // declared in safestack
  197. int sk_ASN1_OBJECT_num(Cryptography_STACK_OF_ASN1_OBJECT *);
  198. ASN1_OBJECT *sk_ASN1_OBJECT_value(Cryptography_STACK_OF_ASN1_OBJECT *, int);
  199. void sk_ASN1_OBJECT_free(Cryptography_STACK_OF_ASN1_OBJECT *);
  200. Cryptography_STACK_OF_ASN1_OBJECT *sk_ASN1_OBJECT_new_null(void);
  201. int sk_ASN1_OBJECT_push(Cryptography_STACK_OF_ASN1_OBJECT *, ASN1_OBJECT *);
  202. /* these functions were added in 1.1.0 */
  203. const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *);
  204. const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *);
  205. void X509_CRL_get0_signature(const X509_CRL *, const ASN1_BIT_STRING **,
  206. const X509_ALGOR **);
  207. int i2d_re_X509_REQ_tbs(X509_REQ *, unsigned char **);
  208. int i2d_re_X509_CRL_tbs(X509_CRL *, unsigned char **);
  209. void X509_REQ_get0_signature(const X509_REQ *, const ASN1_BIT_STRING **,
  210. const X509_ALGOR **);
  211. """
  212. CUSTOMIZATIONS = """
  213. #if CRYPTOGRAPHY_IS_LIBRESSL
  214. int i2d_re_X509_tbs(X509 *x, unsigned char **pp)
  215. {
  216. /* in 1.0.2+ this function also sets x->cert_info->enc.modified = 1
  217. but older OpenSSLs don't have the enc ASN1_ENCODING member in the
  218. X509 struct. Setting modified to 1 marks the encoding
  219. (x->cert_info->enc.enc) as invalid, but since the entire struct isn't
  220. present we don't care. */
  221. return i2d_X509_CINF(x->cert_info, pp);
  222. }
  223. #endif
  224. /* Being kept around for pyOpenSSL */
  225. X509_REVOKED *Cryptography_X509_REVOKED_dup(X509_REVOKED *rev) {
  226. return X509_REVOKED_dup(rev);
  227. }
  228. /* Added in 1.1.0 but we need it in all versions now due to the great
  229. opaquing. */
  230. #if CRYPTOGRAPHY_IS_LIBRESSL
  231. int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp)
  232. {
  233. req->req_info->enc.modified = 1;
  234. return i2d_X509_REQ_INFO(req->req_info, pp);
  235. }
  236. int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **pp) {
  237. crl->crl->enc.modified = 1;
  238. return i2d_X509_CRL_INFO(crl->crl, pp);
  239. }
  240. #endif
  241. """