Coverage Report

Created: 2024-07-23 07:36

/src/gnutls/lib/privkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * GnuTLS PKCS#11 support
3
 * Copyright (C) 2010-2014 Free Software Foundation, Inc.
4
 * Copyright (C) 2012-2015 Nikos Mavrogiannopoulos
5
 * Copyright (C) 2016-2017 Red Hat, Inc.
6
 * 
7
 * Author: Nikos Mavrogiannopoulos
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
#include "gnutls_int.h"
24
#include <gnutls/pkcs11.h>
25
#include <stdio.h>
26
#include <string.h>
27
#include "errors.h"
28
#include "datum.h"
29
#include "pkcs11_int.h"
30
#include <gnutls/abstract.h>
31
#include "pk.h"
32
#include "x509_int.h"
33
#include "tls-sig.h"
34
#include "algorithms.h"
35
#include "fips.h"
36
#include "system-keys.h"
37
#include "urls.h"
38
#include "tpm2.h"
39
#include "pkcs11_int.h"
40
#include "abstract_int.h"
41
42
static int privkey_sign_prehashed(gnutls_privkey_t signer,
43
          const gnutls_sign_entry_st *se,
44
          const gnutls_datum_t *hash_data,
45
          gnutls_datum_t *signature,
46
          gnutls_x509_spki_st *params);
47
48
/**
49
 * gnutls_privkey_get_type:
50
 * @key: should contain a #gnutls_privkey_t type
51
 *
52
 * This function will return the type of the private key. This is
53
 * actually the type of the subsystem used to set this private key.
54
 *
55
 * Returns: a member of the #gnutls_privkey_type_t enumeration on
56
 *   success, or a negative error code on error.
57
 *
58
 * Since: 2.12.0
59
 **/
60
gnutls_privkey_type_t gnutls_privkey_get_type(gnutls_privkey_t key)
61
0
{
62
0
  return key->type;
63
0
}
64
65
/**
66
 * gnutls_privkey_get_seed:
67
 * @key: should contain a #gnutls_privkey_t type
68
 * @digest: if non-NULL it will contain the digest algorithm used for key generation (if applicable)
69
 * @seed: where seed will be copied to
70
 * @seed_size: originally holds the size of @seed, will be updated with actual size
71
 *
72
 * This function will return the seed that was used to generate the
73
 * given private key. That function will succeed only if the key was generated
74
 * as a provable key.
75
 *
76
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
77
 *   negative error value.
78
 *
79
 * Since: 3.5.0
80
 **/
81
int gnutls_privkey_get_seed(gnutls_privkey_t key,
82
          gnutls_digest_algorithm_t *digest, void *seed,
83
          size_t *seed_size)
84
0
{
85
0
  if (key->type != GNUTLS_PRIVKEY_X509)
86
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
87
0
  return gnutls_x509_privkey_get_seed(key->key.x509, digest, seed,
88
0
              seed_size);
89
0
}
90
91
/**
92
 * gnutls_privkey_verify_seed:
93
 * @key: should contain a #gnutls_privkey_t type
94
 * @digest: it contains the digest algorithm used for key generation (if applicable)
95
 * @seed: the seed of the key to be checked with
96
 * @seed_size: holds the size of @seed
97
 *
98
 * This function will verify that the given private key was generated from
99
 * the provided seed.
100
 *
101
 * Returns: In case of a verification failure %GNUTLS_E_PRIVKEY_VERIFICATION_ERROR
102
 * is returned, and zero or positive code on success.
103
 *
104
 * Since: 3.5.0
105
 **/
106
int gnutls_privkey_verify_seed(gnutls_privkey_t key,
107
             gnutls_digest_algorithm_t digest,
108
             const void *seed, size_t seed_size)
109
0
{
110
0
  if (key->type != GNUTLS_PRIVKEY_X509)
111
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
112
0
  return gnutls_x509_privkey_verify_seed(key->key.x509, digest, seed,
113
0
                 seed_size);
114
0
}
115
116
/**
117
 * gnutls_privkey_get_pk_algorithm:
118
 * @key: should contain a #gnutls_privkey_t type
119
 * @bits: If set will return the number of bits of the parameters (may be NULL)
120
 *
121
 * This function will return the public key algorithm of a private
122
 * key and if possible will return a number of bits that indicates
123
 * the security parameter of the key.
124
 *
125
 * Returns: a member of the #gnutls_pk_algorithm_t enumeration on
126
 *   success, or a negative error code on error.
127
 *
128
 * Since: 2.12.0
129
 **/
130
int gnutls_privkey_get_pk_algorithm(gnutls_privkey_t key, unsigned int *bits)
131
0
{
132
0
  switch (key->type) {
133
#ifdef ENABLE_PKCS11
134
  case GNUTLS_PRIVKEY_PKCS11:
135
    return gnutls_pkcs11_privkey_get_pk_algorithm(key->key.pkcs11,
136
                    bits);
137
#endif
138
0
  case GNUTLS_PRIVKEY_X509:
139
0
    if (bits) {
140
0
      *bits = pubkey_to_bits(&key->key.x509->params);
141
0
    }
142
143
0
    return gnutls_x509_privkey_get_pk_algorithm(key->key.x509);
144
0
  case GNUTLS_PRIVKEY_EXT:
145
0
    if (bits)
146
0
      *bits = key->key.ext.bits;
147
148
0
    return key->pk_algorithm;
149
0
  default:
150
0
    gnutls_assert();
151
0
    return GNUTLS_E_INVALID_REQUEST;
152
0
  }
153
0
}
154
155
static int privkey_to_pubkey(gnutls_pk_algorithm_t pk,
156
           const gnutls_pk_params_st *priv,
157
           gnutls_pk_params_st *pub)
158
0
{
159
0
  int ret;
160
161
0
  pub->algo = priv->algo;
162
0
  pub->pkflags = priv->pkflags;
163
0
  pub->curve = priv->curve;
164
0
  pub->gost_params = priv->gost_params;
165
0
  pub->qbits = priv->qbits;
166
0
  ret = _gnutls_x509_spki_copy(&pub->spki, &priv->spki);
167
0
  if (ret < 0) {
168
0
    gnutls_assert();
169
0
    goto cleanup;
170
0
  }
171
172
0
  switch (pk) {
173
0
  case GNUTLS_PK_RSA_PSS:
174
0
  case GNUTLS_PK_RSA_OAEP:
175
0
  case GNUTLS_PK_RSA:
176
0
    pub->params[0] = _gnutls_mpi_copy(priv->params[0]);
177
0
    pub->params[1] = _gnutls_mpi_copy(priv->params[1]);
178
179
0
    pub->params_nr = RSA_PUBLIC_PARAMS;
180
181
0
    if (pub->params[0] == NULL || pub->params[1] == NULL) {
182
0
      gnutls_assert();
183
0
      ret = GNUTLS_E_MEMORY_ERROR;
184
0
      goto cleanup;
185
0
    }
186
187
0
    break;
188
0
  case GNUTLS_PK_DSA:
189
0
    pub->params[DSA_P] = _gnutls_mpi_copy(priv->params[DSA_P]);
190
0
    pub->params[DSA_Q] = _gnutls_mpi_copy(priv->params[DSA_Q]);
191
0
    pub->params[DSA_G] = _gnutls_mpi_copy(priv->params[DSA_G]);
192
0
    pub->params[DSA_Y] = _gnutls_mpi_copy(priv->params[DSA_Y]);
193
194
0
    pub->params_nr = DSA_PUBLIC_PARAMS;
195
196
0
    if (pub->params[DSA_P] == NULL || pub->params[DSA_Q] == NULL ||
197
0
        pub->params[DSA_G] == NULL || pub->params[DSA_Y] == NULL) {
198
0
      gnutls_assert();
199
0
      ret = GNUTLS_E_MEMORY_ERROR;
200
0
      goto cleanup;
201
0
    }
202
203
0
    break;
204
0
  case GNUTLS_PK_DH:
205
0
    pub->params[DH_P] = _gnutls_mpi_copy(priv->params[DH_P]);
206
0
    pub->params[DH_G] = _gnutls_mpi_copy(priv->params[DH_G]);
207
0
    pub->params[DH_Y] = _gnutls_mpi_copy(priv->params[DH_Y]);
208
209
0
    if (pub->params[DH_P] == NULL || pub->params[DH_G] == NULL ||
210
0
        pub->params[DH_Y] == NULL) {
211
0
      gnutls_assert();
212
0
      ret = GNUTLS_E_MEMORY_ERROR;
213
0
      goto cleanup;
214
0
    }
215
216
0
    if (priv->params[DH_Q]) {
217
0
      pub->params[DH_Q] =
218
0
        _gnutls_mpi_copy(priv->params[DH_Q]);
219
0
      if (pub->params[DH_Q] == NULL) {
220
0
        gnutls_assert();
221
0
        ret = GNUTLS_E_MEMORY_ERROR;
222
0
        goto cleanup;
223
0
      }
224
0
    }
225
226
0
    pub->params_nr = DH_PUBLIC_PARAMS;
227
228
0
    break;
229
0
  case GNUTLS_PK_ECDSA:
230
0
    pub->params[ECC_X] = _gnutls_mpi_copy(priv->params[ECC_X]);
231
0
    pub->params[ECC_Y] = _gnutls_mpi_copy(priv->params[ECC_Y]);
232
233
0
    pub->params_nr = ECC_PUBLIC_PARAMS;
234
235
0
    if (pub->params[ECC_X] == NULL || pub->params[ECC_Y] == NULL) {
236
0
      gnutls_assert();
237
0
      ret = GNUTLS_E_MEMORY_ERROR;
238
0
      goto cleanup;
239
0
    }
240
241
0
    break;
242
0
  case GNUTLS_PK_EDDSA_ED25519:
243
0
  case GNUTLS_PK_EDDSA_ED448:
244
0
  case GNUTLS_PK_ECDH_X25519:
245
0
  case GNUTLS_PK_ECDH_X448:
246
0
    ret = _gnutls_set_datum(&pub->raw_pub, priv->raw_pub.data,
247
0
          priv->raw_pub.size);
248
0
    if (ret < 0)
249
0
      return gnutls_assert_val(ret);
250
251
0
    break;
252
0
  case GNUTLS_PK_GOST_01:
253
0
  case GNUTLS_PK_GOST_12_256:
254
0
  case GNUTLS_PK_GOST_12_512:
255
0
    pub->params[GOST_X] = _gnutls_mpi_copy(priv->params[GOST_X]);
256
0
    pub->params[GOST_Y] = _gnutls_mpi_copy(priv->params[GOST_Y]);
257
258
0
    pub->params_nr = GOST_PUBLIC_PARAMS;
259
260
0
    if (pub->params[GOST_X] == NULL ||
261
0
        pub->params[GOST_Y] == NULL) {
262
0
      gnutls_assert();
263
0
      ret = GNUTLS_E_MEMORY_ERROR;
264
0
      goto cleanup;
265
0
    }
266
267
0
    break;
268
0
  default:
269
0
    gnutls_assert();
270
0
    return GNUTLS_E_INVALID_REQUEST;
271
0
  }
272
273
0
  return 0;
274
0
cleanup:
275
0
  gnutls_pk_params_release(pub);
276
0
  return ret;
277
0
}
278
279
/* Returns the public key of the private key (if possible)
280
 */
