X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=openssl.c;h=d27f91878d3f47dfcd2310a2367c090a528cedfb;hp=3c264f16301c81eb134616e21b2f05918b926de2;hb=60513e33c518dd45644143581976e601d4d6aa60;hpb=3e7f4235d8f86a406a9fc7a8f7d4d09a0516d7ed diff --git a/openssl.c b/openssl.c index 3c264f16..d27f9187 100644 --- a/openssl.c +++ b/openssl.c @@ -55,10 +55,10 @@ #define CERTIFICATE_AUTHORITY_KEY "keyid:always" #define CERTIFICATE_ALT_NAME_PREFIX "DNS:" #define CERTIFICATE_VERSION 2 -#define VALID_DATETIME_FMT "%Y%m%d%H%M%SZ" +#define VALID_DATETIME_FMT "%y%m%d%H%M%SZ" #define VALID_DATETIME_BUFLEN 16 -static int generate_webpage_certificate(struct client_state *csp); +static int generate_host_certificate(struct client_state *csp); static void free_client_ssl_structures(struct client_state *csp); static void free_server_ssl_structures(struct client_state *csp); static int ssl_store_cert(struct client_state *csp, X509* crt); @@ -152,19 +152,27 @@ extern size_t is_ssl_pending(struct ssl_attr *ssl_attr) extern int ssl_send_data(struct ssl_attr *ssl_attr, const unsigned char *buf, size_t len) { BIO *bio = ssl_attr->openssl_attr.bio; + SSL *ssl; int ret = 0; int pos = 0; /* Position of unsent part in buffer */ + int fd = -1; if (len == 0) { return 0; } + if (BIO_get_ssl(bio, &ssl) == 1) + { + fd = SSL_get_fd(ssl); + } + while (pos < len) { int send_len = (int)len - pos; - log_error(LOG_LEVEL_WRITING, "TLS: %N", send_len, buf+pos); + log_error(LOG_LEVEL_WRITING, "TLS on socket %d: %N", + fd, send_len, buf+pos); /* * Sending one part of the buffer @@ -176,7 +184,7 @@ extern int ssl_send_data(struct ssl_attr *ssl_attr, const unsigned char *buf, si if (!BIO_should_retry(bio)) { log_ssl_errors(LOG_LEVEL_ERROR, - "Sending data over TLS/SSL failed"); + "Sending data on socket %d over TLS/SSL failed", fd); return -1; } } @@ -207,7 +215,10 @@ extern int ssl_send_data(struct ssl_attr *ssl_attr, const unsigned char *buf, si extern int ssl_recv_data(struct ssl_attr *ssl_attr, unsigned char *buf, size_t max_length) { BIO *bio = ssl_attr->openssl_attr.bio; + SSL *ssl; int ret = 0; + int fd = -1; + memset(buf, 0, max_length); /* @@ -218,15 +229,21 @@ extern int ssl_recv_data(struct ssl_attr *ssl_attr, unsigned char *buf, size_t m ret = BIO_read(bio, buf, (int)max_length); } while (ret <= 0 && BIO_should_retry(bio)); + if (BIO_get_ssl(bio, &ssl) == 1) + { + fd = SSL_get_fd(ssl); + } + if (ret < 0) { log_ssl_errors(LOG_LEVEL_ERROR, - "Receiving data over TLS/SSL failed"); + "Receiving data on socket %d over TLS/SSL failed", fd); return -1; } - log_error(LOG_LEVEL_RECEIVED, "TLS: %N", ret, buf); + log_error(LOG_LEVEL_RECEIVED, "TLS from socket %d: %N", + fd, ret, buf); return ret; } @@ -291,7 +308,7 @@ static int ssl_store_cert(struct client_state *csp, X509* crt) */ if (!PEM_write_bio_X509(bio, crt)) { - log_ssl_errors(LOG_LEVEL_ERROR, "PEM_write_X509() failed"); + log_ssl_errors(LOG_LEVEL_ERROR, "PEM_write_bio_X509() failed"); ret = -1; goto exit; } @@ -379,6 +396,7 @@ static int ssl_store_cert(struct client_state *csp, X509* crt) } else { + int i; if (bs->type == V_ASN1_NEG_INTEGER) { if (BIO_puts(bio, " (Negative)") < 0) @@ -388,7 +406,7 @@ static int ssl_store_cert(struct client_state *csp, X509* crt) goto exit; } } - for (int i = 0; i < bs->length; i++) + for (i = 0; i < bs->length; i++) { if (BIO_printf(bio, "%02x%c", bs->data[i], ((i + 1 == bs->length) ? '\n' : ':')) <= 0) @@ -636,6 +654,13 @@ static int ssl_store_cert(struct client_state *csp, X509* crt) BIO_write(bio, &zero, 1); len = BIO_get_mem_data(bio, &bio_mem_data); + if (len <= 0) + { + log_error(LOG_LEVEL_ERROR, "BIO_get_mem_data() returned %d " + "while gathering certificate information", len); + ret = -1; + goto exit; + } encoded_text = html_encode(bio_mem_data); if (encoded_text == NULL) { @@ -718,7 +743,6 @@ extern int create_client_ssl_connection(struct client_state *csp) struct ssl_attr *ssl_attr = &csp->ssl_client_attr; /* Paths to certificates file and key file */ char *key_file = NULL; - char *ca_file = NULL; char *cert_file = NULL; int ret = 0; SSL *ssl; @@ -742,7 +766,6 @@ extern int create_client_ssl_connection(struct client_state *csp) /* * Preparing paths to certificates files and key file */ - ca_file = csp->config->ca_cert_file; cert_file = make_certs_path(csp->config->certificate_directory, (const char *)csp->http->hash_of_host_hex, CERT_FILE_TYPE); key_file = make_certs_path(csp->config->certificate_directory, @@ -760,11 +783,11 @@ extern int create_client_ssl_connection(struct client_state *csp) */ privoxy_mutex_lock(&certificate_mutex); - ret = generate_webpage_certificate(csp); + ret = generate_host_certificate(csp); if (ret < 0) { log_error(LOG_LEVEL_ERROR, - "Generate_webpage_certificate failed: %d", ret); + "generate_host_certificate failed: %d", ret); privoxy_mutex_unlock(&certificate_mutex); ret = -1; goto exit; @@ -820,6 +843,18 @@ extern int create_client_ssl_connection(struct client_state *csp) goto exit; } + if (csp->config->cipher_list != NULL) + { + if (!SSL_set_cipher_list(ssl, csp->config->cipher_list)) + { + log_ssl_errors(LOG_LEVEL_ERROR, + "Setting the cipher list '%s' for the client connection failed", + csp->config->cipher_list); + ret = -1; + goto exit; + } + } + /* * Handshake with client */ @@ -944,6 +979,7 @@ static void free_client_ssl_structures(struct client_state *csp) extern void close_server_ssl_connection(struct client_state *csp) { struct ssl_attr *ssl_attr = &csp->ssl_server_attr; + SSL *ssl; if (csp->ssl_with_server_is_opened == 0) { @@ -954,6 +990,20 @@ extern void close_server_ssl_connection(struct client_state *csp) * Notifying the peer that the connection is being closed. */ BIO_ssl_shutdown(ssl_attr->openssl_attr.bio); + if (BIO_get_ssl(ssl_attr->openssl_attr.bio, &ssl) != 1) + { + log_ssl_errors(LOG_LEVEL_ERROR, + "BIO_get_ssl() failed in close_server_ssl_connection()"); + } + else + { + /* + * Pretend we received a shutdown alert so + * the BIO_free_all() call later on returns + * quickly. + */ + SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN); + } free_server_ssl_structures(csp); csp->ssl_with_server_is_opened = 0; } @@ -1029,6 +1079,18 @@ extern int create_server_ssl_connection(struct client_state *csp) goto exit; } + if (csp->config->cipher_list != NULL) + { + if (!SSL_set_cipher_list(ssl, csp->config->cipher_list)) + { + log_ssl_errors(LOG_LEVEL_ERROR, + "Setting the cipher list '%s' for the server connection failed", + csp->config->cipher_list); + ret = -1; + goto exit; + } + } + /* * Set the hostname to check against the received server certificate */ @@ -1086,7 +1148,8 @@ extern int create_server_ssl_connection(struct client_state *csp) chain = SSL_get_peer_cert_chain(ssl); if (chain) { - for (int i = 0; i < sk_X509_num(chain); i++) + int i; + for (i = 0; i < sk_X509_num(chain); i++) { if (ssl_store_cert(csp, sk_X509_value(chain, i)) != 0) { @@ -1224,7 +1287,7 @@ extern int ssl_base64_encode(unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen) { *olen = 4 * ((slen/3) + ((slen%3) ? 1 : 0)) + 1; - if (*olen < dlen) + if (*olen > dlen) { return ENOBUFS; } @@ -1409,39 +1472,41 @@ exit: static int generate_key(struct client_state *csp, char **key_buf) { int ret = 0; - char* key_file_path = NULL; - BIGNUM *exp = BN_new(); - RSA *rsa = RSA_new(); - EVP_PKEY *key = EVP_PKEY_new(); + char* key_file_path; + BIGNUM *exp; + RSA *rsa; + EVP_PKEY *key; - if (exp == NULL || rsa == NULL || key == NULL) + key_file_path = make_certs_path(csp->config->certificate_directory, + (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE); + if (key_file_path == NULL) { - log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure"); - ret = -1; - goto exit; + return -1; } - if (BN_set_word(exp, RSA_KEY_PUBLIC_EXPONENT) != 1) + /* + * Test if key already exists. If so, we don't have to create it again. + */ + if (file_exists(key_file_path) == 1) { - log_ssl_errors(LOG_LEVEL_ERROR, "Setting RSA key exponent failed"); - ret = -1; - goto exit; + freez(key_file_path); + return 0; } - key_file_path = make_certs_path(csp->config->certificate_directory, - (char *)csp->http->hash_of_host_hex, KEY_FILE_TYPE); - if (key_file_path == NULL) + exp = BN_new(); + rsa = RSA_new(); + key = EVP_PKEY_new(); + if (exp == NULL || rsa == NULL || key == NULL) { + log_ssl_errors(LOG_LEVEL_ERROR, "RSA key memory allocation failure"); ret = -1; goto exit; } - /* - * Test if key already exists. If so, we don't have to create it again. - */ - if (file_exists(key_file_path) == 1) + if (BN_set_word(exp, RSA_KEY_PUBLIC_EXPONENT) != 1) { - ret = 0; + log_ssl_errors(LOG_LEVEL_ERROR, "Setting RSA key exponent failed"); + ret = -1; goto exit; } @@ -1553,8 +1618,6 @@ static int ssl_certificate_is_invalid(const char *cert_file) if (!(cert = ssl_certificate_load(cert_file))) { - log_ssl_errors(LOG_LEVEL_ERROR, - "Error reading certificate file %s", cert_file); return 1; } @@ -1563,6 +1626,7 @@ static int ssl_certificate_is_invalid(const char *cert_file) { log_ssl_errors(LOG_LEVEL_ERROR, "Error checking certificate %s validity", cert_file); + ret = -1; } X509_free(cert); @@ -1583,7 +1647,7 @@ static int ssl_certificate_is_invalid(const char *cert_file) * 3 : nid = OpenSSL NID * 4 : value = extension value * - * Returns : 0 => Error while setting extensuon data + * Returns : 0 => Error while setting extension data * 1 => It worked * *********************************************************************/ @@ -1645,7 +1709,7 @@ static int set_subject_alternative_name(X509 *cert, X509 *issuer, const char *ho /********************************************************************* * - * Function : generate_webpage_certificate + * Function : generate_host_certificate * * Description : Creates certificate file in presetted directory. * If certificate already exists, no other certificate @@ -1661,7 +1725,7 @@ static int set_subject_alternative_name(X509 *cert, X509 *issuer, const char *ho * 1 => Certificate created * *********************************************************************/ -static int generate_webpage_certificate(struct client_state *csp) +static int generate_host_certificate(struct client_state *csp) { char *key_buf = NULL; /* Buffer for created key */ X509 *issuer_cert = NULL; @@ -1700,6 +1764,15 @@ static int generate_webpage_certificate(struct client_state *csp) return -1; } + if (enforce_sane_certificate_state(cert_opt.output_file, + cert_opt.subject_key)) + { + freez(cert_opt.output_file); + freez(cert_opt.subject_key); + + return -1; + } + if (file_exists(cert_opt.output_file) == 1) { /* The file exists, but is it valid? */ @@ -1859,7 +1932,7 @@ static int generate_webpage_certificate(struct client_state *csp) serial_num = BN_new(); if (!serial_num) { - log_error(LOG_LEVEL_ERROR, "generate_webpage_certificate: memory error"); + log_error(LOG_LEVEL_ERROR, "generate_host_certificate: memory error"); ret = -1; goto exit; } @@ -2163,12 +2236,14 @@ extern void ssl_release(void) { if (ssl_inited == 1) { +#ifndef OPENSSL_NO_COMP SSL_COMP_free_compression_methods(); - +#endif CONF_modules_free(); CONF_modules_unload(1); - +#ifndef OPENSSL_NO_COMP COMP_zlib_cleanup(); +#endif ERR_free_strings(); EVP_cleanup();