Coverage Report

Created: 2026-08-14 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/cert-cred.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2016 Free Software Foundation, Inc.
3
 * Copyright (C) 2015-2017 Red Hat, Inc.
4
 *
5
 * Author: Nikos Mavrogiannopoulos
6
 *
7
 * This file is part of GnuTLS.
8
 *
9
 * The GnuTLS is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; either version 2.1 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
21
 *
22
 */
23
24
/* Some of the stuff needed for Certificate authentication is contained
25
 * in this file.
26
 */
27
28
#include "gnutls_int.h"
29
#include "errors.h"
30
#include "auth/cert.h"
31
#include "datum.h"
32
#include "mpi.h"
33
#include "global.h"
34
#include "algorithms.h"
35
#include "dh.h"
36
#include "str.h"
37
#include "state.h"
38
#include "auth.h"
39
#include "x509.h"
40
#include "str_array.h"
41
#include "x509/verify-high.h"
42
#include "x509/x509_int.h"
43
#include "x509/common.h"
44
#include "dh.h"
45
#include "cert-cred.h"
46
#include "intprops.h"
47
48
/*
49
 * Adds a public/private key pair to a certificate credential
50
 */
51
int _gnutls_certificate_credential_append_keypair(
52
  gnutls_certificate_credentials_t res, gnutls_privkey_t key,
53
  gnutls_str_array_t names, gnutls_pcert_st *crt, int nr)
54
0
{
55
0
  if (unlikely(INT_ADD_OVERFLOW(res->ncerts, 1))) {
56
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
57
0
  }
58
59
0
  res->sorted_cert_idx = _gnutls_reallocarray_fast(
60
0
    res->sorted_cert_idx, res->ncerts + 1, sizeof(unsigned int));
61
0
  if (res->sorted_cert_idx == NULL)
62
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
63
64
0
  res->certs = _gnutls_reallocarray_fast(res->certs, res->ncerts + 1,
65
0
                 sizeof(certs_st));
66
0
  if (res->certs == NULL)
67
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
68
69
0
  memset(&res->certs[res->ncerts], 0, sizeof(res->certs[0]));
70
71
0
  res->certs[res->ncerts].cert_list = crt;
72
0
  res->certs[res->ncerts].cert_list_length = nr;
73
0
  res->certs[res->ncerts].names = names;
74
0
  res->certs[res->ncerts].pkey = key;
75
76
0
  if (_gnutls13_sign_get_compatible_with_privkey(key))
77
0
    res->tls13_ok = 1;
78
79
  /* move RSA-PSS certificates before any RSA key.
80
   * Note that we cannot assume that any previous pointers
81
   * to sorted list are ok, due to the realloc in res->certs. */
82
0
  if (crt->pubkey->params.algo == GNUTLS_PK_RSA_PSS) {
83
0
    unsigned i, ridx;
84
0
    unsigned tmp;
85
86
0
    for (i = 0; i < res->ncerts; i++) {
87
0
      ridx = res->sorted_cert_idx[i];
88
89
0
      if (res->certs[ridx].cert_list->pubkey->params.algo ==
90
0
          GNUTLS_PK_RSA) {
91
0
        tmp = ridx;
92
0
        res->sorted_cert_idx[i] = res->ncerts;
93
0
        res->sorted_cert_idx[res->ncerts] = tmp;
94
0
        goto finish;
95
0
      }
96
0
    }
97
0
  }
98
99
  /* otherwise append it normally on the end */
100
0
  res->sorted_cert_idx[res->ncerts] = res->ncerts;
101
102
0
finish:
103
0
  return 0;
104
0
}
105
106
/**
107
 * gnutls_certificate_set_key:
108
 * @res: is a #gnutls_certificate_credentials_t type.
109
 * @names: is an array of DNS names belonging to the public-key (NULL if none)
110
 * @names_size: holds the size of the names list
111
 * @pcert_list: contains a certificate list (chain) or raw public-key
112
 * @pcert_list_size: holds the size of the certificate list
113
 * @key: is a #gnutls_privkey_t key corresponding to the first public-key in pcert_list
114
 *
115
 * This function sets a public/private key pair in the
116
 * gnutls_certificate_credentials_t type. The given public key may be encapsulated
117
 * in a certificate or can be given as a raw key. This function may be
118
 * called more than once, in case multiple key pairs exist for
119
 * the server. For clients that want to send more than their own end-
120
 * entity certificate (e.g., also an intermediate CA cert), the full
121
 * certificate chain must be provided in @pcert_list.
122
 *
123
 * Note that the @key will become part of the credentials structure and must
124
 * not be deallocated. It will be automatically deallocated when the @res structure
125
 * is deinitialized.
126
 *
127
 * If this function fails, the @res structure is at an undefined state and it must
128
 * not be reused to load other keys or certificates.
129
 *
130
 * Note that, this function by default returns zero on success and a negative value on error.
131
 * Since 3.5.6, when the flag %GNUTLS_CERTIFICATE_API_V2 is set using gnutls_certificate_set_flags()
132
 * it returns an index (greater or equal to zero). That index can be used for other functions to refer to the added key-pair.
133
 *
134
 * Since GnuTLS 3.6.6 this function also handles raw public keys.
135
 *
136
 * Returns: On success this functions returns zero, and otherwise a negative value on error (see above for modifying that behavior).
137
 *
138
 * Since: 3.0
139
 **/
140
int gnutls_certificate_set_key(gnutls_certificate_credentials_t res,
141
             const char **names, int names_size,
142
             gnutls_pcert_st *pcert_list, int pcert_list_size,
143
             gnutls_privkey_t key)
144
0
{
145
0
  int ret, i;
146
0
  gnutls_str_array_t str_names;
147
0
  gnutls_pcert_st *new_pcert_list;
148
149
  /* Sanity checks */
150
  // Check for a valid credential struct
151
0
  if (res == NULL) {
152
0
    return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
153
0
  }
154
  // A complete key pair must be given
155
0
  if (pcert_list == NULL || key == NULL) {
156
0
    return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS);
157
0
  }
158
159
  /* Process the names, if any */
160
0
  _gnutls_str_array_init(&str_names);