281
int _gnutls_privkey_get_mpis(gnutls_privkey_t key, gnutls_pk_params_st *params)
282
0
{
283
0
  int ret;
284
285
0
  switch (key->type) {
286
0
  case GNUTLS_PRIVKEY_X509:
287
0
    ret = _gnutls_pk_params_copy(params, &key->key.x509->params);
288
0
    break;
289
#ifdef ENABLE_PKCS11
290
  case GNUTLS_PRIVKEY_PKCS11: {
291
    gnutls_pubkey_t pubkey;
292
293
    ret = _pkcs11_privkey_get_pubkey(key->key.pkcs11, &pubkey, 0);
294
    if (ret < 0)
295
      return gnutls_assert_val(ret);
296
297
    ret = _gnutls_pubkey_get_mpis(pubkey, params);
298
    gnutls_pubkey_deinit(pubkey);
299
300
    break;
301
  }
302
#endif
303
0
  default:
304
0
    if (key->key.ext.pk_params_func) {
305
0
      ret = key->key.ext.pk_params_func(
306
0
        key, key->key.ext.userdata, params);
307
0
      if (ret < 0)
308
0
        return gnutls_assert_val(ret);
309
0
      return ret;
310
0
    }
311
0
    gnutls_assert();
312
0
    return GNUTLS_E_INVALID_REQUEST;
313
0
  }
314
315
0
  return ret;
316
0
}
317
318
int _gnutls_privkey_get_public_mpis(gnutls_privkey_t key,
319
            gnutls_pk_params_st *params)
320
0
{
321
0
  int ret;
322
0
  gnutls_pk_params_st tmp1;
323
324
0
  gnutls_pk_params_init(&tmp1);
325
326
0
  ret = _gnutls_privkey_get_mpis(key, &tmp1);
327
0
  if (ret < 0)
328
0
    return gnutls_assert_val(ret);
329
330
0
  ret = privkey_to_pubkey(key->pk_algorithm, &tmp1, params);
331
332
0
  gnutls_pk_params_release(&tmp1);
333
334
0
  if (ret < 0)
335
0
    gnutls_assert();
336
337
0
  return ret;
338
0
}
339
340
/* This function retrieves default sign parameters from KEY. */
341
int _gnutls_privkey_get_spki_params(gnutls_privkey_t key,
342
            gnutls_x509_spki_st *params)
343
0
{
344
0
  switch (key->type) {
345
#ifdef ENABLE_PKCS11
346
  case GNUTLS_PRIVKEY_PKCS11:
347
    break;
348
#endif
349
0
  case GNUTLS_PRIVKEY_EXT:
350
0
    break;
351
0
  case GNUTLS_PRIVKEY_X509:
352
0
    return _gnutls_x509_privkey_get_spki_params(key->key.x509,
353
0
                  params);
354
0
  default:
355
0
    gnutls_assert();
356
0
    return GNUTLS_E_INVALID_REQUEST;
357
0
  }
358
359
0
  memset(params, 0, sizeof(gnutls_x509_spki_st));
360
361
0
  return 0;
362
0
}
363
364
/* This function fills in PARAMS with the necessary parameters to sign
365
 * with PK and DIG. PARAMS must be initialized with
366
 * _gnutls_privkey_get_spki_params in advance.
367
 *
368
 * After calling this function the params structure will
369
 * be initialized even if the original SubjectPublicKeyInfo was empty.
370
 */
371
int _gnutls_privkey_update_spki_params(gnutls_privkey_t key,
372
               gnutls_pk_algorithm_t pk,
373
               gnutls_digest_algorithm_t dig,
374
               unsigned flags,
375
               gnutls_x509_spki_st *params)
376
0
{
377
0
  unsigned salt_size = 0;
378
0
  unsigned bits = 0;
379
0
  gnutls_pk_algorithm_t key_pk;
380
381
0
  if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_RSA_PSS) {
382
0
    if (!GNUTLS_PK_IS_RSA(pk))
383
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
384
0
    pk = GNUTLS_PK_RSA_PSS;
385
0
  }
386
387
0
  key_pk = gnutls_privkey_get_pk_algorithm(key, &bits);
388
0
  if ((key_pk != pk) &&
389
0
      !(key_pk == GNUTLS_PK_RSA && pk == GNUTLS_PK_RSA_PSS)) {
390
0
    gnutls_assert();
391
0
    return GNUTLS_E_CONSTRAINT_ERROR;
392
0
  }
393
394
0
  if (pk == GNUTLS_PK_RSA_PSS) {
395
0
    const mac_entry_st *me;
396
0
    int ret;
397
398
0
    me = hash_to_entry(dig);
399
0
    if (unlikely(me == NULL))
400
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
401
402
0
    if (params->pk == GNUTLS_PK_RSA)
403
0
      salt_size = 0;
404
0
    else if (params->pk == GNUTLS_PK_RSA_PSS) {
405
0
      if (params->rsa_pss_dig != GNUTLS_DIG_UNKNOWN &&
406
0
          dig != params->rsa_pss_dig) {
407
0
        return gnutls_assert_val(
408
0
          GNUTLS_E_CONSTRAINT_ERROR);
409
0
      }
410
411
0
      salt_size = params->salt_size;
412
0
    }
413
414
0
    if (flags & GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE)
415
0
      params->salt_size = 0;
416
0
    else {
417
0
      ret = _gnutls_find_rsa_pss_salt_size(bits, me,
418
0
                   salt_size);
419
0
      if (ret < 0)
420
0
        return gnutls_assert_val(ret);
421
0
      if (flags & GNUTLS_PRIVKEY_FLAG_RSA_PSS_FIXED_SALT_LENGTH &&
422
0
          (size_t)ret != _gnutls_hash_get_algo_len(me)) {
423
0
        return gnutls_assert_val(
424
0
          GNUTLS_E_CONSTRAINT_ERROR);
425
0
      }
426
0
      params->salt_size = ret;
427
0
    }
428
0
    params->rsa_pss_dig = dig;
429
0
  }
430
431
0
  params->pk = pk;
432
433
0
  return 0;
434
0
}
435
436
/**
437
 * gnutls_privkey_init:
438
 * @key: A pointer to the type to be initialized
439
 *
440
 * This function will initialize a private key object. The object can
441
 * be used to generate, import, and perform cryptographic operations
442
 * on the associated private key.
443
 *
444
 * Note that when the underlying private key is a PKCS#11 key (i.e.,
445
 * when imported with a PKCS#11 URI), the limitations of gnutls_pkcs11_privkey_init()
446
 * apply to this object as well. In versions of GnuTLS later than 3.5.11 the object
447
 * is protected using locks and a single %gnutls_privkey_t can be re-used
448
 * by many threads. However, for performance it is recommended to utilize
449
 * one object per key per thread.
450
 *
451
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
452
 *   negative error value.
453
 *
454
 * Since: 2.12.0
455
 **/
456
int gnutls_privkey_init(gnutls_privkey_t *key)
457
0
{
458
0
  *key = NULL;
459
0
  FAIL_IF_LIB_ERROR;
460
461
0
  *key = gnutls_calloc(1, sizeof(struct gnutls_privkey_st));
462
0
  if (*key == NULL) {
463
0
    gnutls_assert();
464
0
    return GNUTLS_E_MEMORY_ERROR;
465
0
  }
466
467
0
  return 0;
468
0
}
469
470
/**
471
 * gnutls_privkey_deinit:
472
 * @key: The key to be deinitialized
473
 *
474
 * This function will deinitialize a private key structure.
475
 *
476
 * Since: 2.12.0
477
 **/
478
void gnutls_privkey_deinit(gnutls_privkey_t key)
479
0
{
480
0
  if (key == NULL)
481
0
    return;
482
483
0
  if (key->flags & GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE ||
484
0
      key->flags & GNUTLS_PRIVKEY_IMPORT_COPY)
485
0
    switch (key->type) {
486
#ifdef ENABLE_PKCS11
487
    case GNUTLS_PRIVKEY_PKCS11:
488
      gnutls_pkcs11_privkey_deinit(key->key.pkcs11);
489
      break;
490
#endif
491
0
    case GNUTLS_PRIVKEY_X509:
492
0
      gnutls_x509_privkey_deinit(key->key.x509);
493
0
      break;
494
0
    case GNUTLS_PRIVKEY_EXT:
495
0
      if (key->key.ext.deinit_func != NULL)
496
0
        key->key.ext.deinit_func(key,
497
0
               key->key.ext.userdata);
498
0
      break;
499
0
    default:
500
0
      break;
501
0
    }
502
0
  gnutls_free(key);
503
0
}
504
505
/* Will erase all private key information, except PIN */
506
void _gnutls_privkey_cleanup(gnutls_privkey_t key)
507
0
{
508
0
  memset(&key->key, 0, sizeof(key->key));
509
0
  key->type = 0;
510
0
  key->pk_algorithm = 0;
511
0
  key->flags = 0;
512
0
}
513
514
/* will fail if the private key contains an actual key.
515
 */
