Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[openssl.git] / crypto / dsa / dsa_lib.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * DSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include "internal/refcount.h"
19 #include <openssl/bn.h>
20 #include <openssl/asn1.h>
21 #include <openssl/engine.h>
22 #include <openssl/core_names.h>
23 #include "dsa_local.h"
24 #include "crypto/evp.h"
25 #include "crypto/dsa.h"
26 #include "crypto/dh.h" /* required by DSA_dup_DH() */
27
28 static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
29
30 #ifndef FIPS_MODULE
31
32 int DSA_set_ex_data(DSA *d, int idx, void *arg)
33 {
34     return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
35 }
36
37 void *DSA_get_ex_data(const DSA *d, int idx)
38 {
39     return CRYPTO_get_ex_data(&d->ex_data, idx);
40 }
41
42 # ifndef OPENSSL_NO_DH
43 DH *DSA_dup_DH(const DSA *r)
44 {
45     /*
46      * DSA has p, q, g, optional pub_key, optional priv_key.
47      * DH has p, optional length, g, optional pub_key,
48      * optional priv_key, optional q.
49      */
50     DH *ret = NULL;
51     BIGNUM *pub_key = NULL, *priv_key = NULL;
52
53     if (r == NULL)
54         goto err;
55     ret = DH_new();
56     if (ret == NULL)
57         goto err;
58
59     if (!ossl_ffc_params_copy(dh_get0_params(ret), &r->params))
60         goto err;
61
62     if (r->pub_key != NULL) {
63         pub_key = BN_dup(r->pub_key);
64         if (pub_key == NULL)
65             goto err;
66         if (r->priv_key != NULL) {
67             priv_key = BN_dup(r->priv_key);
68             if (priv_key == NULL)
69                 goto err;
70         }
71         if (!DH_set0_key(ret, pub_key, priv_key))
72             goto err;
73     } else if (r->priv_key != NULL) {
74         /* Shouldn't happen */
75         goto err;
76     }
77
78     return ret;
79
80  err:
81     BN_free(pub_key);
82     BN_free(priv_key);
83     DH_free(ret);
84     return NULL;
85 }
86 # endif /*  OPENSSL_NO_DH */
87
88 void DSA_clear_flags(DSA *d, int flags)
89 {
90     d->flags &= ~flags;
91 }
92
93 int DSA_test_flags(const DSA *d, int flags)
94 {
95     return d->flags & flags;
96 }
97
98 void DSA_set_flags(DSA *d, int flags)
99 {
100     d->flags |= flags;
101 }
102
103 ENGINE *DSA_get0_engine(DSA *d)
104 {
105     return d->engine;
106 }
107
108 int DSA_set_method(DSA *dsa, const DSA_METHOD *meth)
109 {
110     /*
111      * NB: The caller is specifically setting a method, so it's not up to us
112      * to deal with which ENGINE it comes from.
113      */
114     const DSA_METHOD *mtmp;
115     mtmp = dsa->meth;
116     if (mtmp->finish)
117         mtmp->finish(dsa);
118 #ifndef OPENSSL_NO_ENGINE
119     ENGINE_finish(dsa->engine);
120     dsa->engine = NULL;
121 #endif
122     dsa->meth = meth;
123     if (meth->init)
124         meth->init(dsa);
125     return 1;
126 }
127 #endif /* FIPS_MODULE */
128
129
130 const DSA_METHOD *DSA_get_method(DSA *d)
131 {
132     return d->meth;
133 }
134
135 static DSA *dsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
136 {
137     DSA *ret = OPENSSL_zalloc(sizeof(*ret));
138
139     if (ret == NULL) {
140         DSAerr(0, ERR_R_MALLOC_FAILURE);
141         return NULL;
142     }
143
144     ret->references = 1;
145     ret->lock = CRYPTO_THREAD_lock_new();
146     if (ret->lock == NULL) {
147         DSAerr(0, ERR_R_MALLOC_FAILURE);
148         OPENSSL_free(ret);
149         return NULL;
150     }
151
152     ret->libctx = libctx;
153     ret->meth = DSA_get_default_method();
154 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
155     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */
156     if (engine) {
157         if (!ENGINE_init(engine)) {
158             DSAerr(0, ERR_R_ENGINE_LIB);
159             goto err;
160         }
161         ret->engine = engine;
162     } else
163         ret->engine = ENGINE_get_default_DSA();
164     if (ret->engine) {
165         ret->meth = ENGINE_get_DSA(ret->engine);
166         if (ret->meth == NULL) {
167             DSAerr(0, ERR_R_ENGINE_LIB);
168             goto err;
169         }
170     }
171 #endif
172
173     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
174
175 #ifndef FIPS_MODULE
176     if (!crypto_new_ex_data_ex(libctx, CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data))
177         goto err;
178 #endif
179
180     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
181         DSAerr(0, ERR_R_INIT_FAIL);
182         goto err;
183     }
184
185     return ret;
186
187  err:
188     DSA_free(ret);
189     return NULL;
190 }
191
192 DSA *DSA_new_method(ENGINE *engine)
193 {
194     return dsa_new_intern(engine, NULL);
195 }
196
197 DSA *dsa_new_with_ctx(OSSL_LIB_CTX *libctx)
198 {
199     return dsa_new_intern(NULL, libctx);
200 }
201
202 #ifndef FIPS_MODULE
203 DSA *DSA_new(void)
204 {
205     return dsa_new_intern(NULL, NULL);
206 }
207 #endif
208
209 void DSA_free(DSA *r)
210 {
211     int i;
212
213     if (r == NULL)
214         return;
215
216     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
217     REF_PRINT_COUNT("DSA", r);
218     if (i > 0)
219         return;
220     REF_ASSERT_ISNT(i < 0);
221
222     if (r->meth != NULL && r->meth->finish != NULL)
223         r->meth->finish(r);
224 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
225     ENGINE_finish(r->engine);
226 #endif
227
228 #ifndef FIPS_MODULE
229     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data);
230 #endif
231
232     CRYPTO_THREAD_lock_free(r->lock);
233
234     ossl_ffc_params_cleanup(&r->params);
235     BN_clear_free(r->pub_key);
236     BN_clear_free(r->priv_key);
237     OPENSSL_free(r);
238 }
239
240 int DSA_up_ref(DSA *r)
241 {
242     int i;
243
244     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
245         return 0;
246
247     REF_PRINT_COUNT("DSA", r);
248     REF_ASSERT_ISNT(i < 2);
249     return ((i > 1) ? 1 : 0);
250 }
251
252 void DSA_get0_pqg(const DSA *d,
253                   const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
254 {
255     ossl_ffc_params_get0_pqg(&d->params, p, q, g);
256 }
257
258 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
259 {
260     /* If the fields p, q and g in d are NULL, the corresponding input
261      * parameters MUST be non-NULL.
262      */
263     if ((d->params.p == NULL && p == NULL)
264         || (d->params.q == NULL && q == NULL)
265         || (d->params.g == NULL && g == NULL))
266         return 0;
267
268     ossl_ffc_params_set0_pqg(&d->params, p, q, g);
269     d->dirty_cnt++;
270
271     return 1;
272 }
273
274 const BIGNUM *DSA_get0_p(const DSA *d)
275 {
276     return d->params.p;
277 }
278
279 const BIGNUM *DSA_get0_q(const DSA *d)
280 {
281     return d->params.q;
282 }
283
284 const BIGNUM *DSA_get0_g(const DSA *d)
285 {
286     return d->params.g;
287 }
288
289 const BIGNUM *DSA_get0_pub_key(const DSA *d)
290 {
291     return d->pub_key;
292 }
293
294 const BIGNUM *DSA_get0_priv_key(const DSA *d)
295 {
296     return d->priv_key;
297 }
298
299 void DSA_get0_key(const DSA *d,
300                   const BIGNUM **pub_key, const BIGNUM **priv_key)
301 {
302     if (pub_key != NULL)
303         *pub_key = d->pub_key;
304     if (priv_key != NULL)
305         *priv_key = d->priv_key;
306 }
307
308 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
309 {
310     /* If the field pub_key in d is NULL, the corresponding input
311      * parameters MUST be non-NULL.  The priv_key field may
312      * be left NULL.
313      */
314     if (d->pub_key == NULL && pub_key == NULL)
315         return 0;
316
317     if (pub_key != NULL) {
318         BN_free(d->pub_key);
319         d->pub_key = pub_key;
320     }
321     if (priv_key != NULL) {
322         BN_free(d->priv_key);
323         d->priv_key = priv_key;
324     }
325     d->dirty_cnt++;
326
327     return 1;
328 }
329
330 int DSA_security_bits(const DSA *d)
331 {
332     if (d->params.p != NULL && d->params.q != NULL)
333         return BN_security_bits(BN_num_bits(d->params.p),
334                                 BN_num_bits(d->params.q));
335     return -1;
336 }
337
338 int DSA_bits(const DSA *dsa)
339 {
340     return BN_num_bits(dsa->params.p);
341 }
342
343 FFC_PARAMS *dsa_get0_params(DSA *dsa)
344 {
345     return &dsa->params;
346 }
347
348 int dsa_ffc_params_fromdata(DSA *dsa, const OSSL_PARAM params[])
349 {
350     int ret;
351     FFC_PARAMS *ffc;
352
353     if (dsa == NULL)
354         return 0;
355     ffc = dsa_get0_params(dsa);
356     if (ffc == NULL)
357         return 0;
358
359     ret = ossl_ffc_params_fromdata(ffc, params);
360     if (ret)
361         dsa->dirty_cnt++;
362     return ret;
363 }
364
365 static int dsa_paramgen_check(EVP_PKEY_CTX *ctx)
366 {
367     if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
368         ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
369         /* Uses the same return values as EVP_PKEY_CTX_ctrl */
370         return -2;
371     }
372     /* If key type not DSA return error */
373     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_DSA)
374         return -1;
375     return 1;
376 }
377
378 int EVP_PKEY_CTX_set_dsa_paramgen_type(EVP_PKEY_CTX *ctx, const char *name)
379 {
380     int ret;
381     OSSL_PARAM params[2], *p = params;
382
383     if ((ret = dsa_paramgen_check(ctx)) <= 0)
384         return ret;
385
386     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
387                                             (char *)name, 0);
388     *p++ = OSSL_PARAM_construct_end();
389
390     return EVP_PKEY_CTX_set_params(ctx, params);
391 }
392
393 int EVP_PKEY_CTX_set_dsa_paramgen_gindex(EVP_PKEY_CTX *ctx, int gindex)
394 {
395     int ret;
396     OSSL_PARAM params[2], *p = params;
397
398     if ((ret = dsa_paramgen_check(ctx)) <= 0)
399         return ret;
400
401     *p++ = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX, &gindex);
402     *p++ = OSSL_PARAM_construct_end();
403
404     return EVP_PKEY_CTX_set_params(ctx, params);
405 }
406
407 int EVP_PKEY_CTX_set_dsa_paramgen_seed(EVP_PKEY_CTX *ctx,
408                                        const unsigned char *seed,
409                                        size_t seedlen)
410 {
411     int ret;
412     OSSL_PARAM params[2], *p = params;
413
414     if ((ret = dsa_paramgen_check(ctx)) <= 0)
415         return ret;
416
417     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
418                                              (void *)seed, seedlen);
419     *p++ = OSSL_PARAM_construct_end();
420
421     return EVP_PKEY_CTX_set_params(ctx, params);
422 }
423
424 int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX *ctx, int nbits)
425 {
426     int ret;
427     OSSL_PARAM params[2], *p = params;
428     size_t bits = nbits;
429
430     if ((ret = dsa_paramgen_check(ctx)) <= 0)
431         return ret;
432
433 #if !defined(FIPS_MODULE)
434     /* TODO(3.0): Remove this eventually when no more legacy */
435     if (ctx->op.keymgmt.genctx == NULL)
436         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA,  EVP_PKEY_OP_PARAMGEN,
437                                  EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, nbits, NULL);
438 #endif
439
440     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_PBITS, &bits);
441     *p++ = OSSL_PARAM_construct_end();
442
443     return EVP_PKEY_CTX_set_params(ctx, params);
444 }
445
446 int EVP_PKEY_CTX_set_dsa_paramgen_q_bits(EVP_PKEY_CTX *ctx, int qbits)
447 {
448     int ret;
449     OSSL_PARAM params[2], *p = params;
450     size_t bits2 = qbits;
451
452     if ((ret = dsa_paramgen_check(ctx)) <= 0)
453         return ret;
454
455 #if !defined(FIPS_MODULE)
456     /* TODO(3.0): Remove this eventually when no more legacy */
457     if (ctx->op.keymgmt.genctx == NULL)
458         return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA,  EVP_PKEY_OP_PARAMGEN,
459                                  EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits, NULL);
460 #endif
461
462     *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_FFC_QBITS, &bits2);
463     *p++ = OSSL_PARAM_construct_end();
464
465     return EVP_PKEY_CTX_set_params(ctx, params);
466 }
467
468 int EVP_PKEY_CTX_set_dsa_paramgen_md_props(EVP_PKEY_CTX *ctx,
469                                            const char *md_name,
470                                            const char *md_properties)
471 {
472     int ret;
473     OSSL_PARAM params[3], *p = params;
474
475     if ((ret = dsa_paramgen_check(ctx)) <= 0)
476         return ret;
477
478 #if !defined(FIPS_MODULE)
479     /* TODO(3.0): Remove this eventually when no more legacy */
480     if (ctx->op.keymgmt.genctx == NULL) {
481         const EVP_MD *md = EVP_get_digestbyname(md_name);
482
483          EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
484                            EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, (void *)(md));
485     }
486 #endif
487
488     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST,
489                                             (char *)md_name, 0);
490     if (md_properties != NULL)
491         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
492                                                 (char *)md_properties, 0);
493     *p++ = OSSL_PARAM_construct_end();
494
495     return EVP_PKEY_CTX_set_params(ctx, params);
496 }
497
498 #if !defined(FIPS_MODULE)
499 int EVP_PKEY_CTX_set_dsa_paramgen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
500 {
501     const char *md_name = (md == NULL) ? "" : EVP_MD_name(md);
502
503     return EVP_PKEY_CTX_set_dsa_paramgen_md_props(ctx, md_name, NULL);
504 }
505 #endif