| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
|
| 7 |
INCLUDES = """ |
| 8 |
#include <openssl/ssl.h> |
| 9 |
|
| 10 |
/* |
| 11 |
* This is part of a work-around for the difficulty cffi has in dealing with |
| 12 |
* `STACK_OF(foo)` as the name of a type. We invent a new, simpler name that |
| 13 |
* will be an alias for this type and use the alias throughout. This works |
| 14 |
* together with another opaque typedef for the same name in the TYPES section. |
| 15 |
* Note that the result is an opaque type. |
| 16 |
*/ |
| 17 |
typedef STACK_OF(X509) Cryptography_STACK_OF_X509; |
| 18 |
typedef STACK_OF(X509_CRL) Cryptography_STACK_OF_X509_CRL; |
| 19 |
typedef STACK_OF(X509_REVOKED) Cryptography_STACK_OF_X509_REVOKED; |
| 20 |
""" |
| 21 |
|
| 22 |
TYPES = """ |
| 23 |
typedef ... Cryptography_STACK_OF_X509; |
| 24 |
typedef ... Cryptography_STACK_OF_X509_CRL; |
| 25 |
typedef ... Cryptography_STACK_OF_X509_REVOKED; |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
ASN1_OBJECT *algorithm; |
| 29 |
...; |
| 30 |
} X509_ALGOR; |
| 31 |
|
| 32 |
typedef ... X509_EXTENSION; |
| 33 |
typedef ... X509_EXTENSIONS; |
| 34 |
typedef ... X509_REQ; |
| 35 |
typedef ... X509_REVOKED; |
| 36 |
typedef ... X509_CRL; |
| 37 |
typedef ... X509; |
| 38 |
|
| 39 |
typedef ... NETSCAPE_SPKI; |
| 40 |
|
| 41 |
typedef ... PKCS8_PRIV_KEY_INFO; |
| 42 |
|
| 43 |
typedef void (*sk_X509_EXTENSION_freefunc)(X509_EXTENSION *); |
| 44 |
static const long XN_FLAG_RFC2253; |
| 45 |
""" |
| 46 |
|
| 47 |
FUNCTIONS = """ |
| 48 |
X509 *X509_new(void); |
| 49 |
void X509_free(X509 *); |
| 50 |
X509 *X509_dup(X509 *); |
| 51 |
int X509_cmp(const X509 *, const X509 *); |
| 52 |
int X509_up_ref(X509 *); |
| 53 |
|
| 54 |
int X509_print_ex(BIO *, X509 *, unsigned long, unsigned long); |
| 55 |
|
| 56 |
int X509_set_version(X509 *, long); |
| 57 |
|
| 58 |
EVP_PKEY *X509_get_pubkey(X509 *); |
| 59 |
int X509_set_pubkey(X509 *, EVP_PKEY *); |
| 60 |
|
| 61 |
unsigned char *X509_alias_get0(X509 *, int *); |
| 62 |
int X509_sign(X509 *, EVP_PKEY *, const EVP_MD *); |
| 63 |
|
| 64 |
int X509_digest(const X509 *, const EVP_MD *, unsigned char *, unsigned int *); |
| 65 |
|
| 66 |
ASN1_TIME *X509_gmtime_adj(ASN1_TIME *, long); |
| 67 |
|
| 68 |
unsigned long X509_subject_name_hash(X509 *); |
| 69 |
|
| 70 |
int X509_set_subject_name(X509 *, X509_NAME *); |
| 71 |
|
| 72 |
int X509_set_issuer_name(X509 *, X509_NAME *); |
| 73 |
|
| 74 |
int X509_add_ext(X509 *, X509_EXTENSION *, int); |
| 75 |
X509_EXTENSION *X509_EXTENSION_dup(X509_EXTENSION *); |
| 76 |
|
| 77 |
ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *); |
| 78 |
void X509_EXTENSION_free(X509_EXTENSION *); |
| 79 |
|
| 80 |
int i2d_X509(X509 *, unsigned char **); |
| 81 |
|
| 82 |
int X509_REQ_set_version(X509_REQ *, long); |
| 83 |
X509_REQ *X509_REQ_new(void); |
| 84 |
void X509_REQ_free(X509_REQ *); |
| 85 |
int X509_REQ_set_pubkey(X509_REQ *, EVP_PKEY *); |
| 86 |
int X509_REQ_set_subject_name(X509_REQ *, X509_NAME *); |
| 87 |
int X509_REQ_sign(X509_REQ *, EVP_PKEY *, const EVP_MD *); |
| 88 |
int X509_REQ_verify(X509_REQ *, EVP_PKEY *); |
| 89 |
EVP_PKEY *X509_REQ_get_pubkey(X509_REQ *); |
| 90 |
int X509_REQ_print_ex(BIO *, X509_REQ *, unsigned long, unsigned long); |
| 91 |
int X509_REQ_add_extensions(X509_REQ *, X509_EXTENSIONS *); |
| 92 |
X509_EXTENSIONS *X509_REQ_get_extensions(X509_REQ *); |
| 93 |
|
| 94 |
int X509V3_EXT_print(BIO *, X509_EXTENSION *, unsigned long, int); |
| 95 |
ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *); |
| 96 |
|
| 97 |
X509_REVOKED *X509_REVOKED_new(void); |
| 98 |
void X509_REVOKED_free(X509_REVOKED *); |
| 99 |
|
| 100 |
int X509_REVOKED_set_serialNumber(X509_REVOKED *, ASN1_INTEGER *); |
| 101 |
|
| 102 |
int X509_REVOKED_add_ext(X509_REVOKED *, X509_EXTENSION*, int); |
| 103 |
int X509_REVOKED_add1_ext_i2d(X509_REVOKED *, int, void *, int, unsigned long); |
| 104 |
X509_EXTENSION *X509_REVOKED_delete_ext(X509_REVOKED *, int); |
| 105 |
|
| 106 |
int X509_REVOKED_set_revocationDate(X509_REVOKED *, ASN1_TIME *); |
| 107 |
|
| 108 |
X509_CRL *X509_CRL_new(void); |
| 109 |
X509_CRL *X509_CRL_dup(X509_CRL *); |
| 110 |
X509_CRL *d2i_X509_CRL_bio(BIO *, X509_CRL **); |
| 111 |
int X509_CRL_add0_revoked(X509_CRL *, X509_REVOKED *); |
| 112 |
int X509_CRL_add_ext(X509_CRL *, X509_EXTENSION *, int); |
| 113 |
int X509_CRL_cmp(const X509_CRL *, const X509_CRL *); |
| 114 |
int X509_CRL_print(BIO *, X509_CRL *); |
| 115 |
int X509_CRL_set_issuer_name(X509_CRL *, X509_NAME *); |
| 116 |
int X509_CRL_set_version(X509_CRL *, long); |
| 117 |
int X509_CRL_sign(X509_CRL *, EVP_PKEY *, const EVP_MD *); |
| 118 |
int X509_CRL_sort(X509_CRL *); |
| 119 |
int X509_CRL_verify(X509_CRL *, EVP_PKEY *); |
| 120 |
int i2d_X509_CRL_bio(BIO *, X509_CRL *); |
| 121 |
void X509_CRL_free(X509_CRL *); |
| 122 |
|
| 123 |
int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *, EVP_PKEY *); |
| 124 |
int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *, EVP_PKEY *, const EVP_MD *); |
| 125 |
char *NETSCAPE_SPKI_b64_encode(NETSCAPE_SPKI *); |
| 126 |
NETSCAPE_SPKI *NETSCAPE_SPKI_b64_decode(const char *, int); |
| 127 |
EVP_PKEY *NETSCAPE_SPKI_get_pubkey(NETSCAPE_SPKI *); |
| 128 |
int NETSCAPE_SPKI_set_pubkey(NETSCAPE_SPKI *, EVP_PKEY *); |
| 129 |
NETSCAPE_SPKI *NETSCAPE_SPKI_new(void); |
| 130 |
void NETSCAPE_SPKI_free(NETSCAPE_SPKI *); |
| 131 |
|
| 132 |
/* ASN1 serialization */ |
| 133 |
int i2d_X509_bio(BIO *, X509 *); |
| 134 |
X509 *d2i_X509_bio(BIO *, X509 **); |
| 135 |
|
| 136 |
int i2d_X509_REQ_bio(BIO *, X509_REQ *); |
| 137 |
X509_REQ *d2i_X509_REQ_bio(BIO *, X509_REQ **); |
| 138 |
|
| 139 |
int i2d_PrivateKey_bio(BIO *, EVP_PKEY *); |
| 140 |
EVP_PKEY *d2i_PrivateKey_bio(BIO *, EVP_PKEY **); |
| 141 |
int i2d_PUBKEY_bio(BIO *, EVP_PKEY *); |
| 142 |
EVP_PKEY *d2i_PUBKEY_bio(BIO *, EVP_PKEY **); |
| 143 |
|
| 144 |
ASN1_INTEGER *X509_get_serialNumber(X509 *); |
| 145 |
int X509_set_serialNumber(X509 *, ASN1_INTEGER *); |
| 146 |
|
| 147 |
const char *X509_verify_cert_error_string(long); |
| 148 |
|
| 149 |
const char *X509_get_default_cert_dir(void); |
| 150 |
const char *X509_get_default_cert_file(void); |
| 151 |
const char *X509_get_default_cert_dir_env(void); |
| 152 |
const char *X509_get_default_cert_file_env(void); |
| 153 |
|
| 154 |
int i2d_RSAPrivateKey_bio(BIO *, RSA *); |
| 155 |
RSA *d2i_RSAPublicKey_bio(BIO *, RSA **); |
| 156 |
int i2d_RSAPublicKey_bio(BIO *, RSA *); |
| 157 |
int i2d_DSAPrivateKey_bio(BIO *, DSA *); |
| 158 |
|
| 159 |
/* These became const X509 in 1.1.0 */ |
| 160 |
int X509_get_ext_count(X509 *); |
| 161 |
X509_EXTENSION *X509_get_ext(X509 *, int); |
| 162 |
int X509_get_ext_by_NID(X509 *, int, int); |
| 163 |
X509_NAME *X509_get_subject_name(X509 *); |
| 164 |
X509_NAME *X509_get_issuer_name(X509 *); |
| 165 |
|
| 166 |
/* This became const ASN1_OBJECT * in 1.1.0 */ |
| 167 |
X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **, |
| 168 |
ASN1_OBJECT *, int, |
| 169 |
ASN1_OCTET_STRING *); |
| 170 |
|
| 171 |
|
| 172 |
/* This became const X509_EXTENSION * in 1.1.0 */ |
| 173 |
int X509_EXTENSION_get_critical(X509_EXTENSION *); |
| 174 |
|
| 175 |
/* This became const X509_REVOKED * in 1.1.0 */ |
| 176 |
int X509_REVOKED_get_ext_count(X509_REVOKED *); |
| 177 |
X509_EXTENSION *X509_REVOKED_get_ext(X509_REVOKED *, int); |
| 178 |
|
| 179 |
/* This became const X509_CRL * in 1.1.0 */ |
| 180 |
X509_EXTENSION *X509_CRL_get_ext(X509_CRL *, int); |
| 181 |
int X509_CRL_get_ext_count(X509_CRL *); |
| 182 |
|
| 183 |
int X509_CRL_get0_by_serial(X509_CRL *, X509_REVOKED **, ASN1_INTEGER *); |
| 184 |
|
| 185 |
X509_REVOKED *Cryptography_X509_REVOKED_dup(X509_REVOKED *); |
| 186 |
|
| 187 |
/* new in 1.0.2 */ |
| 188 |
int i2d_re_X509_tbs(X509 *, unsigned char **); |
| 189 |
int X509_get_signature_nid(const X509 *); |
| 190 |
|
| 191 |
const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *); |
| 192 |
|
| 193 |
void X509_get0_signature(const ASN1_BIT_STRING **, |
| 194 |
const X509_ALGOR **, const X509 *); |
| 195 |
|
| 196 |
long X509_get_version(X509 *); |
| 197 |
|
| 198 |
ASN1_TIME *X509_get_notBefore(X509 *); |
| 199 |
ASN1_TIME *X509_get_notAfter(X509 *); |
| 200 |
|
| 201 |
long X509_REQ_get_version(X509_REQ *); |
| 202 |
X509_NAME *X509_REQ_get_subject_name(X509_REQ *); |
| 203 |
|
| 204 |
Cryptography_STACK_OF_X509 *sk_X509_new_null(void); |
| 205 |
void sk_X509_free(Cryptography_STACK_OF_X509 *); |
| 206 |
int sk_X509_num(Cryptography_STACK_OF_X509 *); |
| 207 |
int sk_X509_push(Cryptography_STACK_OF_X509 *, X509 *); |
| 208 |
X509 *sk_X509_value(Cryptography_STACK_OF_X509 *, int); |
| 209 |
|
| 210 |
X509_EXTENSIONS *sk_X509_EXTENSION_new_null(void); |
| 211 |
int sk_X509_EXTENSION_num(X509_EXTENSIONS *); |
| 212 |
X509_EXTENSION *sk_X509_EXTENSION_value(X509_EXTENSIONS *, int); |
| 213 |
int sk_X509_EXTENSION_push(X509_EXTENSIONS *, X509_EXTENSION *); |
| 214 |
int sk_X509_EXTENSION_insert(X509_EXTENSIONS *, X509_EXTENSION *, int); |
| 215 |
X509_EXTENSION *sk_X509_EXTENSION_delete(X509_EXTENSIONS *, int); |
| 216 |
void sk_X509_EXTENSION_free(X509_EXTENSIONS *); |
| 217 |
void sk_X509_EXTENSION_pop_free(X509_EXTENSIONS *, sk_X509_EXTENSION_freefunc); |
| 218 |
|
| 219 |
int sk_X509_REVOKED_num(Cryptography_STACK_OF_X509_REVOKED *); |
| 220 |
X509_REVOKED *sk_X509_REVOKED_value(Cryptography_STACK_OF_X509_REVOKED *, int); |
| 221 |
|
| 222 |
Cryptography_STACK_OF_X509_CRL *sk_X509_CRL_new_null(void); |
| 223 |
void sk_X509_CRL_free(Cryptography_STACK_OF_X509_CRL *); |
| 224 |
int sk_X509_CRL_num(Cryptography_STACK_OF_X509_CRL *); |
| 225 |
int sk_X509_CRL_push(Cryptography_STACK_OF_X509_CRL *, X509_CRL *); |
| 226 |
X509_CRL *sk_X509_CRL_value(Cryptography_STACK_OF_X509_CRL *, int); |
| 227 |
|
| 228 |
long X509_CRL_get_version(X509_CRL *); |
| 229 |
X509_NAME *X509_CRL_get_issuer(X509_CRL *); |
| 230 |
Cryptography_STACK_OF_X509_REVOKED *X509_CRL_get_REVOKED(X509_CRL *); |
| 231 |
|
| 232 |
/* These aren't macros these arguments are all const X on openssl > 1.0.x */ |
| 233 |
int X509_CRL_set_lastUpdate(X509_CRL *, ASN1_TIME *); |
| 234 |
int X509_CRL_set_nextUpdate(X509_CRL *, ASN1_TIME *); |
| 235 |
int X509_set_notBefore(X509 *, ASN1_TIME *); |
| 236 |
int X509_set_notAfter(X509 *, ASN1_TIME *); |
| 237 |
|
| 238 |
EC_KEY *d2i_EC_PUBKEY_bio(BIO *, EC_KEY **); |
| 239 |
int i2d_EC_PUBKEY_bio(BIO *, EC_KEY *); |
| 240 |
EC_KEY *d2i_ECPrivateKey_bio(BIO *, EC_KEY **); |
| 241 |
int i2d_ECPrivateKey_bio(BIO *, EC_KEY *); |
| 242 |
|
| 243 |
// declared in safestack |
| 244 |
int sk_ASN1_OBJECT_num(Cryptography_STACK_OF_ASN1_OBJECT *); |
| 245 |
ASN1_OBJECT *sk_ASN1_OBJECT_value(Cryptography_STACK_OF_ASN1_OBJECT *, int); |
| 246 |
void sk_ASN1_OBJECT_free(Cryptography_STACK_OF_ASN1_OBJECT *); |
| 247 |
Cryptography_STACK_OF_ASN1_OBJECT *sk_ASN1_OBJECT_new_null(void); |
| 248 |
int sk_ASN1_OBJECT_push(Cryptography_STACK_OF_ASN1_OBJECT *, ASN1_OBJECT *); |
| 249 |
|
| 250 |
/* these functions were added in 1.1.0 */ |
| 251 |
const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *); |
| 252 |
const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *); |
| 253 |
void X509_CRL_get0_signature(const X509_CRL *, const ASN1_BIT_STRING **, |
| 254 |
const X509_ALGOR **); |
| 255 |
int i2d_re_X509_REQ_tbs(X509_REQ *, unsigned char **); |
| 256 |
int i2d_re_X509_CRL_tbs(X509_CRL *, unsigned char **); |
| 257 |
void X509_REQ_get0_signature(const X509_REQ *, const ASN1_BIT_STRING **, |
| 258 |
const X509_ALGOR **); |
| 259 |
""" |
| 260 |
|
| 261 |
CUSTOMIZATIONS = """ |
| 262 |
/* Added in 1.0.2 beta but we need it in all versions now due to the great |
| 263 |
opaquing. */ |
| 264 |
#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_102 && !CRYPTOGRAPHY_LIBRESSL_27_OR_GREATER |
| 265 |
/* from x509/x_x509.c version 1.0.2 */ |
| 266 |
void X509_get0_signature(const ASN1_BIT_STRING **psig, |
| 267 |
const X509_ALGOR **palg, const X509 *x) |
| 268 |
{ |
| 269 |
if (psig) |
| 270 |
*psig = x->signature; |
| 271 |
if (palg) |
| 272 |
*palg = x->sig_alg; |
| 273 |
} |
| 274 |
|
| 275 |
int X509_get_signature_nid(const X509 *x) |
| 276 |
{ |
| 277 |
return OBJ_obj2nid(x->sig_alg->algorithm); |
| 278 |
} |
| 279 |
|
| 280 |
#endif |
| 281 |
|
| 282 |
/* Added in 1.0.2 but we need it in all versions now due to the great |
| 283 |
opaquing. */ |
| 284 |
#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_102 |
| 285 |
/* from x509/x_x509.c */ |
| 286 |
int i2d_re_X509_tbs(X509 *x, unsigned char **pp) |
| 287 |
{ |
| 288 |
/* in 1.0.2+ this function also sets x->cert_info->enc.modified = 1 |
| 289 |
but older OpenSSLs don't have the enc ASN1_ENCODING member in the |
| 290 |
X509 struct. Setting modified to 1 marks the encoding |
| 291 |
(x->cert_info->enc.enc) as invalid, but since the entire struct isn't |
| 292 |
present we don't care. */ |
| 293 |
return i2d_X509_CINF(x->cert_info, pp); |
| 294 |
} |
| 295 |
#endif |
| 296 |
|
| 297 |
/* X509_REVOKED_dup only exists on 1.0.2+. It is implemented using |
| 298 |
IMPLEMENT_ASN1_DUP_FUNCTION. The below is the equivalent so we have |
| 299 |
it available on all OpenSSLs. */ |
| 300 |
X509_REVOKED *Cryptography_X509_REVOKED_dup(X509_REVOKED *rev) { |
| 301 |
#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_102 |
| 302 |
return ASN1_item_dup(ASN1_ITEM_rptr(X509_REVOKED), rev); |
| 303 |
#else |
| 304 |
return X509_REVOKED_dup(rev); |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
/* Added in 1.1.0 but we need it in all versions now due to the great |
| 309 |
opaquing. */ |
| 310 |
#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110 |
| 311 |
int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) |
| 312 |
{ |
| 313 |
req->req_info->enc.modified = 1; |
| 314 |
return i2d_X509_REQ_INFO(req->req_info, pp); |
| 315 |
} |
| 316 |
int i2d_re_X509_CRL_tbs(X509_CRL *crl, unsigned char **pp) { |
| 317 |
crl->crl->enc.modified = 1; |
| 318 |
return i2d_X509_CRL_INFO(crl->crl, pp); |
| 319 |
} |
| 320 |
|
| 321 |
#if !CRYPTOGRAPHY_LIBRESSL_27_OR_GREATER |
| 322 |
int X509_up_ref(X509 *x) { |
| 323 |
return CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 324 |
} |
| 325 |
|
| 326 |
const X509_ALGOR *X509_get0_tbs_sigalg(const X509 *x) |
| 327 |
{ |
| 328 |
return x->cert_info->signature; |
| 329 |
} |
| 330 |
|
| 331 |
/* from x509/x509_req.c */ |
| 332 |
void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, |
| 333 |
const X509_ALGOR **palg) |
| 334 |
{ |
| 335 |
if (psig != NULL) |
| 336 |
*psig = req->signature; |
| 337 |
if (palg != NULL) |
| 338 |
*palg = req->sig_alg; |
| 339 |
} |
| 340 |
void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig, |
| 341 |
const X509_ALGOR **palg) |
| 342 |
{ |
| 343 |
if (psig != NULL) |
| 344 |
*psig = crl->signature; |
| 345 |
if (palg != NULL) |
| 346 |
*palg = crl->sig_alg; |
| 347 |
} |
| 348 |
const ASN1_TIME *X509_REVOKED_get0_revocationDate(const X509_REVOKED *x) |
| 349 |
{ |
| 350 |
return x->revocationDate; |
| 351 |
} |
| 352 |
const ASN1_INTEGER *X509_REVOKED_get0_serialNumber(const X509_REVOKED *x) |
| 353 |
{ |
| 354 |
return x->serialNumber; |
| 355 |
} |
| 356 |
#endif |
| 357 |
#endif |
| 358 |
""" |