| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include <openssl/asn1.h> |
| 6 |
|
| #include <openssl/objects.h> |
| 7 |
|
| #include <openssl/bio.h> |
| 8 |
|
| #include <openssl/crypto.h> |
| 9 |
|
| #include <openssl/err.h> |
| 10 |
|
| #include <openssl/evp.h> |
| 11 |
|
| #include <openssl/pem.h> |
| 12 |
|
| #include <openssl/x509.h> |
| 13 |
|
| #include <openssl/x509v3.h> |
| 14 |
|
| |
| 15 |
|
| typedef X509_STORE* Crypt__OpenSSL__VerifyX509; |
| 16 |
|
| typedef X509* Crypt__OpenSSL__X509; |
| 17 |
|
| |
| 18 |
5 |
| static int verify_cb(int ok, X509_STORE_CTX *ctx) { |
| 19 |
5 |
▶ | if (!ok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) |
2 |
3 |
5 |
|
| 20 |
2 |
▶ | switch (X509_STORE_CTX_get_error (ctx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (X509_STORE_CTX_get_error (ctx)) { |
1 |
1 |
2 |
|
| 21 |
1 |
| case X509_V_ERR_CERT_HAS_EXPIRED: |
| 22 |
|
| /* case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: */ |
| 23 |
|
| case X509_V_ERR_INVALID_CA: |
| 24 |
|
| case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 25 |
|
| case X509_V_ERR_INVALID_PURPOSE: |
| 26 |
|
| case X509_V_ERR_CRL_HAS_EXPIRED: |
| 27 |
|
| case X509_V_ERR_CRL_NOT_YET_VALID: |
| 28 |
|
| case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: |
| 29 |
1 |
| ok = 1; |
| 30 |
1 |
| break; |
| 31 |
|
| } |
| 32 |
5 |
| return(ok); |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
1 |
| static const char *ssl_error(void) { |
| 36 |
1 |
| return ERR_error_string(ERR_get_error(), NULL); |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
1 |
| static const char *ctx_error(X509_STORE_CTX *ctx) { |
| 40 |
1 |
| return X509_verify_cert_error_string(X509_STORE_CTX_get_error (ctx)); |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| MODULE = Crypt::OpenSSL::VerifyX509 PACKAGE = Crypt::OpenSSL::VerifyX509 |
| 44 |
|
| |
| 45 |
|
| PROTOTYPES: DISABLE |
| 46 |
|
| |
| 47 |
|
| #if OPENSSL_API_COMPAT >= 0x10100000L |
| 48 |
|
| #undef ERR_load_crypto_strings |
| 49 |
|
| #define ERR_load_crypto_strings() /* nothing */ |
| 50 |
|
| #undef OpenSSL_add_all_algorithms |
| 51 |
|
| # define OpenSSL_add_all_algorithms() /* nothing */ |
| 52 |
|
| #endif |
| 53 |
|
| |
| 54 |
|
| BOOT: |
| 55 |
6 |
| ERR_load_crypto_strings(); |
| 56 |
6 |
| ERR_load_ERR_strings(); |
| 57 |
6 |
| OpenSSL_add_all_algorithms(); |
| 58 |
|
| |
| 59 |
|
| Crypt::OpenSSL::VerifyX509 |
| 60 |
|
| new(class, cafile_str) |
| 61 |
|
| SV *class |
| 62 |
|
| SV *cafile_str |
| 63 |
|
| |
| 64 |
|
| PREINIT: |
| 65 |
|
| |
| 66 |
5 |
| int i = 1; |
| 67 |
5 |
| X509_LOOKUP *lookup = NULL; |
| 68 |
|
| STRLEN len; |
| 69 |
|
| char *cafile; |
| 70 |
|
| |
| 71 |
|
| CODE: |
| 72 |
|
| |
| 73 |
|
| (void) SvPV_nolen(class); |
| 74 |
|
| |
| 75 |
5 |
| RETVAL = X509_STORE_new(); |
| 76 |
5 |
▶ | if (RETVAL == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL == NULL) |
0 |
5 |
5 |
|
| 77 |
0 |
| croak("failure to allocate x509 store: %s", ssl_error()); |
| 78 |
|
| |
| 79 |
5 |
| X509_STORE_set_verify_cb_func(RETVAL,verify_cb); |
| 80 |
|
| |
| 81 |
|
| /* load CA file given */ |
| 82 |
5 |
| lookup = X509_STORE_add_lookup(RETVAL, X509_LOOKUP_file()); |
| 83 |
5 |
▶ | if (lookup == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lookup == NULL) |
0 |
5 |
5 |
|
| 84 |
0 |
| croak("failure to add file lookup to store: %s", ssl_error()); |
| 85 |
|
| |
| 86 |
5 |
| cafile = SvPV(cafile_str, len); |
| 87 |
5 |
| i = X509_LOOKUP_load_file(lookup, cafile, X509_FILETYPE_PEM); |
| 88 |
|
| |
| 89 |
5 |
▶ | if (!i) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i) |
1 |
4 |
5 |
|
| 90 |
1 |
| croak("load CA cert: %s", ssl_error()); |
| 91 |
|
| |
| 92 |
|
| /* default hash_dir lookup */ |
| 93 |
4 |
| lookup = X509_STORE_add_lookup(RETVAL,X509_LOOKUP_hash_dir()); |
| 94 |
4 |
▶ | if (lookup == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lookup == NULL) |
0 |
4 |
4 |
|
| 95 |
0 |
| croak("failure to add hash_dir lookup to store: %s", ssl_error()); |
| 96 |
|
| |
| 97 |
4 |
| X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT); |
| 98 |
|
| |
| 99 |
4 |
| ERR_clear_error(); |
| 100 |
|
| |
| 101 |
|
| OUTPUT: |
| 102 |
|
| RETVAL |
| 103 |
|
| |
| 104 |
|
| int |
| 105 |
|
| verify(store, x509) |
| 106 |
|
| Crypt::OpenSSL::VerifyX509 store; |
| 107 |
|
| Crypt::OpenSSL::X509 x509; |
| 108 |
|
| |
| 109 |
|
| PREINIT: |
| 110 |
|
| |
| 111 |
|
| X509_STORE_CTX *csc; |
| 112 |
|
| |
| 113 |
|
| CODE: |
| 114 |
|
| |
| 115 |
2 |
▶ | if (x509 == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (x509 == NULL) |
0 |
2 |
2 |
|
| 116 |
0 |
| croak("no cert to verify"); |
| 117 |
|
| |
| 118 |
2 |
| csc = X509_STORE_CTX_new(); |
| 119 |
2 |
▶ | if (csc == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (csc == NULL) |
0 |
2 |
2 |
|
| 120 |
0 |
| croak("csc new: %s", ssl_error()); |
| 121 |
|
| |
| 122 |
2 |
| X509_STORE_set_flags(store, 0); |
| 123 |
|
| |
| 124 |
2 |
▶ | if (!X509_STORE_CTX_init(csc,store,x509,NULL)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!X509_STORE_CTX_init(csc,store,x509,NULL)) |
0 |
2 |
2 |
|
| 125 |
0 |
| croak("store ctx init: %s", ssl_error()); |
| 126 |
|
| |
| 127 |
2 |
| RETVAL = X509_verify_cert(csc); |
| 128 |
|
| |
| 129 |
2 |
▶ | if (!RETVAL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) |
1 |
1 |
2 |
|
| 130 |
1 |
| croak("verify: %s", ctx_error(csc)); |
| 131 |
|
| |
| 132 |
1 |
| X509_STORE_CTX_free(csc); |
| 133 |
|
| |
| 134 |
|
| OUTPUT: |
| 135 |
|
| RETVAL |
| 136 |
|
| |
| 137 |
|
| void |
| 138 |
|
| DESTROY(store) |
| 139 |
|
| Crypt::OpenSSL::VerifyX509 store; |
| 140 |
|
| |
| 141 |
|
| PPCODE: |
| 142 |
|
| |
| 143 |
4 |
▶ | if (store) X509_STORE_free(store); store = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (store) X509_STORE_free(store); store = 0; |
4 |
0 |
4 |
|
| 144 |
|
| |
| 145 |
|
| |
| 146 |
|
| #if OPENSSL_API_COMPAT >= 0x10100000L |
| 147 |
|
| void |
| 148 |
|
| __X509_cleanup(void) |
| 149 |
|
| PPCODE: |
| 150 |
|
| |
| 151 |
|
| /* deinitialisation is done automatically */ |
| 152 |
|
| |
| 153 |
|
| #else |
| 154 |
|
| void |
| 155 |
|
| __X509_cleanup(void) |
| 156 |
|
| PPCODE: |
| 157 |
|
| |
| 158 |
6 |
| CRYPTO_cleanup_all_ex_data(); |
| 159 |
6 |
| ERR_free_strings(); |
| 160 |
6 |
| ERR_remove_state(0); |
| 161 |
6 |
| EVP_cleanup(); |
| 162 |
|
| |
| 163 |
|
| #endif |
| 164 |
|
| |