161
162
0
  if (names != NULL && names_size > 0) {
163
0
    for (i = 0; i < names_size; i++) {
164
0
      ret = _gnutls_str_array_append_idna(
165
0
        &str_names, names[i], strlen(names[i]));
166
0
      if (ret < 0) {
167
0
        ret = gnutls_assert_val(ret);
168
0
        goto cleanup;
169
0
      }
170
0
    }
171
0
  } else if (names == NULL && pcert_list[0].type == GNUTLS_CRT_X509) {
172
0
    gnutls_x509_crt_t crt;
173
174
0
    ret = gnutls_x509_crt_init(&crt);
175
0
    if (ret < 0) {
176
0
      gnutls_assert();
177
0
      goto cleanup;
178
0
    }
179
180
0
    ret = gnutls_x509_crt_import(crt, &pcert_list[0].cert,
181
0
               GNUTLS_X509_FMT_DER);
182
0
    if (ret < 0) {
183
0
      gnutls_assert();
184
0
      gnutls_x509_crt_deinit(crt);
185
0
      goto cleanup;
186
0
    }
187
188
0
    ret = _gnutls_get_x509_name(crt, &str_names);
189
0
    gnutls_x509_crt_deinit(crt);
190
191
0
    if (ret < 0) {
192
0
      gnutls_assert();
193
0
      goto cleanup;
194
0
    }
195
0
  }
196
197
0
  if (res->pin.cb)
198
0
    gnutls_privkey_set_pin_function(key, res->pin.cb,
199
0
            res->pin.data);
200
201
0
  new_pcert_list = _gnutls_reallocarray(NULL, pcert_list_size,
202
0
                sizeof(gnutls_pcert_st));
203
0
  if (new_pcert_list == NULL) {
204
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
205
0
  }
206
0
  memcpy(new_pcert_list, pcert_list,
207
0
         sizeof(gnutls_pcert_st) * pcert_list_size);
208
209
0
  ret = _gnutls_certificate_credential_append_keypair(
210
0
    res, key, str_names, new_pcert_list, pcert_list_size);
211
0
  if (ret < 0) {
212
0
    gnutls_assert();
213
0
    gnutls_free(new_pcert_list);
214
0
    goto cleanup;
215
0
  }
216
217
0
  res->ncerts++;
218
219
  /* Unlike gnutls_certificate_set_x509_key, we deinitialize everything
220
   * local after a failure. That is because the caller is responsible for
221
   * freeing these values after a failure, and if we keep references we
222
   * lead to double freeing */
223
0
  if ((ret = _gnutls_check_key_cert_match(res)) < 0) {
224
0
    gnutls_assert();
225
0
    gnutls_free(new_pcert_list);
226
0
    res->ncerts--;
227
0
    goto cleanup;
228
0
  }
229
230
0
  CRED_RET_SUCCESS(res);
231
232
0
cleanup:
233
0
  _gnutls_str_array_clear(&str_names);
234
0
  return ret;
235
0
}
236
237
/**
238
 * gnutls_certificate_free_keys:
239
 * @sc: is a #gnutls_certificate_credentials_t type.
240
 *
241
 * This function will delete all the keys and the certificates associated
242
 * with the given credentials. This function must not be called when a
243
 * TLS negotiation that uses the credentials is in progress.
244
 *
245
 **/
246
void gnutls_certificate_free_keys(gnutls_certificate_credentials_t sc)
247
0
{
248
0
  unsigned i, j;
249
250
0
  for (i = 0; i < sc->ncerts; i++) {
251
0
    for (j = 0; j < sc->certs[i].cert_list_length; j++) {
252
0
      gnutls_pcert_deinit(&sc->certs[i].cert_list[j]);
253
0
    }
254
0
    gnutls_free(sc->certs[i].cert_list);
255
256
0
    for (j = 0; j < sc->certs[i].ocsp_data_length; j++) {
257
0
      gnutls_free(sc->certs[i].ocsp_data[j].response.data);
258
0
    }
259
0
    _gnutls_str_array_clear(&sc->certs[i].names);
260
0
    gnutls_privkey_deinit(sc->certs[i].pkey);
261
0
  }
262
263
0
  gnutls_free(sc->certs);
264
0
  gnutls_free(sc->sorted_cert_idx);
265
266
0
  sc->ncerts = 0;
267
0
}
268
269
/**
270
 * gnutls_certificate_free_cas:
271
 * @sc: is a #gnutls_certificate_credentials_t type.
272
 *
273
 * This function was operational on very early versions of gnutls.
274
 * Due to internal refactorings and the fact that this was hardly ever
275
 * used, it is currently a no-op.
276
 *
277
 **/
278
void gnutls_certificate_free_cas(gnutls_certificate_credentials_t sc)
279
0
{
280
0
  return;
281
0
}
282
283
/**
284
 * gnutls_certificate_get_issuer:
285
 * @sc: is a #gnutls_certificate_credentials_t type.
286
 * @cert: is the certificate to find issuer for
287
 * @issuer: Will hold the issuer if any. Should be treated as constant.
288
 * @flags: Use zero or %GNUTLS_TL_GET_COPY
289
 *
290
 * This function will return the issuer of a given certificate.
291
 * If the flag %GNUTLS_TL_GET_COPY is specified a copy of the issuer
292
 * will be returned which must be freed using gnutls_x509_crt_deinit().
293
 * In that case the provided @issuer must not be initialized.
294
 *
295
 * As with gnutls_x509_trust_list_get_issuer() this function requires
296
 * the %GNUTLS_TL_GET_COPY flag in order to operate with PKCS#11 trust
297
 * lists in a thread-safe way.
298
 *
299
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
300
 *   negative error value.
301
 *
302
 * Since: 3.0
303
 **/
304
int gnutls_certificate_get_issuer(gnutls_certificate_credentials_t sc,
305
          gnutls_x509_crt_t cert,
306
          gnutls_x509_crt_t *issuer, unsigned int flags)
307
0
{
308
0
  return gnutls_x509_trust_list_get_issuer(sc->tlist, cert, issuer,
309
0
             flags);
310
0
}
311
312
/**
313
 * gnutls_certificate_get_crt_raw:
314
 * @sc: is a #gnutls_certificate_credentials_t type.
315
 * @idx1: the index of the certificate chain if multiple are present
316
 * @idx2: the index of the certificate in the chain. Zero gives the server's certificate.
317
 * @cert: Will hold the DER encoded certificate.
318
 *
319
 * This function will return the DER encoded certificate of the
320
 * server or any other certificate on its certificate chain (based on @idx2).
321
 * The returned data should be treated as constant and only accessible during the lifetime
322
 * of @sc. The @idx1 matches the value gnutls_certificate_set_x509_key() and friends
323
 * functions.
324
 *
325
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
326
 *   negative error value. In case the indexes are out of bounds %GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
327
 *   is returned.
328
 *
329
 * Since: 3.2.5
330
 **/
