1 /* $OpenBSD: dsa_pmeth.c,v 1.16 2022/11/26 16:08:52 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2006. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <limits.h> 60 #include <stdio.h> 61 #include <string.h> 62 63 #include <openssl/asn1t.h> 64 #include <openssl/bn.h> 65 #include <openssl/err.h> 66 #include <openssl/evp.h> 67 #include <openssl/x509.h> 68 69 #include "bn_local.h" 70 #include "dsa_local.h" 71 #include "evp_local.h" 72 73 /* DSA pkey context structure */ 74 75 typedef struct { 76 /* Parameter gen parameters */ 77 int nbits; /* size of p in bits (default: 1024) */ 78 int qbits; /* size of q in bits (default: 160) */ 79 const EVP_MD *pmd; /* MD for parameter generation */ 80 /* Keygen callback info */ 81 int gentmp[2]; 82 /* message digest */ 83 const EVP_MD *md; /* MD for the signature */ 84 } DSA_PKEY_CTX; 85 86 static int 87 pkey_dsa_init(EVP_PKEY_CTX *ctx) 88 { 89 DSA_PKEY_CTX *dctx; 90 91 dctx = malloc(sizeof(DSA_PKEY_CTX)); 92 if (!dctx) 93 return 0; 94 dctx->nbits = 1024; 95 dctx->qbits = 160; 96 dctx->pmd = NULL; 97 dctx->md = NULL; 98 99 ctx->data = dctx; 100 ctx->keygen_info = dctx->gentmp; 101 ctx->keygen_info_count = 2; 102 103 return 1; 104 } 105 106 static int 107 pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) 108 { 109 DSA_PKEY_CTX *dctx, *sctx; 110 111 if (!pkey_dsa_init(dst)) 112 return 0; 113 sctx = src->data; 114 dctx = dst->data; 115 dctx->nbits = sctx->nbits; 116 dctx->qbits = sctx->qbits; 117 dctx->pmd = sctx->pmd; 118 dctx->md = sctx->md; 119 return 1; 120 } 121 122 static void 123 pkey_dsa_cleanup(EVP_PKEY_CTX *ctx) 124 { 125 DSA_PKEY_CTX *dctx = ctx->data; 126 127 free(dctx); 128 } 129 130 static int 131 pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *out_siglen, 132 const unsigned char *tbs, size_t tbslen) 133 { 134 DSA *dsa = ctx->pkey->pkey.dsa; 135 DSA_PKEY_CTX *dctx = ctx->data; 136 unsigned int siglen; 137 138 *out_siglen = 0; 139 140 if (tbslen > INT_MAX) 141 return 0; 142 143 if (dctx->md != NULL) { 144 if (tbslen != EVP_MD_size(dctx->md)) 145 return 0; 146 } 147 148 if (!DSA_sign(0, tbs, tbslen, sig, &siglen, dsa)) 149 return 0; 150 151 *out_siglen = siglen; 152 153 return 1; 154 } 155 156 static int 157 pkey_dsa_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, 158 const unsigned char *tbs, size_t tbslen) 159 { 160 DSA *dsa = ctx->pkey->pkey.dsa; 161 DSA_PKEY_CTX *dctx = ctx->data; 162 163 if (tbslen > INT_MAX || siglen > INT_MAX) 164 return 0; 165 166 if (dctx->md != NULL) { 167 if (tbslen != EVP_MD_size(dctx->md)) 168 return 0; 169 } 170 171 return DSA_verify(0, tbs, tbslen, sig, siglen, dsa); 172 } 173 174 static int 175 pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) 176 { 177 DSA_PKEY_CTX *dctx = ctx->data; 178 179 switch (type) { 180 case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: 181 if (p1 < 256) 182 return -2; 183 dctx->nbits = p1; 184 return 1; 185 186 case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: 187 if (p1 != 160 && p1 != 224 && p1 && p1 != 256) 188 return -2; 189 dctx->qbits = p1; 190 return 1; 191 192 case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: 193 switch (EVP_MD_type((const EVP_MD *)p2)) { 194 case NID_sha1: 195 case NID_sha224: 196 case NID_sha256: 197 break; 198 default: 199 DSAerror(DSA_R_INVALID_DIGEST_TYPE); 200 return 0; 201 } 202 dctx->md = p2; 203 return 1; 204 205 case EVP_PKEY_CTRL_MD: 206 switch (EVP_MD_type((const EVP_MD *)p2)) { 207 case NID_sha1: 208 case NID_dsa: 209 case NID_dsaWithSHA: 210 case NID_sha224: 211 case NID_sha256: 212 case NID_sha384: 213 case NID_sha512: 214 break; 215 default: 216 DSAerror(DSA_R_INVALID_DIGEST_TYPE); 217 return 0; 218 } 219 dctx->md = p2; 220 return 1; 221 222 case EVP_PKEY_CTRL_GET_MD: 223 *(const EVP_MD **)p2 = dctx->md; 224 return 1; 225 226 case EVP_PKEY_CTRL_DIGESTINIT: 227 case EVP_PKEY_CTRL_PKCS7_SIGN: 228 case EVP_PKEY_CTRL_CMS_SIGN: 229 return 1; 230 231 case EVP_PKEY_CTRL_PEER_KEY: 232 DSAerror(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); 233 return -2; 234 default: 235 return -2; 236 } 237 } 238 239 static int 240 pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value) 241 { 242 long lval; 243 char *ep; 244 245 if (!strcmp(type, "dsa_paramgen_bits")) { 246 int nbits; 247 248 errno = 0; 249 lval = strtol(value, &ep, 10); 250 if (value[0] == '\0' || *ep != '\0') 251 goto not_a_number; 252 if ((errno == ERANGE && 253 (lval == LONG_MAX || lval == LONG_MIN)) || 254 (lval > INT_MAX || lval < INT_MIN)) 255 goto out_of_range; 256 nbits = lval; 257 return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits); 258 } else if (!strcmp(type, "dsa_paramgen_q_bits")) { 259 int qbits; 260 261 errno = 0; 262 lval = strtol(value, &ep, 10); 263 if (value[0] == '\0' || *ep != '\0') 264 goto not_a_number; 265 if ((errno == ERANGE && 266 (lval == LONG_MAX || lval == LONG_MIN)) || 267 (lval > INT_MAX || lval < INT_MIN)) 268 goto out_of_range; 269 qbits = lval; 270 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, 271 EVP_PKEY_OP_PARAMGEN, EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, 272 qbits, NULL); 273 } else if (!strcmp(type, "dsa_paramgen_md")) { 274 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, 275 EVP_PKEY_OP_PARAMGEN, EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0, 276 (void *)EVP_get_digestbyname(value)); 277 } 278 not_a_number: 279 out_of_range: 280 return -2; 281 } 282 283 static int 284 pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) 285 { 286 DSA *dsa = NULL; 287 DSA_PKEY_CTX *dctx = ctx->data; 288 BN_GENCB *pcb, cb; 289 int ret; 290 291 if (ctx->pkey_gencb) { 292 pcb = &cb; 293 evp_pkey_set_cb_translate(pcb, ctx); 294 } else 295 pcb = NULL; 296 dsa = DSA_new(); 297 if (!dsa) 298 return 0; 299 ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd, 300 NULL, 0, NULL, NULL, NULL, pcb); 301 if (ret) 302 EVP_PKEY_assign_DSA(pkey, dsa); 303 else 304 DSA_free(dsa); 305 return ret; 306 } 307 308 static int 309 pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) 310 { 311 DSA *dsa = NULL; 312 313 if (ctx->pkey == NULL) { 314 DSAerror(DSA_R_NO_PARAMETERS_SET); 315 return 0; 316 } 317 dsa = DSA_new(); 318 if (!dsa) 319 return 0; 320 EVP_PKEY_assign_DSA(pkey, dsa); 321 /* Note: if error return, pkey is freed by parent routine */ 322 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey)) 323 return 0; 324 return DSA_generate_key(pkey->pkey.dsa); 325 } 326 327 const EVP_PKEY_METHOD dsa_pkey_meth = { 328 .pkey_id = EVP_PKEY_DSA, 329 .flags = EVP_PKEY_FLAG_AUTOARGLEN, 330 331 .init = pkey_dsa_init, 332 .copy = pkey_dsa_copy, 333 .cleanup = pkey_dsa_cleanup, 334 335 .paramgen = pkey_dsa_paramgen, 336 337 .keygen = pkey_dsa_keygen, 338 339 .sign = pkey_dsa_sign, 340 341 .verify = pkey_dsa_verify, 342 343 .ctrl = pkey_dsa_ctrl, 344 .ctrl_str = pkey_dsa_ctrl_str 345 }; 346