/*
- * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * Licensed under the Apache License 2.0 (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
*/
+/* We need to use some deprecated APIs */
+#define OPENSSL_SUPPRESS_DEPRECATED
+
#ifdef _WIN32
# ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0400
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;
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;
if (capi_idx < 0) {
capi_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, 0);
- if (capi_idx < 0)
- goto memerr;
+ if (capi_idx < 0) {
+ CAPIerr(CAPI_F_CAPI_INIT, ERR_R_ENGINE_LIB);
+ goto err;
+ }
cert_capi_idx = X509_get_ex_new_index(0, NULL, NULL, NULL, 0);
/* Setup RSA_METHOD */
rsa_capi_idx = RSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
ossl_rsa_meth = RSA_PKCS1_OpenSSL();
- if ( !RSA_meth_set_pub_enc(capi_rsa_method,
+ 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_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;
+ CAPIerr(CAPI_F_CAPI_INIT, ERR_R_RSA_LIB);
+ goto err;
}
# 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();
- if ( !DSA_meth_set_sign(capi_dsa_method, capi_dsa_do_sign)
+ 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_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;
+ CAPIerr(CAPI_F_CAPI_INIT, ERR_R_DSA_LIB);
+ goto err;
}
# endif
}
ctx = capi_ctx_new();
- if (ctx == NULL)
- goto memerr;
+ if (ctx == NULL) {
+ CAPIerr(CAPI_F_CAPI_INIT, ERR_R_CAPI_LIB);
+ goto err;
+ }
ENGINE_set_ex_data(e, capi_idx, ctx);
return 1;
- memerr:
- CAPIerr(CAPI_F_CAPI_INIT, ERR_R_MALLOC_FAILURE);
+ err:
return 0;
-
- return 1;
}
static int capi_destroy(ENGINE *e)
ENGINE *toadd = engine_capi();
if (!toadd)
return;
+ ERR_set_mark();
ENGINE_add(toadd);
+ /*
+ * If the "add" worked, it gets a structural reference. So either way, we
+ * release our just-created reference.
+ */
ENGINE_free(toadd);
- ERR_clear_error();
+ /*
+ * If the "add" didn't work, it was probably a conflict because it was
+ * already added (eg. someone calling ENGINE_load_blah then calling
+ * ENGINE_load_builtin_engines() perhaps).
+ */
+ ERR_pop_to_mark();
}
# endif
pubkey = OPENSSL_malloc(len);
if (pubkey == NULL)
- goto memerr;
+ goto err;
if (!CryptExportKey(key->key, 0, PUBLICKEYBLOB, 0, pubkey, &len)) {
CAPIerr(CAPI_F_CAPI_GET_PKEY, CAPI_R_PUBKEY_EXPORT_ERROR);
}
rsa_modulus = (unsigned char *)(rp + 1);
rkey = RSA_new_method(eng);
- if (!rkey)
- goto memerr;
+ if (!rkey) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_RSA_LIB);
+ goto err;
+ }
e = BN_new();
n = BN_new();
if (e == NULL || n == NULL) {
BN_free(e);
BN_free(n);
- goto memerr;
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_BN_LIB);
+ goto err;
}
RSA_set0_key(rkey, n, e, NULL);
- if (!BN_set_word(e, rp->pubexp))
- goto memerr;
+ if (!BN_set_word(e, rp->pubexp)) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_BN_LIB);
+ goto err;
+ }
rsa_modlen = rp->bitlen / 8;
- if (!lend_tobn(n, rsa_modulus, rsa_modlen))
- goto memerr;
+ if (!lend_tobn(n, rsa_modulus, rsa_modlen)) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_BN_LIB);
+ goto err;
+ }
RSA_set_ex_data(rkey, rsa_capi_idx, key);
- if ((ret = EVP_PKEY_new()) == NULL)
- goto memerr;
+ if ((ret = EVP_PKEY_new()) == NULL) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_EVP_LIB);
+ goto err;
+ }
EVP_PKEY_assign_RSA(ret, rkey);
rkey = NULL;
dsa_plen = dp->bitlen / 8;
btmp = (unsigned char *)(dp + 1);
dkey = DSA_new_method(eng);
- if (!dkey)
- goto memerr;
+ if (!dkey) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_DSA_LIB);
+ goto err;
+ }
p = BN_new();
q = BN_new();
g = BN_new();
BN_free(q);
BN_free(g);
BN_free(pub_key);
- goto memerr;
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_BN_LIB);
+ goto err;
}
DSA_set0_pqg(dkey, p, q, g);
DSA_set0_key(dkey, pub_key, NULL);
- if (!lend_tobn(p, btmp, dsa_plen))
- goto memerr;
+ if (!lend_tobn(p, btmp, dsa_plen)) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_CAPI_LIB);
+ goto err;
+ }
btmp += dsa_plen;
- if (!lend_tobn(q, btmp, 20))
- goto memerr;
+ if (!lend_tobn(q, btmp, 20)) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_CAPI_LIB);
+ goto err;
+ }
btmp += 20;
- if (!lend_tobn(g, btmp, dsa_plen))
- goto memerr;
+ if (!lend_tobn(g, btmp, dsa_plen)) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_CAPI_LIB);
+ goto err;
+ }
btmp += dsa_plen;
- if (!lend_tobn(pub_key, btmp, dsa_plen))
- goto memerr;
+ if (!lend_tobn(pub_key, btmp, dsa_plen)) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_CAPI_LIB);
+ goto err;
+ }
btmp += dsa_plen;
DSA_set_ex_data(dkey, dsa_capi_idx, key);
- if ((ret = EVP_PKEY_new()) == NULL)
- goto memerr;
+ if ((ret = EVP_PKEY_new()) == NULL) {
+ CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_EVP_LIB);
+ goto err;
+ }
EVP_PKEY_assign_DSA(ret, dkey);
dkey = NULL;
}
return ret;
-
- memerr:
- CAPIerr(CAPI_F_CAPI_GET_PKEY, ERR_R_MALLOC_FAILURE);
- goto err;
-
}
static EVP_PKEY *capi_load_privkey(ENGINE *eng, const char *key_id,
unsigned char *tmpbuf;
CAPI_KEY *capi_key;
CAPI_CTX *ctx;
+ DWORD flags = 0;
DWORD dlen;
if (flen <= 0)
return -1;
}
- if (padding != RSA_PKCS1_PADDING) {
- char errstr[10];
- BIO_snprintf(errstr, 10, "%d", padding);
- CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_UNSUPPORTED_PADDING);
- ERR_add_error_data(2, "padding=", errstr);
- return -1;
+ switch (padding) {
+ case RSA_PKCS1_PADDING:
+ /* Nothing to do */
+ break;
+#ifdef CRYPT_DECRYPT_RSA_NO_PADDING_CHECK
+ case RSA_NO_PADDING:
+ flags = CRYPT_DECRYPT_RSA_NO_PADDING_CHECK;
+ break;
+#endif
+ default:
+ {
+ char errstr[10];
+ BIO_snprintf(errstr, 10, "%d", padding);
+ CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_UNSUPPORTED_PADDING);
+ ERR_add_error_data(2, "padding=", errstr);
+ return -1;
+ }
}
/* Create temp reverse order version of input */
- if ((tmpbuf = OPENSSL_malloc(flen)) == NULL) {
- CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, ERR_R_MALLOC_FAILURE);
+ if ((tmpbuf = OPENSSL_malloc(flen)) == NULL)
return -1;
- }
for (i = 0; i < flen; i++)
tmpbuf[flen - i - 1] = from[i];
/* Finally decrypt it */
dlen = flen;
- if (!CryptDecrypt(capi_key->key, 0, TRUE, 0, tmpbuf, &dlen)) {
+ if (!CryptDecrypt(capi_key->key, 0, TRUE, flags, tmpbuf, &dlen)) {
CAPIerr(CAPI_F_CAPI_RSA_PRIV_DEC, CAPI_R_DECRYPT_ERROR);
capi_addlasterror();
+ OPENSSL_cleanse(tmpbuf, dlen);
OPENSSL_free(tmpbuf);
return -1;
} else {
memcpy(to, tmpbuf, (flen = (int)dlen));
}
+ OPENSSL_cleanse(tmpbuf, flen);
OPENSSL_free(tmpbuf);
return flen;
{
char *str;
int len_0, sz;
+ size_t len_1;
if (!wstr)
return NULL;
- len_0 = (int)wcslen(wstr) + 1; /* WideCharToMultiByte expects int */
+
+ len_1 = wcslen(wstr) + 1;
+
+ if (len_1 > INT_MAX) {
+ CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_FUNCTION_NOT_SUPPORTED);
+ return NULL;
+ }
+
+ len_0 = (int)len_1; /* WideCharToMultiByte expects int */
sz = WideCharToMultiByte(CP_ACP, 0, wstr, len_0, NULL, 0, NULL, NULL);
if (!sz) {
CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR);
return NULL;
}
str = OPENSSL_malloc(sz);
- if (str == NULL) {
- CAPIerr(CAPI_F_WIDE_TO_ASC, ERR_R_MALLOC_FAILURE);
+ if (str == NULL)
return NULL;
- }
if (!WideCharToMultiByte(CP_ACP, 0, wstr, len_0, str, sz, NULL, NULL)) {
OPENSSL_free(str);
CAPIerr(CAPI_F_WIDE_TO_ASC, CAPI_R_WIN32_ERROR);
return 0;
}
name = OPENSSL_malloc(len);
- if (name == NULL) {
- CAPIerr(CAPI_F_CAPI_GET_PROVNAME, ERR_R_MALLOC_FAILURE);
+ if (name == NULL)
return 0;
- }
if (!CryptEnumProviders(idx, NULL, 0, ptype, name, &len)) {
err = GetLastError();
OPENSSL_free(name);
if (buflen == 0)
buflen = 1024;
cname = OPENSSL_malloc(buflen);
- if (cname == NULL) {
- CAPIerr(CAPI_F_CAPI_LIST_CONTAINERS, ERR_R_MALLOC_FAILURE);
+ if (cname == NULL)
goto err;
- }
for (idx = 0;; idx++) {
clen = buflen;
NULL, &len))
return NULL;
pinfo = OPENSSL_malloc(len);
- if (pinfo == NULL) {
- CAPIerr(CAPI_F_CAPI_GET_PROV_INFO, ERR_R_MALLOC_FAILURE);
+ if (pinfo == NULL)
return NULL;
- }
if (!CertGetCertificateContextProperty(cert, CERT_KEY_PROV_INFO_PROP_ID,
pinfo, &len)) {
CAPIerr(CAPI_F_CAPI_GET_PROV_INFO,
CRYPT_KEY_PROV_INFO *pinfo)
{
char *provname = NULL, *contname = NULL;
- if (!pinfo) {
+
+ if (pinfo == NULL) {
BIO_printf(out, " No Private Key\n");
return;
}
provname = wide_to_asc(pinfo->pwszProvName);
contname = wide_to_asc(pinfo->pwszContainerName);
- if (!provname || !contname)
+ if (provname == NULL || contname == NULL)
goto err;
BIO_printf(out, " Private Key Info:\n");
{
CAPI_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
- if (ctx == NULL) {
- CAPIerr(CAPI_F_CAPI_CTX_NEW, ERR_R_MALLOC_FAILURE);
+ if (ctx == NULL)
return NULL;
- }
ctx->csptype = PROV_RSA_FULL;
ctx->dump_flags = CAPI_DMP_SUMMARY | CAPI_DMP_FNAME;
ctx->keytype = AT_KEYEXCHANGE;
CryptReleaseContext(hprov, 0);
}
tmpcspname = OPENSSL_strdup(pname);
- if (tmpcspname == NULL) {
- CAPIerr(CAPI_F_CAPI_CTX_SET_PROVNAME, ERR_R_MALLOC_FAILURE);
+ if (tmpcspname == NULL)
return 0;
- }
OPENSSL_free(ctx->cspname);
ctx->cspname = tmpcspname;
ctx->csptype = type;
sk_X509_free(certs);
- if (!*pcert)
+ if (*pcert == NULL)
return 0;
/* Setup key for selected certificate */