Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/x509/pkcs7-crypt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2003-2016 Free Software Foundation, Inc.
3
 * Copyright (C) 2014-2016 Red Hat
4
 * Copyright (C) 2014-2016 Nikos Mavrogiannopoulos
5
 *
6
 * Author: Nikos Mavrogiannopoulos
7
 *
8
 * This file is part of GnuTLS.
9
 *
10
 * The GnuTLS is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public License
12
 * as published by the Free Software Foundation; either version 2.1 of
13
 * the License, or (at your option) any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
22
 *
23
 */
24
25
#include "gnutls_int.h"
26
27
#include "datum.h"
28
#include "global.h"
29
#include "errors.h"
30
#include "common.h"
31
#include "x509.h"
32
#include "x509_b64.h"
33
#include "x509_int.h"
34
#include "pkcs7_int.h"
35
#include "algorithms.h"
36
#include "num.h"
37
#include "random.h"
38
#include "pk.h"
39
40
#define PBES1_DES_MD5_OID "1.2.840.113549.1.5.3"
41
42
0
#define PBES2_OID "1.2.840.113549.1.5.13"
43
0
#define PBKDF2_OID "1.2.840.113549.1.5.12"
44
#define DES_EDE3_CBC_OID "1.2.840.113549.3.7"
45
#define AES_128_CBC_OID "2.16.840.1.101.3.4.1.2"
46
#define AES_192_CBC_OID "2.16.840.1.101.3.4.1.22"
47
#define AES_256_CBC_OID "2.16.840.1.101.3.4.1.42"
48
#define DES_CBC_OID "1.3.14.3.2.7"
49
50
/* oid_pbeWithSHAAnd3_KeyTripleDES_CBC */
51
#define PKCS12_PBE_3DES_SHA1_OID "1.2.840.113549.1.12.1.3"
52
#define PKCS12_PBE_ARCFOUR_SHA1_OID "1.2.840.113549.1.12.1.1"
53
#define PKCS12_PBE_RC2_40_SHA1_OID "1.2.840.113549.1.12.1.6"
54
55
static const struct pkcs_cipher_schema_st avail_pkcs_cipher_schemas[] = {
56
  { .schema = PBES1_DES_MD5,
57
    .name = "PBES1-DES-CBC-MD5",
58
    .flag = GNUTLS_PKCS_PBES1_DES_MD5,
59
    .cipher = GNUTLS_CIPHER_DES_CBC,
60
    .pbes2 = 0,
61
    .cipher_oid = PBES1_DES_MD5_OID,
62
    .write_oid = PBES1_DES_MD5_OID,
63
    .desc = NULL,
64
    .iv_name = NULL,
65
    .decrypt_only = 1 },
66
  { .schema = PBES2_3DES,
67
    .name = "PBES2-3DES-CBC",
68
    .flag = GNUTLS_PKCS_PBES2_3DES,
69
    .cipher = GNUTLS_CIPHER_3DES_CBC,
70
    .pbes2 = 1,
71
    .cipher_oid = DES_EDE3_CBC_OID,
72
    .write_oid = PBES2_OID,
73
    .desc = "PKIX1.pkcs-5-des-EDE3-CBC-params",
74
    .iv_name = "",
75
    .decrypt_only = 0 },
76
  { .schema = PBES2_DES,
77
    .name = "PBES2-DES-CBC",
78
    .flag = GNUTLS_PKCS_PBES2_DES,
79
    .cipher = GNUTLS_CIPHER_DES_CBC,
80
    .pbes2 = 1,
81
    .cipher_oid = DES_CBC_OID,
82
    .write_oid = PBES2_OID,
83
    .desc = "PKIX1.pkcs-5-des-CBC-params",
84
    .iv_name = "",
85
    .decrypt_only = 0 },
86
  { .schema = PBES2_AES_128,
87
    .name = "PBES2-AES128-CBC",
88
    .flag = GNUTLS_PKCS_PBES2_AES_128,
89
    .cipher = GNUTLS_CIPHER_AES_128_CBC,
90
    .pbes2 = 1,
91
    .cipher_oid = AES_128_CBC_OID,
92
    .write_oid = PBES2_OID,
93
    .desc = "PKIX1.pkcs-5-aes128-CBC-params",
94
    .iv_name = "",
95
    .decrypt_only = 0 },
96
  { .schema = PBES2_AES_192,
97
    .name = "PBES2-AES192-CBC",
98
    .flag = GNUTLS_PKCS_PBES2_AES_192,
99
    .cipher = GNUTLS_CIPHER_AES_192_CBC,
100
    .pbes2 = 1,
101
    .cipher_oid = AES_192_CBC_OID,
102
    .write_oid = PBES2_OID,
103
    .desc = "PKIX1.pkcs-5-aes192-CBC-params",
104
    .iv_name = "",
105
    .decrypt_only = 0 },
106
  { .schema = PBES2_AES_256,
107
    .name = "PBES2-AES256-CBC",
108
    .flag = GNUTLS_PKCS_PBES2_AES_256,
109
    .cipher = GNUTLS_CIPHER_AES_256_CBC,
110
    .pbes2 = 1,
111
    .cipher_oid = AES_256_CBC_OID,
112
    .write_oid = PBES2_OID,
113
    .desc = "PKIX1.pkcs-5-aes256-CBC-params",
114
    .iv_name = "",
115
    .decrypt_only = 0 },
116
  { .schema = PBES2_GOST28147_89_TC26Z,
117
    .name = "PBES2-GOST28147-89-TC26Z",
118
    .flag = GNUTLS_PKCS_PBES2_GOST_TC26Z,
119
    .cipher = GNUTLS_CIPHER_GOST28147_TC26Z_CFB,
120
    .pbes2 = 1,
121
    .cipher_oid = GOST28147_89_TC26Z_OID,
122
    .write_oid = PBES2_OID,
123
    .desc = "PKIX1.Gost28147-89-Parameters",
124
    .iv_name = "iv",
125
    .decrypt_only = 0 },
126
  { .schema = PBES2_GOST28147_89_CPA,
127
    .name = "PBES2-GOST28147-89-CPA",
128
    .flag = GNUTLS_PKCS_PBES2_GOST_CPA,
129
    .cipher = GNUTLS_CIPHER_GOST28147_CPA_CFB,
130
    .pbes2 = 1,
131
    .cipher_oid = GOST28147_89_CPA_OID,
132
    .write_oid = PBES2_OID,
133
    .desc = "PKIX1.Gost28147-89-Parameters",
134
    .iv_name = "iv",
135
    .decrypt_only = 0 },
136
  { .schema = PBES2_GOST28147_89_CPB,
137
    .name = "PBES2-GOST28147-89-CPB",
138
    .flag = GNUTLS_PKCS_PBES2_GOST_CPB,
139
    .cipher = GNUTLS_CIPHER_GOST28147_CPB_CFB,
140
    .pbes2 = 1,
141
    .cipher_oid = GOST28147_89_CPB_OID,
142
    .write_oid = PBES2_OID,
143
    .desc = "PKIX1.Gost28147-89-Parameters",
144
    .iv_name = "iv",
145
    .decrypt_only = 0 },
146
  { .schema = PBES2_GOST28147_89_CPC,
147
    .name = "PBES2-GOST28147-89-CPC",
148
    .flag = GNUTLS_PKCS_PBES2_GOST_CPC,
149
    .cipher = GNUTLS_CIPHER_GOST28147_CPC_CFB,
150
    .pbes2 = 1,
151
    .cipher_oid = GOST28147_89_CPC_OID,
152
    .write_oid = PBES2_OID,
153
    .desc = "PKIX1.Gost28147-89-Parameters",
154
    .iv_name = "iv",
155
    .decrypt_only = 0 },
156
  { .schema = PBES2_GOST28147_89_CPD,
157
    .name = "PBES2-GOST28147-89-CPD",
158
    .flag = GNUTLS_PKCS_PBES2_GOST_CPD,
159
    .cipher = GNUTLS_CIPHER_GOST28147_CPD_CFB,
160
    .pbes2 = 1,
161
    .cipher_oid = GOST28147_89_CPD_OID,
162
    .write_oid = PBES2_OID,
163
    .desc = "PKIX1.Gost28147-89-Parameters",
164
    .iv_name = "iv",
165
    .decrypt_only = 0 },
166
  { .schema = PKCS12_ARCFOUR_SHA1,
167
    .name = "PKCS12-ARCFOUR-SHA1",
168
    .flag = GNUTLS_PKCS_PKCS12_ARCFOUR,
169
    .cipher = GNUTLS_CIPHER_ARCFOUR,
170
    .pbes2 = 0,
171
    .cipher_oid = PKCS12_PBE_ARCFOUR_SHA1_OID,
172
    .write_oid = PKCS12_PBE_ARCFOUR_SHA1_OID,
173
    .desc = NULL,
174
    .iv_name = NULL,
175
    .decrypt_only = 0 },
176
  { .schema = PKCS12_RC2_40_SHA1,
177
    .name = "PKCS12-RC2-40-SHA1",
178
    .flag = GNUTLS_PKCS_PKCS12_RC2_40,
179
    .cipher = GNUTLS_CIPHER_RC2_40_CBC,
180
    .pbes2 = 0,
181
    .cipher_oid = PKCS12_PBE_RC2_40_SHA1_OID,
182
    .write_oid = PKCS12_PBE_RC2_40_SHA1_OID,
183
    .desc = NULL,
184
    .iv_name = NULL,
185
    .decrypt_only = 0 },
186
  { .schema = PKCS12_3DES_SHA1,
187
    .name = "PKCS12-3DES-SHA1",
188
    .flag = GNUTLS_PKCS_PKCS12_3DES,
189
    .cipher = GNUTLS_CIPHER_3DES_CBC,
190
    .pbes2 = 0,
191
    .cipher_oid = PKCS12_PBE_3DES_SHA1_OID,
192
    .write_oid = PKCS12_PBE_3DES_SHA1_OID,
193
    .desc = NULL,
194
    .iv_name = NULL,
195
    .decrypt_only = 0 },
196
  { 0, 0, 0, 0, 0 }
197
};
198
199
#define PBES2_SCHEMA_LOOP(b)                                                  \
200
0
  {                                                                     \
201
0
    const struct pkcs_cipher_schema_st *_p;                       \
202
0
    for (_p = avail_pkcs_cipher_schemas; _p->schema != 0; _p++) { \
203
0
      b;                                                    \
204
0
    }                                                             \
205
0
  }