516
static int check_if_clean(gnutls_privkey_t key)
517
0
{
518
0
  if (key->type != 0)
519
0
    return GNUTLS_E_INVALID_REQUEST;
520
521
0
  return 0;
522
0
}
523
524
#ifdef ENABLE_PKCS11
525
526
/**
527
 * gnutls_privkey_import_pkcs11:
528
 * @pkey: The private key
529
 * @key: The private key to be imported
530
 * @flags: Flags for the import
531
 *
532
 * This function will import the given private key to the abstract
533
 * #gnutls_privkey_t type.
534
 *
535
 * The #gnutls_pkcs11_privkey_t object must not be deallocated
536
 * during the lifetime of this structure.
537
 *
538
 * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
539
 * and %GNUTLS_PRIVKEY_IMPORT_COPY.
540
 *
541
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
542
 *   negative error value.
543
 *
544
 * Since: 2.12.0
545
 **/
546
int gnutls_privkey_import_pkcs11(gnutls_privkey_t pkey,
547
         gnutls_pkcs11_privkey_t key,
548
         unsigned int flags)
549
{
550
  int ret;
551
552
  ret = check_if_clean(pkey);
553
  if (ret < 0) {
554
    gnutls_assert();
555
    return ret;
556
  }
557
558
  if (flags & GNUTLS_PRIVKEY_IMPORT_COPY)
559
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
560
561
  pkey->key.pkcs11 = key;
562
  pkey->type = GNUTLS_PRIVKEY_PKCS11;
563
  pkey->pk_algorithm = gnutls_pkcs11_privkey_get_pk_algorithm(key, NULL);
564
  pkey->flags = flags;
565
566
  if (pkey->pin.data)
567
    gnutls_pkcs11_privkey_set_pin_function(key, pkey->pin.cb,
568
                   pkey->pin.data);
569
570
  return 0;
571
}
572
573
#if 0
574
/**
575
 * gnutls_privkey_import_pkcs11_url:
576
 * @key: A key of type #gnutls_pubkey_t
577
 * @url: A PKCS 11 url
578
 *
579
 * This function will import a PKCS 11 private key to a #gnutls_privkey_t
580
 * type.
581
 *
582
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
583
 *   negative error value.
584
 *
585
 * Since: 3.1.0
586
 **/
587
588
int gnutls_privkey_import_pkcs11_url(gnutls_privkey_t key, const char *url)
589
{
590
  int x;
591
}
592
#endif
593
594
static int _gnutls_privkey_import_pkcs11_url(gnutls_privkey_t key,
595
               const char *url, unsigned flags)