331
int gnutls_certificate_get_crt_raw(gnutls_certificate_credentials_t sc,
332
           unsigned idx1, unsigned idx2,
333
           gnutls_datum_t *cert)
334
0
{
335
0
  if (idx1 >= sc->ncerts)
336
0
    return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
337
338
0
  if (idx2 >= sc->certs[idx1].cert_list_length)
339
0
    return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
340
341
0
  cert->data = sc->certs[idx1].cert_list[idx2].cert.data;
342
0
  cert->size = sc->certs[idx1].cert_list[idx2].cert.size;
343
344
0
  return 0;
345
0
}
346
347
/**
348
 * gnutls_certificate_free_ca_names:
349
 * @sc: is a #gnutls_certificate_credentials_t type.
350
 *
351
 * This function will delete all the CA name in the given
352
 * credentials. Clients may call this to save some memory since in
353
 * client side the CA names are not used. Servers might want to use
354
 * this function if a large list of trusted CAs is present and
355
 * sending the names of it would just consume bandwidth without providing
356
 * information to client.
357
 *
358
 * CA names are used by servers to advertise the CAs they support to
359
 * clients.
360
 **/
361
void gnutls_certificate_free_ca_names(gnutls_certificate_credentials_t sc)
362
0
{
363
0
  _gnutls_free_datum(&sc->tlist->x509_rdn_sequence);
364
0
}
365
366
/**
367
 * gnutls_certificate_free_credentials:
368
 * @sc: is a #gnutls_certificate_credentials_t type.
369
 *
370
 * Free a gnutls_certificate_credentials_t structure.
371
 *
372
 * This function does not free any temporary parameters associated
373
 * with this structure (ie RSA and DH parameters are not freed by this
374
 * function).
375
 **/
376
void gnutls_certificate_free_credentials(gnutls_certificate_credentials_t sc)
377
0
{
378
  // Check for valid pointer and otherwise do nothing
379
0
  if (sc == NULL)
380
0
    return;
381
382
0
  gnutls_x509_trust_list_deinit(sc->tlist, 1);
383
0
  gnutls_certificate_free_keys(sc);
384
0
  memset(sc->pin_tmp, 0, sizeof(sc->pin_tmp));
385
386
0
  if (sc->deinit_dh_params) {
387
0
    gnutls_dh_params_deinit(sc->dh_params);
388
0
  }
389
390
0
  gnutls_free(sc);
391
0
}
392
393
/**
394
 * gnutls_certificate_allocate_credentials:
395
 * @res: is a pointer to a #gnutls_certificate_credentials_t type.
396
 *
397
 * Allocate a gnutls_certificate_credentials_t structure.
398
 *
399
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
400
 **/
401
int gnutls_certificate_allocate_credentials(
402
  gnutls_certificate_credentials_t *res)
403
0
{
404
0
  int ret;
405
406
0
  *res = gnutls_calloc(1, sizeof(certificate_credentials_st));
407
408
0
  if (*res == NULL)
409
0
    return GNUTLS_E_MEMORY_ERROR;
410
411
0
  ret = gnutls_x509_trust_list_init(&(*res)->tlist, 0);
412
0
  if (ret < 0) {
413
0
    gnutls_assert();
414
0
    gnutls_free(*res);
415
0
    return GNUTLS_E_MEMORY_ERROR;
416
0
  }
417
0
  (*res)->verify_depth = DEFAULT_MAX_VERIFY_DEPTH;
418
419
0
  return 0;
420
0
}
421
422
/* converts the given x509 certificate list to gnutls_pcert_st* and allocates
423
 * space for them.
424
 */
425
static gnutls_pcert_st *alloc_and_load_x509_certs(gnutls_x509_crt_t *certs,
426
              unsigned ncerts)
427
0
{
428
0
  gnutls_pcert_st *local_certs;
429
0
  int ret = 0;
430
0
  unsigned i, j;
431
432
0
  if (ncerts == 0) {
433
0
    return NULL;
434
0
  }
435
436
0
  if (unlikely(certs == NULL)) {
437
0
    gnutls_assert();
438
0
    return NULL;
439
0
  }
440
441
0
  local_certs =
442
0
    _gnutls_reallocarray(NULL, ncerts, sizeof(gnutls_pcert_st));
443
0
  if (local_certs == NULL) {
444
0
    gnutls_assert();
445
0
    return NULL;
446
0
  }
447
448
0
  for (i = 0; i < ncerts; i++) {
449
0
    ret = gnutls_pcert_import_x509(&local_certs[i], certs[i], 0);
450
0
    if (ret < 0)
451
0
      break;
452
0
  }
453
454
0
  if (ret < 0) {
455
0
    gnutls_assert();
456
0
    for (j = 0; j < i; j++) {
457
0
      gnutls_pcert_deinit(&local_certs[j]);
458
0
    }
459
0
    gnutls_free(local_certs);
460
0
    return NULL;
461
0
  }
462
463
0
  return local_certs;
464
0
}
465
466
/* converts the given x509 key to gnutls_privkey* and allocates
467
 * space for it.
468
 */
469
static gnutls_privkey_t alloc_and_load_x509_key(gnutls_x509_privkey_t key,
470
            int deinit)
471
0
{
472
0
  gnutls_privkey_t local_key;
473
0
  int ret = 0;
474
475
0
  if (key == NULL)
476
0
    return NULL;
477
478
0
  ret = gnutls_privkey_init(&local_key);
479
0
  if (ret < 0) {
480
0
    gnutls_assert();
481
0
    return NULL;
482
0
  }
483
484
0
  ret = gnutls_privkey_import_x509(
485
0
    local_key, key,
486
0
    deinit ? GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE : 0);
487
0
  if (ret < 0) {
488
0
    gnutls_assert();
489
0
    gnutls_privkey_deinit(local_key);
490
0
    return NULL;
491
0
  }
492
493
0
  return local_key;
494
0
}
495
496
#ifdef ENABLE_PKCS11
497
498
/* converts the given raw key to gnutls_privkey* and allocates
499
 * space for it.
500
 */