206
207
#define PBES2_SCHEMA_FIND_FROM_FLAGS(fl, what) \
208
0
  PBES2_SCHEMA_LOOP(                     \
209
0
    if (_p->flag == GNUTLS_PKCS_CIPHER_MASK(fl)) { what; })
210
211
int _gnutls_pkcs_flags_to_schema(unsigned int flags)
212
0
{
213
0
  PBES2_SCHEMA_FIND_FROM_FLAGS(flags, return _p->schema;);
214
215
0
  gnutls_assert();
216
0
  _gnutls_debug_log(
217
0
    "Selecting default encryption PBES2_AES_256 (flags: %u).\n",
218
0
    flags);
219
0
  return PBES2_AES_256;
220
0
}
221
222
/**
223
 * gnutls_pkcs_schema_get_name:
224
 * @schema: Holds the PKCS #12 or PBES2 schema (%gnutls_pkcs_encrypt_flags_t)
225
 *
226
 * This function will return a human readable description of the
227
 * PKCS12 or PBES2 schema.
228
 *
229
 * Returns: a constraint string or %NULL on error.
230
 *
231
 * Since: 3.4.0
232
 */
233
const char *gnutls_pkcs_schema_get_name(unsigned int schema)
234
0
{
235
0
  PBES2_SCHEMA_FIND_FROM_FLAGS(schema, return _p->name;);
236
0
  return NULL;
237
0
}
238
239
/**
240
 * gnutls_pkcs_schema_get_oid:
241
 * @schema: Holds the PKCS #12 or PBES2 schema (%gnutls_pkcs_encrypt_flags_t)
242
 *
243
 * This function will return the object identifier of the
244
 * PKCS12 or PBES2 schema.
245
 *
246
 * Returns: a constraint string or %NULL on error.
247
 *
248
 * Since: 3.4.0
249
 */
250
const char *gnutls_pkcs_schema_get_oid(unsigned int schema)
251
0
{
252
0
  PBES2_SCHEMA_FIND_FROM_FLAGS(schema, return _p->cipher_oid;);
253
0
  return NULL;
254
0
}
255
256
static const struct pkcs_cipher_schema_st *
257
algo_to_pbes2_cipher_schema(unsigned cipher)
258
0
{
259
0
  PBES2_SCHEMA_LOOP(
260
0
    if (_p->cipher == cipher && _p->pbes2 != 0) { return _p; });
261
262
0
  gnutls_assert();
263
0
  return NULL;
264
0
}
265
266
/* Converts a PKCS#7 encryption schema OID to an internal
267
 * schema_id or returns a negative value */
268
int _gnutls_check_pkcs_cipher_schema(const char *oid)
269
0
{
270
0
  if (strcmp(oid, PBES2_OID) == 0)
271
0
    return PBES2_GENERIC; /* PBES2 ciphers are under an umbrella OID */
272
273
0
  PBES2_SCHEMA_LOOP(
274
0
    if (_p->pbes2 == 0 && strcmp(oid, _p->write_oid) == 0) {
275
0
      return _p->schema;
276
0
    });
277
0
  _gnutls_debug_log(
278
0
    "PKCS #12 encryption schema OID '%s' is unsupported.\n", oid);
279
280
0
  return GNUTLS_E_UNKNOWN_CIPHER_TYPE;
281
0
}
282
283
const struct pkcs_cipher_schema_st *_gnutls_pkcs_schema_get(schema_id schema)
284
0
{
285
0
  PBES2_SCHEMA_LOOP(if (schema == _p->schema) return _p;);
286
287
0
  gnutls_assert();
288
0
  return NULL;
289
0
}
290
291
/* Converts an OID to a gnutls cipher type.
292
 */
293
static int pbes2_cipher_oid_to_algo(const char *oid,
294
            gnutls_cipher_algorithm_t *algo)
295
0
{
296
0
  *algo = 0;
297
0
  PBES2_SCHEMA_LOOP(
298
0
    if (_p->pbes2 != 0 && strcmp(_p->cipher_oid, oid) == 0) {
299
0
      *algo = _p->cipher;
300
0
      return 0;
301
0
    });
302
303
0
  _gnutls_debug_log("PKCS #8 encryption OID '%s' is unsupported.\n", oid);
304
0
  return GNUTLS_E_UNKNOWN_CIPHER_TYPE;
305
0
}
306
307
/* Decrypts a PKCS #7 encryptedData. The output is allocated
308
 * and stored in dec.
309
 */
310
int _gnutls_pkcs7_decrypt_data(const gnutls_datum_t *data, const char *password,
311
             gnutls_datum_t *dec)
312
0
{
313
0
  int result, len;
314
0
  char enc_oid[MAX_OID_SIZE];
315
0
  gnutls_datum_t tmp;
316
0
  asn1_node pasn = NULL, pkcs7_asn = NULL;
317
0
  int params_start, params_end, params_len;
318
0
  struct pbkdf2_params kdf_params;
319
0
  struct pbe_enc_params enc_params;
320
0
  schema_id schema;
321
322
0
  if ((result = asn1_create_element(_gnutls_get_pkix(),
323
0
            "PKIX1.pkcs-7-EncryptedData",
324
0
            &pkcs7_asn)) != ASN1_SUCCESS) {
325
0
    gnutls_assert();
326
0
    result = _gnutls_asn2err(result);
327
0
    goto error;
328
0
  }
329
330
0
  result = asn1_der_decoding(&pkcs7_asn, data->data, data->size, NULL);
331
0
  if (result != ASN1_SUCCESS) {
332
0
    gnutls_assert();
333
0
    result = _gnutls_asn2err(result);
334
0
    goto error;
335
0
  }
336
337
  /* Check the encryption schema OID
338
   */
339
0
  len = sizeof(enc_oid);
340
0
  result = asn1_read_value(
341
0
    pkcs7_asn,
342
0
    "encryptedContentInfo.contentEncryptionAlgorithm.algorithm",
343
0
    enc_oid, &len);
344
0
  if (result != ASN1_SUCCESS) {
345
0
    gnutls_assert();
346
0
    result = _gnutls_asn2err(result);
347
0
    goto error;
348
0
  }
349
350
0
  if ((result = _gnutls_check_pkcs_cipher_schema(enc_oid)) < 0) {
351
0
    gnutls_assert();
352
0
    goto error;
353
0
  }
354
0
  schema = result;
355
356
  /* Get the DER encoding of the parameters.
357
   */
358
0
  result = asn1_der_decoding_startEnd(
359
0
    pkcs7_asn, data->data, data->size,
360
0
    "encryptedContentInfo.contentEncryptionAlgorithm.parameters",
361
0
    &params_start, &params_end);
362
0
  if (result != ASN1_SUCCESS) {
363
0
    gnutls_assert();
364
0
    result = _gnutls_asn2err(result);
365
0
    goto error;
366
0
  }
367
0
  params_len = params_end - params_start + 1;
368
369
0
  result = _gnutls_read_pkcs_schema_params(&schema, password,
370
0
             &data->data[params_start],
371
0
             params_len, &kdf_params,
372
0
             &enc_params);
373
0
  if (result < 0) {
374
0
    gnutls_assert();
375
0
    goto error;
376
0
  }
377
378
  /* Parameters have been decoded. Now
379
   * decrypt the EncryptedData.
380
   */
381
382
0
  result = _gnutls_pkcs_raw_decrypt_data(
383
0
    schema, pkcs7_asn, "encryptedContentInfo.encryptedContent",
384
0
    password, &kdf_params, &enc_params, &tmp);
385
0
  if (result < 0) {
386
0
    gnutls_assert();
387
0
    goto error;
388
0
  }
389
390
0
  asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE);