596
{
597
  gnutls_pkcs11_privkey_t pkey;
598
  int ret;
599
600
  ret = gnutls_pkcs11_privkey_init(&pkey);
601
  if (ret < 0) {
602
    gnutls_assert();
603
    return ret;
604
  }
605
606
  if (key->pin.cb)
607
    gnutls_pkcs11_privkey_set_pin_function(pkey, key->pin.cb,
608
                   key->pin.data);
609
610
  ret = gnutls_pkcs11_privkey_import_url(pkey, url, flags);
611
  if (ret < 0) {
612
    gnutls_assert();
613
    goto cleanup;
614
  }
615
616
  ret = gnutls_privkey_import_pkcs11(key, pkey,
617
             GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
618
  if (ret < 0) {
619
    gnutls_assert();
620
    goto cleanup;
621
  }
622
623
  return 0;
624
625
cleanup:
626
  gnutls_pkcs11_privkey_deinit(pkey);
627
628
  return ret;
629
}
630
631
/**
632
 * gnutls_privkey_export_pkcs11:
633
 * @pkey: The private key
634
 * @key: Location for the key to be exported.
635
 *
636
 * Converts the given abstract private key to a #gnutls_pkcs11_privkey_t
637
 * type. The key must be of type %GNUTLS_PRIVKEY_PKCS11. The key
638
 * returned in @key must be deinitialized with
639
 * gnutls_pkcs11_privkey_deinit().
640
 *
641
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
642
 *   negative error value.
643
 *
644
 * Since: 3.4.0
645
 */
646
int gnutls_privkey_export_pkcs11(gnutls_privkey_t pkey,
647
         gnutls_pkcs11_privkey_t *key)
648
{
649
  int ret;
650
651
  *key = NULL;
652
  if (pkey->type != GNUTLS_PRIVKEY_PKCS11) {
653
    gnutls_assert();
654
    return GNUTLS_E_INVALID_REQUEST;
655
  }
656
657
  ret = gnutls_pkcs11_privkey_init(key);
658
  if (ret < 0)
659
    return gnutls_assert_val(ret);
660
661
  ret = gnutls_pkcs11_privkey_cpy(*key, pkey->key.pkcs11);
662
  if (ret < 0) {
663
    gnutls_pkcs11_privkey_deinit(*key);
664
    *key = NULL;
665
666
    return gnutls_assert_val(ret);
667
  }
668
669
  return 0;
670
}
671
#endif /* ENABLE_PKCS11 */
672
673
/**
674
 * gnutls_privkey_import_ext:
675
 * @pkey: The private key
676
 * @pk: The public key algorithm
677
 * @userdata: private data to be provided to the callbacks
678
 * @sign_func: callback for signature operations
679
 * @decrypt_func: callback for decryption operations
680
 * @flags: Flags for the import
681
 *
682
 * This function will associate the given callbacks with the
683
 * #gnutls_privkey_t type. At least one of the two callbacks
684
 * must be non-null.
685
 *
686
 * Note that the signing function is supposed to "raw" sign data, i.e.,
687
 * without any hashing or preprocessing. In case of RSA the DigestInfo
688
 * will be provided, and the signing function is expected to do the PKCS #1
689
 * 1.5 padding and the exponentiation.
690
 *
691
 * See also gnutls_privkey_import_ext3().
692
 *
693
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
694
 *   negative error value.
695
 *
696
 * Since: 3.0
697
 **/
698
int gnutls_privkey_import_ext(gnutls_privkey_t pkey, gnutls_pk_algorithm_t pk,
699
            void *userdata,
700
            gnutls_privkey_sign_func sign_func,
701
            gnutls_privkey_decrypt_func decrypt_func,
702
            unsigned int flags)
703
0
{
704
0
  return gnutls_privkey_import_ext2(pkey, pk, userdata, sign_func,
705
0
            decrypt_func, NULL, flags);
706
0
}
707
708
#define PK_IS_OK_FOR_EXT2(pk)                                \
709
0
  ((pk == GNUTLS_PK_RSA) || (pk == GNUTLS_PK_ECDSA) || \
710
0
   (pk == GNUTLS_PK_DSA))
711
712
/**
713
 * gnutls_privkey_import_ext2:
714
 * @pkey: The private key
715
 * @pk: The public key algorithm
716
 * @userdata: private data to be provided to the callbacks
717
 * @sign_fn: callback for signature operations
718
 * @decrypt_fn: callback for decryption operations
719
 * @deinit_fn: a deinitialization function
720
 * @flags: Flags for the import
721
 *
722
 * This function will associate the given callbacks with the
723
 * #gnutls_privkey_t type. At least one of the two callbacks
724
 * must be non-null. If a deinitialization function is provided
725
 * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE.
726
 *
727
 * Note that the signing function is supposed to "raw" sign data, i.e.,
728
 * without any hashing or preprocessing. In case of RSA the DigestInfo
729
 * will be provided, and the signing function is expected to do the PKCS #1
730
 * 1.5 padding and the exponentiation.
731
 *
732
 * See also gnutls_privkey_import_ext3().
733
 *
734
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
735
 *   negative error value.
736
 *
737
 * Since: 3.1
738
 **/
739
int gnutls_privkey_import_ext2(gnutls_privkey_t pkey, gnutls_pk_algorithm_t pk,
740
             void *userdata, gnutls_privkey_sign_func sign_fn,
741
             gnutls_privkey_decrypt_func decrypt_fn,
742
             gnutls_privkey_deinit_func deinit_fn,
743
             unsigned int flags)
744
0
{
745
0
  int ret;
746
747
0
  ret = check_if_clean(pkey);
748
0
  if (ret < 0) {
749
0
    gnutls_assert();
750
0
    return ret;
751
0
  }
752
753
0
  if (!PK_IS_OK_FOR_EXT2(pk))
754
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
755
756
0
  if (sign_fn == NULL && decrypt_fn == NULL)
757
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
758
759
0
  pkey->key.ext.sign_func = sign_fn;
760
0
  pkey->key.ext.decrypt_func = decrypt_fn;
761
0
  pkey->key.ext.deinit_func = deinit_fn;
762
0
  pkey->key.ext.userdata = userdata;
763
0
  pkey->type = GNUTLS_PRIVKEY_EXT;
764
0
  pkey->pk_algorithm = pk;
765
0
  pkey->flags = flags;
766
767
  /* Ensure gnutls_privkey_deinit() calls the deinit_func */
768
0
  if (deinit_fn)
769
0
    pkey->flags |= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE;
770
771
0
  return 0;
772
0
}
773
774
/**
775
 * gnutls_privkey_import_ext3:
776
 * @pkey: The private key
777
 * @userdata: private data to be provided to the callbacks
778
 * @sign_fn: callback for signature operations
779
 * @decrypt_fn: callback for decryption operations
780
 * @deinit_fn: a deinitialization function
781
 * @info_fn: returns info about the public key algorithm (should not be %NULL)
782
 * @flags: Flags for the import
783
 *
784
 * This function will associate the given callbacks with the
785
 * #gnutls_privkey_t type. At least one of the two callbacks
786
 * must be non-null. If a deinitialization function is provided
787
 * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE.
788
 *
789
 * Note that the signing function is supposed to "raw" sign data, i.e.,
790
 * without any hashing or preprocessing. In case of RSA the DigestInfo
791
 * will be provided, and the signing function is expected to do the PKCS #1
792
 * 1.5 padding and the exponentiation.
793
 *
794
 * The @info_fn must provide information on the algorithms supported by
795
 * this private key, and should support the flags %GNUTLS_PRIVKEY_INFO_PK_ALGO and
796
 * %GNUTLS_PRIVKEY_INFO_SIGN_ALGO. It must return -1 on unknown flags.
797
 *
798
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
799
 *   negative error value.
800
 *
801
 * Since: 3.4.0
802
 **/
803
int gnutls_privkey_import_ext3(gnutls_privkey_t pkey, void *userdata,
804
             gnutls_privkey_sign_func sign_fn,
805
             gnutls_privkey_decrypt_func decrypt_fn,
806
             gnutls_privkey_deinit_func deinit_fn,
807
             gnutls_privkey_info_func info_fn,
808
             unsigned int flags)
809
0
{
810
0
  int ret;
811
812
0
  ret = check_if_clean(pkey);
813
0
  if (ret < 0) {
814
0
    gnutls_assert();
815
0
    return ret;
816
0
  }
817
818
0
  if (sign_fn == NULL && decrypt_fn == NULL)
819
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
820
821
0
  if (info_fn == NULL)
822
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
823
824
0
  pkey->key.ext.sign_func = sign_fn;
825
0
  pkey->key.ext.decrypt_func = decrypt_fn;
826
0
  pkey->key.ext.deinit_func = deinit_fn;
827
0
  pkey->key.ext.info_func = info_fn;
828
0
  pkey->key.ext.userdata = userdata;
829
0
  pkey->type = GNUTLS_PRIVKEY_EXT;
830
0
  pkey->flags = flags;
831
832
0
  pkey->pk_algorithm = pkey->key.ext.info_func(
833
0
    pkey, GNUTLS_PRIVKEY_INFO_PK_ALGO, pkey->key.ext.userdata);
834
835
0
  if (!PK_IS_OK_FOR_EXT2(pkey->pk_algorithm))
836
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
837
838
  /* Ensure gnutls_privkey_deinit() calls the deinit_func */
839
0
  if (deinit_fn)
840
0
    pkey->flags |= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE;
841
842
0
  return 0;
843
0
}
844
845
/**
846
 * gnutls_privkey_import_ext4:
847
 * @pkey: The private key
848
 * @userdata: private data to be provided to the callbacks
849
 * @sign_data_fn: callback for signature operations (may be %NULL)
850
 * @sign_hash_fn: callback for signature operations (may be %NULL)
851
 * @decrypt_fn: callback for decryption operations (may be %NULL)
852
 * @deinit_fn: a deinitialization function
853
 * @info_fn: returns info about the public key algorithm (should not be %NULL)
854
 * @flags: Flags for the import
855
 *
856
 * This function will associate the given callbacks with the
857
 * #gnutls_privkey_t type. At least one of the callbacks
858
 * must be non-null. If a deinitialization function is provided
859
 * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE.
860
 *
861
 * Note that in contrast with the signing function of
862
 * gnutls_privkey_import_ext3(), the signing functions provided to this
863
 * function take explicitly the signature algorithm as parameter and
864
 * different functions are provided to sign the data and hashes.
865
 *
866
 * The @sign_hash_fn is to be called to sign pre-hashed data. The input
867
 * to the callback is the output of the hash (such as SHA256) corresponding
868
 * to the signature algorithm. For RSA PKCS#1 signatures, the signature
869
 * algorithm can be set to %GNUTLS_SIGN_RSA_RAW, and in that case the data
870
 * should be handled as if they were an RSA PKCS#1 DigestInfo structure.
871
 *
872
 * The @sign_data_fn is to be called to sign data. The input data will be
873
 * he data to be signed (and hashed), with the provided signature
874
 * algorithm. This function is to be used for signature algorithms like
875
 * Ed25519 which cannot take pre-hashed data as input.
876
 *
877
 * When both @sign_data_fn and @sign_hash_fn functions are provided they
878
 * must be able to operate on all the supported signature algorithms,
879
 * unless prohibited by the type of the algorithm (e.g., as with Ed25519).
880
 *
881
 * The @info_fn must provide information on the signature algorithms supported by
882
 * this private key, and should support the flags %GNUTLS_PRIVKEY_INFO_PK_ALGO,
883
 * %GNUTLS_PRIVKEY_INFO_HAVE_SIGN_ALGO and %GNUTLS_PRIVKEY_INFO_PK_ALGO_BITS.
884
 * It must return -1 on unknown flags.
885
 *
886
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
887
 *   negative error value.
888
 *
889
 * Since: 3.6.0
890
 **/
891
int gnutls_privkey_import_ext4(gnutls_privkey_t pkey, void *userdata,
892
             gnutls_privkey_sign_data_func sign_data_fn,
893
             gnutls_privkey_sign_hash_func sign_hash_fn,
894
             gnutls_privkey_decrypt_func decrypt_fn,
895
             gnutls_privkey_deinit_func deinit_fn,
896
             gnutls_privkey_info_func info_fn,
897
             unsigned int flags)
898
0
{
899
0
  int ret;
900
901
0
  ret = check_if_clean(pkey);
902
0
  if (ret < 0) {
903
0
    gnutls_assert();
904
0
    return ret;
905
0
  }
906
907
0
  if (sign_data_fn == NULL && sign_hash_fn == NULL && decrypt_fn == NULL)
908
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
909
910
0
  if (info_fn == NULL)
911
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
912
913
0
  pkey->key.ext.sign_data_func = sign_data_fn;
914
0
  pkey->key.ext.sign_hash_func = sign_hash_fn;
915
0
  pkey->key.ext.decrypt_func = decrypt_fn;
916
0
  pkey->key.ext.deinit_func = deinit_fn;
917
0
  pkey->key.ext.info_func = info_fn;
918
0
  pkey->key.ext.userdata = userdata;
919
0
  pkey->type = GNUTLS_PRIVKEY_EXT;
920
0
  pkey->flags = flags;
921
922
0
  pkey->pk_algorithm = pkey->key.ext.info_func(
923
0
    pkey, GNUTLS_PRIVKEY_INFO_PK_ALGO, pkey->key.ext.userdata);
924
925
0
  ret = pkey->key.ext.info_func(pkey, GNUTLS_PRIVKEY_INFO_PK_ALGO_BITS,
926
0
              pkey->key.ext.userdata);
927
0
  if (ret >= 0)
928
0
    pkey->key.ext.bits = ret;
929
930
  /* Ensure gnutls_privkey_deinit() calls the deinit_func */
931
0
  if (deinit_fn)
932
0
    pkey->flags |= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE;
933
934
0
  return 0;
935
0
}
936
937
/**
938
 * gnutls_privkey_import_x509:
939
 * @pkey: The private key
940
 * @key: The private key to be imported
941
 * @flags: Flags for the import
942
 *
943
 * This function will import the given private key to the abstract
944
 * #gnutls_privkey_t type.
945
 *
946
 * The #gnutls_x509_privkey_t object must not be deallocated
947
 * during the lifetime of this structure.
948
 *
949
 * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
950
 * and %GNUTLS_PRIVKEY_IMPORT_COPY.
951
 *
952
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
953
 *   negative error value.
954
 *
955
 * Since: 2.12.0
956
 **/
957
int gnutls_privkey_import_x509(gnutls_privkey_t pkey, gnutls_x509_privkey_t key,
958
             unsigned int flags)
959
0
{
960
0
  int ret;
961
962
0
  ret = check_if_clean(pkey);
963
0
  if (ret < 0) {
964
0
    gnutls_assert();
965
0
    return ret;
966
0
  }
967
968
0
  if (flags & GNUTLS_PRIVKEY_IMPORT_COPY) {
969
0
    ret = gnutls_x509_privkey_init(&pkey->key.x509);
970
0
    if (ret < 0)
971
0
      return gnutls_assert_val(ret);
972
973
0
    ret = gnutls_x509_privkey_cpy(pkey->key.x509, key);
974
0
    if (ret < 0) {
975
0
      gnutls_x509_privkey_deinit(pkey->key.x509);
976
0
      return gnutls_assert_val(ret);
977
0
    }
978
0
  } else
979
0
    pkey->key.x509 = key;
980
981
0
  pkey->type = GNUTLS_PRIVKEY_X509;
982
0
  pkey->pk_algorithm = gnutls_x509_privkey_get_pk_algorithm(key);
983
0
  pkey->flags = flags;
984
985
0
  return 0;
986
0
}
987
988
/**
989
 * gnutls_privkey_export_x509:
990
 * @pkey: The private key
991
 * @key: Location for the key to be exported.
992
 *
993
 * Converts the given abstract private key to a #gnutls_x509_privkey_t
994
 * type. The abstract key must be of type %GNUTLS_PRIVKEY_X509. The input
995
 * @key must not be initialized. The key returned in @key should be deinitialized
996
 * using gnutls_x509_privkey_deinit().
997
 *
998
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
999
 *   negative error value.
1000
 *
1001
 * Since: 3.4.0
1002
 */
1003
int gnutls_privkey_export_x509(gnutls_privkey_t pkey,
1004
             gnutls_x509_privkey_t *key)
1005
0
{
1006
0
  int ret;
1007
1008
0
  *key = NULL;
1009
0
  if (pkey->type != GNUTLS_PRIVKEY_X509) {
1010
0
    gnutls_assert();
1011
0
    return GNUTLS_E_INVALID_REQUEST;
1012
0
  }
1013
1014
0
  ret = gnutls_x509_privkey_init(key);
1015
0
  if (ret < 0)
1016
0
    return gnutls_assert_val(ret);
1017
1018
0
  ret = gnutls_x509_privkey_cpy(*key, pkey->key.x509);
1019
0
  if (ret < 0) {
1020
0
    gnutls_x509_privkey_deinit(*key);
1021
0
    *key = NULL;
1022
1023
0
    return gnutls_assert_val(ret);
1024
0
  }
1025
1026
0
  return 0;
1027
0
}
1028
1029
/**
1030
 * gnutls_privkey_generate:
1031
 * @pkey: An initialized private key
1032
 * @algo: is one of the algorithms in #gnutls_pk_algorithm_t.
1033
 * @bits: the size of the parameters to generate
1034
 * @flags: Must be zero or flags from #gnutls_privkey_flags_t.
1035
 *
1036
 * This function will generate a random private key. Note that this
1037
 * function must be called on an initialized private key.
1038
 *
1039
 * The flag %GNUTLS_PRIVKEY_FLAG_PROVABLE
1040
 * instructs the key generation process to use algorithms like Shawe-Taylor
1041
 * (from FIPS PUB186-4) which generate provable parameters out of a seed
1042
 * for RSA and DSA keys. See gnutls_privkey_generate2() for more
1043
 * information.
1044
 *
1045
 * Note that when generating an elliptic curve key, the curve
1046
 * can be substituted in the place of the bits parameter using the
1047
 * GNUTLS_CURVE_TO_BITS() macro. The input to the macro is any curve from
1048
 * %gnutls_ecc_curve_t.
1049
 *
1050
 * For DSA keys, if the subgroup size needs to be specified check
1051
 * the GNUTLS_SUBGROUP_TO_BITS() macro.
1052
 *
1053
 * It is recommended to do not set the number of @bits directly, use gnutls_sec_param_to_pk_bits() instead .
1054
 *
1055
 * See also gnutls_privkey_generate2().
1056
 *
1057
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1058
 *   negative error value.
1059
 *
1060
 * Since: 3.3.0
1061
 **/
1062
int gnutls_privkey_generate(gnutls_privkey_t pkey, gnutls_pk_algorithm_t algo,
1063
          unsigned int bits, unsigned int flags)
1064
0
{
1065
0
  return gnutls_privkey_generate2(pkey, algo, bits, flags, NULL, 0);
1066
0
}
1067
1068
/**
1069
 * gnutls_privkey_generate2:
1070
 * @pkey: The private key
1071
 * @algo: is one of the algorithms in #gnutls_pk_algorithm_t.
1072
 * @bits: the size of the modulus
1073
 * @flags: Must be zero or flags from #gnutls_privkey_flags_t.
1074
 * @data: Allow specifying %gnutls_keygen_data_st types such as the seed to be used.
1075
 * @data_size: The number of @data available.
1076
 *
1077
 * This function will generate a random private key. Note that this
1078
 * function must be called on an initialized private key.
1079
 *
1080
 * The flag %GNUTLS_PRIVKEY_FLAG_PROVABLE
1081
 * instructs the key generation process to use algorithms like Shawe-Taylor
1082
 * (from FIPS PUB186-4) which generate provable parameters out of a seed
1083
 * for RSA and DSA keys. On DSA keys the PQG parameters are generated using the
1084
 * seed, while on RSA the two primes. To specify an explicit seed
1085
 * (by default a random seed is used), use the @data with a %GNUTLS_KEYGEN_SEED
1086
 * type.
1087
 *
1088
 * Note that when generating an elliptic curve key, the curve
1089
 * can be substituted in the place of the bits parameter using the
1090
 * GNUTLS_CURVE_TO_BITS() macro.
1091
 *
1092
 * To export the generated keys in memory or in files it is recommended to use the
1093
 * PKCS#8 form as it can handle all key types, and can store additional parameters
1094
 * such as the seed, in case of provable RSA or DSA keys.
1095
 * Generated keys can be exported in memory using gnutls_privkey_export_x509(),
1096
 * and then with gnutls_x509_privkey_export2_pkcs8().
1097
 *
1098
 * If key generation is part of your application, avoid setting the number
1099
 * of bits directly, and instead use gnutls_sec_param_to_pk_bits().
1100
 * That way the generated keys will adapt to the security levels
1101
 * of the underlying GnuTLS library.
1102
 *
1103
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1104
 *   negative error value.
1105
 *
1106
 * Since: 3.5.0
1107
 **/
1108
int gnutls_privkey_generate2(gnutls_privkey_t pkey, gnutls_pk_algorithm_t algo,
1109
           unsigned int bits, unsigned int flags,
1110
           const gnutls_keygen_data_st *data,
1111
           unsigned data_size)
1112
0
{
1113
0
  int ret;
1114
1115
0
  ret = gnutls_x509_privkey_init(&pkey->key.x509);
1116
0
  if (ret < 0)
1117
0
    return gnutls_assert_val(ret);
1118
1119
0
  ret = gnutls_x509_privkey_generate2(pkey->key.x509, algo, bits, flags,
1120
0
              data, data_size);
1121
0
  if (ret < 0) {
1122
0
    gnutls_x509_privkey_deinit(pkey->key.x509);
1123
0
    pkey->key.x509 = NULL;
1124
0
    return gnutls_assert_val(ret);
1125
0
  }
1126
1127
0
  pkey->type = GNUTLS_PRIVKEY_X509;
1128
0
  pkey->pk_algorithm = algo;
1129
0
  pkey->flags = flags | GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE;
1130
1131
0
  return 0;
1132
0
}
1133
1134
/**
1135
 * gnutls_privkey_sign_data:
1136
 * @signer: Holds the key
1137
 * @hash: should be a digest algorithm
1138
 * @flags: Zero or one of %gnutls_privkey_flags_t
1139
 * @data: holds the data to be signed
1140
 * @signature: will contain the signature allocated with gnutls_malloc()
1141
 *
1142
 * This function will sign the given data using a signature algorithm
1143
 * supported by the private key. Signature algorithms are always used
1144
 * together with a hash functions.  Different hash functions may be
1145
 * used for the RSA algorithm, but only the SHA family for the DSA keys.
1146
 *
1147
 * You may use gnutls_pubkey_get_preferred_hash_algorithm() to determine
1148
 * the hash algorithm.
1149
 *
1150
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1151
 * negative error value.
1152
 *
1153
 * Since: 2.12.0
1154
 **/
1155
int gnutls_privkey_sign_data(gnutls_privkey_t signer,
1156
           gnutls_digest_algorithm_t hash, unsigned int flags,
1157
           const gnutls_datum_t *data,
1158
           gnutls_datum_t *signature)
1159
0
{
1160
0
  int ret;
1161
0
  gnutls_x509_spki_st params;
1162
1163
0
  if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA)
1164
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1165
1166
0
  ret = _gnutls_privkey_get_spki_params(signer, &params);
1167
0
  if (ret < 0) {
1168
0
    gnutls_assert();
1169
0
    return ret;
1170
0
  }
1171
1172
0
  ret = _gnutls_privkey_update_spki_params(signer, signer->pk_algorithm,
1173
0
             hash, flags, &params);
1174
0
  if (ret < 0) {
1175
0
    gnutls_assert();
1176
0
    return ret;
1177
0
  }
1178
1179
0
  FIX_SIGN_PARAMS(params, flags, hash);
1180
1181
0
  return privkey_sign_and_hash_data(
1182
0
    signer, _gnutls_pk_to_sign_entry(params.pk, hash), data,
1183
0
    signature, &params);
1184
0
}
1185
1186
/**
1187
 * gnutls_privkey_sign_data2:
1188
 * @signer: Holds the key
1189
 * @algo: The signature algorithm used
1190
 * @flags: Zero or one of %gnutls_privkey_flags_t
1191
 * @data: holds the data to be signed
1192
 * @signature: will contain the signature allocated with gnutls_malloc()
1193
 *
1194
 * This function will sign the given data using the specified signature
1195
 * algorithm. This function is an enhancement of gnutls_privkey_sign_data(),
1196
 * as it allows utilizing a alternative signature algorithm where possible
1197
 * (e.g, use an RSA key with RSA-PSS).
1198
 *
1199
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1200
 * negative error value.
1201
 *
1202
 * Since: 3.6.0
1203
 **/
