Rename all getters to use get/get0 in name
[openssl.git] / crypto / dsa / dsa_pmeth.c
1 /*
2  * Copyright 2006-2021 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 <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/evp.h>
21 #include <openssl/bn.h>
22 #include "crypto/evp.h"
23 #include "dsa_local.h"
24
25 /* DSA pkey context structure */
26
27 typedef struct {
28     /* Parameter gen parameters */
29     int nbits;                  /* size of p in bits (default: 2048) */
30     int qbits;                  /* size of q in bits (default: 224) */
31     const EVP_MD *pmd;          /* MD for parameter generation */
32     /* Keygen callback info */
33     int gentmp[2];
34     /* message digest */
35     const EVP_MD *md;           /* MD for the signature */
36 } DSA_PKEY_CTX;
37
38 static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
39 {
40     DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
41
42     if (dctx == NULL)
43         return 0;
44     dctx->nbits = 2048;
45     dctx->qbits = 224;
46     dctx->pmd = NULL;
47     dctx->md = NULL;
48
49     ctx->data = dctx;
50     ctx->keygen_info = dctx->gentmp;
51     ctx->keygen_info_count = 2;
52
53     return 1;
54 }
55
56 static int pkey_dsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
57 {
58     DSA_PKEY_CTX *dctx, *sctx;
59
60     if (!pkey_dsa_init(dst))
61         return 0;
62     sctx = src->data;
63     dctx = dst->data;
64     dctx->nbits = sctx->nbits;
65     dctx->qbits = sctx->qbits;
66     dctx->pmd = sctx->pmd;
67     dctx->md = sctx->md;
68     return 1;
69 }
70
71 static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
72 {
73     DSA_PKEY_CTX *dctx = ctx->data;
74     OPENSSL_free(dctx);
75 }
76
77 static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
78                          size_t *siglen, const unsigned char *tbs,
79                          size_t tbslen)
80 {
81     int ret;
82     unsigned int sltmp;
83     DSA_PKEY_CTX *dctx = ctx->data;
84     DSA *dsa = ctx->pkey->pkey.dsa;
85
86     if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
87         return 0;
88
89     ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
90
91     if (ret <= 0)
92         return ret;
93     *siglen = sltmp;
94     return 1;
95 }
96
97 static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
98                            const unsigned char *sig, size_t siglen,
99                            const unsigned char *tbs, size_t tbslen)
100 {
101     int ret;
102     DSA_PKEY_CTX *dctx = ctx->data;
103     DSA *dsa = ctx->pkey->pkey.dsa;
104
105     if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
106         return 0;
107
108     ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
109
110     return ret;
111 }
112
113 static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
114 {
115     DSA_PKEY_CTX *dctx = ctx->data;
116
117     switch (type) {
118     case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
119         if (p1 < 256)
120             return -2;
121         dctx->nbits = p1;
122         return 1;
123
124     case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
125         if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
126             return -2;
127         dctx->qbits = p1;
128         return 1;
129
130     case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
131         if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
132             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
133             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256) {
134             ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
135             return 0;
136         }
137         dctx->pmd = p2;
138         return 1;
139
140     case EVP_PKEY_CTRL_MD:
141         if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
142             EVP_MD_get_type((const EVP_MD *)p2) != NID_dsa &&
143             EVP_MD_get_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
144             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
145             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
146             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
147             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
148             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
149             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
150             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
151             EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512) {
152             ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
153             return 0;
154         }
155         dctx->md = p2;
156         return 1;
157
158     case EVP_PKEY_CTRL_GET_MD:
159         *(const EVP_MD **)p2 = dctx->md;
160         return 1;
161
162     case EVP_PKEY_CTRL_DIGESTINIT:
163     case EVP_PKEY_CTRL_PKCS7_SIGN:
164     case EVP_PKEY_CTRL_CMS_SIGN:
165         return 1;
166
167     case EVP_PKEY_CTRL_PEER_KEY:
168         ERR_raise(ERR_LIB_DSA, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
169         return -2;
170     default:
171         return -2;
172
173     }
174 }
175
176 static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
177                              const char *type, const char *value)
178 {
179     if (strcmp(type, "dsa_paramgen_bits") == 0) {
180         int nbits;
181         nbits = atoi(value);
182         return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
183     }
184     if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
185         int qbits = atoi(value);
186         return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits);
187     }
188     if (strcmp(type, "dsa_paramgen_md") == 0) {
189         const EVP_MD *md = EVP_get_digestbyname(value);
190
191         if (md == NULL) {
192             ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
193             return 0;
194         }
195         return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md);
196     }
197     return -2;
198 }
199
200 static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
201 {
202     DSA *dsa = NULL;
203     DSA_PKEY_CTX *dctx = ctx->data;
204     BN_GENCB *pcb;
205     int ret, res;
206
207     if (ctx->pkey_gencb) {
208         pcb = BN_GENCB_new();
209         if (pcb == NULL)
210             return 0;
211         evp_pkey_set_cb_translate(pcb, ctx);
212     } else
213         pcb = NULL;
214     dsa = DSA_new();
215     if (dsa == NULL) {
216         BN_GENCB_free(pcb);
217         return 0;
218     }
219     if (dctx->md != NULL)
220         ossl_ffc_set_digest(&dsa->params, EVP_MD_get0_name(dctx->md), NULL);
221
222     ret = ossl_ffc_params_FIPS186_4_generate(NULL, &dsa->params,
223                                              FFC_PARAM_TYPE_DSA, dctx->nbits,
224                                              dctx->qbits, &res, pcb);
225     BN_GENCB_free(pcb);
226     if (ret > 0)
227         EVP_PKEY_assign_DSA(pkey, dsa);
228     else
229         DSA_free(dsa);
230     return ret;
231 }
232
233 static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
234 {
235     DSA *dsa = NULL;
236
237     if (ctx->pkey == NULL) {
238         ERR_raise(ERR_LIB_DSA, DSA_R_NO_PARAMETERS_SET);
239         return 0;
240     }
241     dsa = DSA_new();
242     if (dsa == NULL)
243         return 0;
244     EVP_PKEY_assign_DSA(pkey, dsa);
245     /* Note: if error return, pkey is freed by parent routine */
246     if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
247         return 0;
248     return DSA_generate_key(pkey->pkey.dsa);
249 }
250
251 static const EVP_PKEY_METHOD dsa_pkey_meth = {
252     EVP_PKEY_DSA,
253     EVP_PKEY_FLAG_AUTOARGLEN,
254     pkey_dsa_init,
255     pkey_dsa_copy,
256     pkey_dsa_cleanup,
257
258     0,
259     pkey_dsa_paramgen,
260
261     0,
262     pkey_dsa_keygen,
263
264     0,
265     pkey_dsa_sign,
266
267     0,
268     pkey_dsa_verify,
269
270     0, 0,
271
272     0, 0, 0, 0,
273
274     0, 0,
275
276     0, 0,
277
278     0, 0,
279
280     pkey_dsa_ctrl,
281     pkey_dsa_ctrl_str
282 };
283
284 const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void)
285 {
286     return &dsa_pkey_meth;
287 }