391
392
0
  *dec = tmp;
393
394
0
  return 0;
395
396
0
error:
397
0
  asn1_delete_structure(&pasn);
398
0
  asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE);
399
0
  return result;
400
0
}
401
402
int _gnutls_pkcs7_data_enc_info(const gnutls_datum_t *data,
403
        const struct pkcs_cipher_schema_st **p,
404
        struct pbkdf2_params *kdf_params, char **oid)
405
0
{
406
0
  int result, len;
407
0
  char enc_oid[MAX_OID_SIZE];
408
0
  asn1_node pasn = NULL, pkcs7_asn = NULL;
409
0
  int params_start, params_end, params_len;
410
0
  struct pbe_enc_params enc_params;
411
0
  schema_id schema;
412
413
0
  if ((result = asn1_create_element(_gnutls_get_pkix(),
414
0
            "PKIX1.pkcs-7-EncryptedData",
415
0
            &pkcs7_asn)) != ASN1_SUCCESS) {
416
0
    gnutls_assert();
417
0
    result = _gnutls_asn2err(result);
418
0
    goto error;
419
0
  }
420
421
0
  result = asn1_der_decoding(&pkcs7_asn, data->data, data->size, NULL);
422
0
  if (result != ASN1_SUCCESS) {
423
0
    gnutls_assert();
424
0
    result = _gnutls_asn2err(result);
425
0
    goto error;
426
0
  }
427
428
  /* Check the encryption schema OID
429
   */
430
0
  len = sizeof(enc_oid);
431
0
  result = asn1_read_value(
432
0
    pkcs7_asn,
433
0
    "encryptedContentInfo.contentEncryptionAlgorithm.algorithm",
434
0
    enc_oid, &len);
435
0
  if (result != ASN1_SUCCESS) {
436
0
    gnutls_assert();
437
0
    result = _gnutls_asn2err(result);
438
0
    goto error;
439
0
  }
440
441
0
  if (oid) {
442
0
    *oid = gnutls_strdup(enc_oid);
443
0
  }
444
445
0
  if ((result = _gnutls_check_pkcs_cipher_schema(enc_oid)) < 0) {
446
0
    gnutls_assert();
447
0
    goto error;
448
0
  }
449
0
  schema = result;
450
451
  /* Get the DER encoding of the parameters.
452
   */
453
0
  result = asn1_der_decoding_startEnd(
454
0
    pkcs7_asn, data->data, data->size,
455
0
    "encryptedContentInfo.contentEncryptionAlgorithm.parameters",
456
0
    &params_start, &params_end);
457
0
  if (result != ASN1_SUCCESS) {
458
0
    gnutls_assert();
459
0
    result = _gnutls_asn2err(result);
460
0
    goto error;
461
0
  }
462
0
  params_len = params_end - params_start + 1;
463
464
0
  result = _gnutls_read_pkcs_schema_params(&schema, NULL,
465
0
             &data->data[params_start],
466
0
             params_len, kdf_params,
467
0
             &enc_params);
468
0
  if (result < 0) {
469
0
    gnutls_assert();
470
0
    goto error;
471
0
  }
472
473
0
  *p = _gnutls_pkcs_schema_get(schema);
474
0
  if (*p == NULL) {
475
0
    gnutls_assert();
476
0
    result = GNUTLS_E_UNKNOWN_CIPHER_TYPE;
477
0
    goto error;
478
0
  }
479
480
0
  asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE);
481
482
0
  return 0;
483
484
0
error:
485
0
  asn1_delete_structure(&pasn);
486
0
  asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE);
487
0
  return result;
488
0
}
489
490
/* Encrypts to a PKCS #7 encryptedData. The output is allocated
491
 * and stored in enc.
492
 */
493
int _gnutls_pkcs7_encrypt_data(schema_id schema, const gnutls_datum_t *data,
494
             const char *password, gnutls_datum_t *enc)
495
0
{
496
0
  int result;
497
0
  gnutls_datum_t key = { NULL, 0 };
498
0
  gnutls_datum_t tmp = { NULL, 0 };
499
0
  asn1_node pkcs7_asn = NULL;
500
0
  struct pbkdf2_params kdf_params;
501
0
  struct pbe_enc_params enc_params;
502
0
  const struct pkcs_cipher_schema_st *s;
503
504
0
  s = _gnutls_pkcs_schema_get(schema);
505
0
  if (s == NULL || s->decrypt_only) {
506
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
507
0
  }
508
509
0
  if ((result = asn1_create_element(_gnutls_get_pkix(),
510
0
            "PKIX1.pkcs-7-EncryptedData",
511
0
            &pkcs7_asn)) != ASN1_SUCCESS) {
512
0
    gnutls_assert();
513
0
    result = _gnutls_asn2err(result);
514
0
    goto error;
515
0
  }
516
517
0
  result = asn1_write_value(
518
0
    pkcs7_asn,
519
0
    "encryptedContentInfo.contentEncryptionAlgorithm.algorithm",
520
0
    s->write_oid, 1);
521
522
0
  if (result != ASN1_SUCCESS) {
523
0
    gnutls_assert();
524
0
    result = _gnutls_asn2err(result);
525
0
    goto error;
526
0
  }
527
528
  /* Generate a symmetric key.
529
   */
530
531
0
  result = _gnutls_pkcs_generate_key(schema, password, &kdf_params,
532
0
             &enc_params, &key);
533
0
  if (result < 0) {
534
0
    gnutls_assert();
535
0
    goto error;
536
0
  }
537
538
0
  result = _gnutls_pkcs_write_schema_params(
539
0
    schema, pkcs7_asn,
540
0
    "encryptedContentInfo.contentEncryptionAlgorithm.parameters",
541
0
    &kdf_params, &enc_params);
542
0
  if (result < 0) {
543
0
    gnutls_assert();
544
0
    goto error;
545
0
  }
546
547
  /* Parameters have been encoded. Now
548
   * encrypt the Data.
549
   */
550
0
  result = _gnutls_pkcs_raw_encrypt_data(data, &enc_params, &key, &tmp);
551
0
  if (result < 0) {
552
0
    gnutls_assert();
553
0
    goto error;
554
0
  }
555
556
  /* write the encrypted data.
557
   */
558
0
  result = asn1_write_value(pkcs7_asn,
559
0
          "encryptedContentInfo.encryptedContent",
560
0
          tmp.data, tmp.size);
561
0
  if (result != ASN1_SUCCESS) {
562
0
    gnutls_assert();
563
0
    result = _gnutls_asn2err(result);
564
0
    goto error;
565
0
  }
566
567
0
  _gnutls_free_datum(&tmp);
568
0
  _gnutls_free_key_datum(&key);
569
570
  /* Now write the rest of the pkcs-7 stuff.
571
   */
572
573
0
  result = _gnutls_x509_write_uint32(pkcs7_asn, "version", 0);
574
0
  if (result < 0) {
575
0
    gnutls_assert();
576
0
    goto error;
577
0
  }
578
579
0
  result = asn1_write_value(pkcs7_asn, "encryptedContentInfo.contentType",
580
0
          DATA_OID, 1);
581
0
  if (result != ASN1_SUCCESS) {
582
0
    gnutls_assert();
583
0
    result = _gnutls_asn2err(result);
584
0
    goto error;
585
0
  }
586
587
0
  result = asn1_write_value(pkcs7_asn, "unprotectedAttrs", NULL, 0);
588
0
  if (result != ASN1_SUCCESS) {
589
0
    gnutls_assert();
590
0
    result = _gnutls_asn2err(result);
591
0
    goto error;
592
0
  }
593
594
  /* Now encode and copy the DER stuff.
595
   */
596
0
  result = _gnutls_x509_der_encode(pkcs7_asn, "", enc, 0);
597
598
0
  asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE);
599
600
0
  if (result < 0) {
601
0
    gnutls_assert();
602
0
    goto error;
603
0
  }
604
605
0
error:
606
0
  _gnutls_free_key_datum(&key);
607
0
  _gnutls_free_datum(&tmp);
608
0
  asn1_delete_structure2(&pkcs7_asn, ASN1_DELETE_FLAG_ZEROIZE);
609
0
  return result;
610
0
}
611
612
/* Reads the PBKDF2 parameters.
613
 */
614
static int read_pbkdf2_params(asn1_node pasn, const gnutls_datum_t *der,
615
            struct pbkdf2_params *params)
