diff options
| author | tb <> | 2023-08-12 06:14:36 +0000 |
|---|---|---|
| committer | tb <> | 2023-08-12 06:14:36 +0000 |
| commit | 43f9166987ef626b65b6af9f5e4b7a4179c0ecac (patch) | |
| tree | 8a0516229988a4b4d44324162099ad13ebef80fa /src/lib/libcrypto/dsa/dsa_lib.c | |
| parent | 207136fa30c71e8c7b67e46cb5ec9af2b42a6626 (diff) | |
| download | openbsd-43f9166987ef626b65b6af9f5e4b7a4179c0ecac.tar.gz openbsd-43f9166987ef626b65b6af9f5e4b7a4179c0ecac.tar.bz2 openbsd-43f9166987ef626b65b6af9f5e4b7a4179c0ecac.zip | |
Convert {DH,DSA}_new_method() to using calloc()
Due to OPENSSL_NO_ENGINE the engine member of dh and dsa is currently
uninitialized. As a consequence, {DH,DSA}_get0_engine() will return a
garbage pointer, which is particularly bad because the only reason we
kept them in the first place is that they are used by some software...
A side effect of freeing with {DH,DSA}_free() instead of a hand-rolled
version is that we may call ->meth->finish() before ->meth->init() was
called. We need a NULL check for ->meth to be on the safe side in case
we should need to bring ENGINE back.
with nits from djm
ok deraadt djm
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_lib.c')
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_lib.c | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_lib.c b/src/lib/libcrypto/dsa/dsa_lib.c index 46a7dbcfbe..a9d2179aeb 100644 --- a/src/lib/libcrypto/dsa/dsa_lib.c +++ b/src/lib/libcrypto/dsa/dsa_lib.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: dsa_lib.c,v 1.43 2023/07/08 14:28:15 beck Exp $ */ | 1 | /* $OpenBSD: dsa_lib.c,v 1.44 2023/08/12 06:14:36 tb Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -127,61 +127,46 @@ LCRYPTO_ALIAS(DSA_set_method); | |||
| 127 | DSA * | 127 | DSA * |
| 128 | DSA_new_method(ENGINE *engine) | 128 | DSA_new_method(ENGINE *engine) |
| 129 | { | 129 | { |
| 130 | DSA *ret; | 130 | DSA *dsa; |
| 131 | 131 | ||
| 132 | ret = malloc(sizeof(DSA)); | 132 | if ((dsa = calloc(1, sizeof(DSA))) == NULL) { |
| 133 | if (ret == NULL) { | ||
| 134 | DSAerror(ERR_R_MALLOC_FAILURE); | 133 | DSAerror(ERR_R_MALLOC_FAILURE); |
| 135 | return NULL; | 134 | goto err; |
| 136 | } | 135 | } |
| 137 | ret->meth = DSA_get_default_method(); | 136 | |
| 137 | dsa->meth = DSA_get_default_method(); | ||
| 138 | dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; | ||
| 139 | dsa->references = 1; | ||
| 140 | |||
| 138 | #ifndef OPENSSL_NO_ENGINE | 141 | #ifndef OPENSSL_NO_ENGINE |
| 139 | if (engine) { | 142 | if (engine) { |
| 140 | if (!ENGINE_init(engine)) { | 143 | if (!ENGINE_init(engine)) { |
| 141 | DSAerror(ERR_R_ENGINE_LIB); | 144 | DSAerror(ERR_R_ENGINE_LIB); |
| 142 | free(ret); | 145 | goto err; |
| 143 | return NULL; | ||
| 144 | } | 146 | } |
| 145 | ret->engine = engine; | 147 | dsa->engine = engine; |
| 146 | } else | 148 | } else |
| 147 | ret->engine = ENGINE_get_default_DSA(); | 149 | dsa->engine = ENGINE_get_default_DSA(); |
| 148 | if (ret->engine) { | 150 | if (dsa->engine != NULL) { |
| 149 | ret->meth = ENGINE_get_DSA(ret->engine); | 151 | if ((dsa->meth = ENGINE_get_DSA(dsa->engine)) == NULL) { |
| 150 | if (ret->meth == NULL) { | ||
| 151 | DSAerror(ERR_R_ENGINE_LIB); | 152 | DSAerror(ERR_R_ENGINE_LIB); |
| 152 | ENGINE_finish(ret->engine); | 153 | goto err; |
| 153 | free(ret); | ||
| 154 | return NULL; | ||
| 155 | } | 154 | } |
| 155 | dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; | ||
| 156 | } | 156 | } |
| 157 | #endif | 157 | #endif |
| 158 | 158 | ||
| 159 | ret->pad = 0; | 159 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data)) |
| 160 | ret->version = 0; | 160 | goto err; |
| 161 | ret->p = NULL; | 161 | if (dsa->meth->init != NULL && !dsa->meth->init(dsa)) |
| 162 | ret->q = NULL; | 162 | goto err; |
| 163 | ret->g = NULL; | ||
| 164 | |||
| 165 | ret->pub_key = NULL; | ||
| 166 | ret->priv_key = NULL; | ||
| 167 | 163 | ||
| 168 | ret->kinv = NULL; | 164 | return dsa; |
| 169 | ret->r = NULL; | ||
| 170 | ret->method_mont_p = NULL; | ||
| 171 | 165 | ||
| 172 | ret->references = 1; | 166 | err: |
| 173 | ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; | 167 | DSA_free(dsa); |
| 174 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | ||
| 175 | if (ret->meth->init != NULL && !ret->meth->init(ret)) { | ||
| 176 | #ifndef OPENSSL_NO_ENGINE | ||
| 177 | ENGINE_finish(ret->engine); | ||
| 178 | #endif | ||
| 179 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); | ||
| 180 | free(ret); | ||
| 181 | ret = NULL; | ||
| 182 | } | ||
| 183 | 168 | ||
| 184 | return ret; | 169 | return NULL; |
| 185 | } | 170 | } |
| 186 | LCRYPTO_ALIAS(DSA_new_method); | 171 | LCRYPTO_ALIAS(DSA_new_method); |
| 187 | 172 | ||
| @@ -197,7 +182,7 @@ DSA_free(DSA *r) | |||
| 197 | if (i > 0) | 182 | if (i > 0) |
| 198 | return; | 183 | return; |
| 199 | 184 | ||
| 200 | if (r->meth->finish) | 185 | if (r->meth != NULL && r->meth->finish != NULL) |
| 201 | r->meth->finish(r); | 186 | r->meth->finish(r); |
| 202 | #ifndef OPENSSL_NO_ENGINE | 187 | #ifndef OPENSSL_NO_ENGINE |
| 203 | ENGINE_finish(r->engine); | 188 | ENGINE_finish(r->engine); |