1204
int gnutls_privkey_sign_data2(gnutls_privkey_t signer,
1205
            gnutls_sign_algorithm_t algo, unsigned int flags,
1206
            const gnutls_datum_t *data,
1207
            gnutls_datum_t *signature)
1208
0
{
1209
0
  int ret;
1210
0
  gnutls_x509_spki_st params;
1211
0
  const gnutls_sign_entry_st *se;
1212
1213
0
  if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA)
1214
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1215
1216
0
  se = _gnutls_sign_to_entry(algo);
1217
0
  if (se == NULL)
1218
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1219
1220
0
  ret = _gnutls_privkey_get_spki_params(signer, &params);
1221
0
  if (ret < 0) {
1222
0
    gnutls_assert();
1223
0
    return ret;
1224
0
  }
1225
1226
0
  ret = _gnutls_privkey_update_spki_params(signer, se->pk, se->hash,
1227
0
             flags, &params);
1228
0
  if (ret < 0) {
1229
0
    gnutls_assert();
1230
0
    return ret;
1231
0
  }
1232
1233
0
  FIX_SIGN_PARAMS(params, flags, se->hash);
1234
1235
0
  return privkey_sign_and_hash_data(signer, se, data, signature, &params);
1236
0
}
1237
1238
/**
1239
 * gnutls_privkey_sign_hash2:
1240
 * @signer: Holds the signer's key
1241
 * @algo: The signature algorithm used
1242
 * @flags: Zero or one of %gnutls_privkey_flags_t
1243
 * @hash_data: holds the data to be signed
1244
 * @signature: will contain newly allocated signature
1245
 *
1246
 * This function will sign the given hashed data using the specified signature
1247
 * algorithm. This function is an enhancement of gnutls_privkey_sign_hash(),
1248
 * as it allows utilizing a alternative signature algorithm where possible
1249
 * (e.g, use an RSA key with RSA-PSS).
1250
 *
1251
 * The flags may be %GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA.
1252
 * In that case this function will ignore @hash_algo and perform a raw PKCS1 signature.
1253
 * Note that this flag is supported since 3.6.9.
1254
 *
1255
 * Note also that, not all algorithm support signing already hashed data. When
1256
 * signing with Ed25519, gnutls_privkey_sign_data2() should be used instead.
1257
 *
1258
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1259
 *   negative error value.
1260
 *
1261
 * Since: 3.6.0
1262
 **/
1263
int gnutls_privkey_sign_hash2(gnutls_privkey_t signer,
1264
            gnutls_sign_algorithm_t algo, unsigned int flags,
1265
            const gnutls_datum_t *hash_data,
1266
            gnutls_datum_t *signature)
1267
0
{
1268
0
  int ret;
1269
0
  gnutls_x509_spki_st params;
1270
0
  const gnutls_sign_entry_st *se;
1271
1272
0
  if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA) {
1273
    /* the corresponding signature algorithm is SIGN_RSA_RAW,
1274
     * irrespective of hash algorithm. */
1275
0
    se = _gnutls_sign_to_entry(GNUTLS_SIGN_RSA_RAW);
1276
0
  } else {
1277
0
    se = _gnutls_sign_to_entry(algo);
1278
0
    if (unlikely(se == NULL)) {
1279
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1280
0
      goto cleanup;
1281
0
    }
1282
0
  }
1283
1284
0
  ret = _gnutls_privkey_get_spki_params(signer, &params);
1285
0
  if (ret < 0) {
1286
0
    gnutls_assert();
1287
0
    goto cleanup;
1288
0
  }
1289
1290
0
  ret = _gnutls_privkey_update_spki_params(signer, se->pk, se->hash,
1291
0
             flags, &params);
1292
0
  if (ret < 0) {
1293
0
    gnutls_assert();
1294
0
    goto cleanup;
1295
0
  }
1296
1297
0
  FIX_SIGN_PARAMS(params, flags, se->hash);