616
0
{
617
0
  int params_start, params_end;
618
0
  int params_len, len, result;
619
0
  asn1_node pbkdf2_asn = NULL;
620
0
  char oid[MAX_OID_SIZE];
621
622
0
  memset(params, 0, sizeof(*params));
623
624
0
  params->mac = GNUTLS_MAC_SHA1;
625
626
  /* Check the key derivation algorithm
627
   */
628
0
  len = sizeof(oid);
629
0
  result =
630
0
    asn1_read_value(pasn, "keyDerivationFunc.algorithm", oid, &len);
631
0
  if (result != ASN1_SUCCESS) {
632
0
    gnutls_assert();
633
0
    return _gnutls_asn2err(result);
634
0
  }
635
0
  _gnutls_hard_log("keyDerivationFunc.algorithm: %s\n", oid);
636
637
0
  if (strcmp(oid, PBKDF2_OID) != 0) {
638
0
    gnutls_assert();
639
0
    _gnutls_debug_log(
640
0
      "PKCS #8 key derivation OID '%s' is unsupported.\n",
641
0
      oid);
642
0
    return _gnutls_asn2err(result);
643
0
  }
644
645
0
  result = asn1_der_decoding_startEnd(pasn, der->data, der->size,
646
0
              "keyDerivationFunc.parameters",
647
0
              &params_start, &params_end);
648
0
  if (result != ASN1_SUCCESS) {
649
0
    gnutls_assert();
650
0
    return _gnutls_asn2err(result);
651
0
  }
652
0
  params_len = params_end - params_start + 1;
653
654
  /* Now check the key derivation and the encryption
655
   * functions.
656
   */
657
0
  if ((result = asn1_create_element(_gnutls_get_pkix(),
658
0
            "PKIX1.pkcs-5-PBKDF2-params",
659
0
            &pbkdf2_asn)) != ASN1_SUCCESS) {
660
0
    gnutls_assert();
661
0
    return _gnutls_asn2err(result);
662
0
  }
663
664
0
  result = _asn1_strict_der_decode(&pbkdf2_asn, &der->data[params_start],
665
0
           params_len, NULL);
666
0
  if (result != ASN1_SUCCESS) {
667
0
    gnutls_assert();
668
0
    result = _gnutls_asn2err(result);
669
0
    goto error;
670
0
  }
671
672
  /* read the salt */
673
0
  params->salt_size = sizeof(params->salt);
674
0
  result = asn1_read_value(pbkdf2_asn, "salt.specified", params->salt,
675
0
         &params->salt_size);
676
0
  if (result != ASN1_SUCCESS) {
677
0
    gnutls_assert();
678
0
    result = _gnutls_asn2err(result);
679
0
    goto error;
680
0
  }
681
0
  _gnutls_hard_log("salt.specified.size: %d\n", params->salt_size);
682
683
0
  if (params->salt_size < 0) {
684
0
    result = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
685
0
    goto error;
686
0
  }
687
688
  /* read the iteration count 
689
   */
690
0
  result = _gnutls_x509_read_uint(pbkdf2_asn, "iterationCount",
691
0
          &params->iter_count);
692
0
  if (result < 0) {
693
0
    gnutls_assert();
694
0
    goto error;
695
0
  }
696
697
0
  if (params->iter_count >= MAX_ITER_COUNT || params->iter_count == 0) {
698
0
    result = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
699
0
    goto error;
700
0
  }
701
702
0
  _gnutls_hard_log("iterationCount: %d\n", params->iter_count);
703
704
  /* read the keylength, if it is set.
705
   */
706
0
  result = _gnutls_x509_read_uint(pbkdf2_asn, "keyLength",
707
0
          &params->key_size);
708
0
  if (result < 0) {
709
0
    params->key_size = 0;
710
0
  }
711
712
0
  if (params->key_size > MAX_CIPHER_KEY_SIZE) {
713
0
    result = gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
714
0
    goto error;
715
0
  }
716
717
0
  _gnutls_hard_log("keyLength: %d\n", params->key_size);
718
719
0
  len = sizeof(oid);
720
0
  result = asn1_read_value(pbkdf2_asn, "prf.algorithm", oid, &len);
721
0
  if (result != ASN1_SUCCESS) {
722
    /* use the default MAC */
723
0
    result = 0;
724
0
    goto error;
725
0
  }
726
727
0
  params->mac = gnutls_oid_to_mac(oid);
728
0
  if (params->mac == GNUTLS_MAC_UNKNOWN) {
729
0
    gnutls_assert();
730
0
    _gnutls_debug_log("Unsupported hash algorithm: %s\n", oid);
731
0
    result = GNUTLS_E_UNKNOWN_HASH_ALGORITHM;
732
0
    goto error;
733
0
  }
734
735
0
  result = 0;
736
737
0
error:
738
0
  asn1_delete_structure(&pbkdf2_asn);
739
0
  return result;
740
0
}
741
742
/* Reads the PBE parameters from PKCS-12 schemas (*&#%*&#% RSA).
743
 */
744
static int read_pkcs12_kdf_params(asn1_node pasn, struct pbkdf2_params *params)
745
0
{
746
0
  int result;
747
748
0
  memset(params, 0, sizeof(*params));
749
750
  /* read the salt */
751
0
  params->salt_size = sizeof(params->salt);
752
0
  result =
753
0
    asn1_read_value(pasn, "salt", params->salt, &params->salt_size);
754
0
  if (result != ASN1_SUCCESS) {
755
0
    gnutls_assert();
756
0
    return _gnutls_asn2err(result);
757
0
  }
758
759
0
  if (params->salt_size < 0)
760
0
    return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
761
762
0
  _gnutls_hard_log("salt.size: %d\n", params->salt_size);
763
764
  /* read the iteration count 
765
   */
766
0
  result =
767
0
    _gnutls_x509_read_uint(pasn, "iterations", &params->iter_count);
768
0
  if (result < 0)
769
0
    return gnutls_assert_val(result);
770
771
0
  if (params->iter_count >= MAX_ITER_COUNT || params->iter_count == 0)
772
0
    return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
773
774
0
  _gnutls_hard_log("iterationCount: %d\n", params->iter_count);
775
776
0
  params->key_size = 0;
777
778
0
  return 0;
779
0
}
780
781
/* Writes the PBE parameters for PKCS-12 schemas.
782
 */
783
static int write_pkcs12_kdf_params(asn1_node pasn,
784
           const struct pbkdf2_params *kdf_params)
785
0
{
786
0
  int result;
787
788
  /* write the salt 
789
   */
790
0
  result = asn1_write_value(pasn, "salt", kdf_params->salt,
791
0
          kdf_params->salt_size);
792
0
  if (result != ASN1_SUCCESS) {
793
0
    gnutls_assert();
794
0
    result = _gnutls_asn2err(result);
795
0
    goto error;
796
0
  }
797
0
  _gnutls_hard_log("salt.size: %d\n", kdf_params->salt_size);
798
799
  /* write the iteration count 
800
   */
801
0
  result = _gnutls_x509_write_uint32(pasn, "iterations",
802
0
             kdf_params->iter_count);
803
0
  if (result < 0) {
804
0
    gnutls_assert();
805
0
    goto error;
806
0
  }
807
0
  _gnutls_hard_log("iterationCount: %d\n", kdf_params->iter_count);
808
809
0
  return 0;
810
811
0
error:
812
0
  return result;
813
0
}
814
815
static int read_pbes2_gost_oid(uint8_t *der, size_t len, char *oid,
816
             int oid_size)
817
0
{
818
0
  int result;
819
0
  asn1_node pbe_asn = NULL;
820
821
0
  if ((result = asn1_create_element(_gnutls_get_pkix(),
822
0
            "PKIX1.Gost28147-89-Parameters",
823
0
            &pbe_asn)) != ASN1_SUCCESS) {
824
0
    gnutls_assert();
825
0
    return _gnutls_asn2err(result);
826
0
  }
827
828
0
  result = _asn1_strict_der_decode(&pbe_asn, der, len, NULL);
829
0
  if (result != ASN1_SUCCESS) {
830
0
    gnutls_assert();
831
0
    result = _gnutls_asn2err(result);
832
0
    goto error;
833
0
  }
834
835
0
  result = asn1_read_value(pbe_asn, "encryptionParamSet", oid, &oid_size);
836
0
  if (result != ASN1_SUCCESS) {
837
0
    gnutls_assert();
838
0
    result = _gnutls_asn2err(result);
839
0
    goto error;
840
0
  }
841
842
0
  result = 0;
843
844
0
error:
845
0
  asn1_delete_structure(&pbe_asn);
846
0
  return result;
847
0
}
848
849
static int read_pbes2_enc_params(asn1_node pasn, const gnutls_datum_t *der,
850
         struct pbe_enc_params *params)