501
static gnutls_privkey_t alloc_and_load_pkcs11_key(gnutls_pkcs11_privkey_t key,
502
              int deinit)
503
{
504
  gnutls_privkey_t local_key;
505
  int ret = 0;
506
507
  if (key == NULL)
508
    return NULL;
509
510
  ret = gnutls_privkey_init(&local_key);
511
  if (ret < 0) {
512
    gnutls_assert();
513
    return NULL;
514
  }
515
516
  ret = gnutls_privkey_import_pkcs11(
517
    local_key, key,
518
    deinit ? GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE : 0);
519
  if (ret < 0) {
520
    gnutls_assert();
521
    gnutls_privkey_deinit(local_key);
522
    return NULL;
523
  }
524
525
  return local_key;
526
}
527
528
#endif
529
530
/**
531
 * gnutls_certificate_server_set_request:
532
 * @session: is a #gnutls_session_t type.
533
 * @req: is one of GNUTLS_CERT_REQUEST, GNUTLS_CERT_REQUIRE, GNUTLS_CERT_IGNORE
534
 *
535
 * This function specifies if we (in case of a server) are going to
536
 * send a certificate request message to the client. If @req is
537
 * GNUTLS_CERT_REQUIRE then the server will return the %GNUTLS_E_NO_CERTIFICATE_FOUND
538
 * error if the peer does not provide a certificate. If you do not call this
539
 * function then the client will not be asked to send a certificate. Invoking
540
 * the function with @req GNUTLS_CERT_IGNORE has the same effect.
541
 **/
542
void gnutls_certificate_server_set_request(gnutls_session_t session,
543
             gnutls_certificate_request_t req)
544
0
{
545
0
  session->internals.send_cert_req = req;
546
0
}
547
548
static int call_legacy_cert_cb1(gnutls_session_t session,
549
        const struct gnutls_cert_retr_st *info,
550
        gnutls_pcert_st **certs,
551
        unsigned int *pcert_length,
552
        gnutls_ocsp_data_st **ocsp,
553
        unsigned int *ocsp_length,
554
        gnutls_privkey_t *privkey, unsigned int *flags)
555
0
{
556
0
  gnutls_retr2_st st2;
557
0
  gnutls_pcert_st *local_certs = NULL;
558
0
  gnutls_privkey_t local_key = NULL;
559
0
  unsigned i;
560
0
  int ret;
561
562
0
  *ocsp_length = 0;
563
564
0
  memset(&st2, 0, sizeof(st2));
565
566
0
  ret = info->cred->legacy_cert_cb1(session, info->req_ca_rdn,
567
0
            info->nreqs, info->pk_algos,
568
0
            info->pk_algos_length, &st2);
569
0
  if (ret < 0)
570
0
    return gnutls_assert_val(ret);
571
572
0
  if (st2.ncerts == 0) {
573
0
    *pcert_length = 0;
574
0
    *ocsp_length = 0;
575
0
    *privkey = NULL;
576
0
    return 0;
577
0
  }
578
579
0
  if (st2.cert_type != GNUTLS_CRT_X509) {
580
0
    gnutls_assert();
581
0
    ret = GNUTLS_E_INVALID_REQUEST;
582
0
    goto cleanup;
583
0
  }
584
585
0
  local_certs = alloc_and_load_x509_certs(st2.cert.x509, st2.ncerts);
586
0
  if (local_certs == NULL) {
587
0
    gnutls_assert();
588
0
    ret = GNUTLS_E_MEMORY_ERROR;
589
0
    goto cleanup;
590
0
  }
591
592
0
  switch (st2.key_type) {
593
#ifdef ENABLE_PKCS11
594
  case GNUTLS_PRIVKEY_PKCS11:
595
    if (st2.key.pkcs11 != NULL) {
596
      local_key = alloc_and_load_pkcs11_key(st2.key.pkcs11,
597
                    st2.deinit_all);
598
      if (local_key == NULL) {
599
        gnutls_assert();
600
        ret = GNUTLS_E_INTERNAL_ERROR;
601
        goto cleanup;
602
      }
603
    }
604
    break;
605
#endif
606
0
  case GNUTLS_PRIVKEY_X509:
607
0
    if (st2.key.x509 != NULL) {
608
0
      local_key = alloc_and_load_x509_key(st2.key.x509,
609
0
                  st2.deinit_all);
610
0
      if (local_key == NULL) {
611
0
        gnutls_assert();
612
0
        ret = GNUTLS_E_INTERNAL_ERROR;
613
0
        goto cleanup;
614
0
      }
615
0
    }
616
0
    break;
617
0
  default:
618
0
    gnutls_assert();
619
0
    ret = GNUTLS_E_INVALID_REQUEST;
620
0
    goto cleanup;
621
0
  }
622
623
0
  *privkey = local_key;
624
0
  *certs = local_certs;
625
0
  *pcert_length = st2.ncerts;
626
627
  /* flag the caller to deinitialize our values */
628
0
  *flags |= GNUTLS_CERT_RETR_DEINIT_ALL;
629
630
0
  ret = 0;
631
632
0
cleanup:
633
634
0
  if (st2.cert_type == GNUTLS_CRT_X509) {
635
0
    if (st2.deinit_all) {
636
0
      for (i = 0; i < st2.ncerts; i++) {
637
0
        gnutls_x509_crt_deinit(st2.cert.x509[i]);
638
0
      }
639
0
      gnutls_free(st2.cert.x509);
640
0
    }
641
0
  }
642
643
0
  return ret;
644
0
}
645
646
/**
647
 * gnutls_certificate_set_retrieve_function:
648
 * @cred: is a #gnutls_certificate_credentials_t type.
649
 * @func: is the callback function
650
 *
651
 * This function sets a callback to be called in order to retrieve the
652
 * certificate to be used in the handshake. The callback will take control
653
 * only if a certificate is requested by the peer. You are advised
654
 * to use gnutls_certificate_set_retrieve_function2() because it
655
 * is much more efficient in the processing it requires from gnutls.
656
 *
657
 * The callback's function prototype is:
658
 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
659
 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_retr2_st* st);
660
 *
661
 * @req_ca_dn is only used in X.509 certificates.
662
 * Contains a list with the CA names that the server considers trusted.
663
 * This is a hint and typically the client should send a certificate that is signed
664
 * by one of these CAs. These names, when available, are DER encoded. To get a more
665
 * meaningful value use the function gnutls_x509_rdn_get().
666
 *
667
 * @pk_algos contains a list with server's acceptable public key algorithms.
668
 * The certificate returned should support the server's given algorithms.
669
 *
670
 * @st should contain the certificates and private keys.
671
 *
672
 * If the callback function is provided then gnutls will call it, in the
673
 * handshake, after the certificate request message has been received.
674
 *
675
 * In server side pk_algos and req_ca_dn are NULL.
676
 *
677
 * The callback function should set the certificate list to be sent,
678
 * and return 0 on success. If no certificate was selected then the
679
 * number of certificates should be set to zero. The value (-1)
680
 * indicates error and the handshake will be terminated. If both certificates
681
 * are set in the credentials and a callback is available, the callback
682
 * takes predence.
683
 *
684
 * Since: 3.0
685
 **/
