Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/dh/dh_pmeth.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: dh_pmeth.c,v 1.12 2022/01/07 09:27:13 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/dh.h>
66
#include <openssl/err.h>
67
#include <openssl/evp.h>
68
#include <openssl/x509.h>
69
70
#include "bn_lcl.h"
71
#include "dh_local.h"
72
#include "evp_locl.h"
73
74
/* DH pkey context structure */
75
76
typedef struct {
77
  /* Parameter gen parameters */
78
  int prime_len;
79
  int generator;
80
  int use_dsa;
81
  /* Keygen callback info */
82
  int gentmp[2];
83
  /* message digest */
84
} DH_PKEY_CTX;
85
86
static int
87
pkey_dh_init(EVP_PKEY_CTX *ctx)
88
0
{
89
0
  DH_PKEY_CTX *dctx;
90
91
0
  dctx = malloc(sizeof(DH_PKEY_CTX));
92
0
  if (!dctx)
93
0
    return 0;
94
0
  dctx->prime_len = 1024;
95
0
  dctx->generator = 2;
96
0
  dctx->use_dsa = 0;
97
98
0
  ctx->data = dctx;
99
0
  ctx->keygen_info = dctx->gentmp;
100
0
  ctx->keygen_info_count = 2;
101
  
102
0
  return 1;
103
0
}
104
105
static int
106
pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
107
0
{
108
0
  DH_PKEY_CTX *dctx, *sctx;
109
110
0
  if (!pkey_dh_init(dst))
111
0
    return 0;
112
0
        sctx = src->data;
113
0
  dctx = dst->data;
114
0
  dctx->prime_len = sctx->prime_len;
115
0
  dctx->generator = sctx->generator;
116
0
  dctx->use_dsa = sctx->use_dsa;
117
0
  return 1;
118
0
}
119
120
static void
121
pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
122
0
{
123
0
  DH_PKEY_CTX *dctx = ctx->data;
124
125
0
  free(dctx);
126
0
}
127
128
static int
129
pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
130
0
{
131
0
  DH_PKEY_CTX *dctx = ctx->data;
132
133
0
  switch (type) {
134
0
  case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
135
0
    if (p1 < 256)
136
0
      return -2;
137
0
    dctx->prime_len = p1;
138
0
    return 1;
139
140
0
  case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
141
0
    dctx->generator = p1;
142
0
    return 1;
143
144
0
  case EVP_PKEY_CTRL_PEER_KEY:
145
    /* Default behaviour is OK */
146
0
    return 1;
147
148
0
  default:
149
0
    return -2;
150
0
  }
151
0
}
152
      
153
static int
154
pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value)
155
0
{
156
0
  long lval;
157
0
  char *ep;
158
0
  int len;
159
160
0
  if (!strcmp(type, "dh_paramgen_prime_len")) {
161
0
    errno = 0;
162
0
    lval = strtol(value, &ep, 10);
163
0
    if (value[0] == '\0' || *ep != '\0')
164
0
      goto not_a_number;
165
0
    if ((errno == ERANGE &&
166
0
        (lval == LONG_MAX || lval == LONG_MIN)) ||
167
0
        (lval > INT_MAX || lval < INT_MIN))
168
0
      goto out_of_range;
169
0
    len = lval;
170
0
    return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
171
0
  } else if (!strcmp(type, "dh_paramgen_generator")) {
172
0
    errno = 0;
173
0
    lval = strtol(value, &ep, 10);
174
0
    if (value[0] == '\0' || *ep != '\0')
175
0
      goto not_a_number;
176
0
    if ((errno == ERANGE &&
177
0
        (lval == LONG_MAX || lval == LONG_MIN)) ||
178
0
        (lval > INT_MAX || lval < INT_MIN))
179
0
      goto out_of_range;
180
0
    len = lval;
181
0
    return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
182
0
  }
183
184
0
not_a_number:
185
0
out_of_range:
186
0
  return -2;
187
0
}
188
189
static int
190
pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
191
0
{
192
0
  DH *dh = NULL;
193
0
  DH_PKEY_CTX *dctx = ctx->data;
194
0
  BN_GENCB *pcb, cb;
195
0
  int ret;
196
197
0
  if (ctx->pkey_gencb) {
198
0
    pcb = &cb;
199
0
    evp_pkey_set_cb_translate(pcb, ctx);
200
0
  } else
201
0
    pcb = NULL;
202
0
  dh = DH_new();
203
0
  if (!dh)
204
0
    return 0;
205
0
  ret = DH_generate_parameters_ex(dh, dctx->prime_len, dctx->generator,
206
0
      pcb);
207
0
  if (ret)
208
0
    EVP_PKEY_assign_DH(pkey, dh);
209
0
  else
210
0
    DH_free(dh);
211
0
  return ret;
212
0
}
213
214
static int
215
pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
216
0
{
217
0
  DH *dh = NULL;
218
219
0
  if (ctx->pkey == NULL) {
220
0
    DHerror(DH_R_NO_PARAMETERS_SET);
221
0
    return 0;
222
0
  }
223
0
  dh = DH_new();
224
0
  if (!dh)
225
0
    return 0;
226
0
  EVP_PKEY_assign_DH(pkey, dh);
227
  /* Note: if error return, pkey is freed by parent routine */
228
0
  if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
229
0
    return 0;
230
0
  return DH_generate_key(pkey->pkey.dh);
231
0
}
232
233
static int
234
pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
235
0
{
236
0
  int ret;
237
238
0
  if (!ctx->pkey || !ctx->peerkey) {
239
0
    DHerror(DH_R_KEYS_NOT_SET);
240
0
    return 0;
241
0
  }
242
0
  ret = DH_compute_key(key, ctx->peerkey->pkey.dh->pub_key,
243
0
      ctx->pkey->pkey.dh);
244
0
  if (ret < 0)
245
0
    return ret;
246
0
  *keylen = ret;
247
0
  return 1;
248
0
}
249
250
const EVP_PKEY_METHOD dh_pkey_meth = {
251
  .pkey_id = EVP_PKEY_DH,
252
  .flags = EVP_PKEY_FLAG_AUTOARGLEN,
253
254
  .init = pkey_dh_init,
255
  .copy = pkey_dh_copy,
256
  .cleanup = pkey_dh_cleanup,
257
258
  .paramgen = pkey_dh_paramgen,
259
260
  .keygen = pkey_dh_keygen,
261
262
  .derive = pkey_dh_derive,
263
264
  .ctrl = pkey_dh_ctrl,
265
  .ctrl_str = pkey_dh_ctrl_str
266
};