851
0
{
852
0
  int params_start, params_end;
853
0
  int params_len, len, result;
854
0
  asn1_node pbe_asn = NULL;
855
0
  const struct pkcs_cipher_schema_st *p;
856
857
0
  memset(params, 0, sizeof(*params));
858
859
  /* Check the encryption algorithm
860
   */
861
0
  len = sizeof(params->pbes2_oid);
862
0
  result = asn1_read_value(pasn, "encryptionScheme.algorithm",
863
0
         params->pbes2_oid, &len);
864
0
  if (result != ASN1_SUCCESS) {
865
0
    gnutls_assert();
866
0
    return _gnutls_asn2err(result);
867
0
  }
868
0
  _gnutls_hard_log("encryptionScheme.algorithm: %s\n", params->pbes2_oid);
869
870
0
  result = asn1_der_decoding_startEnd(pasn, der->data, der->size,
871
0
              "encryptionScheme.parameters",
872
0
              &params_start, &params_end);
873
0
  if (result != ASN1_SUCCESS) {
874
0
    gnutls_assert();
875
0
    return _gnutls_asn2err(result);
876
0
  }
877
0
  params_len = params_end - params_start + 1;
878
879
  /* For GOST we have to read params to determine actual cipher */
880
0
  if (!strcmp(params->pbes2_oid, GOST28147_89_OID)) {
881
0
    len = sizeof(params->pbes2_oid);
882
0
    result = read_pbes2_gost_oid(&der->data[params_start],
883
0
               params_len, params->pbes2_oid,
884
0
               len);
885
0
    if (result < 0) {
886
0
      gnutls_assert();
887
0
      return result;
888
0
    }
889
0
  }
890
891
0
  if ((result = pbes2_cipher_oid_to_algo(params->pbes2_oid,
892
0
                 &params->cipher)) < 0) {
893
0
    gnutls_assert();
894
0
    return result;
895
0
  }
896
897
  /* Now check the encryption parameters.
898
   */
899
0
  p = algo_to_pbes2_cipher_schema(params->cipher);
900
0
  if (p == NULL) {
901
0
    gnutls_assert();
902
0
    return GNUTLS_E_INVALID_REQUEST;
903
0
  }
904
905
0
  if ((result = asn1_create_element(_gnutls_get_pkix(), p->desc,
906
0
            &pbe_asn)) != ASN1_SUCCESS) {
907
0
    gnutls_assert();
908
0
    return _gnutls_asn2err(result);
909
0
  }
910
911
0
  result = _asn1_strict_der_decode(&pbe_asn, &der->data[params_start],
912
0
           params_len, NULL);
913
0
  if (result != ASN1_SUCCESS) {
914
0
    gnutls_assert();
915
0
    result = _gnutls_asn2err(result);
916
0
    goto error;
917
0
  }
918
919
  /* read the IV */
920
0
  params->iv_size = sizeof(params->iv);
921
0
  result = asn1_read_value(pbe_asn, p->iv_name, params->iv,
922
0
         &params->iv_size);
923
0
  if (result != ASN1_SUCCESS) {
924
0
    gnutls_assert();
925
0
    result = _gnutls_asn2err(result);
926
0
    goto error;
927
0
  }
928
0
  _gnutls_hard_log("IV.size: %d\n", params->iv_size);
929
930
0
  result = 0;
931
932
0
error:
933
0
  asn1_delete_structure(&pbe_asn);
934
0
  return result;
935
0
}
936
937
/* Read the parameters cipher, IV, salt etc using the given
938
 * schema ID. Initially the schema ID should have PBES2_GENERIC, for
939
 * PBES2 schemas, and will be updated by this function for details.
940
 */
941
int _gnutls_read_pkcs_schema_params(schema_id *schema, const char *password,
942
            const uint8_t *data, int data_size,
943
            struct pbkdf2_params *kdf_params,
944
            struct pbe_enc_params *enc_params)
945
0
{
946
0
  asn1_node pasn = NULL;
947
0
  int result;
948
0
  gnutls_datum_t tmp;
949
0
  const struct pkcs_cipher_schema_st *p;
950
951
0
  if (*schema == PBES2_GENERIC) {
952
    /* Now check the key derivation and the encryption
953
     * functions.
954
     */
955
0
    if ((result = asn1_create_element(_gnutls_get_pkix(),
956
0
              "PKIX1.pkcs-5-PBES2-params",
957
0
              &pasn)) != ASN1_SUCCESS) {
958
0
      gnutls_assert();
959
0
      result = _gnutls_asn2err(result);
960
0
      goto error;
961
0
    }
962
963
    /* Decode the parameters.
964
     */
965
0
    result = _asn1_strict_der_decode(&pasn, data, data_size, NULL);
966
0
    if (result != ASN1_SUCCESS) {
967
0
      gnutls_assert();
968
0
      result = _gnutls_asn2err(result);
969
0
      goto error;
970
0
    }
971
972
0
    tmp.data = (uint8_t *)data;
973
0
    tmp.size = data_size;
974
975
0
    result = read_pbkdf2_params(pasn, &tmp, kdf_params);
976
0
    if (result < 0) {
977
0
      gnutls_assert();
978
0
      goto error;
979
0
    }
980
981
0
    result = read_pbes2_enc_params(pasn, &tmp, enc_params);
982
0
    if (result < 0) {
983
0
      gnutls_assert();
984
0
      goto error;
985
0
    }
986
987
0
    asn1_delete_structure2(&pasn, ASN1_DELETE_FLAG_ZEROIZE);
988
989
0
    p = algo_to_pbes2_cipher_schema(enc_params->cipher);
990
0
    if (p == NULL) {
991
0
      result = GNUTLS_E_INVALID_REQUEST;
992
0
      gnutls_assert();
993
0
      goto error;
994
0
    }
995
996
0
    *schema = p->schema;
997
0
    return 0;
998
0
  } else if (*schema == PBES1_DES_MD5) {
999
0
    return _gnutls_read_pbkdf1_params(data, data_size, kdf_params,
1000
0
              enc_params);
1001
0
  } else { /* PKCS #12 schema */
1002
0
    memset(enc_params, 0, sizeof(*enc_params));
1003
1004
0
    p = _gnutls_pkcs_schema_get(*schema);
1005
0
    if (p == NULL) {
1006
0
      gnutls_assert();
1007
0
      result = GNUTLS_E_UNKNOWN_CIPHER_TYPE;
1008
0
      goto error;
1009
0
    }
1010
0
    enc_params->cipher = p->cipher;
1011
0
    enc_params->iv_size = gnutls_cipher_get_iv_size(p->cipher);
1012
1013
0
    if ((result = asn1_create_element(_gnutls_get_pkix(),
1014
0
              "PKIX1.pkcs-12-PbeParams",
1015
0
              &pasn)) != ASN1_SUCCESS) {
1016
0
      gnutls_assert();
1017
0
      result = _gnutls_asn2err(result);
1018
0
      goto error;
1019
0
    }
1020
1021
    /* Decode the parameters.
1022
     */
1023
0
    result = _asn1_strict_der_decode(&pasn, data, data_size, NULL);
1024
0
    if (result != ASN1_SUCCESS) {
1025
0
      gnutls_assert();
1026
0
      result = _gnutls_asn2err(result);
1027
0
      goto error;
1028
0
    }
1029
1030
0
    result = read_pkcs12_kdf_params(pasn, kdf_params);
1031
0
    if (result < 0) {
1032
0
      gnutls_assert();
1033
0
      goto error;
1034
0
    }
1035
1036
0
    if (enc_params->iv_size) {
1037
0
      result = _gnutls_pkcs12_string_to_key(
1038
0
        mac_to_entry(GNUTLS_MAC_SHA1), 2 /*IV*/,
1039
0
        kdf_params->salt, kdf_params->salt_size,
1040
0
        kdf_params->iter_count, password,
1041
0
        enc_params->iv_size, enc_params->iv);
1042
0
      if (result < 0) {
1043
0
        gnutls_assert();
1044
0
        goto error;
1045
0
      }
1046
0
    }
1047
1048
0
    asn1_delete_structure(&pasn);
1049
1050
0
    return 0;
1051
0
  } /* switch */
1052
1053
0
error:
1054
0
  asn1_delete_structure(&pasn);
1055
0
  return result;
1056
0
}
1057
1058
static int _gnutls_pbes2_string_to_key(unsigned int pass_len,
1059
               const char *password,
1060
               const struct pbkdf2_params *kdf_params,
1061
               int key_size, uint8_t *key)
1062
0
{
1063
0
  gnutls_datum_t _key;
1064
0
  gnutls_datum_t salt;
1065
1066
0
  _key.data = (void *)password;
1067
0
  _key.size = pass_len;
1068
0
  salt.data = (void *)kdf_params->salt;
1069
0
  salt.size = kdf_params->salt_size;
1070
1071
0
  return gnutls_pbkdf2(kdf_params->mac, &_key, &salt,
1072
0
           kdf_params->iter_count, key, key_size);
1073
0
}
1074
1075
int _gnutls_pkcs_raw_decrypt_data(schema_id schema, asn1_node pkcs8_asn,
1076
          const char *root, const char *_password,
1077
          const struct pbkdf2_params *kdf_params,
1078
          const struct pbe_enc_params *enc_params,
1079
          gnutls_datum_t *decrypted_data)