1298
1299
0
  ret = privkey_sign_prehashed(signer, se, hash_data, signature, &params);
1300
1301
0
cleanup:
1302
0
  if (ret < 0) {
1303
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1304
0
  } else {
1305
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1306
0
  }
1307
0
  return ret;
1308
0
}
1309
1310
int privkey_sign_and_hash_data(gnutls_privkey_t signer,
1311
             const gnutls_sign_entry_st *se,
1312
             const gnutls_datum_t *data,
1313
             gnutls_datum_t *signature,
1314
             gnutls_x509_spki_st *params)
1315
0
{
1316
0
  int ret;
1317
0
  gnutls_datum_t digest;
1318
0
  const mac_entry_st *me;
1319
1320
0
  if (unlikely(se == NULL))
1321
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1322
1323
0
  if (_gnutls_pk_is_not_prehashed(se->pk)) {
1324
0
    return privkey_sign_raw_data(signer, se, data, signature,
1325
0
               params);
1326
0
  }
1327
1328
0
  me = hash_to_entry(se->hash);
1329
0
  if (me == NULL)
1330
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1331
1332
0
  ret = pk_hash_data(se->pk, me, NULL, data, &digest);
1333
0
  if (ret < 0) {
1334
0
    gnutls_assert();
1335
0
    return ret;
1336
0
  }
1337
1338
0
  ret = pk_prepare_hash(se->pk, me, &digest);
1339
0
  if (ret < 0) {
1340
0
    gnutls_assert();
1341
0
    goto cleanup;
1342
0
  }
1343
1344
0
  ret = privkey_sign_raw_data(signer, se, &digest, signature, params);
1345
0
  _gnutls_free_datum(&digest);
1346
1347
0
  if (ret < 0) {
1348
0
    gnutls_assert();
1349
0
    return ret;
1350
0
  }
1351
1352
0
  return 0;
1353
1354
0
cleanup:
1355
0
  _gnutls_free_datum(&digest);
1356
0
  return ret;
1357
0
}
1358
1359
/**
1360
 * gnutls_privkey_sign_hash:
1361
 * @signer: Holds the signer's key
1362
 * @hash_algo: The hash algorithm used
1363
 * @flags: Zero or one of %gnutls_privkey_flags_t
1364
 * @hash_data: holds the data to be signed
1365
 * @signature: will contain newly allocated signature
1366
 *
1367
 * This function will sign the given hashed data using a signature algorithm
1368
 * supported by the private key. Signature algorithms are always used
1369
 * together with a hash functions.  Different hash functions may be
1370
 * used for the RSA algorithm, but only SHA-XXX for the DSA keys.
1371
 *
1372
 * You may use gnutls_pubkey_get_preferred_hash_algorithm() to determine
1373
 * the hash algorithm.
1374
 *
1375
 * The flags may be %GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA or %GNUTLS_PRIVKEY_SIGN_FLAG_RSA_PSS.
1376
 * In the former case this function will ignore @hash_algo and perform a raw PKCS1 signature,
1377
 * and in the latter an RSA-PSS signature will be generated.
1378
 *
1379
 * Note that, not all algorithm support signing already hashed data. When
1380
 * signing with Ed25519, gnutls_privkey_sign_data() should be used.
1381
 *
1382
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1383
 *   negative error value.
1384
 *
1385
 * Since: 2.12.0
1386
 **/
1387
int gnutls_privkey_sign_hash(gnutls_privkey_t signer,
1388
           gnutls_digest_algorithm_t hash_algo,
1389
           unsigned int flags,
1390
           const gnutls_datum_t *hash_data,
1391
           gnutls_datum_t *signature)
1392
0
{
1393
0
  int ret;
1394
0
  gnutls_x509_spki_st params;
1395
0
  const gnutls_sign_entry_st *se;
1396
1397
0
  ret = _gnutls_privkey_get_spki_params(signer, &params);
1398
0
  if (ret < 0) {
1399
0
    gnutls_assert();
1400
0
    goto cleanup;
1401
0
  }
1402
1403
0
  ret = _gnutls_privkey_update_spki_params(signer, signer->pk_algorithm,
1404
0
             hash_algo, flags, &params);
1405
0
  if (ret < 0) {
1406
0
    gnutls_assert();
1407
0
    goto cleanup;
1408
0
  }
1409
1410
  /* legacy callers of this API could use a hash algorithm of 0 (unknown)
1411
   * to indicate raw hashing. As we now always want to know the signing
1412
   * algorithm involved, we try discovering the hash algorithm. */
1413
0
  if (hash_algo == 0 &&
1414
0
      (params.pk == GNUTLS_PK_DSA || params.pk == GNUTLS_PK_ECDSA)) {
1415
0
    hash_algo = _gnutls_hash_size_to_sha_hash(hash_data->size);
1416
0
  }
1417
1418
0
  if (params.pk == GNUTLS_PK_RSA &&
1419
0
      (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA)) {
1420
    /* the corresponding signature algorithm is SIGN_RSA_RAW,
1421
     * irrespective of hash algorithm. */
1422
0
    se = _gnutls_sign_to_entry(GNUTLS_SIGN_RSA_RAW);
1423
0
  } else {
1424
0
    se = _gnutls_pk_to_sign_entry(params.pk, hash_algo);
1425
0
  }
1426
1427
0
  if (unlikely(se == NULL)) {
1428
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1429
0
    goto cleanup;
1430
0
  }
1431
1432
0
  FIX_SIGN_PARAMS(params, flags, hash_algo);
1433
1434
0
  ret = privkey_sign_prehashed(signer, se, hash_data, signature, &params);
1435
0
cleanup:
1436
0
  if (ret < 0) {
1437
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR);
1438
0
  } else {
1439
0
    _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED);
1440
0
  }
1441
0
  return ret;
1442
0
}
1443
1444
static int privkey_sign_prehashed(gnutls_privkey_t signer,
1445
          const gnutls_sign_entry_st *se,
1446
          const gnutls_datum_t *hash_data,
1447
          gnutls_datum_t *signature,
1448
          gnutls_x509_spki_st *params)
1449
0
{
1450
0
  int ret;
1451
0
  gnutls_datum_t digest;
1452
1453
0
  if (unlikely(se == NULL))
1454
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1455
1456
0
  if (se->id == GNUTLS_SIGN_RSA_RAW) {
1457
0
    return privkey_sign_raw_data(signer, se, hash_data, signature,
1458
0
               params);
1459
0
  }
1460
1461
0
  if (_gnutls_pk_is_not_prehashed(signer->pk_algorithm)) {
1462
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1463
0
  }
1464
1465
0
  digest.data = gnutls_malloc(hash_data->size);
1466
0
  if (digest.data == NULL) {
1467
0
    gnutls_assert();
1468
0
    return GNUTLS_E_MEMORY_ERROR;
1469
0
  }
1470
0
  digest.size = hash_data->size;
1471
0
  memcpy(digest.data, hash_data->data, digest.size);
1472
1473
0
  ret = pk_prepare_hash(se->pk, hash_to_entry(se->hash), &digest);
1474
0
  if (ret < 0) {
1475
0
    gnutls_assert();
1476
0
    goto cleanup;
1477
0
  }
1478
1479
0
  ret = privkey_sign_raw_data(signer, se, &digest, signature, params);
1480
0
  if (ret < 0) {
1481
0
    gnutls_assert();
1482
0
    goto cleanup;
1483
0
  }
1484
1485
0
  ret = 0;
1486
1487
0
cleanup:
1488
0
  _gnutls_free_datum(&digest);
1489
0
  return ret;
1490
0
}
1491
1492
/*-
1493
 * privkey_sign_raw_data:
1494
 * @key: Holds the key
1495
 * @data: holds the data to be signed
1496
 * @signature: will contain the signature allocated with gnutls_malloc()
1497
 * @params: holds the signing parameters
1498
 *
1499
 * This function will sign the given data using a signature algorithm
1500
 * supported by the private key. Note that this is a low-level function
1501
 * and does not apply any preprocessing or hash on the signed data. 
1502
 * For example on an RSA key the input @data should be of the DigestInfo
1503
 * PKCS #1 1.5 format, on RSA-PSS, DSA or ECDSA the input should be a hash output
1504
 * and on Ed25519 the raw data to be signed.
1505
 *
1506
 * Note this function is equivalent to using the %GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA
1507
 * flag with gnutls_privkey_sign_hash().
1508
 *
1509
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1510
 * negative error value.
1511
 *
1512
 * Since: 3.1.10
1513
 -*/
1514
int privkey_sign_raw_data(gnutls_privkey_t key, const gnutls_sign_entry_st *se,
1515
        const gnutls_datum_t *data, gnutls_datum_t *signature,
1516
        gnutls_x509_spki_st *params)
1517
0
{
1518
0
  if (unlikely(se == NULL))
1519
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1520
1521
0
  switch (key->type) {
1522
#ifdef ENABLE_PKCS11
1523
  case GNUTLS_PRIVKEY_PKCS11:
1524
    return _gnutls_pkcs11_privkey_sign(key->key.pkcs11, se, data,
1525
               signature, params);
1526
#endif
1527
0
  case GNUTLS_PRIVKEY_X509:
1528
0
    return _gnutls_pk_sign(se->pk, signature, data,
1529
0
               &key->key.x509->params, params);
1530
0
  case GNUTLS_PRIVKEY_EXT:
1531
0
    if (unlikely(key->key.ext.sign_data_func == NULL &&
1532
0
           key->key.ext.sign_hash_func == NULL &&
1533
0
           key->key.ext.sign_func == NULL))
1534
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1535
1536
0
    if (_gnutls_pk_is_not_prehashed(se->pk)) {
1537
0
      if (!key->key.ext.sign_data_func)
1538
0
        return gnutls_assert_val(
1539
0
          GNUTLS_E_INVALID_REQUEST);
1540
1541
0
      return key->key.ext.sign_data_func(
1542
0
        key, se->id, key->key.ext.userdata, 0, data,
1543
0
        signature);
1544
0
    } else if (key->key.ext.sign_hash_func) {
1545
0
      if (se->pk == GNUTLS_PK_RSA) {
1546
0
        se = _gnutls_sign_to_entry(GNUTLS_SIGN_RSA_RAW);
1547
0
        assert(se != NULL);
1548
0
      }
1549
1550
      /* se may not be set here if we are doing legacy RSA */
1551
0
      return key->key.ext.sign_hash_func(
1552
0
        key, se->id, key->key.ext.userdata, 0, data,
1553
0
        signature);
1554
0
    } else {
1555
0
      if (!PK_IS_OK_FOR_EXT2(se->pk))
1556
0
        return gnutls_assert_val(
1557
0
          GNUTLS_E_INVALID_REQUEST);
1558
1559
0
      return key->key.ext.sign_func(
1560
0
        key, key->key.ext.userdata, data, signature);
1561
0
    }
1562
0
  default:
1563
0
    gnutls_assert();
1564
0
    return GNUTLS_E_INVALID_REQUEST;
1565
0
  }
1566
0
}
1567
1568
/**
1569
 * gnutls_privkey_decrypt_data:
1570
 * @key: Holds the key
1571
 * @flags: zero for now
1572
 * @ciphertext: holds the data to be decrypted
1573
 * @plaintext: will contain the decrypted data, allocated with gnutls_malloc()
1574
 *
1575
 * This function will decrypt the given data using the algorithm
1576
 * supported by the private key.
1577
 *
1578
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1579
 * negative error value.
1580
 *
1581
 * Since: 2.12.0
1582
 **/