686
void gnutls_certificate_set_retrieve_function(
687
  gnutls_certificate_credentials_t cred,
688
  gnutls_certificate_retrieve_function *func)
689
0
{
690
0
  cred->legacy_cert_cb1 = func;
691
0
  if (!func)
692
0
    cred->get_cert_callback3 = NULL;
693
0
  else
694
0
    cred->get_cert_callback3 = call_legacy_cert_cb1;
695
0
}
696
697
static int call_legacy_cert_cb2(gnutls_session_t session,
698
        const struct gnutls_cert_retr_st *info,
699
        gnutls_pcert_st **certs,
700
        unsigned int *pcert_length,
701
        gnutls_ocsp_data_st **ocsp,
702
        unsigned int *ocsp_length,
703
        gnutls_privkey_t *privkey, unsigned int *flags)
704
0
{
705
0
  int ret;
706
0
  *ocsp_length = 0;
707
  /* flags will be assumed to be zero */
708
709
0
  ret = info->cred->legacy_cert_cb2(session, info->req_ca_rdn,
710
0
            info->nreqs, info->pk_algos,
711
0
            info->pk_algos_length, certs,
712
0
            pcert_length, privkey);
713
0
  if (ret < 0) {
714
0
    gnutls_assert();
715
0
  }
716
0
  return ret;
717
0
}
718
719
/**
720
 * gnutls_certificate_set_retrieve_function2:
721
 * @cred: is a #gnutls_certificate_credentials_t type.
722
 * @func: is the callback function
723
 *
724
 * This function sets a callback to be called in order to retrieve the
725
 * certificate to be used in the handshake. The callback will take control
726
 * only if a certificate is requested by the peer.
727
 *
728
 * The callback's function prototype is:
729
 * int (*callback)(gnutls_session_t, const gnutls_datum_t* req_ca_dn, int nreqs,
730
 * const gnutls_pk_algorithm_t* pk_algos, int pk_algos_length, gnutls_pcert_st** pcert,
731
 * unsigned int *pcert_length, gnutls_privkey_t * pkey);
732
 *
733
 * @req_ca_dn is only used in X.509 certificates.
734
 * Contains a list with the CA names that the server considers trusted.
735
 * This is a hint and typically the client should send a certificate that is signed
736
 * by one of these CAs. These names, when available, are DER encoded. To get a more
737
 * meaningful value use the function gnutls_x509_rdn_get().
738
 *
739
 * @pk_algos contains a list with server's acceptable public key algorithms.
740
 * The certificate returned should support the server's given algorithms.
741
 *
742
 * @pcert should contain a single certificate and public key or a list of them.
743
 *
744
 * @pcert_length is the size of the previous list.
745
 *
746
 * @pkey is the private key.
747
 *
748
 * If the callback function is provided then gnutls will call it, in the
749
 * handshake, after the certificate request message has been received.
750
 * All the provided by the callback values will not be released or
751
 * modified by gnutls.
752
 *
753
 * In server side pk_algos and req_ca_dn are NULL.
754
 *
755
 * The callback function should set the certificate list to be sent,
756
 * and return 0 on success. If no certificate was selected then the
757
 * number of certificates should be set to zero. The value (-1)
758
 * indicates error and the handshake will be terminated. If both certificates
759
 * are set in the credentials and a callback is available, the callback
760
 * takes predence.
761
 *
762
 * Since: 3.0
763
 **/
764
void gnutls_certificate_set_retrieve_function2(
765
  gnutls_certificate_credentials_t cred,
766
  gnutls_certificate_retrieve_function2 *func)
767
0
{
768
0
  cred->legacy_cert_cb2 = func;
769
0
  if (!func)
770
0
    cred->get_cert_callback3 = NULL;
771
0
  else
772
0
    cred->get_cert_callback3 = call_legacy_cert_cb2;
773
0
}
774
775
/**
776
 * gnutls_certificate_set_retrieve_function3:
777
 * @cred: is a #gnutls_certificate_credentials_t type.
778
 * @func: is the callback function
779
 *
780
 * This function sets a callback to be called in order to retrieve the
781
 * certificate and OCSP responses to be used in the handshake. @func will
782
 * be called only if the peer requests a certificate either during handshake
783
 * or during post-handshake authentication.
784
 *
785
 * The callback's function prototype is defined in `abstract.h':
786
 *
787
 * int gnutls_certificate_retrieve_function3(
788
 * gnutls_session_t,
789
 * const struct gnutls_cert_retr_st *info,
790
 * gnutls_pcert_st **certs,
791
 * unsigned int *certs_length,
792
 * gnutls_ocsp_data_st **ocsp,
793
 * unsigned int *ocsp_length,
794
 * gnutls_privkey_t *privkey,
795
 * unsigned int *flags);
796
 *
797
 * The info field of the callback contains:
798
 * @req_ca_dn which is a list with the CA names that the server considers trusted.
799
 * This is a hint and typically the client should send a certificate that is signed
800
 * by one of these CAs. These names, when available, are DER encoded. To get a more
801
 * meaningful value use the function gnutls_x509_rdn_get().
802
 * @pk_algos contains a list with server's acceptable public key algorithms.
803
 * The certificate returned should support the server's given algorithms.
804
 *
805
 * The callback should fill-in the following values:
806
 *
807
 * @certs should contain an allocated list of certificates and public keys.
808
 * @certs_length is the size of the previous list.
809
 * @ocsp should contain an allocated list of OCSP responses.
810
 * @ocsp_length is the size of the previous list.
811
 * @privkey is the private key.
812
 *
813
 * If flags in the callback are set to %GNUTLS_CERT_RETR_DEINIT_ALL then
814
 * all provided values must be allocated using gnutls_malloc(), and will
815
 * be released by gnutls; otherwise they will not be touched by gnutls.
816
 *
817
 * The callback function should set the certificate and OCSP response
818
 * list to be sent, and return 0 on success. If no certificates are available,
819
 * the @certs_length and @ocsp_length should be set to zero. The return
820
 * value (-1) indicates error and the handshake will be terminated. If both
821
 * certificates are set in the credentials and a callback is available, the
822
 * callback takes predence.
823
 *
824
 * Raw public-keys:
825
 * In case raw public-keys are negotiated as certificate type, certificates
826
 * that would normally hold the public-key material are not available. In that case,
827
 * @certs contains an allocated list with only the public key. Since there is no
828
 * certificate, there is also no certificate status. Therefore, OCSP information
829
 * should not be set.
830
 *
831
 * Since: 3.6.3
832
 **/