1080
0
{
1081
0
  gnutls_datum_t enc = { NULL, 0 };
1082
0
  uint8_t *key = NULL;
1083
0
  gnutls_datum_t dkey, d_iv;
1084
0
  gnutls_cipher_hd_t ch = NULL;
1085
0
  int key_size, ret;
1086
0
  unsigned int pass_len = 0;
1087
0
  const struct pkcs_cipher_schema_st *p;
1088
0
  unsigned block_size;
1089
0
  const cipher_entry_st *ce;
1090
0
  char *password;
1091
1092
0
  if (_password) {
1093
0
    gnutls_datum_t pout;
1094
0
    ret = _gnutls_utf8_password_normalize(
1095
0
      _password, strlen(_password), &pout, 1);
1096
0
    if (ret < 0)
1097
0
      return gnutls_assert_val(ret);
1098
1099
0
    password = (char *)pout.data;
1100
0
    pass_len = pout.size;
1101
0
  } else {
1102
0
    password = NULL;
1103
0
    pass_len = 0;
1104
0
  }
1105
1106
0
  ret = _gnutls_x509_read_value(pkcs8_asn, root, &enc);
1107
0
  if (ret < 0) {
1108
0
    gnutls_assert();
1109
0
    enc.data = NULL;
1110
0
    goto cleanup;
1111
0
  }
1112
1113
0
  if (schema == PBES1_DES_MD5) {
1114
0
    ret = _gnutls_decrypt_pbes1_des_md5_data(password, pass_len,
1115
0
               kdf_params, enc_params,
1116
0
               &enc, decrypted_data);
1117
0
    if (ret < 0)
1118
0
      goto error;
1119
0
    goto cleanup;
1120
0
  }
1121
1122
0
  if (kdf_params->key_size == 0) {
1123
0
    key_size = gnutls_cipher_get_key_size(enc_params->cipher);
1124
0
  } else
1125
0
    key_size = kdf_params->key_size;
1126
1127
0
  key = gnutls_malloc(key_size);
1128
0
  if (key == NULL) {
1129
0
    gnutls_assert();
1130
0
    ret = GNUTLS_E_MEMORY_ERROR;
1131
0
    goto error;
1132
0
  }
1133
1134
  /* generate the key
1135
   */
1136
0
  p = _gnutls_pkcs_schema_get(schema);
1137
0
  if (p != NULL && p->pbes2 != 0) { /* PBES2 */
1138
0
    ret = _gnutls_pbes2_string_to_key(pass_len, password,
1139
0
              kdf_params, key_size, key);
1140
0
    if (ret < 0) {
1141
0
      gnutls_assert();
1142
0
      goto error;
1143
0
    }
1144
0
  } else if (p != NULL) { /* PKCS 12 schema */
1145
0
    ret = _gnutls_pkcs12_string_to_key(
1146
0
      mac_to_entry(GNUTLS_MAC_SHA1), 1 /*KEY*/,
1147
0
      kdf_params->salt, kdf_params->salt_size,
1148
0
      kdf_params->iter_count, password, key_size, key);
1149
1150
0
    if (ret < 0) {
1151
0
      gnutls_assert();
1152
0
      goto error;
1153
0
    }
1154
0
  } else {
1155
0
    gnutls_assert();
1156
0
    ret = GNUTLS_E_UNKNOWN_CIPHER_TYPE;
1157
0
    goto error;
1158
0
  }
1159
1160
0
  ce = cipher_to_entry(enc_params->cipher);
1161
0
  if (unlikely(ce == NULL)) {
1162
0
    ret = gnutls_assert_val(GNUTLS_E_UNKNOWN_CIPHER_TYPE);
1163
0
    goto error;
1164
0
  }
1165
0
  block_size = _gnutls_cipher_get_block_size(ce);
1166
1167
0
  if (ce->type == CIPHER_BLOCK) {
1168
0
    if (enc.size % block_size != 0 ||
1169
0
        (unsigned)enc_params->iv_size != block_size) {
1170
0
      gnutls_assert();
1171
0
      ret = GNUTLS_E_DECRYPTION_FAILED;
1172
0
      goto error;
1173
0
    }
1174
0
  } else {
1175
0
    unsigned iv_size = _gnutls_cipher_get_iv_size(ce);
1176
0
    if (iv_size > (unsigned)enc_params->iv_size) {
1177
0
      gnutls_assert();
1178
0
      ret = GNUTLS_E_DECRYPTION_FAILED;
1179
0
      goto error;
1180
0
    }
1181
0
  }
1182
1183
  /* do the decryption.
1184
   */
1185
0
  dkey.data = key;
1186
0
  dkey.size = key_size;
1187
1188
0
  d_iv.data = (uint8_t *)enc_params->iv;
1189
0
  d_iv.size = enc_params->iv_size;
1190
1191
0
  ret = gnutls_cipher_init(&ch, ce->id, &dkey, &d_iv);
1192
1193
0
  zeroize_temp_key(key, key_size);
1194
0
  gnutls_free(key);
1195
1196
0
  if (ret < 0) {
1197
0
    gnutls_assert();
1198
0
    goto error;
1199
0
  }
1200
1201
0
  ret = gnutls_cipher_decrypt(ch, enc.data, enc.size);
1202
0
  if (ret < 0) {
1203
0
    gnutls_assert();
1204
0
    ret = GNUTLS_E_DECRYPTION_FAILED;
1205
0
    goto error;
1206
0
  }
1207
1208
0
  decrypted_data->data = enc.data;
1209
1210
0
  if (ce->type == CIPHER_BLOCK && block_size != 1) {
1211
0
    unsigned pslen = (uint8_t)enc.data[enc.size - 1];
1212
0
    unsigned i;
1213
1214
0
    if (pslen > block_size || pslen >= enc.size || pslen == 0) {
1215
0
      gnutls_assert();
1216
0
      ret = GNUTLS_E_DECRYPTION_FAILED;
1217
0
      goto error;
1218
0
    }
1219
1220
    /* verify padding according to rfc2898 */
1221
0
    decrypted_data->size = enc.size - pslen;
1222
0
    for (i = 0; i < pslen; i++) {
1223
0
      if (enc.data[enc.size - 1 - i] != pslen) {
1224
0
        gnutls_assert();
1225
0
        ret = GNUTLS_E_DECRYPTION_FAILED;
1226
0
        goto error;
1227
0
      }
1228
0
    }
1229
0
  } else {
1230
0
    decrypted_data->size = enc.size;
1231
0
  }
1232
1233
0
  gnutls_cipher_deinit(ch);
1234
1235
0
  ret = 0;
1236
1237
0
cleanup:
1238
0
  if (password) {
1239
0
    zeroize_temp_key(password, pass_len);
1240
0
    gnutls_free(password);
1241
0
  }
1242
1243
0
  return ret;
1244
1245
0
error:
1246
0
  if (password) {
1247
0
    zeroize_temp_key(password, pass_len);
1248
0
    gnutls_free(password);
1249
0
  }
1250
0
  if (enc.data) {
1251
0
    zeroize_temp_key(enc.data, enc.size);
1252
0
    gnutls_free(enc.data);
1253
0
  }
1254
0
  if (key) {
1255
0
    zeroize_temp_key(key, key_size);
1256
0
    gnutls_free(key);
1257
0
  }
1258
0
  if (ch) {
1259
0
    gnutls_cipher_deinit(ch);
1260
0
  }
1261
0
  return ret;
1262
0
}
1263
1264
/* Writes the PBKDF2 parameters.
1265
 */
1266
static int write_pbkdf2_params(asn1_node pasn,
1267
             const struct pbkdf2_params *kdf_params)