1583
int gnutls_privkey_decrypt_data(gnutls_privkey_t key, unsigned int flags,
1584
        const gnutls_datum_t *ciphertext,
1585
        gnutls_datum_t *plaintext)
1586
0
{
1587
0
  switch (key->type) {
1588
0
  case GNUTLS_PRIVKEY_X509:
1589
0
    return _gnutls_pk_decrypt(key->pk_algorithm, plaintext,
1590
0
            ciphertext, &key->key.x509->params);
1591
#ifdef ENABLE_PKCS11
1592
  case GNUTLS_PRIVKEY_PKCS11:
1593
    return _gnutls_pkcs11_privkey_decrypt_data(
1594
      key->key.pkcs11, flags, ciphertext, plaintext);
1595
#endif
1596
0
  case GNUTLS_PRIVKEY_EXT:
1597
0
    if (key->key.ext.decrypt_func == NULL)
1598
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1599
1600
0
    return key->key.ext.decrypt_func(key, key->key.ext.userdata,
1601
0
             ciphertext, plaintext);
1602
0
  default:
1603
0
    gnutls_assert();
1604
0
    return GNUTLS_E_INVALID_REQUEST;
1605
0
  }
1606
0
}
1607
1608
/**
1609
 * gnutls_privkey_decrypt_data2:
1610
 * @key: Holds the key
1611
 * @flags: zero for now
1612
 * @ciphertext: holds the data to be decrypted
1613
 * @plaintext: a preallocated buffer that will be filled with the plaintext
1614
 * @plaintext_size: in/out size of the plaintext
1615
 *
1616
 * This function will decrypt the given data using the algorithm
1617
 * supported by the private key. Unlike with gnutls_privkey_decrypt_data()
1618
 * this function operates in constant time and constant memory access.
1619
 *
1620
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1621
 * negative error value.
1622
 *
1623
 * Since: 3.6.5
1624
 **/
1625
1626
int gnutls_privkey_decrypt_data2(gnutls_privkey_t key, unsigned int flags,
1627
         const gnutls_datum_t *ciphertext,
1628
         unsigned char *plaintext,
1629
         size_t plaintext_size)
1630
0
{
1631
  /* Note: except for the backwards compatibility function, no
1632
   * conditional code should be called after the decryption
1633
   * function call, to avoid creating oracle attacks based
1634
   * on cache/timing side channels */
1635
1636
  /* backwards compatibility */
1637
0
  if (key->type == GNUTLS_PRIVKEY_EXT &&
1638
0
      key->key.ext.decrypt_func2 == NULL &&
1639
0
      key->key.ext.decrypt_func != NULL) {
1640
0
    gnutls_datum_t plain;
1641
0
    int ret;
1642
0
    ret = key->key.ext.decrypt_func(key, key->key.ext.userdata,
1643
0
            ciphertext, &plain);
1644
0
    if (plain.size != plaintext_size) {
1645
0
      ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1646
0
    } else {
1647
0
      memcpy(plaintext, plain.data, plain.size);
1648
0
    }
1649
0
    gnutls_free(plain.data);
1650
0
    return ret;
1651
0
  }
1652
1653
0
  switch (key->type) {
1654
0
  case GNUTLS_PRIVKEY_X509:
1655
0
    return _gnutls_pk_decrypt2(key->pk_algorithm, ciphertext,
1656
0
             plaintext, plaintext_size,
1657
0
             &key->key.x509->params);
1658
#ifdef ENABLE_PKCS11
1659
  case GNUTLS_PRIVKEY_PKCS11:
1660
    return _gnutls_pkcs11_privkey_decrypt_data2(key->key.pkcs11,
1661
                  flags, ciphertext,
1662
                  plaintext,
1663
                  plaintext_size);
1664
#endif
1665
0
  case GNUTLS_PRIVKEY_EXT:
1666
0
    if (key->key.ext.decrypt_func2 == NULL)
1667
0
      return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1668
1669
0
    return key->key.ext.decrypt_func2(key, key->key.ext.userdata,
1670
0
              ciphertext, plaintext,
1671
0
              plaintext_size);
1672
0
  default:
1673
0
    gnutls_assert();
1674
0
    return GNUTLS_E_INVALID_REQUEST;
1675
0
  }
1676
0
}
1677
1678
/**
1679
 * gnutls_privkey_import_x509_raw:
1680
 * @pkey: The private key
1681
 * @data: The private key data to be imported
1682
 * @format: The format of the private key
1683
 * @password: A password (optional)
1684
 * @flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t
1685
 *
1686
 * This function will import the given private key to the abstract
1687
 * #gnutls_privkey_t type. 
1688
 *
1689
 * The supported formats are basic unencrypted key, PKCS8, PKCS12, 
1690
 * TSS2, and the openssl format.
1691
 *
1692
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1693
 *   negative error value.
1694
 *
1695
 * Since: 3.1.0
1696
 **/
1697
int gnutls_privkey_import_x509_raw(gnutls_privkey_t pkey,
1698
           const gnutls_datum_t *data,
1699
           gnutls_x509_crt_fmt_t format,
1700
           const char *password, unsigned int flags)
1701
0
{
1702
0
  gnutls_x509_privkey_t xpriv;
1703
0
  int ret;
1704
1705
#ifdef HAVE_TSS2
1706
  if (format == GNUTLS_X509_FMT_PEM &&
1707
      memmem(data->data, data->size, "--BEGIN TSS2", 12) != NULL) {
1708
    ret = _gnutls_load_tpm2_key(pkey, data);
1709
    if (ret < 0)
1710
      return gnutls_assert_val(ret);
1711
1712
    return 0;
1713
  }
1714
#endif
1715
1716
0
  ret = gnutls_x509_privkey_init(&xpriv);
1717
0
  if (ret < 0)
1718
0
    return gnutls_assert_val(ret);
1719
1720
0
  if (pkey->pin.cb) {
1721
0
    gnutls_x509_privkey_set_pin_function(xpriv, pkey->pin.cb,
1722
0
                 pkey->pin.data);
1723
0
  }
1724
1725
0
  ret = gnutls_x509_privkey_import2(xpriv, data, format, password, flags);
1726
0
  if (ret < 0) {
1727
0
    gnutls_assert();
1728
0
    goto cleanup;
1729
0
  }
1730
1731
0
  ret = gnutls_privkey_import_x509(pkey, xpriv,
1732
0
           GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
1733
0
  if (ret < 0) {
1734
0
    gnutls_assert();
1735
0
    goto cleanup;
1736
0
  }
1737
1738
0
  return 0;
1739
1740
0
cleanup:
1741
0
  gnutls_x509_privkey_deinit(xpriv);
1742
1743
0
  return ret;
1744
0
}
1745
1746
/**
1747
 * gnutls_privkey_import_url:
1748
 * @key: A key of type #gnutls_privkey_t
1749
 * @url: A PKCS 11 url
1750
 * @flags: should be zero
1751
 *
1752
 * This function will import a PKCS11 or TPM URL as a
1753
 * private key. The supported URL types can be checked
1754
 * using gnutls_url_is_supported().
1755
 *
1756
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1757
 *   negative error value.
1758
 *
1759
 * Since: 3.1.0
1760
 **/
1761
int gnutls_privkey_import_url(gnutls_privkey_t key, const char *url,
1762
            unsigned int flags)
1763
0
{
1764
0
  unsigned i;
1765
0
  int ret;
1766
1767
0
  for (i = 0; i < _gnutls_custom_urls_size; i++) {
1768
0
    if (strncmp(url, _gnutls_custom_urls[i].name,
1769
0
          _gnutls_custom_urls[i].name_size) == 0) {
1770
0
      if (_gnutls_custom_urls[i].import_key) {
1771
0
        ret = _gnutls_custom_urls[i].import_key(
1772
0
          key, url, flags);
1773
0
        goto cleanup;
1774
0
      }
1775
0
      break;
1776
0
    }
1777
0
  }
1778
1779
0
  if (strncmp(url, PKCS11_URL, PKCS11_URL_SIZE) == 0) {
1780
#ifdef ENABLE_PKCS11
1781
    ret = _gnutls_privkey_import_pkcs11_url(key, url, flags);
1782
#else
1783
0
    ret = gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);
1784
0
#endif
1785
0
    goto cleanup;
1786
0
  }
1787
1788
0
  if (strncmp(url, TPMKEY_URL, TPMKEY_URL_SIZE) == 0) {
1789
#ifdef HAVE_TROUSERS
1790
    ret = gnutls_privkey_import_tpm_url(key, url, NULL, NULL, 0);
1791
#else
1792
0
    ret = gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);
1793
0
#endif
1794
0
    goto cleanup;
1795
0
  }
1796
1797
0
  if (strncmp(url, SYSTEM_URL, SYSTEM_URL_SIZE) == 0) {
1798
0
    ret = _gnutls_privkey_import_system_url(key, url);
1799
0
    goto cleanup;
1800
0
  }
1801
1802
0
  ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
1803
0
cleanup:
1804
0
  return ret;