833
void gnutls_certificate_set_retrieve_function3(
834
  gnutls_certificate_credentials_t cred,
835
  gnutls_certificate_retrieve_function3 *func)
836
0
{
837
0
  cred->get_cert_callback3 = func;
838
0
}
839
840
/**
841
 * gnutls_certificate_set_verify_function:
842
 * @cred: is a #gnutls_certificate_credentials_t type.
843
 * @func: is the callback function
844
 *
845
 * This function sets a callback to be called when peer's certificate
846
 * has been received in order to verify it on receipt rather than
847
 * doing after the handshake is completed.
848
 *
849
 * The callback's function prototype is:
850
 * int (*callback)(gnutls_session_t);
851
 *
852
 * If the callback function is provided then gnutls will call it, in the
853
 * handshake, just after the certificate message has been received.
854
 * To verify or obtain the certificate the gnutls_certificate_verify_peers2(),
855
 * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions
856
 * can be used.
857
 *
858
 * The callback function should return 0 for the handshake to continue
859
 * or non-zero to terminate.
860
 *
861
 * Since: 2.10.0
862
 **/
863
void gnutls_certificate_set_verify_function(
864
  gnutls_certificate_credentials_t cred,
865
  gnutls_certificate_verify_function *func)
866
0
{
867
0
  cred->verify_callback = func;
868
0
}
869
870
/**
871
 * gnutls_x509_trust_list_set_getissuer_function:
872
 * @tlist: is a #gnutls_x509_trust_list_t type.
873
 * @func: is the callback function
874
 *
875
 * This function sets a callback to be called when the peer's certificate
876
 * chain is incomplete due a missing intermediate certificate. The callback
877
 * may provide the missing certificate for use during verification.
878
 *
879
 * The callback's function prototype is defined in gnutls/x509.h as:
880
 *
881
 *   int (*callback)(gnutls_x509_trust_list_t list,
882
 *                   const gnutls_x509_crt_t cert,
883
 *                   gnutls_x509_crt_t **issuers,
884
 *                   unsigned int *issuers_size);
885
 *
886
 * If the callback function is provided then gnutls will call it during the
887
 * certificate verification procedure. The callback may wish to use
888
 * gnutls_x509_crt_get_authority_info_access() to get a URI from which
889
 * to attempt to download the missing issuer certificate, if available.
890
 *
891
 * On a successful call, the callback shall set '*issuers' and '*issuers_size'
892
 * even if the result is empty; in that case '*issuers' will point to %NULL and
893
 * '*issuers_size' will be 0.  Otherwise, the '*issuers' array shall be
894
 * allocated using gnutls_x509_crt_list_import2(). The ownership of both the
895
 * array and the elements is transferred to the caller and thus the application
896
 * does not need to maintain the memory after the call.
897
 *
898
 * The callback function should return 0 if the attempt to retrieve the issuer
899
 * certificates for 'crt' succeeded, or non-zero to indicate any error occurred
900
 * during the attempt. In the latter case, '*issuers' and '*issuers_size' are
901
 * not set.
902
 *
903
 * Since: 3.7.0
904
 **/
905
void gnutls_x509_trust_list_set_getissuer_function(
906
  gnutls_x509_trust_list_t tlist,
907
  gnutls_x509_trust_list_getissuer_function *func)
908
0
{
909
0
  tlist->issuer_callback = func;
910
0
}
911
912
/**
913
 * gnutls_x509_trust_list_set_ptr:
914
 * @tlist: is a #gnutls_x509_trust_list_t type.
915
 * @ptr: is the user pointer
916
 *
917
 * This function will set (associate) the user given pointer @ptr to
918
 * the tlist structure. This pointer can be accessed with
919
 * gnutls_x509_trust_list_get_ptr(). Useful in the callback function
920
 * gnutls_x509_trust_list_set_getissuer_function.
921
 *
922
 * Since: 3.7.0
923
 **/
924
void gnutls_x509_trust_list_set_ptr(gnutls_x509_trust_list_t tlist, void *ptr)
925
0
{
926
0
  tlist->usr_ptr = ptr;
927
0
}
928
929
/**
930
 * gnutls_x509_trust_list_get_ptr:
931
 * @tlist: is a #gnutls_x509_trust_list_t type.
932
 *
933
 * Get user pointer for tlist. Useful in callback function
934
 * gnutls_x509_trust_list_set_getissuer_function.
935
 * This is the pointer set with gnutls_x509_trust_list_set_ptr().
936
 *
937
 * Returns: the user given pointer from the tlist structure, or
938
 *   %NULL if it was never set.
939
 *
940
 * Since: 3.7.0
941
 **/