1268
0
{
1269
0
  int result;
1270
0
  asn1_node pbkdf2_asn = NULL;
1271
0
  uint8_t tmp[MAX_OID_SIZE];
1272
0
  const mac_entry_st *me;
1273
1274
  /* Write the key derivation algorithm
1275
   */
1276
0
  result = asn1_write_value(pasn, "keyDerivationFunc.algorithm",
1277
0
          PBKDF2_OID, 1);
1278
0
  if (result != ASN1_SUCCESS) {
1279
0
    gnutls_assert();
1280
0
    return _gnutls_asn2err(result);
1281
0
  }
1282
1283
  /* Now write the key derivation and the encryption
1284
   * functions.
1285
   */
1286
0
  if ((result = asn1_create_element(_gnutls_get_pkix(),
1287
0
            "PKIX1.pkcs-5-PBKDF2-params",
1288
0
            &pbkdf2_asn)) != ASN1_SUCCESS) {
1289
0
    gnutls_assert();
1290
0
    return _gnutls_asn2err(result);
1291
0
  }
1292
1293
0
  result = asn1_write_value(pbkdf2_asn, "salt", "specified", 1);
1294
0
  if (result != ASN1_SUCCESS) {
1295
0
    gnutls_assert();
1296
0
    result = _gnutls_asn2err(result);
1297
0
    goto error;
1298
0
  }
1299
1300
  /* write the salt 
1301
   */
1302
0
  result = asn1_write_value(pbkdf2_asn, "salt.specified",
1303
0
          kdf_params->salt, kdf_params->salt_size);
1304
0
  if (result != ASN1_SUCCESS) {
1305
0
    gnutls_assert();
1306
0
    result = _gnutls_asn2err(result);
1307
0
    goto error;
1308
0
  }
1309
0
  _gnutls_hard_log("salt.specified.size: %d\n", kdf_params->salt_size);
1310
1311
  /* write the iteration count 
1312
   */
1313
0
  _gnutls_write_uint32(kdf_params->iter_count, tmp);
1314
1315
0
  result = asn1_write_value(pbkdf2_asn, "iterationCount", tmp, 4);
1316
0
  if (result != ASN1_SUCCESS) {
1317
0
    gnutls_assert();
1318
0
    result = _gnutls_asn2err(result);
1319
0
    goto error;
1320
0
  }
1321
0
  _gnutls_hard_log("iterationCount: %d\n", kdf_params->iter_count);
1322
1323
  /* write the keylength, if it is set.
1324
   */
1325
0
  result = asn1_write_value(pbkdf2_asn, "keyLength", NULL, 0);
1326
0
  if (result != ASN1_SUCCESS) {
1327
0
    gnutls_assert();
1328
0
    result = _gnutls_asn2err(result);
1329
0
    goto error;
1330
0
  }
1331
1332
0
  me = _gnutls_mac_to_entry(kdf_params->mac);
1333
0
  if (!me || !me->mac_oid) {
1334
0
    gnutls_assert();
1335
0
    result = GNUTLS_E_INTERNAL_ERROR;
1336
0
    goto error;
1337
0
  }
1338
1339
0
  result = asn1_write_value(pbkdf2_asn, "prf.algorithm", me->mac_oid,
1340
0
          strlen(me->mac_oid));
1341
0
  if (result != ASN1_SUCCESS) {
1342
0
    gnutls_assert();
1343
0
    result = _gnutls_asn2err(result);
1344
0
    goto error;
1345
0
  }
1346
1347
0
  result = asn1_write_value(pbkdf2_asn, "prf.parameters", NULL, 0);
1348
0
  if (result != ASN1_SUCCESS) {
1349
0
    gnutls_assert();
1350
0
    result = _gnutls_asn2err(result);
1351
0
    goto error;
1352
0
  }
1353
1354
  /* now encode them an put the DER output
1355
   * in the keyDerivationFunc.parameters
1356
   */
1357
0
  result = _gnutls_x509_der_encode_and_copy(
1358
0
    pbkdf2_asn, "", pasn, "keyDerivationFunc.parameters", 0);
1359
0
  if (result < 0) {
1360
0
    gnutls_assert();
1361
0
    goto error;
1362
0
  }
1363
1364
0
  result = 0;
1365
1366
0
error:
1367
0
  asn1_delete_structure(&pbkdf2_asn);
1368
0
  return result;
1369
0
}
1370
1371
static int write_pbes2_enc_params(asn1_node pasn,
1372
          const struct pbe_enc_params *params)
1373
0
{
1374
0
  int result;
1375
0
  asn1_node pbe_asn = NULL;
1376
0
  const struct pkcs_cipher_schema_st *p;
1377
0
  const char *cipher_oid;
1378
1379
  /* Write the encryption algorithm
1380
   */
1381
0
  p = algo_to_pbes2_cipher_schema(params->cipher);
1382
0
  if (p == NULL || p->pbes2 == 0) {
1383
0
    gnutls_assert();
1384
0
    return GNUTLS_E_INVALID_REQUEST;
1385
0
  }
1386
1387
  /* Now check the encryption parameters.
1388
   */
1389
0
  if ((result = asn1_create_element(_gnutls_get_pkix(), p->desc,
1390
0
            &pbe_asn)) != ASN1_SUCCESS) {
1391
0
    gnutls_assert();
1392
0
    return _gnutls_asn2err(result);
1393
0
  }
1394
1395
0
  if (p->schema == PBES2_GOST28147_89_TC26Z ||
1396
0
      p->schema == PBES2_GOST28147_89_CPA ||
1397
0
      p->schema == PBES2_GOST28147_89_CPB ||
1398
0
      p->schema == PBES2_GOST28147_89_CPC ||
1399
0
      p->schema == PBES2_GOST28147_89_CPD) {
1400
0
    cipher_oid = GOST28147_89_OID;
1401
0
    result = asn1_write_value(pbe_asn, "encryptionParamSet",
1402
0
            p->cipher_oid, 1);
1403
0
    if (result != ASN1_SUCCESS) {
1404
0
      gnutls_assert();
1405
0
      result = _gnutls_asn2err(result);
1406
0
      goto error;
1407
0
    }
1408
0
  } else {
1409
0
    cipher_oid = p->cipher_oid;
1410
0
  }
1411
1412
0
  result = asn1_write_value(pasn, "encryptionScheme.algorithm",
1413
0
          cipher_oid, 1);
1414
0
  if (result != ASN1_SUCCESS) {
1415
0
    gnutls_assert();
1416
0
    goto error;
1417
0
  }
1418
0
  _gnutls_hard_log("encryptionScheme.algorithm: %s\n", cipher_oid);
1419
1420
  /* read the salt */
1421
0
  result = asn1_write_value(pbe_asn, p->iv_name, params->iv,
1422
0
          params->iv_size);
1423
0
  if (result != ASN1_SUCCESS) {
1424
0
    gnutls_assert();
1425
0
    result = _gnutls_asn2err(result);
1426
0
    goto error;
1427
0
  }
1428
0
  _gnutls_hard_log("IV.size: %d\n", params->iv_size);
1429
1430
  /* now encode them an put the DER output
1431
   * in the encryptionScheme.parameters
1432
   */
1433
0
  result = _gnutls_x509_der_encode_and_copy(
1434
0
    pbe_asn, "", pasn, "encryptionScheme.parameters", 0);
1435
0
  if (result < 0) {
1436
0
    gnutls_assert();
1437
0
    goto error;
1438
0
  }
1439
1440
0
  result = 0;
1441
1442
0
error:
1443
0
  asn1_delete_structure(&pbe_asn);
1444
0
  return result;
1445
0
}
1446
1447
/* Generates a key and also stores the key parameters.
1448
 */
1449
int _gnutls_pkcs_generate_key(schema_id schema, const char *_password,
1450
            struct pbkdf2_params *kdf_params,
1451
            struct pbe_enc_params *enc_params,
1452
            gnutls_datum_t *key)
