X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=engines%2Fe_capi.c;h=f44acc91f3152b19ee49b58ded67353eb3059972;hp=8b3cfeef9520619bcceaf67e534f296ad8f67399;hb=b72c9121379a5de0c8be0d4e1a4a6b9495042621;hpb=64b25758edca688a30f02c260262150f7ad0bc7d diff --git a/engines/e_capi.c b/engines/e_capi.c index 8b3cfeef95..f44acc91f3 100644 --- a/engines/e_capi.c +++ b/engines/e_capi.c @@ -1,4 +1,3 @@ -/* engines/e_capi.c */ /* * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project. @@ -134,6 +133,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 @@ -188,6 +191,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_internal(void); + typedef PCCERT_CONTEXT(WINAPI *CERTDLG) (HCERTSTORE, HWND, LPCWSTR, LPCWSTR, DWORD, DWORD, void *); typedef HWND(WINAPI *GETCONSWIN) (void); @@ -239,7 +244,7 @@ struct CAPI_CTX_st { GETCONSWIN getconswindow; }; -static CAPI_CTX *capi_ctx_new(); +static CAPI_CTX *capi_ctx_new(void); static void capi_ctx_free(CAPI_CTX * ctx); static int capi_ctx_set_provname(CAPI_CTX * ctx, LPSTR pname, DWORD type, int check); @@ -361,7 +366,7 @@ static int capi_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) case CAPI_CMD_STORE_NAME: OPENSSL_free(ctx->storename); - ctx->storename = BUF_strdup(p); + ctx->storename = OPENSSL_strdup(p); CAPI_trace(ctx, "Setting store name to %s\n", p); break; @@ -382,7 +387,7 @@ 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); + ctx->debug_file = OPENSSL_strdup(p); CAPI_trace(ctx, "Setting debug file to %s\n", ctx->debug_file); break; @@ -436,32 +441,22 @@ static RSA_METHOD capi_rsa_method = { 0, /* bn_mod_exp */ 0, /* init */ capi_rsa_free, /* finish */ - RSA_FLAG_SIGN_VER, /* flags */ + 0, /* flags */ NULL, /* app_data */ capi_rsa_sign, /* rsa_sign */ 0 /* rsa_verify */ }; -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 DSA_METHOD *capi_dsa_method = NULL; + +static int use_aes_csp = 0; static int capi_init(ENGINE *e) { CAPI_CTX *ctx; const RSA_METHOD *ossl_rsa_meth; const DSA_METHOD *ossl_dsa_meth; + HCRYPTPROV hprov; if (capi_idx < 0) { capi_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 0); @@ -472,7 +467,7 @@ 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_SSLeay(); + 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; @@ -481,13 +476,20 @@ static int capi_init(ENGINE *e) /* 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; + } } ctx = capi_ctx_new(); - if (!ctx) + if (ctx == NULL) goto memerr; ENGINE_set_ex_data(e, capi_idx, ctx); @@ -508,6 +510,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 +529,8 @@ static int capi_init(ENGINE *e) static int capi_destroy(ENGINE *e) { + DSA_meth_free(capi_dsa_method); + capi_dsa_method = NULL; ERR_unload_CAPI_strings(); return 1; } @@ -548,6 +560,9 @@ struct CAPI_KEY_st { static int bind_capi(ENGINE *e) { + capi_dsa_method = DSA_meth_new("CryptoAPI DSA method", 0); + if (capi_dsa_method == NULL) + return 0; if (!ENGINE_set_id(e, engine_capi_id) || !ENGINE_set_name(e, engine_capi_name) || !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) @@ -555,7 +570,7 @@ static int bind_capi(ENGINE *e) || !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_DSA(e, capi_dsa_method) || !ENGINE_set_load_privkey_function(e, capi_load_privkey) || !ENGINE_set_load_ssl_client_cert_function(e, capi_load_ssl_client_cert) @@ -584,7 +599,7 @@ IMPLEMENT_DYNAMIC_CHECK_FN() static ENGINE *engine_capi(void) { ENGINE *ret = ENGINE_new(); - if (!ret) + if (ret == NULL) return NULL; if (!bind_capi(ret)) { ENGINE_free(ret); @@ -593,7 +608,7 @@ static ENGINE *engine_capi(void) return ret; } -void ENGINE_load_capi(void) +void engine_load_capi_internal(void) { /* Copied from eng_[openssl|dyn].c */ ENGINE *toadd = engine_capi(); @@ -643,7 +658,7 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) pubkey = OPENSSL_malloc(len); - if (!pubkey) + if (pubkey == NULL) goto memerr; if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len)) { @@ -678,7 +693,7 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) rkey->e = BN_new(); rkey->n = BN_new(); - if (!rkey->e || !rkey->n) + if (rkey->e == NULL || rkey->n == NULL) goto memerr; if (!BN_set_word(rkey->e, rp->pubexp)) @@ -700,6 +715,7 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) 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,22 +730,24 @@ 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 || !dkey->q || !dkey->g || !dkey->pub_key) + p = BN_new(); + q = BN_new(); + g = BN_new(); + pub_key = BN_new(); + if (p == NULL || q == NULL || g == NULL || pub_key == NULL) 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; @@ -742,7 +760,7 @@ static EVP_PKEY *capi_get_pkey(ENGINE *eng, CAPI_KEY * key) dkey = NULL; } else { char algstr[10]; - BIO_snprintf(algstr, 10, "%lx", bh->aiKeyAlg); + BIO_snprintf(algstr, 10, "%ux", bh->aiKeyAlg); CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_UNSUPPORTED_PUBLIC_KEY_ALGORITHM); ERR_add_error_data(2, "aiKeyAlg=0x", algstr); @@ -847,7 +865,7 @@ int capi_rsa_sign(int dtype, const unsigned char *m, unsigned int m_len, default: { char algstr[10]; - BIO_snprintf(algstr, 10, "%lx", dtype); + BIO_snprintf(algstr, 10, "%x", dtype); CAPIerr(CAPI_F_CAPI_RSA_SIGN, CAPI_R_UNSUPPORTED_ALGORITHM_NID); ERR_add_error_data(2, "NID=0x", algstr); return -1; @@ -901,6 +919,11 @@ int capi_rsa_priv_dec(int flen, const unsigned char *from, unsigned char *tmpbuf; CAPI_KEY *capi_key; CAPI_CTX *ctx; + DWORD dlen; + + if (flen <= 0) + return flen; + ctx = ENGINE_get_ex_data(rsa->engine, capi_idx); CAPI_trace(ctx, "Called capi_rsa_priv_dec()\n"); @@ -928,13 +951,14 @@ int capi_rsa_priv_dec(int flen, const unsigned char *from, tmpbuf[flen - i - 1] = from[i]; /* Finally decrypt it */ - if (!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &flen)) { + dlen = flen; + if (!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &dlen)) { CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_DECRYPT_ERROR); capi_addlasterror(); OPENSSL_free(tmpbuf); return -1; } else - memcpy(to, tmpbuf, flen); + memcpy(to, tmpbuf, (flen = (int)dlen)); OPENSSL_free(tmpbuf); @@ -962,7 +986,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"); @@ -999,15 +1023,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) + if (ret == NULL) goto err; - ret->r = BN_new(); - ret->s = BN_new(); - if (!ret->r || !ret->s) - 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; @@ -1081,7 +1103,7 @@ static char *wide_to_asc(LPCWSTR wstr) return NULL; } str = OPENSSL_malloc(sz); - if (!str) { + if (str == NULL) { CAPIerr(CAPI_F_WIDE_TO_ASC, ERR_R_MALLOC_FAILURE); return NULL; } @@ -1147,7 +1169,7 @@ static int capi_list_providers(CAPI_CTX * ctx, BIO *out) break; if (ret == 0) break; - BIO_printf(out, "%d. %s, type %d\n", idx, provname, ptype); + BIO_printf(out, "%lu. %s, type %lu\n", idx, provname, ptype); OPENSSL_free(provname); } return 1; @@ -1195,7 +1217,7 @@ static int capi_list_containers(CAPI_CTX * ctx, BIO *out) if (buflen == 0) buflen = 1024; cname = OPENSSL_malloc(buflen); - if (!cname) { + if (cname == NULL) { CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE); goto err; } @@ -1223,7 +1245,7 @@ static int capi_list_containers(CAPI_CTX * ctx, BIO *out) CAPI_trace(ctx, "Enumerate bug: using workaround\n"); goto done; } - BIO_printf(out, "%d. %s\n", idx, cname); + BIO_printf(out, "%lu. %s\n", idx, cname); } err: @@ -1236,7 +1258,7 @@ static int capi_list_containers(CAPI_CTX * ctx, BIO *out) return ret; } -CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX * ctx, PCCERT_CONTEXT cert) +static CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX * ctx, PCCERT_CONTEXT cert) { DWORD len; CRYPT_KEY_PROV_INFO *pinfo; @@ -1245,7 +1267,7 @@ CRYPT_KEY_PROV_INFO *capi_get_prov_info(CAPI_CTX * ctx, PCCERT_CONTEXT cert) (cert, CERT_KEY_PROV_INFO_PROP_ID, NULL, &len)) return NULL; pinfo = OPENSSL_malloc(len); - if (!pinfo) { + if (pinfo == NULL) { CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, ERR_R_MALLOC_FAILURE); return NULL; } @@ -1274,16 +1296,16 @@ static void capi_dump_prov_info(CAPI_CTX * ctx, BIO *out, goto err; BIO_printf(out, " Private Key Info:\n"); - BIO_printf(out, " Provider Name: %s, Provider Type %d\n", provname, + BIO_printf(out, " Provider Name: %s, Provider Type %lu\n", provname, pinfo->dwProvType); - BIO_printf(out, " Container Name: %s, Key Type %d\n", contname, + BIO_printf(out, " Container Name: %s, Key Type %lu\n", contname, pinfo->dwKeySpec); err: OPENSSL_free(provname); OPENSSL_free(contname); } -char *capi_cert_get_fname(CAPI_CTX * ctx, PCCERT_CONTEXT cert) +static char *capi_cert_get_fname(CAPI_CTX * ctx, PCCERT_CONTEXT cert) { LPWSTR wfname; DWORD dlen; @@ -1308,10 +1330,10 @@ char *capi_cert_get_fname(CAPI_CTX * ctx, PCCERT_CONTEXT cert) return NULL; } -void capi_dump_cert(CAPI_CTX * ctx, BIO *out, PCCERT_CONTEXT cert) +static void capi_dump_cert(CAPI_CTX * ctx, BIO *out, PCCERT_CONTEXT cert) { X509 *x; - unsigned char *p; + const unsigned char *p; unsigned long flags = ctx->dump_flags; if (flags & CAPI_DMP_FNAME) { char *fname; @@ -1349,7 +1371,7 @@ void capi_dump_cert(CAPI_CTX * ctx, BIO *out, PCCERT_CONTEXT cert) X509_free(x); } -HCERTSTORE capi_open_store(CAPI_CTX * ctx, char *storename) +static HCERTSTORE capi_open_store(CAPI_CTX * ctx, char *storename) { HCERTSTORE hstore; @@ -1446,10 +1468,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); @@ -1571,11 +1598,11 @@ void capi_free_key(CAPI_KEY * key) /* Initialize a CAPI_CTX structure */ -static CAPI_CTX *capi_ctx_new() +static CAPI_CTX *capi_ctx_new(void) { CAPI_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); - if (!ctx) { + if (ctx == NULL) { CAPIerr(CAPI_F_CAPI_CTX_NEW, ERR_R_MALLOC_FAILURE); return NULL; } @@ -1628,7 +1655,7 @@ static int capi_ctx_set_provname(CAPI_CTX * ctx, LPSTR pname, DWORD type, CryptReleaseContext(hprov, 0); } OPENSSL_free(ctx->cspname); - ctx->cspname = BUF_strdup(pname); + ctx->cspname = OPENSSL_strdup(pname); ctx->csptype = type; return 1; } @@ -1669,7 +1696,7 @@ static int capi_load_ssl_client_cert(ENGINE *e, SSL *ssl, STACK_OF(X509) *certs = NULL; X509 *x; char *storename; - const char *p; + const unsigned char *p; int i, client_cert_idx; HCERTSTORE hstore; PCCERT_CONTEXT cert = NULL, excert = NULL; @@ -1868,7 +1895,8 @@ OPENSSL_EXPORT IMPLEMENT_DYNAMIC_CHECK_FN() # else -void ENGINE_load_capi(void) +void engine_load_capi_internal(void); +void engine_load_capi_internal(void) { } # endif