X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=engines%2Fe_capi.c;h=f2d5c3e0912856969b6ef1a868426ad686a85c5d;hp=b2ae577987d6e96bd6c9cc73546553b1d56af4eb;hb=04473165815e93c8453f6c0b55a5dd0b93f668fa;hpb=55646005a9ce3c85e394c6afae5f6ed6045494c6 diff --git a/engines/e_capi.c b/engines/e_capi.c index b2ae577987..f2d5c3e091 100644 --- a/engines/e_capi.c +++ b/engines/e_capi.c @@ -1,55 +1,10 @@ -/* engines/e_capi.c */ /* - * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL - * project. - */ -/* ==================================================================== - * Copyright (c) 2008 The OpenSSL Project. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" - * - * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to - * endorse or promote products derived from this software without - * prior written permission. For written permission, please contact - * licensing@OpenSSL.org. - * - * 5. Products derived from this software may not be called "OpenSSL" - * nor may "OpenSSL" appear in their names without prior written - * permission of the OpenSSL Project. + * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved. * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the OpenSSL Project - * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" - * - * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY - * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * ==================================================================== + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html */ #include @@ -134,6 +89,10 @@ # define CALG_SHA_512 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_512) # endif +# ifndef PROV_RSA_AES +# define PROV_RSA_AES 24 +# endif + # include # include # include @@ -173,9 +132,11 @@ static int capi_rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding); static int capi_rsa_free(RSA *rsa); +# ifndef OPENSSL_NO_DSA static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen, DSA *dsa); static int capi_dsa_free(DSA *dsa); +# endif static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl, STACK_OF(X509_NAME) *ca_dn, X509 **pcert, @@ -188,6 +149,8 @@ static int cert_select_simple(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs); static int cert_select_dialog(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs); # endif +void engine_load_capi_int(void); + typedef PCCERT_CONTEXT(WINAPI *CERTDLG) (HCERTSTORE, HWND, LPCWSTR, LPCWSTR, DWORD, DWORD, void *); typedef HWND(WINAPI *GETCONSWIN) (void); @@ -332,6 +295,7 @@ static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) int ret = 1; CAPI_CTX *ctx; BIO *out; + LPSTR tmpstr; if (capi_idx == -1) { CAPIerr(CAPI_F_CAPI_CTRL, CAPI_R_ENGINE_NOT_INITIALIZED); return 0; @@ -360,9 +324,15 @@ static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) break; case CAPI_CMD_STORE_NAME: - OPENSSL_free(ctx->storename); - ctx->storename = BUF_strdup(p); - CAPI_trace(ctx, "Setting store name to %s\n", p); + tmpstr = OPENSSL_strdup(p); + if (tmpstr != NULL) { + OPENSSL_free(ctx->storename); + ctx->storename = tmpstr; + CAPI_trace(ctx, "Setting store name to %s\n", p); + } else { + CAPIerr(CAPI_F_CAPI_CTRL, ERR_R_MALLOC_FAILURE); + ret = 0; + } break; case CAPI_CMD_STORE_FLAGS: @@ -382,8 +352,14 @@ static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) break; case CAPI_CMD_DEBUG_FILE: - ctx->debug_file = BUF_strdup(p); - CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file); + tmpstr = OPENSSL_strdup(p); + if (tmpstr != NULL) { + ctx->debug_file = tmpstr; + CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file); + } else { + CAPIerr(CAPI_F_CAPI_CTRL, ERR_R_MALLOC_FAILURE); + ret = 0; + } break; case CAPI_CMD_KEYTYPE: @@ -426,42 +402,21 @@ static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) } -static RSA_METHOD capi_rsa_method = { - "CryptoAPI RSA method", - 0, /* pub_enc */ - 0, /* pub_dec */ - capi_rsa_priv_enc, /* priv_enc */ - capi_rsa_priv_dec, /* priv_dec */ - 0, /* rsa_mod_exp */ - 0, /* bn_mod_exp */ - 0, /* init */ - capi_rsa_free, /* finish */ - RSA_FLAG_SIGN_VER, /* flags */ - NULL, /* app_data */ - capi_rsa_sign, /* rsa_sign */ - 0 /* rsa_verify */ -}; +static RSA_METHOD *capi_rsa_method = NULL; +# ifndef OPENSSL_NO_DSA +static DSA_METHOD *capi_dsa_method = NULL; +# endif -static DSA_METHOD capi_dsa_method = { - "CryptoAPI DSA method", - capi_dsa_do_sign, /* dsa_do_sign */ - 0, /* dsa_sign_setup */ - 0, /* dsa_do_verify */ - 0, /* dsa_mod_exp */ - 0, /* bn_mod_exp */ - 0, /* init */ - capi_dsa_free, /* finish */ - 0, /* flags */ - NULL, /* app_data */ - 0, /* dsa_paramgen */ - 0 /* dsa_keygen */ -}; +static int use_aes_csp = 0; static int capi_init(ENGINE *e) { CAPI_CTX *ctx; const RSA_METHOD *ossl_rsa_meth; +# ifndef OPENSSL_NO_DSA const DSA_METHOD *ossl_dsa_meth; +# endif + HCRYPTPROV hprov; if (capi_idx < 0) { capi_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 0); @@ -473,17 +428,36 @@ static int capi_init(ENGINE *e) /* Setup RSA_METHOD */ rsa_capi_idx = RSA_get_ex_new_index(0, NULL, NULL, NULL, 0); ossl_rsa_meth = RSA_PKCS1_OpenSSL(); - capi_rsa_method.rsa_pub_enc = ossl_rsa_meth->rsa_pub_enc; - capi_rsa_method.rsa_pub_dec = ossl_rsa_meth->rsa_pub_dec; - capi_rsa_method.rsa_mod_exp = ossl_rsa_meth->rsa_mod_exp; - capi_rsa_method.bn_mod_exp = ossl_rsa_meth->bn_mod_exp; + if ( !RSA_meth_set_pub_enc(capi_rsa_method, + RSA_meth_get_pub_enc(ossl_rsa_meth)) + || !RSA_meth_set_pub_dec(capi_rsa_method, + RSA_meth_get_pub_dec(ossl_rsa_meth)) + || !RSA_meth_set_priv_enc(capi_rsa_method, capi_rsa_priv_enc) + || !RSA_meth_set_priv_dec(capi_rsa_method, capi_rsa_priv_dec) + || !RSA_meth_set_mod_exp(capi_rsa_method, + RSA_meth_get_mod_exp(ossl_rsa_meth)) + || !RSA_meth_set_bn_mod_exp(capi_rsa_method, + RSA_meth_get_bn_mod_exp(ossl_rsa_meth)) + || !RSA_meth_set_finish(capi_rsa_method, capi_rsa_free) + || !RSA_meth_set_sign(capi_rsa_method, capi_rsa_sign)) { + goto memerr; + } +# ifndef OPENSSL_NO_DSA /* Setup DSA Method */ dsa_capi_idx = DSA_get_ex_new_index(0, NULL, NULL, NULL, 0); ossl_dsa_meth = DSA_OpenSSL(); - capi_dsa_method.dsa_do_verify = ossl_dsa_meth->dsa_do_verify; - capi_dsa_method.dsa_mod_exp = ossl_dsa_meth->dsa_mod_exp; - capi_dsa_method.bn_mod_exp = ossl_dsa_meth->bn_mod_exp; + if ( !DSA_meth_set_sign(capi_dsa_method, capi_dsa_do_sign) + || !DSA_meth_set_verify(capi_dsa_method, + DSA_meth_get_verify(ossl_dsa_meth)) + || !DSA_meth_set_finish(capi_dsa_method, capi_dsa_free) + || !DSA_meth_set_mod_exp(capi_dsa_method, + DSA_meth_get_mod_exp(ossl_dsa_meth)) + || !DSA_meth_set_bn_mod_exp(capi_dsa_method, + DSA_meth_get_bn_mod_exp(ossl_dsa_meth))) { + goto memerr; + } +# endif } ctx = capi_ctx_new(); @@ -508,6 +482,14 @@ static int capi_init(ENGINE *e) } # endif + /* See if we support AES CSP */ + + if (CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_AES, + CRYPT_VERIFYCONTEXT)) { + use_aes_csp = 1; + CryptReleaseContext(hprov, 0); + } + return 1; memerr: @@ -519,6 +501,12 @@ static int capi_init(ENGINE *e) static int capi_destroy(ENGINE *e) { + RSA_meth_free(capi_rsa_method); + capi_rsa_method = NULL; +# ifndef OPENSSL_NO_DSA + DSA_meth_free(capi_dsa_method); + capi_dsa_method = NULL; +# endif ERR_unload_CAPI_strings(); return 1; } @@ -548,24 +536,41 @@ struct CAPI_KEY_st { static int bind_capi(ENGINE *e) { + capi_rsa_method = RSA_meth_new("CryptoAPI RSA method", 0); + if (capi_rsa_method == NULL) + return 0; +# ifndef OPENSSL_NO_DSA + capi_dsa_method = DSA_meth_new("CryptoAPI DSA method", 0); + if (capi_dsa_method == NULL) + goto memerr; +# endif if (!ENGINE_set_id(e, engine_capi_id) || !ENGINE_set_name(e, engine_capi_name) || !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) || !ENGINE_set_init_function(e, capi_init) || !ENGINE_set_finish_function(e, capi_finish) || !ENGINE_set_destroy_function(e, capi_destroy) - || !ENGINE_set_RSA(e, &capi_rsa_method) - || !ENGINE_set_DSA(e, &capi_dsa_method) + || !ENGINE_set_RSA(e, capi_rsa_method) +# ifndef OPENSSL_NO_DSA + || !ENGINE_set_DSA(e, capi_dsa_method) +# endif || !ENGINE_set_load_privkey_function(e, capi_load_privkey) || !ENGINE_set_load_ssl_client_cert_function(e, capi_load_ssl_client_cert) || !ENGINE_set_cmd_defns(e, capi_cmd_defns) || !ENGINE_set_ctrl_function(e, capi_ctrl)) - return 0; + goto memerr; ERR_load_CAPI_strings(); return 1; - + memerr: + RSA_meth_free(capi_rsa_method); + capi_rsa_method = NULL; +# ifndef OPENSSL_NO_DSA + DSA_meth_free(capi_dsa_method); + capi_dsa_method = NULL; +# endif + return 0; } # ifndef OPENSSL_NO_DYNAMIC_ENGINE @@ -593,7 +598,7 @@ static ENGINE *engine_capi(void) return ret; } -void ENGINE_load_capi(void) +void engine_load_capi_int(void) { /* Copied from eng_[openssl|dyn].c */ ENGINE *toadd = engine_capi(); @@ -660,6 +665,7 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) if (bh->aiKeyAlg == CALG_RSA_SIGN || bh->aiKeyAlg == CALG_RSA_KEYX) { RSAPUBKEY *rp; DWORD rsa_modlen; + BIGNUM *e = NULL, *n = NULL; unsigned char *rsa_modulus; rp = (RSAPUBKEY *) (bh + 1); if (rp->magic != 0x31415352) { @@ -675,17 +681,22 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) if (!rkey) goto memerr; - rkey->e = BN_new(); - rkey->n = BN_new(); + e = BN_new(); + n = BN_new(); - if (rkey->e == NULL || rkey->n == NULL) + if (e == NULL || n == NULL) { + BN_free(e); + BN_free(n); goto memerr; + } - if (!BN_set_word(rkey->e, rp->pubexp)) + RSA_set0_key(rkey, n, e, NULL); + + if (!BN_set_word(e, rp->pubexp)) goto memerr; rsa_modlen = rp->bitlen / 8; - if (!lend_tobn(rkey->n, rsa_modulus, rsa_modlen)) + if (!lend_tobn(n, rsa_modulus, rsa_modlen)) goto memerr; RSA_set_ex_data(rkey, rsa_capi_idx, key); @@ -696,10 +707,12 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) EVP_PKEY_assign_RSA(ret, rkey); rkey = NULL; +# ifndef OPENSSL_NO_DSA } else if (bh->aiKeyAlg == CALG_DSS_SIGN) { DSSPUBKEY *dp; DWORD dsa_plen; unsigned char *btmp; + BIGNUM *p, *q, *g, *pub_key; dp = (DSSPUBKEY *) (bh + 1); if (dp->magic != 0x31535344) { char magstr[10]; @@ -714,23 +727,29 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) dkey = DSA_new_method(eng); if (!dkey) goto memerr; - dkey->p = BN_new(); - dkey->q = BN_new(); - dkey->g = BN_new(); - dkey->pub_key = BN_new(); - if (dkey->p == NULL || dkey->q == NULL || dkey->g == NULL - || dkey->pub_key == NULL) + p = BN_new(); + q = BN_new(); + g = BN_new(); + pub_key = BN_new(); + if (p == NULL || q == NULL || g == NULL || pub_key == NULL) { + BN_free(p); + BN_free(q); + BN_free(g); + BN_free(pub_key); goto memerr; - if (!lend_tobn(dkey->p, btmp, dsa_plen)) + } + DSA_set0_pqg(dkey, p, q, g); + DSA_set0_key(dkey, pub_key, NULL); + if (!lend_tobn(p, btmp, dsa_plen)) goto memerr; btmp += dsa_plen; - if (!lend_tobn(dkey->q, btmp, 20)) + if (!lend_tobn(q, btmp, 20)) goto memerr; btmp += 20; - if (!lend_tobn(dkey->g, btmp, dsa_plen)) + if (!lend_tobn(g, btmp, dsa_plen)) goto memerr; btmp += dsa_plen; - if (!lend_tobn(dkey->pub_key, btmp, dsa_plen)) + if (!lend_tobn(pub_key, btmp, dsa_plen)) goto memerr; btmp += dsa_plen; @@ -741,6 +760,7 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) EVP_PKEY_assign_DSA(ret, dkey); dkey = NULL; +# endif } else { char algstr[10]; BIO_snprintf(algstr, 10, "%ux", bh->aiKeyAlg); @@ -754,7 +774,9 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) OPENSSL_free(pubkey); if (!ret) { RSA_free(rkey); +# ifndef OPENSSL_NO_DSA DSA_free(dkey); +# endif } return ret; @@ -811,7 +833,7 @@ int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len, CAPI_KEY *capi_key; CAPI_CTX *ctx; - ctx = ENGINE_get_ex_data(rsa->engine, capi_idx); + ctx = ENGINE_get_ex_data(RSA_get0_engine(rsa), capi_idx); CAPI_trace(ctx, "Called CAPI_rsa_sign()\n"); @@ -907,7 +929,7 @@ int capi_rsa_priv_dec(int flen, const unsigned char *from, if (flen <= 0) return flen; - ctx = ENGINE_get_ex_data(rsa->engine, capi_idx); + ctx = ENGINE_get_ex_data(RSA_get0_engine(rsa), capi_idx); CAPI_trace(ctx, "Called capi_rsa_priv_dec()\n"); @@ -957,6 +979,7 @@ static int capi_rsa_free(RSA *rsa) return 1; } +# ifndef OPENSSL_NO_DSA /* CryptoAPI DSA operations */ static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen, @@ -969,7 +992,7 @@ static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen, CAPI_CTX *ctx; unsigned char csigbuf[40]; - ctx = ENGINE_get_ex_data(dsa->engine, capi_idx); + ctx = ENGINE_get_ex_data(DSA_get0_engine(dsa), capi_idx); CAPI_trace(ctx, "Called CAPI_dsa_do_sign()\n"); @@ -1006,15 +1029,13 @@ static DSA_SIG *capi_dsa_do_sign(const unsigned char *digest, int dlen, capi_addlasterror(); goto err; } else { + BIGNUM *r = NULL, *s = NULL; ret = DSA_SIG_new(); if (ret == NULL) goto err; - ret->r = BN_new(); - ret->s = BN_new(); - if (ret->r == NULL || ret->s == NULL) - goto err; - if (!lend_tobn(ret->r, csigbuf, 20) - || !lend_tobn(ret->s, csigbuf + 20, 20)) { + DSA_SIG_get0(&r, &s, ret); + if (!lend_tobn(r, csigbuf, 20) + || !lend_tobn(s, csigbuf + 20, 20)) { DSA_SIG_free(ret); ret = NULL; goto err; @@ -1037,6 +1058,7 @@ static int capi_dsa_free(DSA *dsa) DSA_set_ex_data(dsa, dsa_capi_idx, 0); return 1; } +# endif static void capi_vtrace(CAPI_CTX * ctx, int level, char *format, va_list argptr) @@ -1453,10 +1475,15 @@ static CAPI_KEY *capi_get_key(CAPI_CTX * ctx, const TCHAR *contname, if (key == NULL) return NULL; - if (sizeof(TCHAR) == sizeof(char)) + /* If PROV_RSA_AES supported use it instead */ + if (ptype == PROV_RSA_FULL && use_aes_csp) { + provname = NULL; + ptype = PROV_RSA_AES; + CAPI_trace(ctx, "capi_get_key, contname=%s, RSA_AES_CSP\n", contname); + } else if (sizeof(TCHAR) == sizeof(char)) { CAPI_trace(ctx, "capi_get_key, contname=%s, provname=%s, type=%d\n", contname, provname, ptype); - else if (ctx && ctx->debug_level >= CAPI_DBG_TRACE && ctx->debug_file) { + } else if (ctx && ctx->debug_level >= CAPI_DBG_TRACE && ctx->debug_file) { /* above 'if' is optimization to minimize malloc-ations */ char *_contname = wide_to_asc((WCHAR *)contname); char *_provname = wide_to_asc((WCHAR *)provname); @@ -1611,6 +1638,8 @@ static void capi_ctx_free(CAPI_CTX * ctx) static int capi_ctx_set_provname(CAPI_CTX * ctx, LPSTR pname, DWORD type, int check) { + LPSTR tmpcspname; + CAPI_trace(ctx, "capi_ctx_set_provname, name=%s, type=%d\n", pname, type); if (check) { HCRYPTPROV hprov; @@ -1634,8 +1663,13 @@ static int capi_ctx_set_provname(CAPI_CTX * ctx, LPSTR pname, DWORD type, } CryptReleaseContext(hprov, 0); } + tmpcspname = OPENSSL_strdup(pname); + if (tmpcspname == NULL) { + CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, ERR_R_MALLOC_FAILURE); + return 0; + } OPENSSL_free(ctx->cspname); - ctx->cspname = BUF_strdup(pname); + ctx->cspname = tmpcspname; ctx->csptype = type; return 1; } @@ -1875,7 +1909,8 @@ OPENSSL_EXPORT IMPLEMENT_DYNAMIC_CHECK_FN() # else -void ENGINE_load_capi(void) +void engine_load_capi_int(void); +void engine_load_capi_int(void) { } # endif