1453
0
{
1454
0
  unsigned char rnd[2];
1455
0
  unsigned int pass_len = 0;
1456
0
  int ret;
1457
0
  const struct pkcs_cipher_schema_st *p;
1458
0
  char *password = NULL;
1459
1460
0
  if (_password) {
1461
0
    gnutls_datum_t pout;
1462
0
    ret = _gnutls_utf8_password_normalize(
1463
0
      _password, strlen(_password), &pout, 0);
1464
0
    if (ret < 0)
1465
0
      return gnutls_assert_val(ret);
1466
1467
0
    password = (char *)pout.data;
1468
0
    pass_len = pout.size;
1469
0
  } else {
1470
0
    password = NULL;
1471
0
    pass_len = 0;
1472
0
  }
1473
1474
0
  ret = gnutls_rnd(GNUTLS_RND_RANDOM, rnd, 2);
1475
0
  if (ret < 0) {
1476
0
    gnutls_assert();
1477
0
    goto cleanup;
1478
0
  }
1479
1480
  /* generate salt */
1481
0
  kdf_params->salt_size =
1482
0
    MIN(sizeof(kdf_params->salt), (unsigned)(12 + (rnd[1] % 10)));
1483
1484
0
  p = _gnutls_pkcs_schema_get(schema);
1485
0
  if (p != NULL && p->pbes2 != 0) { /* PBES2 */
1486
0
    enc_params->cipher = p->cipher;
1487
0
  } else if (p != NULL) {
1488
    /* non PBES2 algorithms */
1489
0
    enc_params->cipher = p->cipher;
1490
0
    kdf_params->salt_size = 8;
1491
0
  } else {
1492
0
    gnutls_assert();
1493
0
    ret = GNUTLS_E_INVALID_REQUEST;
1494
0
    goto cleanup;
1495
0
  }
1496
1497
0
  ret = gnutls_rnd(GNUTLS_RND_RANDOM, kdf_params->salt,
1498
0
       kdf_params->salt_size);
1499
0
  if (ret < 0) {
1500
0
    gnutls_assert();
1501
0
    goto cleanup;
1502
0
  }
1503
1504
0
  kdf_params->iter_count = PKCS12_ITER_COUNT;
1505
0
  key->size = kdf_params->key_size =
1506
0
    gnutls_cipher_get_key_size(enc_params->cipher);
1507
1508
0
  enc_params->iv_size = gnutls_cipher_get_iv_size(enc_params->cipher);
1509
0
  key->data = gnutls_malloc(key->size);
1510
0
  if (key->data == NULL) {
1511
0
    gnutls_assert();
1512
0
    ret = GNUTLS_E_MEMORY_ERROR;
1513
0
    goto cleanup;
1514
0
  }
1515
1516
  /* now generate the key. 
1517
   */
1518
1519
0
  if (p->pbes2 != 0) {
1520
0
    if (p->schema == PBES2_GOST28147_89_TC26Z)
1521
0
      kdf_params->mac = GNUTLS_MAC_STREEBOG_512;
1522
0
    else if (p->schema == PBES2_GOST28147_89_CPA ||
1523
0
       p->schema == PBES2_GOST28147_89_CPB ||
1524
0
       p->schema == PBES2_GOST28147_89_CPC ||
1525
0
       p->schema == PBES2_GOST28147_89_CPD)
1526
0
      kdf_params->mac = GNUTLS_MAC_GOSTR_94;
1527
0
    else
1528
0
      kdf_params->mac = GNUTLS_MAC_SHA256;
1529
0
    ret = _gnutls_pbes2_string_to_key(pass_len, password,
1530
0
              kdf_params,
1531
0
              kdf_params->key_size,
1532
0
              key->data);
1533
0
    if (ret < 0) {
1534
0
      gnutls_assert();
1535
0
      return ret;
1536
0
    }
1537
1538
0
    if (enc_params->iv_size) {
1539
0
      ret = gnutls_rnd(GNUTLS_RND_NONCE, enc_params->iv,
1540
0
           enc_params->iv_size);
1541
0
      if (ret < 0) {
1542
0
        gnutls_assert();
1543
0
        goto cleanup;
1544
0
      }
1545
0
    }
1546
0
  } else { /* PKCS 12 schema */
1547
0
    ret = _gnutls_pkcs12_string_to_key(
1548
0
      mac_to_entry(GNUTLS_MAC_SHA1), 1 /*KEY*/,
1549
0
      kdf_params->salt, kdf_params->salt_size,
1550
0
      kdf_params->iter_count, password, kdf_params->key_size,
1551
0
      key->data);
1552
0
    if (ret < 0) {
1553
0
      gnutls_assert();
1554
0
      goto cleanup;
1555
0
    }
1556
1557
    /* Now generate the IV
1558
     */
1559
0
    if (enc_params->iv_size) {
1560
0
      ret = _gnutls_pkcs12_string_to_key(
1561
0
        mac_to_entry(GNUTLS_MAC_SHA1), 2 /*IV*/,
1562
0
        kdf_params->salt, kdf_params->salt_size,
1563
0
        kdf_params->iter_count, password,
1564
0
        enc_params->iv_size, enc_params->iv);
1565
0
      if (ret < 0) {
1566
0
        gnutls_assert();
1567
0
        goto cleanup;
1568
0
      }
1569
0
    }
1570
0
  }
1571
1572
0
  ret = 0;
1573
1574
0
cleanup:
1575
0
  gnutls_free(password);
1576
0
  return ret;
1577
0
}
1578
1579
/* Encodes the parameters to be written in the encryptionAlgorithm.parameters
1580
 * part.
1581
 */
1582
int _gnutls_pkcs_write_schema_params(schema_id schema, asn1_node pkcs8_asn,
1583
             const char *where,
1584
             const struct pbkdf2_params *kdf_params,
1585
             const struct pbe_enc_params *enc_params)
1586
0
{
1587
0
  int result;
1588
0
  asn1_node pasn = NULL;
1589
0
  const struct pkcs_cipher_schema_st *p;
1590
1591
0
  p = _gnutls_pkcs_schema_get(schema);
1592
1593
0
  if (p != NULL && p->pbes2 != 0) { /* PBES2 */
1594
0
    if ((result = asn1_create_element(_gnutls_get_pkix(),
1595
0
              "PKIX1.pkcs-5-PBES2-params",
1596
0
              &pasn)) != ASN1_SUCCESS) {
1597
0
      gnutls_assert();
1598
0
      return _gnutls_asn2err(result);
1599
0
    }
1600
1601
0
    result = write_pbkdf2_params(pasn, kdf_params);
1602
0
    if (result < 0) {
1603
0
      gnutls_assert();
1604
0
      goto error;
1605
0
    }
1606
1607
0
    result = write_pbes2_enc_params(pasn, enc_params);
1608
0
    if (result < 0) {
1609
0
      gnutls_assert();
1610
0
      goto error;
1611
0
    }
1612
1613
0
    result = _gnutls_x509_der_encode_and_copy(pasn, "", pkcs8_asn,
1614
0
                where, 0);
1615
0
    if (result < 0) {
1616
0
      gnutls_assert();
1617
0
      goto error;
1618
0
    }
1619
1620
0
    asn1_delete_structure(&pasn);
1621
1622
0
  } else if (p != NULL) { /* PKCS #12 */
1623
1624
0
    if ((result = asn1_create_element(_gnutls_get_pkix(),
1625
0
              "PKIX1.pkcs-12-PbeParams",
1626
0
              &pasn)) != ASN1_SUCCESS) {
1627
0
      gnutls_assert();
1628
0
      result = _gnutls_asn2err(result);
1629
0
      goto error;
1630
0
    }
1631
1632
0
    result = write_pkcs12_kdf_params(pasn, kdf_params);
1633
0
    if (result < 0) {
1634
0
      gnutls_assert();
1635
0
      goto error;
1636
0
    }
1637
1638
0
    result = _gnutls_x509_der_encode_and_copy(pasn, "", pkcs8_asn,
1639
0
                where, 0);
1640
0
    if (result < 0) {
1641
0
      gnutls_assert();
1642
0
      goto error;
1643
0
    }
1644
1645
0
    asn1_delete_structure(&pasn);
1646
0
  }
1647
1648
0
  return 0;
1649
1650
0
error:
1651
0
  asn1_delete_structure(&pasn);
1652
0
  return result;
1653
0
}
1654
1655
int _gnutls_pkcs_raw_encrypt_data(const gnutls_datum_t *plain,
1656
          const struct pbe_enc_params *enc_params,
1657
          const gnutls_datum_t *key,
1658
          gnutls_datum_t *encrypted)
1659
0
{
1660
0
  int result;
1661
0
  int data_size;
1662
0
  uint8_t *data = NULL;
1663
0
  gnutls_datum_t d_iv;
1664
0
  gnutls_cipher_hd_t ch = NULL;
1665
0
  uint8_t pad, pad_size;
1666
0
  const cipher_entry_st *ce;
1667
1668
0
  ce = cipher_to_entry(enc_params->cipher);
1669
0
  pad_size = _gnutls_cipher_get_block_size(ce);
1670
1671
0
  if (pad_size == 1 || ce->type == CIPHER_STREAM) /* stream */
1672
0
    pad_size = 0;
1673
1674
0
  data = gnutls_malloc(plain->size + pad_size);
1675
0
  if (data == NULL) {
1676
0
    gnutls_assert();
1677
0
    return GNUTLS_E_MEMORY_ERROR;
1678
0
  }
1679
1680
0
  memcpy(data, plain->data, plain->size);
1681
1682
0
  if (pad_size > 0) {
1683
0
    pad = pad_size - (plain->size % pad_size);
1684
0
    if (pad == 0)
1685
0
      pad = pad_size;
1686
0
    memset(&data[plain->size], pad, pad);
1687
0
  } else
1688
0
    pad = 0;
1689
1690
0
  data_size = plain->size + pad;
1691
1692
0
  d_iv.data = (uint8_t *)enc_params->iv;
1693
0
  d_iv.size = enc_params->iv_size;
1694
0
  result = gnutls_cipher_init(&ch, enc_params->cipher, key, &d_iv);
1695
0
  if (result < 0) {
1696
0
    gnutls_assert();
1697
0
    goto error;
1698
0
  }
1699
1700
0
  result = gnutls_cipher_encrypt(ch, data, data_size);
1701
0
  if (result < 0) {
1702
0
    gnutls_assert();
1703
0
    goto error;
1704
0
  }
1705
1706
0
  encrypted->data = data;
1707
0
  encrypted->size = data_size;
1708
1709
0
  gnutls_cipher_deinit(ch);
1710
1711
0
  return 0;
1712
1713
0
error:
1714
0
  gnutls_free(data);
1715
0
  if (ch) {
1716
0
    gnutls_cipher_deinit(ch);
1717
0
  }
1718
0
  return result;
1719
0
}