1805
0
}
1806
1807
/**
1808
 * gnutls_privkey_set_pin_function:
1809
 * @key: A key of type #gnutls_privkey_t
1810
 * @fn: the callback
1811
 * @userdata: data associated with the callback
1812
 *
1813
 * This function will set a callback function to be used when
1814
 * required to access the object. This function overrides any other
1815
 * global PIN functions.
1816
 *
1817
 * Note that this function must be called right after initialization
1818
 * to have effect.
1819
 *
1820
 * Since: 3.1.0
1821
 *
1822
 **/
1823
void gnutls_privkey_set_pin_function(gnutls_privkey_t key,
1824
             gnutls_pin_callback_t fn, void *userdata)
1825
0
{
1826
0
  key->pin.cb = fn;
1827
0
  key->pin.data = userdata;
1828
0
}
1829
1830
/**
1831
 * gnutls_privkey_set_flags:
1832
 * @key: A key of type #gnutls_privkey_t
1833
 * @flags: flags from the %gnutls_privkey_flags
1834
 *
1835
 * This function will set flags for the specified private key, after
1836
 * it is generated. Currently this is useful for the %GNUTLS_PRIVKEY_FLAG_EXPORT_COMPAT
1837
 * to allow exporting a "provable" private key in backwards compatible way.
1838
 *
1839
 * Since: 3.5.0
1840
 *
1841
 **/
1842
void gnutls_privkey_set_flags(gnutls_privkey_t key, unsigned int flags)
1843
0
{
1844
0
  key->flags |= flags;
1845
0
  if (key->type == GNUTLS_PRIVKEY_X509)
1846
0
    gnutls_x509_privkey_set_flags(key->key.x509, flags);
1847
0
}
1848
1849
/**
1850
 * gnutls_privkey_status:
1851
 * @key: Holds the key
1852
 *
1853
 * Checks the status of the private key token. This function
1854
 * is an actual wrapper over gnutls_pkcs11_privkey_status(), and
1855
 * if the private key is a PKCS #11 token it will check whether
1856
 * it is inserted or not.
1857
 *
1858
 * Returns: this function will return non-zero if the token 
1859
 * holding the private key is still available (inserted), and zero otherwise.
1860
 * 
1861
 * Since: 3.1.10
1862
 *
1863
 **/
1864
int gnutls_privkey_status(gnutls_privkey_t key)
1865
0
{
1866
0
  switch (key->type) {
1867
#ifdef ENABLE_PKCS11
1868
  case GNUTLS_PRIVKEY_PKCS11:
1869
    return gnutls_pkcs11_privkey_status(key->key.pkcs11);
1870
#endif
1871
0
  default:
1872
0
    return 1;
1873
0
  }
1874
0
}
1875
1876
/**
1877
 * gnutls_privkey_verify_params:
1878
 * @key: should contain a #gnutls_privkey_t type
1879
 *
1880
 * This function will verify the private key parameters.
1881
 *
1882
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1883
 *   negative error value.
1884
 *
1885
 * Since: 3.3.0
1886
 **/
1887
int gnutls_privkey_verify_params(gnutls_privkey_t key)
1888
0
{
1889
0
  gnutls_pk_params_st params;
1890
0
  int ret;
1891
1892
0
  gnutls_pk_params_init(&params);
1893
1894
0
  ret = _gnutls_privkey_get_mpis(key, &params);
1895
0
  if (ret < 0)
1896
0
    return gnutls_assert_val(ret);
1897
1898
0
  ret = _gnutls_pk_verify_priv_params(key->pk_algorithm, &params);
1899
1900
0
  gnutls_pk_params_release(&params);
1901
1902
0
  if (ret < 0) {
1903
0
    gnutls_assert();
1904
0
    return ret;
1905
0
  }
1906
1907
0
  return 0;
1908
0
}
1909
1910
/**
1911
 * gnutls_privkey_get_spki:
1912
 * @privkey: a public key of type #gnutls_privkey_t
1913
 * @spki: a SubjectPublicKeyInfo structure of type #gnutls_privkey_spki_t
1914
 * @flags: must be zero
1915
 *
1916
 * This function will return the public key information if available.
1917
 * The provided @spki must be initialized.
1918
 *
1919
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1920
 *   negative error value.
1921
 *
1922
 * Since: 3.6.0
1923
 **/
1924
int gnutls_privkey_get_spki(gnutls_privkey_t privkey, gnutls_x509_spki_t spki,
1925
          unsigned int flags)
1926
0
{
1927
0
  gnutls_x509_spki_t p = &privkey->key.x509->params.spki;
1928
1929
0
  if (privkey == NULL || privkey->type != GNUTLS_PRIVKEY_X509) {
1930
0
    gnutls_assert();
1931
0
    return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
1932
0
  }
1933
1934
0
  if (p->pk == GNUTLS_PK_UNKNOWN)
1935
0
    return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
1936
1937
0
  return _gnutls_x509_spki_copy(spki, p);
1938
0
}
1939
1940
/**
1941
 * gnutls_privkey_set_spki:
1942
 * @privkey: a public key of type #gnutls_privkey_t
1943
 * @spki: a SubjectPublicKeyInfo structure of type #gnutls_privkey_spki_t
1944
 * @flags: must be zero
1945
 *
1946
 * This function will set the public key information.
1947
 * The provided @spki must be initialized.
1948
 *
1949
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1950
 *   negative error value.
1951
 *
1952
 * Since: 3.6.0
1953
 **/
1954
int gnutls_privkey_set_spki(gnutls_privkey_t privkey,
1955
          const gnutls_x509_spki_t spki, unsigned int flags)
1956
0
{
1957
0
  if (privkey == NULL || privkey->type != GNUTLS_PRIVKEY_X509) {
1958
0
    gnutls_assert();
1959
0
    return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
1960
0
  }
1961
1962
0
  return gnutls_x509_privkey_set_spki(privkey->key.x509, spki, flags);
1963
0
}
1964
1965
/* Checks whether the public key given is compatible with the
1966
 * signature algorithm used. The session is only used for audit logging, and
1967
 * it may be null.
1968
 */
1969
unsigned _gnutls_privkey_compatible_with_sig(gnutls_privkey_t privkey,
1970
               gnutls_sign_algorithm_t sign)
1971
0
{
1972
0
  const gnutls_sign_entry_st *se;
1973
1974
0
  if (unlikely(privkey == NULL))
1975
0
    return gnutls_assert_val(0);
1976
1977
0
  se = _gnutls_sign_to_entry(sign);
1978
0
  if (unlikely(se == NULL))
1979
0
    return gnutls_assert_val(0);
1980
1981
  /* Prevent RSA-PSS private keys from negotiating an RSA signature,
1982
   * and RSA keys which cannot do RSA-PSS (e.g., smart card) from
1983
   * negotiating RSA-PSS sig.
1984
   */
1985
1986
0
  if (se->pk !=
1987
0
      privkey->pk_algorithm) { /* if the PK algorithm of the signature differs to the one on the pubkey */
1988
0
    if (!sign_supports_priv_pk_algorithm(se,
1989
0
                 privkey->pk_algorithm)) {
1990
0
      _gnutls_handshake_log(
1991
0
        "cannot use privkey of %s with %s\n",
1992
0
        gnutls_pk_get_name(privkey->pk_algorithm),
1993
0
        se->name);
1994
0
      return 0;
1995
0
    }
1996
0
  }
1997
1998
0
  if (privkey->type == GNUTLS_PRIVKEY_EXT) {
1999
0
    if (privkey->key.ext.info_func) {
2000
0
      int ret;
2001
2002
0
      ret = privkey->key.ext.info_func(
2003
0
        privkey,
2004
0
        GNUTLS_SIGN_ALGO_TO_FLAGS(sign) |
2005
0
          GNUTLS_PRIVKEY_INFO_HAVE_SIGN_ALGO,
2006
0
        privkey->key.ext.userdata);
2007
0
      if (ret != -1)
2008
0
        return ret;
2009
2010
      /* use the old flag */
2011
0
      ret = privkey->key.ext.info_func(
2012
0
        privkey, GNUTLS_PRIVKEY_INFO_SIGN_ALGO,
2013
0
        privkey->key.ext.userdata);
2014
0
      if (ret == (int)sign)
2015
0
        return 1;
2016
0
    }
2017
2018
    /* This key type is very limited on what it can handle */
2019
0
    if (!PK_IS_OK_FOR_EXT2(se->pk))
2020
0
      return gnutls_assert_val(0);
2021
0
  }
2022
#ifdef ENABLE_PKCS11
2023
  else if (privkey->type == GNUTLS_PRIVKEY_PKCS11) {
2024
    if (privkey->pk_algorithm == GNUTLS_PK_RSA &&
2025
        se->pk == GNUTLS_PK_RSA_PSS) {
2026
      if (!privkey->key.pkcs11->rsa_pss_ok)
2027
        return 0;
2028
    }
2029
  }
2030
#endif
2031
2032
0
  return 1;
2033
0
}
2034
2035
/**
2036
 * gnutls_privkey_derive_secret:
2037
 * @privkey: a private key of type #gnutls_privkey_t
2038
 * @pubkey: a public key of type #gnutls_pubkey_t
2039
 * @nonce: an optional nonce value
2040
 * @secret: where shared secret will be stored
2041
 * @flags: must be zero
2042
 *
2043
 * This function will calculate a shared secret from our @privkey and
2044
 * peer's @pubkey. The result will be stored in @secret, whose data
2045
 * member should be freed after use using gnutls_free(). @privkey and
2046
 * @pubkey must be backed by the X.509 keys.
2047
 *
2048
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
2049
 *   negative error value.
2050
 *
2051
 * Since: 3.8.2
2052
 **/
2053
int gnutls_privkey_derive_secret(gnutls_privkey_t privkey,
2054
         gnutls_pubkey_t pubkey,
2055
         const gnutls_datum_t *nonce,
2056
         gnutls_datum_t *secret, unsigned int flags)
2057
0
{
2058
0
  if (unlikely(privkey == NULL || privkey->type != GNUTLS_PRIVKEY_X509)) {
2059
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
2060
0
  }
2061
2062
0
  if (unlikely(pubkey == NULL ||
2063
0
         pubkey->params.algo != privkey->pk_algorithm)) {
2064
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
2065
0
  }
2066
2067
0
  return _gnutls_pk_derive_nonce(privkey->pk_algorithm, secret,
2068
0
               &privkey->key.x509->params,
2069
0
               &pubkey->params, nonce);
2070
0
}