942
void *gnutls_x509_trust_list_get_ptr(gnutls_x509_trust_list_t tlist)
943
0
{
944
0
  return tlist->usr_ptr;
945
0
}
946
947
/**
948
 * gnutls_session_set_verify_output_function:
949
 * @session: is a #gnutls_x509_trust_list_t type.
950
 * @func: is the callback function
951
 *
952
 * This function sets a callback to be called when the peer's certificate
953
 * chain has to be verified and full path to the trusted root has to be
954
 * printed.
955
 *
956
 * The callback's function prototype is defined in `x509.h':
957
 * int (*callback)(
958
 * gnutls_x509_crt_t cert,
959
 * gnutls_x509_crt_t issuer,
960
 * gnutls_x509_crl_t crl,
961
 * unsigned int verification_output);
962
 *
963
 * If the callback function is provided then gnutls will call it, in the
964
 * certificate verification procedure.
965
 * To verify the certificate chain and print its path uptp the trusted root,
966
 * functions such as gnutls_certificate_verify_peers(),
967
 * gnutls_x509_trust_list_verify_crt(), and gnutls_x509_trust_list_verify_crt2()
968
 * can be used. The callback is set in _gnutls_verify_crt_status() and
969
 * _gnutls_pkcs11_verify_crt_status().
970
 *
971
 * Since: 3.7.0
972
 **/
973
void gnutls_session_set_verify_output_function(
974
  gnutls_session_t session, gnutls_verify_output_function *func)
975
0
{
976
0
  session->internals.cert_output_callback = func;
977
0
}
978
979
0
#define TEST_TEXT "test text"
980
/* returns error if the certificate has different algorithm than
981
 * the given key parameters.
982
 */
983
int _gnutls_check_key_cert_match(gnutls_certificate_credentials_t res)
984
0
{
985
0
  gnutls_datum_t test = { (void *)TEST_TEXT, sizeof(TEST_TEXT) - 1 };
986
0
  gnutls_datum_t sig = { NULL, 0 };
987
0
  gnutls_digest_algorithm_t dig;
988
0
  int pk, pk2, ret;
989
0
  unsigned sign_algo;
990
991
0
  if (res->flags & GNUTLS_CERTIFICATE_SKIP_KEY_CERT_MATCH)
992
0
    return 0;
993
994
0
  pk = gnutls_pubkey_get_pk_algorithm(
995
0
    res->certs[res->ncerts - 1].cert_list[0].pubkey, NULL);
996
0
  pk2 = gnutls_privkey_get_pk_algorithm(res->certs[res->ncerts - 1].pkey,
997
0
                NULL);
998
999
0
  if (GNUTLS_PK_IS_RSA(pk) && GNUTLS_PK_IS_RSA(pk2)) {
1000
0
    if (pk2 == GNUTLS_PK_RSA_PSS && pk == GNUTLS_PK_RSA) {
1001
0
      _gnutls_debug_log(
1002
0
        "you cannot mix an RSA-PSS key with an RSA certificate\n");
1003
0
      return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
1004
0
    }
1005
1006
0
    if (pk2 == GNUTLS_PK_RSA_PSS || pk == GNUTLS_PK_RSA_PSS)
1007
0
      pk = GNUTLS_PK_RSA_PSS;
1008
0
  } else if (pk2 != pk) {
1009
0
    gnutls_assert();
1010
0
    _gnutls_debug_log("key is %s, certificate is %s\n",
1011
0
          gnutls_pk_get_name(pk2),
1012
0
          gnutls_pk_get_name(pk));
1013
0
    return GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
1014
0
  }
1015
1016
0
  if (pk == GNUTLS_PK_GOST_01)
1017
0
    dig = GNUTLS_DIG_GOSTR_94;
1018
0
  else if (pk == GNUTLS_PK_GOST_12_256)
1019
0
    dig = GNUTLS_DIG_STREEBOG_256;
1020
0
  else if (pk == GNUTLS_PK_GOST_12_512)
1021
0
    dig = GNUTLS_DIG_STREEBOG_512;
1022
0
  else
1023
0
    dig = GNUTLS_DIG_SHA256;
1024
1025
0
  sign_algo = gnutls_pk_to_sign(pk, dig);
1026
1027
  /* now check if keys really match. We use the sign/verify approach
1028
   * because we cannot always obtain the parameters from the abstract
1029
   * keys (e.g. PKCS #11). */
1030
0
  ret = gnutls_privkey_sign_data2(res->certs[res->ncerts - 1].pkey,
1031
0
          sign_algo, 0, &test, &sig);
1032
0
  if (ret < 0) {
1033
    /* for some reason we couldn't sign that. That shouldn't have
1034
     * happened, but since it did, report the issue and do not
1035
     * try the key matching test */
1036
0
    _gnutls_debug_log("%s: failed signing\n", __func__);
1037
0
    goto finish;
1038
0
  }
1039
1040
0
  ret = gnutls_pubkey_verify_data2(
1041
0
    res->certs[res->ncerts - 1].cert_list[0].pubkey, sign_algo,
1042
0
    GNUTLS_VERIFY_ALLOW_BROKEN, &test, &sig);
1043
1044
0
  gnutls_free(sig.data);
1045
1046
0
  if (ret < 0)
1047
0
    return gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
1048
1049
0
finish:
1050
0
  return 0;
1051
0
}
1052
1053
/**
1054
 * gnutls_certificate_verification_status_print:
1055
 * @status: The status flags to be printed
1056
 * @type: The certificate type
1057
 * @out: Newly allocated datum with (0) terminated string.
1058
 * @flags: should be zero
1059
 *
1060
 * This function will pretty print the status of a verification
1061
 * process -- eg. the one obtained by gnutls_certificate_verify_peers3().
1062
 *
1063
 * The output @out needs to be deallocated using gnutls_free().
1064
 *
1065
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1066
 *   negative error value.
1067
 *
1068
 * Since: 3.1.4
1069
 **/
1070
int gnutls_certificate_verification_status_print(unsigned int status,
1071
             gnutls_certificate_type_t type,
1072
             gnutls_datum_t *out,
1073
             unsigned int flags)
