=> 31b29c8624cff3837305298846571ba97b91780e
[1mdiff --git a/include/the_Foundation/tlsrequest.h b/include/the_Foundation/tlsrequest.h[m [1mindex 0fd9acf..5a24448 100644[m [1m--- a/include/the_Foundation/tlsrequest.h[m [1m+++ b/include/the_Foundation/tlsrequest.h[m [36m@@ -99,8 +99,10 @@[m [miBool verifyIp_TlsCertificate (const iTlsCertificate *, const[m iBool equal_TlsCertificate (const iTlsCertificate *, const iTlsCertificate *);[m iString * pem_TlsCertificate (const iTlsCertificate *);[m iString * privateKeyPem_TlsCertificate(const iTlsCertificate *);[m [31m-iBlock * fingerprint_TlsCertificate (const iTlsCertificate *);[m [31m-iBlock * privateKeyFingerprint_TlsCertificate(const iTlsCertificate *d);[m [32m+[m [32m+[m[32miBlock * fingerprint_TlsCertificate (const iTlsCertificate *);[m [32m+[m[32miBlock * publicKeyFingerprint_TlsCertificate (const iTlsCertificate *);[m [32m+[m[32miBlock * privateKeyFingerprint_TlsCertificate(const iTlsCertificate *);[m [m /*----------------------------------------------------------------------------------------------*/[m [m [36m@@ -135,7 +137,10 @@[m [menum iTlsRequestStatus status_TlsRequest (const iTlsRequest *);[m const iString * errorMessage_TlsRequest (const iTlsRequest *);[m const iTlsCertificate * serverCertificate_TlsRequest(const iTlsRequest *);[m [m [32m+[m[32mtypedef iBool (*iTlsRequestVerifyFunc)(iAny *, const iTlsCertificate *, int depth);[m [32m+[m void setCACertificates_TlsRequest(const iString *caFile, const iString *caPath);[m void setCiphers_TlsRequest (const char *cipherList);[m [32m+[m[32mvoid setVerifyFunc_TlsRequest (iTlsRequestVerifyFunc verifyFunc, iAny *context);[m [m iEndPublic[m [1mdiff --git a/src/tlsrequest.c b/src/tlsrequest.c[m [1mindex 8c1ede7..6fbbe96 100644[m [1m--- a/src/tlsrequest.c[m [1m+++ b/src/tlsrequest.c[m [36m@@ -50,12 +50,42 @@[m [mstatic iContext *context_;[m static iBool isPrngSeeded_;[m [m static void initContext_(void);[m [32m+[m[32mstatic iTlsCertificate *newX509Chain_TlsCertificate_(X509 *cert, STACK_OF(X509) *chain);[m [m struct Impl_Context {[m [31m- SSL_CTX *ctx;[m [31m- X509_STORE *certStore;[m [32m+[m[32m SSL_CTX * ctx;[m [32m+[m[32m X509_STORE * certStore;[m [32m+[m[32m iTlsRequestVerifyFunc userVerifyFunc;[m [32m+[m[32m iAny * userVerifyContext;[m };[m [m [32m+[m[32mstatic int verifyCallback_Context_(int preverifyOk, X509_STORE_CTX *storeCtx) {[m [32m+[m[32m iUnused(preverifyOk);[m [32m+[m[32m iAssert(context_); /* must've been initialized by now */[m [32m+[m[32m iContext *d = context_;[m [32m+[m[32m const int depth = X509_STORE_CTX_get_error_depth(storeCtx);[m [32m+[m[32m X509 * x509 = X509_STORE_CTX_get_current_cert(storeCtx);[m [32m+[m[32m X509_up_ref(x509); /* keep a reference */[m [32m+[m[32m iTlsCertificate *cert = newX509Chain_TlsCertificate_(x509, NULL);[m [32m+[m[32m#if 0[m [32m+[m[32m /* Debugging. */ {[m [32m+[m[32m X509_NAME *name = X509_get_subject_name(x509);[m [32m+[m[32m printf("[TlsRequest] verifyCallback_Context: depth=%d\n", depth);[m [32m+[m[32m X509_NAME_print_ex_fp(stdout, name, 0, 0);[m [32m+[m[32m printf("\n");[m [32m+[m[32m iBlock *fp = publicKeyFingerprint_TlsCertificate(cert);[m [32m+[m[32m printf("key fp: %s\n", cstrCollect_String(hexEncode_Block(fp)));[m [32m+[m[32m delete_Block(fp);[m [32m+[m[32m }[m [32m+[m[32m#endif[m [32m+[m[32m int result = 1; /* accept everything by default */[m [32m+[m[32m if (d->userVerifyFunc) {[m [32m+[m[32m result = d->userVerifyFunc(d->userVerifyContext, cert, depth) ? 1 : 0;[m[41m [m [32m+[m[32m }[m[41m [m [32m+[m[32m delete_TlsCertificate(cert); /* free the reference */[m [32m+[m[32m return result;[m [32m+[m[32m}[m [32m+[m void init_Context(iContext *d) {[m #if OPENSSL_API_COMPAT >= 0x10100000L[m OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);[m [36m@@ -73,7 +103,10 @@[m [mvoid init_Context(iContext *d) {[m iDebug("[TlsRequest] Failed to initialize OpenSSL\n");[m iAssert(d->ctx);[m }[m [31m- SSL_CTX_set_verify(d->ctx, SSL_VERIFY_NONE, NULL); /* allow manual verification */[m [32m+[m[32m d->userVerifyContext = NULL;[m [32m+[m[32m d->userVerifyFunc = NULL;[m [32m+[m[32m// SSL_CTX_set_verify(d->ctx, SSL_VERIFY_NONE, NULL); /* disable verification */[m [32m+[m[32m SSL_CTX_set_verify(d->ctx, SSL_VERIFY_PEER, verifyCallback_Context_); /* allow manual verification */[m /* Bug workarounds: https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_options.html */[m SSL_CTX_set_options(d->ctx, SSL_OP_ALL);[m d->certStore = NULL;[m [36m@@ -101,6 +134,13 @@[m [mvoid setCACertificates_TlsRequest(const iString *caFile, const iString *caPath)[m }[m }[m [m [32m+[m[32mvoid setVerifyFunc_TlsRequest(iTlsRequestVerifyFunc verifyFunc, iAny *context) {[m [32m+[m[32m initContext_();[m [32m+[m[32m iContext *d = context_;[m [32m+[m[32m d->userVerifyContext = context;[m [32m+[m[32m d->userVerifyFunc = verifyFunc;[m [32m+[m[32m}[m [32m+[m iDefineTypeConstruction(Context)[m [m static void globalCleanup_TlsRequest_(void) {[m [36m@@ -159,13 +199,9 @@[m [mvoid deinit_TlsCertificate(iTlsCertificate *d) {[m }[m }[m [m [31m-//static iTlsCertificate *newX509_TlsCertificate_(X509 *cert) {[m [31m-// iTlsCertificate *d = new_TlsCertificate();[m [31m-// d->cert = cert;[m [31m-// return d;[m [31m-//}[m [31m-[m static iTlsCertificate *newX509Chain_TlsCertificate_(X509 *cert, STACK_OF(X509) *chain) {[m [32m+[m[32m /* Note that `chain` includes `cert`; a bit redundant. However, the chain is only[m [32m+[m[32m used when verifying the `cert` via OpenSSL's API. */[m iTlsCertificate *d = new_TlsCertificate();[m d->cert = cert;[m d->chain = chain;[m [36m@@ -406,19 +442,39 @@[m [miBool equal_TlsCertificate(const iTlsCertificate *d, const iTlsCertificate *othe[m return X509_cmp(d->cert, other->cert) == 0;[m }[m [m [32m+[m[32mstatic void calcSHA256_(BIO *src, iBlock *dst) {[m [32m+[m[32m iBlock der;[m [32m+[m[32m init_Block(&der, 0);[m [32m+[m[32m readAllFromBIO_(src, &der);[m [32m+[m[32m SHA256(constData_Block(&der), size_Block(&der), data_Block(dst));[m [32m+[m[32m deinit_Block(&der);[m [32m+[m[32m}[m [32m+[m iBlock *fingerprint_TlsCertificate(const iTlsCertificate *d) {[m iBlock *sha = new_Block(SHA256_DIGEST_LENGTH);[m if (d->cert) {[m [31m- iBlock der;[m [31m- init_Block(&der, 0);[m [31m- /* Get the DER serialization of the certificate. */ {[m [31m- BIO *buf = BIO_new(BIO_s_mem());[m [31m- i2d_X509_bio(buf, d->cert);[m [31m- readAllFromBIO_(buf, &der);[m [31m- BIO_free(buf);[m [31m- }[m [31m- SHA256(constData_Block(&der), size_Block(&der), data_Block(sha));[m [31m- deinit_Block(&der);[m [32m+[m[32m /* Get the DER serialization of the certificate. */[m [32m+[m[32m BIO *buf = BIO_new(BIO_s_mem());[m [32m+[m[32m i2d_X509_bio(buf, d->cert);[m [32m+[m[32m calcSHA256_(buf, sha);[m [32m+[m[32m BIO_free(buf);[m [32m+[m[32m }[m [32m+[m[32m return sha;[m [32m+[m[32m}[m [32m+[m [32m+[m[32miBlock *publicKeyFingerprint_TlsCertificate(const iTlsCertificate *d) {[m [32m+[m[32m iBlock *sha = new_Block(SHA256_DIGEST_LENGTH);[m [32m+[m[32m if (!d->cert) {[m [32m+[m[32m return sha;[m [32m+[m[32m }[m [32m+[m[32m EVP_PKEY *pub = X509_get_pubkey(d->cert);[m [32m+[m[32m if (pub) {[m [32m+[m[32m /* Get the DER serialization of the public key. */[m [32m+[m[32m BIO *buf = BIO_new(BIO_s_mem());[m [32m+[m[32m i2d_PUBKEY_bio(buf, pub);[m [32m+[m[32m calcSHA256_(buf, sha);[m [32m+[m[32m BIO_free(buf);[m [32m+[m[32m EVP_PKEY_free(pub);[m }[m return sha;[m }[m [36m@@ -426,16 +482,11 @@[m [miBlock *fingerprint_TlsCertificate(const iTlsCertificate *d) {[m iBlock *privateKeyFingerprint_TlsCertificate(const iTlsCertificate *d) {[m iBlock *sha = new_Block(SHA256_DIGEST_LENGTH);[m if (d->pkey) {[m [31m- iBlock der;[m [31m- init_Block(&der, 0);[m [31m- /* Get the DER serialization of the private key. */ {[m [31m- BIO *buf = BIO_new(BIO_s_mem());[m [31m- i2d_PrivateKey_bio(buf, d->pkey);[m [31m- readAllFromBIO_(buf, &der);[m [31m- BIO_free(buf);[m [31m- }[m [31m- SHA256(constData_Block(&der), size_Block(&der), data_Block(sha));[m [31m- deinit_Block(&der);[m [32m+[m[32m /* Get the DER serialization of the private key. */[m [32m+[m[32m BIO *buf = BIO_new(BIO_s_mem());[m [32m+[m[32m i2d_PrivateKey_bio(buf, d->pkey);[m [32m+[m[32m calcSHA256_(buf, sha);[m [32m+[m[32m BIO_free(buf);[m }[m return sha;[m }[m
text/gemini; charset=utf-8
This content has been proxied by September (3851b).