X-Git-Url: https://git.openssl.org/gitweb/?a=blobdiff_plain;f=crypto%2Fdsa%2Fdsa_ameth.c;h=73dd158ceea70de76720ad3bd9c4d3e234168022;hb=74924dcb3802640d7e2ae2e80ca6515d0a53de7a;hp=c3a85dae64c45ef05d5f4247e688e7527f96a33b;hpb=a8ae0891d4bfd18f224777aed1fbb172504421f1;p=openssl.git diff --git a/crypto/dsa/dsa_ameth.c b/crypto/dsa/dsa_ameth.c index c3a85dae64..73dd158cee 100644 --- a/crypto/dsa/dsa_ameth.c +++ b/crypto/dsa/dsa_ameth.c @@ -57,7 +57,7 @@ */ #include -#include "cryptlib.h" +#include "internal/cryptlib.h" #include #include #include @@ -65,7 +65,7 @@ #ifndef OPENSSL_NO_CMS # include #endif -#include "asn1_locl.h" +#include "internal/asn1_int.h" static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) { @@ -88,13 +88,13 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) pm = pstr->data; pmlen = pstr->length; - if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) { + if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) { DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); goto err; } } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) { - if (!(dsa = DSA_new())) { + if ((dsa = DSA_new()) == NULL) { DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE); goto err; } @@ -103,12 +103,12 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) goto err; } - if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) { + if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) { DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR); goto err; } - if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) { + if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) { DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR); goto err; } @@ -118,10 +118,8 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) return 1; err: - if (public_key) - ASN1_INTEGER_free(public_key); - if (dsa) - DSA_free(dsa); + ASN1_INTEGER_free(public_key); + DSA_free(dsa); return 0; } @@ -129,28 +127,37 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) { DSA *dsa; - void *pval = NULL; int ptype; unsigned char *penc = NULL; int penclen; + ASN1_STRING *str = NULL; + ASN1_INTEGER *pubint = NULL; dsa = pkey->pkey.dsa; if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) { - ASN1_STRING *str; str = ASN1_STRING_new(); + if (!str) { + DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); + goto err; + } str->length = i2d_DSAparams(dsa, &str->data); if (str->length <= 0) { DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); goto err; } - pval = str; ptype = V_ASN1_SEQUENCE; } else ptype = V_ASN1_UNDEF; - dsa->write_params = 0; + pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL); - penclen = i2d_DSAPublicKey(dsa, &penc); + if (pubint == NULL) { + DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); + goto err; + } + + penclen = i2d_ASN1_INTEGER(pubint, &penc); + ASN1_INTEGER_free(pubint); if (penclen <= 0) { DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); @@ -158,14 +165,12 @@ static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) } if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA), - ptype, pval, penc, penclen)) + ptype, str, penc, penclen)) return 1; err: - if (penc) - OPENSSL_free(penc); - if (pval) - ASN1_STRING_free(pval); + OPENSSL_free(penc); + ASN1_STRING_free(str); return 0; } @@ -196,7 +201,7 @@ static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) /* Check for broken DSA PKCS#8, UGH! */ if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) { ASN1_TYPE *t1, *t2; - if (!(ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen))) + if ((ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen)) == NULL) goto decerr; if (sk_ASN1_TYPE_num(ndsa) != 2) goto decerr; @@ -222,12 +227,12 @@ static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) privkey = t2->value.integer; } else { const unsigned char *q = p; - if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pklen))) + if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) goto decerr; if (privkey->type == V_ASN1_NEG_INTEGER) { p8->broken = PKCS8_NEG_PRIVKEY; ASN1_STRING_clear_free(privkey); - if (!(privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen))) + if ((privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen)) == NULL) goto decerr; } if (ptype != V_ASN1_SEQUENCE) @@ -237,19 +242,20 @@ static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) pstr = pval; pm = pstr->data; pmlen = pstr->length; - if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) + if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) goto decerr; /* We have parameters now set private key */ - if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) { + if ((dsa->priv_key = BN_secure_new()) == NULL + || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) { DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); goto dsaerr; } /* Calculate public key */ - if (!(dsa->pub_key = BN_new())) { + if ((dsa->pub_key = BN_new()) == NULL) { DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); goto dsaerr; } - if (!(ctx = BN_CTX_new())) { + if ((ctx = BN_CTX_new()) == NULL) { DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); goto dsaerr; } @@ -272,8 +278,7 @@ static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR); dsaerr: BN_CTX_free(ctx); - if (privkey) - ASN1_STRING_clear_free(privkey); + ASN1_STRING_clear_free(privkey); sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); DSA_free(dsa); return 0; @@ -324,12 +329,9 @@ static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) return 1; err: - if (dp != NULL) - OPENSSL_free(dp); - if (params != NULL) - ASN1_STRING_free(params); - if (prkey != NULL) - ASN1_STRING_clear_free(prkey); + OPENSSL_free(dp); + ASN1_STRING_free(params); + ASN1_STRING_clear_free(prkey); return 0; } @@ -363,20 +365,17 @@ static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) if ((a = BN_dup(from->pkey.dsa->p)) == NULL) return 0; - if (to->pkey.dsa->p != NULL) - BN_free(to->pkey.dsa->p); + BN_free(to->pkey.dsa->p); to->pkey.dsa->p = a; if ((a = BN_dup(from->pkey.dsa->q)) == NULL) return 0; - if (to->pkey.dsa->q != NULL) - BN_free(to->pkey.dsa->q); + BN_free(to->pkey.dsa->q); to->pkey.dsa->q = a; if ((a = BN_dup(from->pkey.dsa->g)) == NULL) return 0; - if (to->pkey.dsa->g != NULL) - BN_free(to->pkey.dsa->g); + BN_free(to->pkey.dsa->g); to->pkey.dsa->g = a; return 1; } @@ -445,7 +444,7 @@ static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) update_buflen(priv_key, &buf_len); update_buflen(pub_key, &buf_len); - m = (unsigned char *)OPENSSL_malloc(buf_len + 10); + m = OPENSSL_malloc(buf_len + 10); if (m == NULL) { DSAerr(DSA_F_DO_DSA_PRINT, ERR_R_MALLOC_FAILURE); goto err; @@ -471,8 +470,7 @@ static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) goto err; ret = 1; err: - if (m != NULL) - OPENSSL_free(m); + OPENSSL_free(m); return (ret); } @@ -480,7 +478,8 @@ static int dsa_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) { DSA *dsa; - if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) { + + if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) { DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB); return 0; } @@ -515,7 +514,8 @@ static int old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) { DSA *dsa; - if (!(dsa = d2i_DSAPrivateKey(NULL, pder, derlen))) { + + if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) { DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB); return 0; } @@ -562,8 +562,7 @@ static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, goto err; rv = 1; err: - if (m) - OPENSSL_free(m); + OPENSSL_free(m); DSA_SIG_free(dsa_sig); return rv; }