1074
0
{
1075
0
  gnutls_buffer_st str;
1076
1077
0
  _gnutls_buffer_init(&str);
1078
1079
0
  if (status == 0)
1080
0
    _gnutls_buffer_append_str(&str,
1081
0
            _("The certificate is trusted. "));
1082
0
  else
1083
0
    _gnutls_buffer_append_str(
1084
0
      &str, _("The certificate is NOT trusted. "));
1085
1086
0
  if (type == GNUTLS_CRT_X509) {
1087
0
    if (status & GNUTLS_CERT_REVOKED)
1088
0
      _gnutls_buffer_append_str(
1089
0
        &str, _("The certificate chain is revoked. "));
1090
1091
0
    if (status & GNUTLS_CERT_MISMATCH)
1092
0
      _gnutls_buffer_append_str(
1093
0
        &str,
1094
0
        _("The certificate doesn't match the local copy (TOFU). "));
1095
1096
0
    if (status & GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED)
1097
0
      _gnutls_buffer_append_str(
1098
0
        &str,
1099
0
        _("The revocation or OCSP data are old and have been superseded. "));
1100
1101
0
    if (status & GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE)
1102
0
      _gnutls_buffer_append_str(
1103
0
        &str,
1104
0
        _("The revocation or OCSP data are issued with a future date. "));
1105
1106
0
    if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
1107
0
      _gnutls_buffer_append_str(
1108
0
        &str, _("The certificate issuer is unknown. "));
1109
1110
0
    if (status & GNUTLS_CERT_SIGNER_NOT_CA)
1111
0
      _gnutls_buffer_append_str(
1112
0
        &str,
1113
0
        _("The certificate issuer is not a CA. "));
1114
0
  }
1115
1116
0
  if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
1117
0
    _gnutls_buffer_append_str(
1118
0
      &str,
1119
0
      _("The certificate chain uses insecure algorithm. "));
1120
1121
0
  if (status & GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE)
1122
0
    _gnutls_buffer_append_str(
1123
0
      &str,
1124
0
      _("The certificate chain violates the signer's constraints. "));
1125
1126
0
  if (status & GNUTLS_CERT_PURPOSE_MISMATCH)
1127
0
    _gnutls_buffer_append_str(
1128
0
      &str,
1129
0
      _("The certificate chain does not match the intended purpose. "));
1130
1131
0
  if (status & GNUTLS_CERT_NOT_ACTIVATED)
1132
0
    _gnutls_buffer_append_str(
1133
0
      &str,
1134
0
      _("The certificate chain uses not yet valid certificate. "));
1135
1136
0
  if (status & GNUTLS_CERT_EXPIRED)
1137
0
    _gnutls_buffer_append_str(
1138
0
      &str,
1139
0
      _("The certificate chain uses expired certificate. "));
1140
1141
0
  if (status & GNUTLS_CERT_SIGNATURE_FAILURE)
1142
0
    _gnutls_buffer_append_str(
1143
0
      &str,
1144
0
      _("The signature in the certificate is invalid. "));
1145
1146
0
  if (status & GNUTLS_CERT_UNEXPECTED_OWNER)
1147
0
    _gnutls_buffer_append_str(
1148
0
      &str,
1149
0
      _("The name in the certificate does not match the expected. "));
1150
1151
0
  if (status & GNUTLS_CERT_MISSING_OCSP_STATUS)
1152
0
    _gnutls_buffer_append_str(
1153
0
      &str,
1154
0
      _("The certificate requires the server to include an OCSP status in its response, but the OCSP status is missing. "));
1155
1156
0
  if (status & GNUTLS_CERT_INVALID_OCSP_STATUS)
1157
0
    _gnutls_buffer_append_str(
1158
0
      &str,
1159
0
      _("The received OCSP status response is invalid. "));
1160
1161
0
  if (status & GNUTLS_CERT_UNKNOWN_CRIT_EXTENSIONS)
1162
0
    _gnutls_buffer_append_str(
1163
0
      &str,
1164
0
      _("The certificate contains an unknown critical extension. "));
1165
1166
0
  return _gnutls_buffer_to_datum(&str, out, 1);
1167
0
}
1168
1169
#if defined(ENABLE_DHE) || defined(ENABLE_ANON)
1170
/**
1171
 * gnutls_certificate_set_dh_params:
1172
 * @res: is a gnutls_certificate_credentials_t type
1173
 * @dh_params: the Diffie-Hellman parameters.
1174
 *
1175
 * This function will set the Diffie-Hellman parameters for a
1176
 * certificate server to use. These parameters will be used in
1177
 * Ephemeral Diffie-Hellman cipher suites.  Note that only a pointer
1178
 * to the parameters are stored in the certificate handle, so you
1179
 * must not deallocate the parameters before the certificate is deallocated.
1180
 *
1181
 * Deprecated: This function is unnecessary and discouraged on GnuTLS 3.6.0
1182
 * or later. Since 3.6.0, DH parameters are negotiated
1183
 * following RFC7919.
1184
 *
1185
 **/
1186
void gnutls_certificate_set_dh_params(gnutls_certificate_credentials_t res,
1187
              gnutls_dh_params_t dh_params)
1188
0
{
1189
0
  if (res->deinit_dh_params) {
1190
0
    res->deinit_dh_params = 0;
1191
0
    gnutls_dh_params_deinit(res->dh_params);
1192
0
    res->dh_params = NULL;
1193
0
  }
1194
1195
0
  res->dh_params = dh_params;
1196
0
  res->dh_sec_param = gnutls_pk_bits_to_sec_param(
1197
0
    GNUTLS_PK_DH, _gnutls_mpi_get_nbits(dh_params->params[0]));
1198
0
}
1199
1200
/**
1201
 * gnutls_certificate_set_known_dh_params:
1202
 * @res: is a gnutls_certificate_credentials_t type
1203
 * @sec_param: is an option of the %gnutls_sec_param_t enumeration
1204
 *
1205
 * This function will set the Diffie-Hellman parameters for a
1206
 * certificate server to use. These parameters will be used in
1207
 * Ephemeral Diffie-Hellman cipher suites and will be selected from
1208
 * the FFDHE set of RFC7919 according to the security level provided.
1209
 *
1210
 * Deprecated: This function is unnecessary and discouraged on GnuTLS 3.6.0
1211
 * or later. Since 3.6.0, DH parameters are negotiated
1212
 * following RFC7919.
1213
 *
1214
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1215
 *   negative error value.
1216
 *
1217
 * Since: 3.5.6
1218
 **/
1219
int gnutls_certificate_set_known_dh_params(gnutls_certificate_credentials_t res,
1220
             gnutls_sec_param_t sec_param)
1221
0
{
1222
0
  res->dh_sec_param = sec_param;
1223
1224
0
  return 0;
1225
0
}
1226
1227
#endif /* DH */