/*
- * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2018 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
/* Attempt to decode public key and cache in pubkey structure. */
X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
EVP_PKEY_free(pubkey->pkey);
+ pubkey->pkey = NULL;
/*
* Opportunistically decode the key but remove any non fatal errors
* from the queue. Subsequent explicit attempts to decode/use the key
IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
+/* TODO should better be called X509_PUBKEY_set1 */
int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
{
X509_PUBKEY *pk = NULL;
if ((pk = X509_PUBKEY_new()) == NULL)
goto error;
- if (pkey->ameth) {
+ if (pkey != NULL && pkey->ameth) {
if (pkey->ameth->pub_encode) {
if (!pkey->ameth->pub_encode(pk, pkey)) {
X509err(X509_F_X509_PUBKEY_SET,
X509_PUBKEY_free(*x);
*x = pk;
pk->pkey = pkey;
- EVP_PKEY_up_ref(pkey);
- return 1;
+ return EVP_PKEY_up_ref(pkey);
error:
X509_PUBKEY_free(pk);
static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)
- {
+{
EVP_PKEY *pkey = EVP_PKEY_new();
if (pkey == NULL) {
return pktmp;
}
-int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
+int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
{
X509_PUBKEY *xpk = NULL;
- int ret;
- if (!a)
- return 0;
- if (!X509_PUBKEY_set(&xpk, a))
+ int ret = -1;
+
+ if (a == NULL)
return 0;
+ if ((xpk = X509_PUBKEY_new()) == NULL)
+ return -1;
+ if (a->ameth != NULL && a->ameth->pub_encode != NULL
+ && !a->ameth->pub_encode(xpk, a))
+ goto error;
+ xpk->pkey = (EVP_PKEY *)a;
ret = i2d_X509_PUBKEY(xpk, pp);
+ xpk->pkey = NULL;
+ error:
X509_PUBKEY_free(xpk);
return ret;
}
return key;
}
-int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
+int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
{
EVP_PKEY *pktmp;
int ret;
pktmp = EVP_PKEY_new();
if (pktmp == NULL) {
ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
- return 0;
+ return -1;
}
- EVP_PKEY_set1_RSA(pktmp, a);
+ (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
ret = i2d_PUBKEY(pktmp, pp);
+ pktmp->pkey.ptr = NULL;
EVP_PKEY_free(pktmp);
return ret;
}
return key;
}
-int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
+int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
{
EVP_PKEY *pktmp;
int ret;
pktmp = EVP_PKEY_new();
if (pktmp == NULL) {
ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
- return 0;
+ return -1;
}
- EVP_PKEY_set1_DSA(pktmp, a);
+ (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
ret = i2d_PUBKEY(pktmp, pp);
+ pktmp->pkey.ptr = NULL;
EVP_PKEY_free(pktmp);
return ret;
}
return key;
}
-int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
+int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
{
EVP_PKEY *pktmp;
int ret;
return 0;
if ((pktmp = EVP_PKEY_new()) == NULL) {
ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
- return 0;
+ return -1;
}
- EVP_PKEY_set1_EC_KEY(pktmp, a);
+ (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
ret = i2d_PUBKEY(pktmp, pp);
+ pktmp->pkey.ptr = NULL;
EVP_PKEY_free(pktmp);
return ret;
}