From a00ef5d044e76a8ff01f49302bb46a4561fbb602 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc-Andr=C3=A9=20Moreau?= Date: Fri, 19 Sep 2014 16:23:19 -0400 Subject: [PATCH] winpr-makecert: remove dependency on OPENSSL_Applink on Windows --- winpr/tools/makecert/makecert.c | 238 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 224 insertions(+), 14 deletions(-) diff --git a/winpr/tools/makecert/makecert.c b/winpr/tools/makecert/makecert.c index 9c31c18..9b49197 100644 --- a/winpr/tools/makecert/makecert.c +++ b/winpr/tools/makecert/makecert.c @@ -40,7 +40,6 @@ struct _MAKECERT_CONTEXT int argc; char** argv; - BIO* bio; RSA* rsa; X509* x509; EVP_PKEY* pkey; @@ -454,7 +453,9 @@ int makecert_context_set_output_file_name(MAKECERT_CONTEXT* context, char* name) int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* path) { FILE* fp; + int status; int length; + int offset; char* filename; char* fullpath; @@ -485,6 +486,9 @@ int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* pa if (fp) { + BIO* bio; + BYTE* x509_str; + if (context->pfxFormat) { if (!context->password) @@ -497,17 +501,136 @@ int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* pa OpenSSL_add_all_ciphers(); OpenSSL_add_all_digests(); - context->pkcs12 = PKCS12_create(context->password, context->default_name, context->pkey, - context->x509, NULL, 0, 0, 0, 0, 0); + context->pkcs12 = PKCS12_create(context->password, context->default_name, + context->pkey, context->x509, NULL, 0, 0, 0, 0, 0); + + bio = BIO_new(BIO_s_mem()); + + if (!bio) + return -1; + + status = i2d_PKCS12_bio(bio, context->pkcs12); + + offset = 0; + length = 2048; + x509_str = (BYTE*) malloc(length); + + status = BIO_read(bio, x509_str, length); + + if (status < 0) + return -1; + + offset += status; + + while (offset >= length) + { + length *= 2; + x509_str = (BYTE*) realloc(x509_str, length); + + status = BIO_read(bio, &x509_str[offset], length); - i2d_PKCS12_fp(fp, context->pkcs12); + if (status < 0) + break; + + offset += status; + } + + if (status < 0) + return -1; + + length = offset; + + fwrite((void*) x509_str, length, 1, fp); + + free(x509_str); + BIO_free(bio); } else { - PEM_write_X509(fp, context->x509); + bio = BIO_new(BIO_s_mem()); + + if (!bio) + return -1; + + status = PEM_write_bio_X509(bio, context->x509); + + offset = 0; + length = 2048; + x509_str = (BYTE*) malloc(length); + + status = BIO_read(bio, x509_str, length); + + if (status < 0) + return -1; + + offset += status; + + while (offset >= length) + { + length *= 2; + x509_str = (BYTE*) realloc(x509_str, length); + + status = BIO_read(bio, &x509_str[offset], length); + + if (status < 0) + break; + + offset += status; + } + + if (status < 0) + return -1; + + length = offset; + + fwrite((void*) x509_str, length, 1, fp); + + free(x509_str); + BIO_free(bio); if (context->pemFormat) - PEM_write_PrivateKey(fp, context->pkey, NULL, NULL, 0, NULL, NULL); + { + bio = BIO_new(BIO_s_mem()); + + if (!bio) + return -1; + + status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL); + + offset = 0; + length = 2048; + x509_str = (BYTE*) malloc(length); + + status = BIO_read(bio, x509_str, length); + + if (status < 0) + return -1; + + offset += status; + + while (offset >= length) + { + length *= 2; + x509_str = (BYTE*) realloc(x509_str, length); + + status = BIO_read(bio, &x509_str[offset], length); + + if (status < 0) + break; + + offset += status; + } + + if (status < 0) + return -1; + + length = offset; + + fwrite((void*) x509_str, length, 1, fp); + + free(x509_str); + BIO_free(bio); + } } fclose(fp); @@ -522,7 +645,9 @@ int makecert_context_output_certificate_file(MAKECERT_CONTEXT* context, char* pa int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, char* path) { FILE* fp; + int status; int length; + int offset; char* filename; char* fullpath; @@ -551,7 +676,50 @@ int makecert_context_output_private_key_file(MAKECERT_CONTEXT* context, char* pa if (fp) { - PEM_write_PrivateKey(fp, context->pkey, NULL, NULL, 0, NULL, NULL); + BIO* bio; + BYTE* x509_str; + + bio = BIO_new(BIO_s_mem()); + + if (!bio) + return -1; + + status = PEM_write_bio_PrivateKey(bio, context->pkey, NULL, NULL, 0, NULL, NULL); + + offset = 0; + length = 2048; + x509_str = (BYTE*) malloc(length); + + status = BIO_read(bio, x509_str, length); + + if (status < 0) + return -1; + + offset += status; + + while (offset >= length) + { + length *= 2; + x509_str = (BYTE*) realloc(x509_str, length); + + status = BIO_read(bio, &x509_str[offset], length); + + if (status < 0) + break; + + offset += status; + } + + if (status < 0) + return -1; + + length = offset; + + fwrite((void*) x509_str, length, 1, fp); + + free(x509_str); + BIO_free(bio); + fclose(fp); } @@ -582,9 +750,6 @@ int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv) if (!context->common_name) context->common_name = _strdup(context->default_name); - CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); - context->bio = BIO_new_fp(stderr, BIO_NOCLOSE); - if (!context->pkey) context->pkey = EVP_PKEY_new(); @@ -712,7 +877,55 @@ int makecert_context_process(MAKECERT_CONTEXT* context, int argc, char** argv) */ if (!context->silent) - X509_print_fp(stdout, context->x509); + { + BIO* bio; + int status; + int length; + int offset; + BYTE* x509_str; + + bio = BIO_new(BIO_s_mem()); + + if (!bio) + return -1; + + status = X509_print(bio, context->x509); + + offset = 0; + length = 2048; + x509_str = (BYTE*) malloc(length + 1); + + status = BIO_read(bio, x509_str, length); + + if (status < 0) + return -1; + + offset += status; + + while (offset >= length) + { + length *= 2; + x509_str = (BYTE*) realloc(x509_str, length + 1); + + status = BIO_read(bio, &x509_str[offset], length); + + if (status < 0) + break; + + offset += status; + } + + if (status < 0) + return -1; + + length = offset; + x509_str[length] = '\0'; + + printf("%s", x509_str); + + free(x509_str); + BIO_free(bio); + } /** * Output certificate and private key to files @@ -757,9 +970,6 @@ void makecert_context_free(MAKECERT_CONTEXT* context) CRYPTO_cleanup_all_ex_data(); - CRYPTO_mem_leaks(context->bio); - BIO_free(context->bio); - free(context); } } -- 2.7.4