2 * Copyright (C) 2003-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
25 #include <gnutls_datum.h>
26 #include <gnutls_global.h>
27 #include <gnutls_errors.h>
28 #include <gnutls_rsa_export.h>
30 #include <gnutls_x509.h>
33 #include <algorithms.h>
34 #include <gnutls_num.h>
36 #include <pbkdf2-sha1.h>
38 static int _decode_pkcs8_ecc_key (ASN1_TYPE pkcs8_asn
, gnutls_x509_privkey_t pkey
);
40 #define PBES2_OID "1.2.840.113549.1.5.13"
41 #define PBKDF2_OID "1.2.840.113549.1.5.12"
42 #define DES_EDE3_CBC_OID "1.2.840.113549.3.7"
43 #define AES_128_CBC_OID "2.16.840.1.101.3.4.1.2"
44 #define AES_192_CBC_OID "2.16.840.1.101.3.4.1.22"
45 #define AES_256_CBC_OID "2.16.840.1.101.3.4.1.42"
46 #define DES_CBC_OID "1.3.14.3.2.7"
48 /* oid_pbeWithSHAAnd3_KeyTripleDES_CBC */
49 #define PKCS12_PBE_3DES_SHA1_OID "1.2.840.113549.1.12.1.3"
50 #define PKCS12_PBE_ARCFOUR_SHA1_OID "1.2.840.113549.1.12.1.1"
51 #define PKCS12_PBE_RC2_40_SHA1_OID "1.2.840.113549.1.12.1.6"
57 unsigned int iter_count
;
58 unsigned int key_size
;
63 gnutls_cipher_algorithm_t cipher
;
64 uint8_t iv
[MAX_CIPHER_BLOCK_SIZE
];
68 static int generate_key (schema_id schema
, const char *password
,
69 struct pbkdf2_params
*kdf_params
,
70 struct pbe_enc_params
*enc_params
,
71 gnutls_datum_t
* key
);
72 static int read_pbkdf2_params (ASN1_TYPE pbes2_asn
,
73 const gnutls_datum_t
* der
,
74 struct pbkdf2_params
*params
);
75 static int read_pbe_enc_params (ASN1_TYPE pbes2_asn
,
76 const gnutls_datum_t
* der
,
77 struct pbe_enc_params
*params
);
78 static int decrypt_data (schema_id
, ASN1_TYPE pkcs8_asn
, const char *root
,
80 const struct pbkdf2_params
*kdf_params
,
81 const struct pbe_enc_params
*enc_params
,
82 gnutls_datum_t
* decrypted_data
);
83 static int decode_private_key_info (const gnutls_datum_t
* der
,
84 gnutls_x509_privkey_t pkey
);
85 static int write_schema_params (schema_id schema
, ASN1_TYPE pkcs8_asn
,
87 const struct pbkdf2_params
*kdf_params
,
88 const struct pbe_enc_params
*enc_params
);
89 static int encrypt_data (const gnutls_datum_t
* plain
,
90 const struct pbe_enc_params
*enc_params
,
91 gnutls_datum_t
* key
, gnutls_datum_t
* encrypted
);
93 static int read_pkcs12_kdf_params (ASN1_TYPE pbes2_asn
,
94 struct pbkdf2_params
*params
);
95 static int write_pkcs12_kdf_params (ASN1_TYPE pbes2_asn
,
96 const struct pbkdf2_params
*params
);
98 #define PEM_PKCS8 "ENCRYPTED PRIVATE KEY"
99 #define PEM_UNENCRYPTED_PKCS8 "PRIVATE KEY"
101 /* Returns a negative error code if the encryption schema in
102 * the OID is not supported. The schema ID is returned.
105 check_schema (const char *oid
)
108 if (strcmp (oid
, PBES2_OID
) == 0)
109 return PBES2_GENERIC
; /* ok */
111 if (strcmp (oid
, PKCS12_PBE_3DES_SHA1_OID
) == 0)
112 return PKCS12_3DES_SHA1
;
114 if (strcmp (oid
, PKCS12_PBE_ARCFOUR_SHA1_OID
) == 0)
115 return PKCS12_ARCFOUR_SHA1
;
117 if (strcmp (oid
, PKCS12_PBE_RC2_40_SHA1_OID
) == 0)
118 return PKCS12_RC2_40_SHA1
;
120 _gnutls_debug_log ("PKCS encryption schema OID '%s' is unsupported.\n", oid
);
122 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
125 /* Encodes a private key to the raw format PKCS #8 needs.
126 * For RSA it is a PKCS #1 DER private key and for DSA it is
127 * an ASN.1 INTEGER of the x value.
130 _encode_privkey (gnutls_x509_privkey_t pkey
, gnutls_datum_t
* raw
)
133 uint8_t *data
= NULL
;
135 ASN1_TYPE spk
= ASN1_TYPE_EMPTY
;
137 switch (pkey
->pk_algorithm
)
142 gnutls_x509_privkey_export (pkey
, GNUTLS_X509_FMT_DER
, NULL
, &size
);
143 if (ret
!= GNUTLS_E_SHORT_MEMORY_BUFFER
)
149 data
= gnutls_malloc (size
);
153 ret
= GNUTLS_E_MEMORY_ERROR
;
159 gnutls_x509_privkey_export (pkey
, GNUTLS_X509_FMT_DER
, data
, &size
);
170 /* DSAPublicKey == INTEGER */
171 if ((ret
= asn1_create_element
172 (_gnutls_get_gnutls_asn (), "GNUTLS.DSAPublicKey", &spk
))
176 return _gnutls_asn2err (ret
);
179 ret
= _gnutls_x509_write_int (spk
, "", pkey
->params
.params
[4], 1);
185 ret
= _gnutls_x509_der_encode (spk
, "", raw
, 0);
192 asn1_delete_structure (&spk
);
197 return GNUTLS_E_INVALID_REQUEST
;
204 asn1_delete_structure (&spk
);
210 * Encodes a PKCS #1 private key to a PKCS #8 private key
211 * info. The output will be allocated and stored into der. Also
212 * the ASN1_TYPE of private key info will be returned.
215 encode_to_private_key_info (gnutls_x509_privkey_t pkey
,
216 gnutls_datum_t
* der
, ASN1_TYPE
* pkey_info
)
221 gnutls_datum_t algo_params
= { NULL
, 0 };
222 gnutls_datum_t algo_privkey
= { NULL
, 0 };
224 oid
= _gnutls_x509_pk_to_oid(pkey
->pk_algorithm
);
228 return GNUTLS_E_UNIMPLEMENTED_FEATURE
;
232 _gnutls_x509_write_pubkey_params (pkey
->pk_algorithm
, &pkey
->params
, &algo_params
);
240 asn1_create_element (_gnutls_get_pkix (),
241 "PKIX1.pkcs-8-PrivateKeyInfo",
242 pkey_info
)) != ASN1_SUCCESS
)
245 result
= _gnutls_asn2err (result
);
249 /* Write the version.
251 result
= asn1_write_value (*pkey_info
, "version", &null
, 1);
252 if (result
!= ASN1_SUCCESS
)
255 result
= _gnutls_asn2err (result
);
259 /* write the privateKeyAlgorithm
260 * fields. (OID+NULL data)
263 asn1_write_value (*pkey_info
, "privateKeyAlgorithm.algorithm", oid
, 1);
264 if (result
!= ASN1_SUCCESS
)
267 result
= _gnutls_asn2err (result
);
272 asn1_write_value (*pkey_info
, "privateKeyAlgorithm.parameters",
273 algo_params
.data
, algo_params
.size
);
274 _gnutls_free_datum (&algo_params
);
275 if (result
!= ASN1_SUCCESS
)
278 result
= _gnutls_asn2err (result
);
283 /* Write the raw private key
285 result
= _encode_privkey (pkey
, &algo_privkey
);
293 asn1_write_value (*pkey_info
, "privateKey", algo_privkey
.data
,
295 _gnutls_free_datum (&algo_privkey
);
297 if (result
!= ASN1_SUCCESS
)
300 result
= _gnutls_asn2err (result
);
304 /* Append an empty Attributes field.
306 result
= asn1_write_value (*pkey_info
, "attributes", NULL
, 0);
307 if (result
!= ASN1_SUCCESS
)
310 result
= _gnutls_asn2err (result
);
314 /* DER Encode the generated private key info.
317 result
= asn1_der_coding (*pkey_info
, "", NULL
, &len
, NULL
);
318 if (result
!= ASN1_MEM_ERROR
)
321 result
= _gnutls_asn2err (result
);
325 /* allocate data for the der
328 der
->data
= gnutls_malloc (len
);
329 if (der
->data
== NULL
)
332 return GNUTLS_E_MEMORY_ERROR
;
335 result
= asn1_der_coding (*pkey_info
, "", der
->data
, &len
, NULL
);
336 if (result
!= ASN1_SUCCESS
)
339 result
= _gnutls_asn2err (result
);
346 asn1_delete_structure (pkey_info
);
347 _gnutls_free_datum (&algo_params
);
348 _gnutls_free_datum (&algo_privkey
);
354 cipher_to_pkcs_params (int cipher
, const char **oid
)
358 case GNUTLS_CIPHER_AES_128_CBC
:
360 *oid
= AES_128_CBC_OID
;
361 return "PKIX1.pkcs-5-aes128-CBC-params";
363 case GNUTLS_CIPHER_AES_192_CBC
:
365 *oid
= AES_192_CBC_OID
;
366 return "PKIX1.pkcs-5-aes192-CBC-params";
368 case GNUTLS_CIPHER_AES_256_CBC
:
370 *oid
= AES_256_CBC_OID
;
371 return "PKIX1.pkcs-5-aes256-CBC-params";
373 case GNUTLS_CIPHER_3DES_CBC
:
375 *oid
= DES_EDE3_CBC_OID
;
376 return "PKIX1.pkcs-5-des-EDE3-CBC-params";
385 cipher_to_schema (int cipher
)
389 case GNUTLS_CIPHER_AES_128_CBC
:
390 return PBES2_AES_128
;
392 case GNUTLS_CIPHER_AES_192_CBC
:
393 return PBES2_AES_192
;
395 case GNUTLS_CIPHER_AES_256_CBC
:
396 return PBES2_AES_256
;
398 case GNUTLS_CIPHER_3DES_CBC
:
402 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
409 _gnutls_pkcs_flags_to_schema (unsigned int flags
)
413 if (flags
& GNUTLS_PKCS_USE_PKCS12_ARCFOUR
)
414 schema
= PKCS12_ARCFOUR_SHA1
;
415 else if (flags
& GNUTLS_PKCS_USE_PKCS12_RC2_40
)
416 schema
= PKCS12_RC2_40_SHA1
;
417 else if (flags
& GNUTLS_PKCS_USE_PBES2_3DES
)
419 else if (flags
& GNUTLS_PKCS_USE_PBES2_AES_128
)
420 schema
= PBES2_AES_128
;
421 else if (flags
& GNUTLS_PKCS_USE_PBES2_AES_192
)
422 schema
= PBES2_AES_192
;
423 else if (flags
& GNUTLS_PKCS_USE_PBES2_AES_256
)
424 schema
= PBES2_AES_256
;
429 ("Selecting default encryption PKCS12_3DES_SHA1 (flags: %u).\n",
431 schema
= PKCS12_3DES_SHA1
;
437 /* returns the OID corresponding to given schema
440 schema_to_oid (schema_id schema
, const char **str_oid
)
450 *str_oid
= PBES2_OID
;
452 case PKCS12_3DES_SHA1
:
453 *str_oid
= PKCS12_PBE_3DES_SHA1_OID
;
455 case PKCS12_ARCFOUR_SHA1
:
456 *str_oid
= PKCS12_PBE_ARCFOUR_SHA1_OID
;
458 case PKCS12_RC2_40_SHA1
:
459 *str_oid
= PKCS12_PBE_RC2_40_SHA1_OID
;
463 result
= GNUTLS_E_INTERNAL_ERROR
;
469 /* Converts a PKCS #8 private key info to
470 * a PKCS #8 EncryptedPrivateKeyInfo.
473 encode_to_pkcs8_key (schema_id schema
, const gnutls_datum_t
* der_key
,
474 const char *password
, ASN1_TYPE
* out
)
477 gnutls_datum_t key
= { NULL
, 0 };
478 gnutls_datum_t tmp
= { NULL
, 0 };
479 ASN1_TYPE pkcs8_asn
= ASN1_TYPE_EMPTY
;
480 struct pbkdf2_params kdf_params
;
481 struct pbe_enc_params enc_params
;
486 asn1_create_element (_gnutls_get_pkix (),
487 "PKIX1.pkcs-8-EncryptedPrivateKeyInfo",
488 &pkcs8_asn
)) != ASN1_SUCCESS
)
491 result
= _gnutls_asn2err (result
);
495 /* Write the encryption schema OID
497 result
= schema_to_oid (schema
, &str_oid
);
505 asn1_write_value (pkcs8_asn
, "encryptionAlgorithm.algorithm", str_oid
, 1);
507 if (result
!= ASN1_SUCCESS
)
510 result
= _gnutls_asn2err (result
);
514 /* Generate a symmetric key.
517 result
= generate_key (schema
, password
, &kdf_params
, &enc_params
, &key
);
525 write_schema_params (schema
, pkcs8_asn
,
526 "encryptionAlgorithm.parameters", &kdf_params
,
534 /* Parameters have been encoded. Now
537 result
= encrypt_data (der_key
, &enc_params
, &key
, &tmp
);
544 /* write the encrypted data.
546 result
= asn1_write_value (pkcs8_asn
, "encryptedData", tmp
.data
, tmp
.size
);
547 if (result
!= ASN1_SUCCESS
)
550 result
= _gnutls_asn2err (result
);
554 _gnutls_free_datum (&tmp
);
555 _gnutls_free_datum (&key
);
562 _gnutls_free_datum (&key
);
563 _gnutls_free_datum (&tmp
);
564 asn1_delete_structure (&pkcs8_asn
);
570 * gnutls_x509_privkey_export_pkcs8:
571 * @key: Holds the key
572 * @format: the format of output params. One of PEM or DER.
573 * @password: the password that will be used to encrypt the key.
574 * @flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t
575 * @output_data: will contain a private key PEM or DER encoded
576 * @output_data_size: holds the size of output_data (and will be
577 * replaced by the actual size of parameters)
579 * This function will export the private key to a PKCS8 structure.
580 * Both RSA and DSA keys can be exported. For DSA keys we use
581 * PKCS #11 definitions. If the flags do not specify the encryption
582 * cipher, then the default 3DES (PBES2) will be used.
584 * The @password can be either ASCII or UTF-8 in the default PBES2
585 * encryption schemas, or ASCII for the PKCS12 schemas.
587 * If the buffer provided is not long enough to hold the output, then
588 * *output_data_size is updated and GNUTLS_E_SHORT_MEMORY_BUFFER will
591 * If the structure is PEM encoded, it will have a header
592 * of "BEGIN ENCRYPTED PRIVATE KEY" or "BEGIN PRIVATE KEY" if
593 * encryption is not used.
595 * Returns: In case of failure a negative error code will be
596 * returned, and 0 on success.
599 gnutls_x509_privkey_export_pkcs8 (gnutls_x509_privkey_t key
,
600 gnutls_x509_crt_fmt_t format
,
601 const char *password
,
604 size_t * output_data_size
)
606 ASN1_TYPE pkcs8_asn
, pkey_info
;
614 return GNUTLS_E_INVALID_REQUEST
;
617 /* Get the private key info
618 * tmp holds the DER encoding.
620 ret
= encode_to_private_key_info (key
, &tmp
, &pkey_info
);
627 schema
= _gnutls_pkcs_flags_to_schema (flags
);
629 if (((flags
& GNUTLS_PKCS_PLAIN
) || password
== NULL
) && !(flags
& GNUTLS_PKCS_NULL_PASSWORD
))
631 _gnutls_free_datum (&tmp
);
634 _gnutls_x509_export_int (pkey_info
, format
,
635 PEM_UNENCRYPTED_PKCS8
,
636 output_data
, output_data_size
);
638 asn1_delete_structure (&pkey_info
);
642 asn1_delete_structure (&pkey_info
); /* we don't need it */
644 ret
= encode_to_pkcs8_key (schema
, &tmp
, password
, &pkcs8_asn
);
645 _gnutls_free_datum (&tmp
);
654 _gnutls_x509_export_int (pkcs8_asn
, format
, PEM_PKCS8
,
655 output_data
, output_data_size
);
657 asn1_delete_structure (&pkcs8_asn
);
664 * gnutls_x509_privkey_export2_pkcs8:
665 * @key: Holds the key
666 * @format: the format of output params. One of PEM or DER.
667 * @password: the password that will be used to encrypt the key.
668 * @flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t
669 * @out: will contain a private key PEM or DER encoded
671 * This function will export the private key to a PKCS8 structure.
672 * Both RSA and DSA keys can be exported. For DSA keys we use
673 * PKCS #11 definitions. If the flags do not specify the encryption
674 * cipher, then the default 3DES (PBES2) will be used.
676 * The @password can be either ASCII or UTF-8 in the default PBES2
677 * encryption schemas, or ASCII for the PKCS12 schemas.
679 * The output buffer is allocated using gnutls_malloc().
681 * If the structure is PEM encoded, it will have a header
682 * of "BEGIN ENCRYPTED PRIVATE KEY" or "BEGIN PRIVATE KEY" if
683 * encryption is not used.
685 * Returns: In case of failure a negative error code will be
686 * returned, and 0 on success.
691 gnutls_x509_privkey_export2_pkcs8 (gnutls_x509_privkey_t key
,
692 gnutls_x509_crt_fmt_t format
,
693 const char *password
,
697 ASN1_TYPE pkcs8_asn
, pkey_info
;
705 return GNUTLS_E_INVALID_REQUEST
;
708 /* Get the private key info
709 * tmp holds the DER encoding.
711 ret
= encode_to_private_key_info (key
, &tmp
, &pkey_info
);
718 schema
= _gnutls_pkcs_flags_to_schema (flags
);
720 if (((flags
& GNUTLS_PKCS_PLAIN
) || password
== NULL
) && !(flags
& GNUTLS_PKCS_NULL_PASSWORD
))
722 _gnutls_free_datum (&tmp
);
725 _gnutls_x509_export_int2 (pkey_info
, format
,
726 PEM_UNENCRYPTED_PKCS8
, out
);
728 asn1_delete_structure (&pkey_info
);
732 asn1_delete_structure (&pkey_info
); /* we don't need it */
734 ret
= encode_to_pkcs8_key (schema
, &tmp
, password
, &pkcs8_asn
);
735 _gnutls_free_datum (&tmp
);
744 _gnutls_x509_export_int2 (pkcs8_asn
, format
, PEM_PKCS8
, out
);
746 asn1_delete_structure (&pkcs8_asn
);
753 /* Read the parameters cipher, IV, salt etc using the given
757 read_pkcs_schema_params (schema_id
* schema
, const char *password
,
758 const uint8_t * data
, int data_size
,
759 struct pbkdf2_params
*kdf_params
,
760 struct pbe_enc_params
*enc_params
)
762 ASN1_TYPE pbes2_asn
= ASN1_TYPE_EMPTY
;
771 /* Now check the key derivation and the encryption
775 asn1_create_element (_gnutls_get_pkix (),
776 "PKIX1.pkcs-5-PBES2-params",
777 &pbes2_asn
)) != ASN1_SUCCESS
)
780 result
= _gnutls_asn2err (result
);
784 /* Decode the parameters.
786 result
= asn1_der_decoding (&pbes2_asn
, data
, data_size
, NULL
);
787 if (result
!= ASN1_SUCCESS
)
790 result
= _gnutls_asn2err (result
);
794 tmp
.data
= (uint8_t *) data
;
795 tmp
.size
= data_size
;
797 result
= read_pbkdf2_params (pbes2_asn
, &tmp
, kdf_params
);
801 result
= _gnutls_asn2err (result
);
805 result
= read_pbe_enc_params (pbes2_asn
, &tmp
, enc_params
);
809 result
= _gnutls_asn2err (result
);
813 asn1_delete_structure (&pbes2_asn
);
815 result
= cipher_to_schema (enc_params
->cipher
);
825 case PKCS12_3DES_SHA1
:
826 case PKCS12_ARCFOUR_SHA1
:
827 case PKCS12_RC2_40_SHA1
:
829 if ((*schema
) == PKCS12_3DES_SHA1
)
831 enc_params
->cipher
= GNUTLS_CIPHER_3DES_CBC
;
832 enc_params
->iv_size
= 8;
834 else if ((*schema
) == PKCS12_ARCFOUR_SHA1
)
836 enc_params
->cipher
= GNUTLS_CIPHER_ARCFOUR_128
;
837 enc_params
->iv_size
= 0;
839 else if ((*schema
) == PKCS12_RC2_40_SHA1
)
841 enc_params
->cipher
= GNUTLS_CIPHER_RC2_40_CBC
;
842 enc_params
->iv_size
= 8;
846 asn1_create_element (_gnutls_get_pkix (),
847 "PKIX1.pkcs-12-PbeParams",
848 &pbes2_asn
)) != ASN1_SUCCESS
)
851 result
= _gnutls_asn2err (result
);
855 /* Decode the parameters.
857 result
= asn1_der_decoding (&pbes2_asn
, data
, data_size
, NULL
);
858 if (result
!= ASN1_SUCCESS
)
861 result
= _gnutls_asn2err (result
);
865 result
= read_pkcs12_kdf_params (pbes2_asn
, kdf_params
);
872 if (enc_params
->iv_size
)
875 _gnutls_pkcs12_string_to_key (2 /*IV*/, kdf_params
->salt
,
876 kdf_params
->salt_size
,
877 kdf_params
->iter_count
, password
,
888 asn1_delete_structure (&pbes2_asn
);
896 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
899 asn1_delete_structure (&pbes2_asn
);
903 static int decrypt_pkcs8_key(const gnutls_datum_t
* raw_key
,
904 ASN1_TYPE pkcs8_asn
, const char *password
,
905 gnutls_x509_privkey_t pkey
)
910 ASN1_TYPE pbes2_asn
= ASN1_TYPE_EMPTY
;
911 int params_start
, params_end
, params_len
;
912 struct pbkdf2_params kdf_params
;
913 struct pbe_enc_params enc_params
;
916 /* Check the encryption schema OID
918 len
= sizeof (enc_oid
);
920 asn1_read_value (pkcs8_asn
, "encryptionAlgorithm.algorithm",
922 if (result
!= ASN1_SUCCESS
)
928 if ((result
= check_schema (enc_oid
)) < 0)
936 /* Get the DER encoding of the parameters.
939 asn1_der_decoding_startEnd (pkcs8_asn
, raw_key
->data
,
941 "encryptionAlgorithm.parameters",
942 ¶ms_start
, ¶ms_end
);
943 if (result
!= ASN1_SUCCESS
)
946 result
= _gnutls_asn2err (result
);
949 params_len
= params_end
- params_start
+ 1;
952 read_pkcs_schema_params (&schema
, password
,
953 &raw_key
->data
[params_start
],
954 params_len
, &kdf_params
, &enc_params
);
962 /* Parameters have been decoded. Now
963 * decrypt the EncryptedData.
966 decrypt_data (schema
, pkcs8_asn
, "encryptedData", password
,
967 &kdf_params
, &enc_params
, &tmp
);
974 result
= decode_private_key_info (&tmp
, pkey
);
975 _gnutls_free_datum (&tmp
);
979 /* We've gotten this far. In the real world it's almost certain
980 * that we're dealing with a good file, but wrong password.
981 * Sadly like 90% of random data is somehow valid DER for the
982 * a first small number of bytes, so no easy way to guarantee. */
983 if (result
== GNUTLS_E_ASN1_ELEMENT_NOT_FOUND
||
984 result
== GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND
||
985 result
== GNUTLS_E_ASN1_DER_ERROR
||
986 result
== GNUTLS_E_ASN1_VALUE_NOT_FOUND
||
987 result
== GNUTLS_E_ASN1_GENERIC_ERROR
||
988 result
== GNUTLS_E_ASN1_VALUE_NOT_VALID
||
989 result
== GNUTLS_E_ASN1_TAG_ERROR
||
990 result
== GNUTLS_E_ASN1_TAG_IMPLICIT
||
991 result
== GNUTLS_E_ASN1_TYPE_ANY_ERROR
||
992 result
== GNUTLS_E_ASN1_SYNTAX_ERROR
||
993 result
== GNUTLS_E_ASN1_DER_OVERFLOW
)
995 result
= GNUTLS_E_DECRYPTION_FAILED
;
1005 asn1_delete_structure (&pbes2_asn
);
1009 /* Converts a PKCS #8 key to
1010 * an internal structure (gnutls_private_key)
1011 * (normally a PKCS #1 encoded RSA key)
1014 decode_pkcs8_key (const gnutls_datum_t
* raw_key
,
1015 const char *password
, gnutls_x509_privkey_t pkey
,
1016 unsigned int decrypt
)
1019 ASN1_TYPE pkcs8_asn
= ASN1_TYPE_EMPTY
;
1022 asn1_create_element (_gnutls_get_pkix (),
1023 "PKIX1.pkcs-8-EncryptedPrivateKeyInfo",
1024 &pkcs8_asn
)) != ASN1_SUCCESS
)
1027 result
= _gnutls_asn2err (result
);
1031 result
= asn1_der_decoding (&pkcs8_asn
, raw_key
->data
, raw_key
->size
, NULL
);
1032 if (result
!= ASN1_SUCCESS
)
1035 result
= _gnutls_asn2err (result
);
1040 result
= decrypt_pkcs8_key(raw_key
, pkcs8_asn
, password
, pkey
);
1045 asn1_delete_structure (&pkcs8_asn
);
1050 /* Decodes an RSA privateKey from a PKCS8 structure.
1053 _decode_pkcs8_rsa_key (ASN1_TYPE pkcs8_asn
, gnutls_x509_privkey_t pkey
)
1058 ret
= _gnutls_x509_read_value (pkcs8_asn
, "privateKey", &tmp
);
1065 pkey
->key
= _gnutls_privkey_decode_pkcs1_rsa_key (&tmp
, pkey
);
1066 _gnutls_free_datum (&tmp
);
1067 if (pkey
->key
== NULL
)
1079 /* Decodes an ECC privateKey from a PKCS8 structure.
1082 _decode_pkcs8_ecc_key (ASN1_TYPE pkcs8_asn
, gnutls_x509_privkey_t pkey
)
1087 ret
= _gnutls_x509_read_value (pkcs8_asn
, "privateKey", &tmp
);
1094 pkey
->key
= _gnutls_privkey_decode_ecc_key (&tmp
, pkey
);
1095 _gnutls_free_datum (&tmp
);
1096 if (pkey
->key
== NULL
)
1098 ret
= GNUTLS_E_PARSING_ERROR
;
1109 /* Decodes an DSA privateKey and params from a PKCS8 structure.
1112 _decode_pkcs8_dsa_key (ASN1_TYPE pkcs8_asn
, gnutls_x509_privkey_t pkey
)
1117 ret
= _gnutls_x509_read_value (pkcs8_asn
, "privateKey", &tmp
);
1124 ret
= _gnutls_x509_read_der_int (tmp
.data
, tmp
.size
, &pkey
->params
.params
[4]);
1125 _gnutls_free_datum (&tmp
);
1134 _gnutls_x509_read_value (pkcs8_asn
, "privateKeyAlgorithm.parameters",
1142 ret
= _gnutls_x509_read_pubkey_params (GNUTLS_PK_DSA
, tmp
.data
, tmp
.size
, &pkey
->params
);
1143 _gnutls_free_datum (&tmp
);
1150 /* the public key can be generated as g^x mod p */
1151 pkey
->params
.params
[3] = _gnutls_mpi_alloc_like (pkey
->params
.params
[0]);
1152 if (pkey
->params
.params
[3] == NULL
)
1158 _gnutls_mpi_powm (pkey
->params
.params
[3], pkey
->params
.params
[2], pkey
->params
.params
[4],
1159 pkey
->params
.params
[0]);
1161 ret
= _gnutls_asn1_encode_privkey (GNUTLS_PK_DSA
, &pkey
->key
, &pkey
->params
);
1168 pkey
->params
.params_nr
= DSA_PRIVATE_PARAMS
;
1178 decode_private_key_info (const gnutls_datum_t
* der
,
1179 gnutls_x509_privkey_t pkey
)
1183 ASN1_TYPE pkcs8_asn
= ASN1_TYPE_EMPTY
;
1187 asn1_create_element (_gnutls_get_pkix (),
1188 "PKIX1.pkcs-8-PrivateKeyInfo",
1189 &pkcs8_asn
)) != ASN1_SUCCESS
)
1192 result
= _gnutls_asn2err (result
);
1196 result
= asn1_der_decoding (&pkcs8_asn
, der
->data
, der
->size
, NULL
);
1197 if (result
!= ASN1_SUCCESS
)
1200 result
= _gnutls_asn2err (result
);
1204 /* Check the private key algorithm OID
1208 asn1_read_value (pkcs8_asn
, "privateKeyAlgorithm.algorithm", oid
, &len
);
1209 if (result
!= ASN1_SUCCESS
)
1212 result
= _gnutls_asn2err (result
);
1216 /* we only support RSA and DSA private keys.
1219 pkey
->pk_algorithm
= _gnutls_x509_oid2pk_algorithm(oid
);
1220 if (pkey
->pk_algorithm
== GNUTLS_PK_UNKNOWN
)
1224 ("PKCS #8 private key OID '%s' is unsupported.\n", oid
);
1225 result
= GNUTLS_E_UNKNOWN_PK_ALGORITHM
;
1229 /* Get the DER encoding of the actual private key.
1232 if (pkey
->pk_algorithm
== GNUTLS_PK_RSA
)
1233 result
= _decode_pkcs8_rsa_key (pkcs8_asn
, pkey
);
1234 else if (pkey
->pk_algorithm
== GNUTLS_PK_DSA
)
1235 result
= _decode_pkcs8_dsa_key (pkcs8_asn
, pkey
);
1236 else if (pkey
->pk_algorithm
== GNUTLS_PK_EC
)
1237 result
= _decode_pkcs8_ecc_key (pkcs8_asn
, pkey
);
1238 else return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE
);
1249 asn1_delete_structure (&pkcs8_asn
);
1256 * gnutls_x509_privkey_import_pkcs8:
1257 * @key: The structure to store the parsed key
1258 * @data: The DER or PEM encoded key.
1259 * @format: One of DER or PEM
1260 * @password: the password to decrypt the key (if it is encrypted).
1261 * @flags: 0 if encrypted or GNUTLS_PKCS_PLAIN if not encrypted.
1263 * This function will convert the given DER or PEM encoded PKCS8 2.0
1264 * encrypted key to the native gnutls_x509_privkey_t format. The
1265 * output will be stored in @key. Both RSA and DSA keys can be
1266 * imported, and flags can only be used to indicate an unencrypted
1269 * The @password can be either ASCII or UTF-8 in the default PBES2
1270 * encryption schemas, or ASCII for the PKCS12 schemas.
1272 * If the Certificate is PEM encoded it should have a header of
1273 * "ENCRYPTED PRIVATE KEY", or "PRIVATE KEY". You only need to
1274 * specify the flags if the key is DER encoded, since in that case
1275 * the encryption status cannot be auto-detected.
1277 * If the %GNUTLS_PKCS_PLAIN flag is specified and the supplied data
1278 * are encrypted then %GNUTLS_E_DECRYPTION_FAILED is returned.
1280 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
1281 * negative error value.
1284 gnutls_x509_privkey_import_pkcs8 (gnutls_x509_privkey_t key
,
1285 const gnutls_datum_t
* data
,
1286 gnutls_x509_crt_fmt_t format
,
1287 const char *password
, unsigned int flags
)
1289 int result
= 0, need_free
= 0;
1290 gnutls_datum_t _data
;
1295 return GNUTLS_E_INVALID_REQUEST
;
1298 _data
.data
= data
->data
;
1299 _data
.size
= data
->size
;
1301 key
->pk_algorithm
= GNUTLS_PK_UNKNOWN
;
1303 /* If the Certificate is in PEM format then decode it
1305 if (format
== GNUTLS_X509_FMT_PEM
)
1307 /* Try the first header
1310 _gnutls_fbase64_decode (PEM_UNENCRYPTED_PKCS8
,
1311 data
->data
, data
->size
, &_data
);
1314 { /* Try the encrypted header
1317 _gnutls_fbase64_decode (PEM_PKCS8
, data
->data
, data
->size
, &_data
);
1325 else if (flags
== 0)
1326 flags
|= GNUTLS_PKCS_PLAIN
;
1331 /* Here we don't check for password == NULL to maintain a backwards
1332 * compatibility behavior, with old versions that were encrypting using
1335 if (flags
& GNUTLS_PKCS_PLAIN
)
1337 result
= decode_private_key_info (&_data
, key
);
1339 { /* check if it is encrypted */
1340 if (decode_pkcs8_key(&_data
, "", key
, 0) == 0)
1341 result
= GNUTLS_E_DECRYPTION_FAILED
;
1346 result
= decode_pkcs8_key (&_data
, password
, key
, 1);
1356 _gnutls_free_datum (&_data
);
1358 /* The key has now been decoded.
1364 key
->pk_algorithm
= GNUTLS_PK_UNKNOWN
;
1366 _gnutls_free_datum (&_data
);
1370 /* Reads the PBKDF2 parameters.
1373 read_pbkdf2_params (ASN1_TYPE pbes2_asn
,
1374 const gnutls_datum_t
* der
, struct pbkdf2_params
*params
)
1376 int params_start
, params_end
;
1377 int params_len
, len
, result
;
1378 ASN1_TYPE pbkdf2_asn
= ASN1_TYPE_EMPTY
;
1381 memset (params
, 0, sizeof (*params
));
1383 /* Check the key derivation algorithm
1387 asn1_read_value (pbes2_asn
, "keyDerivationFunc.algorithm", oid
, &len
);
1388 if (result
!= ASN1_SUCCESS
)
1391 return _gnutls_asn2err (result
);
1393 _gnutls_hard_log ("keyDerivationFunc.algorithm: %s\n", oid
);
1395 if (strcmp (oid
, PBKDF2_OID
) != 0)
1399 ("PKCS #8 key derivation OID '%s' is unsupported.\n", oid
);
1400 return _gnutls_asn2err (result
);
1404 asn1_der_decoding_startEnd (pbes2_asn
, der
->data
, der
->size
,
1405 "keyDerivationFunc.parameters",
1406 ¶ms_start
, ¶ms_end
);
1407 if (result
!= ASN1_SUCCESS
)
1410 return _gnutls_asn2err (result
);
1412 params_len
= params_end
- params_start
+ 1;
1414 /* Now check the key derivation and the encryption
1418 asn1_create_element (_gnutls_get_pkix (),
1419 "PKIX1.pkcs-5-PBKDF2-params",
1420 &pbkdf2_asn
)) != ASN1_SUCCESS
)
1423 return _gnutls_asn2err (result
);
1427 asn1_der_decoding (&pbkdf2_asn
, &der
->data
[params_start
],
1429 if (result
!= ASN1_SUCCESS
)
1432 result
= _gnutls_asn2err (result
);
1437 params
->salt_size
= sizeof (params
->salt
);
1439 asn1_read_value (pbkdf2_asn
, "salt.specified", params
->salt
,
1440 ¶ms
->salt_size
);
1441 if (result
!= ASN1_SUCCESS
)
1444 result
= _gnutls_asn2err (result
);
1447 _gnutls_hard_log ("salt.specified.size: %d\n", params
->salt_size
);
1449 /* read the iteration count
1452 _gnutls_x509_read_uint (pbkdf2_asn
, "iterationCount",
1453 ¶ms
->iter_count
);
1454 if (result
!= ASN1_SUCCESS
)
1459 _gnutls_hard_log ("iterationCount: %d\n", params
->iter_count
);
1461 /* read the keylength, if it is set.
1464 _gnutls_x509_read_uint (pbkdf2_asn
, "keyLength", ¶ms
->key_size
);
1467 params
->key_size
= 0;
1469 _gnutls_hard_log ("keyLength: %d\n", params
->key_size
);
1471 /* We don't read the PRF. We only use the default.
1477 asn1_delete_structure (&pbkdf2_asn
);
1482 /* Reads the PBE parameters from PKCS-12 schemas (*&#%*&#% RSA).
1485 read_pkcs12_kdf_params (ASN1_TYPE pbes2_asn
, struct pbkdf2_params
*params
)
1489 memset (params
, 0, sizeof (*params
));
1492 params
->salt_size
= sizeof (params
->salt
);
1494 asn1_read_value (pbes2_asn
, "salt", params
->salt
, ¶ms
->salt_size
);
1495 if (result
!= ASN1_SUCCESS
)
1498 result
= _gnutls_asn2err (result
);
1501 _gnutls_hard_log ("salt.size: %d\n", params
->salt_size
);
1503 /* read the iteration count
1506 _gnutls_x509_read_uint (pbes2_asn
, "iterations", ¶ms
->iter_count
);
1507 if (result
!= ASN1_SUCCESS
)
1512 _gnutls_hard_log ("iterationCount: %d\n", params
->iter_count
);
1514 params
->key_size
= 0;
1523 /* Writes the PBE parameters for PKCS-12 schemas.
1526 write_pkcs12_kdf_params (ASN1_TYPE pbes2_asn
,
1527 const struct pbkdf2_params
*kdf_params
)
1534 asn1_write_value (pbes2_asn
, "salt",
1535 kdf_params
->salt
, kdf_params
->salt_size
);
1536 if (result
!= ASN1_SUCCESS
)
1539 result
= _gnutls_asn2err (result
);
1542 _gnutls_hard_log ("salt.size: %d\n", kdf_params
->salt_size
);
1544 /* write the iteration count
1547 _gnutls_x509_write_uint32 (pbes2_asn
, "iterations",
1548 kdf_params
->iter_count
);
1554 _gnutls_hard_log ("iterationCount: %d\n", kdf_params
->iter_count
);
1564 /* Converts an OID to a gnutls cipher type.
1567 oid2cipher (const char *oid
, gnutls_cipher_algorithm_t
* algo
)
1572 if (strcmp (oid
, DES_EDE3_CBC_OID
) == 0)
1574 *algo
= GNUTLS_CIPHER_3DES_CBC
;
1577 else if (strcmp (oid
, DES_CBC_OID
) == 0)
1579 *algo
= GNUTLS_CIPHER_DES_CBC
;
1582 else if (strcmp (oid
, AES_128_CBC_OID
) == 0)
1584 *algo
= GNUTLS_CIPHER_AES_128_CBC
;
1587 else if (strcmp (oid
, AES_192_CBC_OID
) == 0)
1589 *algo
= GNUTLS_CIPHER_AES_192_CBC
;
1592 else if (strcmp (oid
, AES_256_CBC_OID
) == 0)
1594 *algo
= GNUTLS_CIPHER_AES_256_CBC
;
1598 _gnutls_debug_log ("PKCS #8 encryption OID '%s' is unsupported.\n", oid
);
1599 return GNUTLS_E_UNKNOWN_CIPHER_TYPE
;
1605 read_pbe_enc_params (ASN1_TYPE pbes2_asn
,
1606 const gnutls_datum_t
* der
,
1607 struct pbe_enc_params
*params
)
1609 int params_start
, params_end
;
1610 int params_len
, len
, result
;
1611 ASN1_TYPE pbe_asn
= ASN1_TYPE_EMPTY
;
1613 const char *eparams
;
1615 memset (params
, 0, sizeof (*params
));
1617 /* Check the encryption algorithm
1621 asn1_read_value (pbes2_asn
, "encryptionScheme.algorithm", oid
, &len
);
1622 if (result
!= ASN1_SUCCESS
)
1627 _gnutls_hard_log ("encryptionScheme.algorithm: %s\n", oid
);
1629 if ((result
= oid2cipher (oid
, ¶ms
->cipher
)) < 0)
1636 asn1_der_decoding_startEnd (pbes2_asn
, der
->data
, der
->size
,
1637 "encryptionScheme.parameters",
1638 ¶ms_start
, ¶ms_end
);
1639 if (result
!= ASN1_SUCCESS
)
1642 return _gnutls_asn2err (result
);
1644 params_len
= params_end
- params_start
+ 1;
1646 /* Now check the encryption parameters.
1648 eparams
= cipher_to_pkcs_params (params
->cipher
, NULL
);
1649 if (eparams
== NULL
)
1652 return GNUTLS_E_INVALID_REQUEST
;
1656 asn1_create_element (_gnutls_get_pkix (),
1657 eparams
, &pbe_asn
)) != ASN1_SUCCESS
)
1660 return _gnutls_asn2err (result
);
1664 asn1_der_decoding (&pbe_asn
, &der
->data
[params_start
], params_len
, NULL
);
1665 if (result
!= ASN1_SUCCESS
)
1668 result
= _gnutls_asn2err (result
);
1673 params
->iv_size
= sizeof (params
->iv
);
1674 result
= asn1_read_value (pbe_asn
, "", params
->iv
, ¶ms
->iv_size
);
1675 if (result
!= ASN1_SUCCESS
)
1678 result
= _gnutls_asn2err (result
);
1681 _gnutls_hard_log ("IV.size: %d\n", params
->iv_size
);
1686 asn1_delete_structure (&pbe_asn
);
1692 decrypt_data (schema_id schema
, ASN1_TYPE pkcs8_asn
,
1693 const char *root
, const char *password
,
1694 const struct pbkdf2_params
*kdf_params
,
1695 const struct pbe_enc_params
*enc_params
,
1696 gnutls_datum_t
* decrypted_data
)
1700 uint8_t *data
= NULL
, *key
= NULL
;
1701 gnutls_datum_t dkey
, d_iv
;
1705 unsigned int pass_len
= 0;
1708 pass_len
= strlen(password
);
1711 result
= asn1_read_value (pkcs8_asn
, root
, NULL
, &data_size
);
1712 if (result
!= ASN1_MEM_ERROR
)
1715 return _gnutls_asn2err (result
);
1718 data
= gnutls_malloc (data_size
);
1722 return GNUTLS_E_MEMORY_ERROR
;
1725 result
= asn1_read_value (pkcs8_asn
, root
, data
, &data_size
);
1726 if (result
!= ASN1_SUCCESS
)
1729 result
= _gnutls_asn2err (result
);
1733 if (kdf_params
->key_size
== 0)
1735 key_size
= gnutls_cipher_get_key_size (enc_params
->cipher
);
1738 key_size
= kdf_params
->key_size
;
1740 key
= gnutls_malloc (key_size
);
1744 result
= GNUTLS_E_MEMORY_ERROR
;
1757 result
= _gnutls_pbkdf2_sha1 (password
, pass_len
,
1758 kdf_params
->salt
, kdf_params
->salt_size
,
1759 kdf_params
->iter_count
, key
, key_size
);
1769 _gnutls_pkcs12_string_to_key (1 /*KEY*/, kdf_params
->salt
,
1770 kdf_params
->salt_size
,
1771 kdf_params
->iter_count
, password
,
1781 /* do the decryption.
1784 dkey
.size
= key_size
;
1786 d_iv
.data
= (uint8_t *) enc_params
->iv
;
1787 d_iv
.size
= enc_params
->iv_size
;
1788 result
= _gnutls_cipher_init (&ch
, enc_params
->cipher
, &dkey
, &d_iv
, 0);
1801 result
= _gnutls_cipher_decrypt (&ch
, data
, data_size
);
1808 decrypted_data
->data
= data
;
1810 if (gnutls_cipher_get_block_size (enc_params
->cipher
) != 1)
1811 decrypted_data
->size
= data_size
- data
[data_size
- 1];
1813 decrypted_data
->size
= data_size
;
1815 _gnutls_cipher_deinit (&ch
);
1823 _gnutls_cipher_deinit (&ch
);
1828 /* Writes the PBKDF2 parameters.
1831 write_pbkdf2_params (ASN1_TYPE pbes2_asn
,
1832 const struct pbkdf2_params
*kdf_params
)
1835 ASN1_TYPE pbkdf2_asn
= ASN1_TYPE_EMPTY
;
1838 /* Write the key derivation algorithm
1841 asn1_write_value (pbes2_asn
, "keyDerivationFunc.algorithm",
1843 if (result
!= ASN1_SUCCESS
)
1846 return _gnutls_asn2err (result
);
1849 /* Now write the key derivation and the encryption
1853 asn1_create_element (_gnutls_get_pkix (),
1854 "PKIX1.pkcs-5-PBKDF2-params",
1855 &pbkdf2_asn
)) != ASN1_SUCCESS
)
1858 return _gnutls_asn2err (result
);
1861 result
= asn1_write_value (pbkdf2_asn
, "salt", "specified", 1);
1862 if (result
!= ASN1_SUCCESS
)
1865 result
= _gnutls_asn2err (result
);
1872 asn1_write_value (pbkdf2_asn
, "salt.specified",
1873 kdf_params
->salt
, kdf_params
->salt_size
);
1874 if (result
!= ASN1_SUCCESS
)
1877 result
= _gnutls_asn2err (result
);
1880 _gnutls_hard_log ("salt.specified.size: %d\n", kdf_params
->salt_size
);
1882 /* write the iteration count
1884 _gnutls_write_uint32 (kdf_params
->iter_count
, tmp
);
1886 result
= asn1_write_value (pbkdf2_asn
, "iterationCount", tmp
, 4);
1887 if (result
!= ASN1_SUCCESS
)
1890 result
= _gnutls_asn2err (result
);
1893 _gnutls_hard_log ("iterationCount: %d\n", kdf_params
->iter_count
);
1895 /* write the keylength, if it is set.
1897 result
= asn1_write_value (pbkdf2_asn
, "keyLength", NULL
, 0);
1898 if (result
!= ASN1_SUCCESS
)
1901 result
= _gnutls_asn2err (result
);
1905 /* We write an emptry prf.
1907 result
= asn1_write_value (pbkdf2_asn
, "prf", NULL
, 0);
1908 if (result
!= ASN1_SUCCESS
)
1911 result
= _gnutls_asn2err (result
);
1915 /* now encode them an put the DER output
1916 * in the keyDerivationFunc.parameters
1918 result
= _gnutls_x509_der_encode_and_copy (pbkdf2_asn
, "",
1920 "keyDerivationFunc.parameters",
1931 asn1_delete_structure (&pbkdf2_asn
);
1938 write_pbe_enc_params (ASN1_TYPE pbes2_asn
,
1939 const struct pbe_enc_params
*params
)
1942 ASN1_TYPE pbe_asn
= ASN1_TYPE_EMPTY
;
1943 const char *oid
, *eparams
;
1945 /* Write the encryption algorithm
1947 eparams
= cipher_to_pkcs_params (params
->cipher
, &oid
);
1948 if (eparams
== NULL
)
1951 return GNUTLS_E_INVALID_REQUEST
;
1954 result
= asn1_write_value (pbes2_asn
, "encryptionScheme.algorithm", oid
, 1);
1955 if (result
!= ASN1_SUCCESS
)
1960 _gnutls_hard_log ("encryptionScheme.algorithm: %s\n", oid
);
1962 /* Now check the encryption parameters.
1965 asn1_create_element (_gnutls_get_pkix (),
1966 eparams
, &pbe_asn
)) != ASN1_SUCCESS
)
1969 return _gnutls_asn2err (result
);
1973 result
= asn1_write_value (pbe_asn
, "", params
->iv
, params
->iv_size
);
1974 if (result
!= ASN1_SUCCESS
)
1977 result
= _gnutls_asn2err (result
);
1980 _gnutls_hard_log ("IV.size: %d\n", params
->iv_size
);
1982 /* now encode them an put the DER output
1983 * in the encryptionScheme.parameters
1985 result
= _gnutls_x509_der_encode_and_copy (pbe_asn
, "",
1987 "encryptionScheme.parameters",
1998 asn1_delete_structure (&pbe_asn
);
2003 /* Generates a key and also stores the key parameters.
2006 generate_key (schema_id schema
,
2007 const char *password
,
2008 struct pbkdf2_params
*kdf_params
,
2009 struct pbe_enc_params
*enc_params
, gnutls_datum_t
* key
)
2011 unsigned char rnd
[2];
2012 unsigned int pass_len
= 0;
2016 pass_len
= strlen(password
);
2018 ret
= _gnutls_rnd (GNUTLS_RND_RANDOM
, rnd
, 2);
2026 kdf_params
->salt_size
=
2027 MIN (sizeof (kdf_params
->salt
), (unsigned) (10 + (rnd
[1] % 10)));
2032 enc_params
->cipher
= GNUTLS_CIPHER_3DES_CBC
;
2035 enc_params
->cipher
= GNUTLS_CIPHER_AES_128_CBC
;
2038 enc_params
->cipher
= GNUTLS_CIPHER_AES_192_CBC
;
2041 enc_params
->cipher
= GNUTLS_CIPHER_AES_256_CBC
;
2043 /* non PBES2 algorithms */
2044 case PKCS12_ARCFOUR_SHA1
:
2045 enc_params
->cipher
= GNUTLS_CIPHER_ARCFOUR_128
;
2046 kdf_params
->salt_size
= 8;
2048 case PKCS12_3DES_SHA1
:
2049 enc_params
->cipher
= GNUTLS_CIPHER_3DES_CBC
;
2050 kdf_params
->salt_size
= 8;
2052 case PKCS12_RC2_40_SHA1
:
2053 enc_params
->cipher
= GNUTLS_CIPHER_RC2_40_CBC
;
2054 kdf_params
->salt_size
= 8;
2058 return GNUTLS_E_INVALID_REQUEST
;
2061 ret
= _gnutls_rnd (GNUTLS_RND_RANDOM
, kdf_params
->salt
,
2062 kdf_params
->salt_size
);
2066 return GNUTLS_E_RANDOM_FAILED
;
2069 kdf_params
->iter_count
= 256 + rnd
[0];
2070 key
->size
= kdf_params
->key_size
=
2071 gnutls_cipher_get_key_size (enc_params
->cipher
);
2073 enc_params
->iv_size
= _gnutls_cipher_get_iv_size (enc_params
->cipher
);
2074 key
->data
= gnutls_malloc (key
->size
);
2075 if (key
->data
== NULL
)
2078 return GNUTLS_E_MEMORY_ERROR
;
2081 /* now generate the key.
2091 ret
= _gnutls_pbkdf2_sha1 (password
, pass_len
,
2092 kdf_params
->salt
, kdf_params
->salt_size
,
2093 kdf_params
->iter_count
,
2094 key
->data
, kdf_params
->key_size
);
2101 if (enc_params
->iv_size
)
2103 ret
= _gnutls_rnd (GNUTLS_RND_NONCE
,
2104 enc_params
->iv
, enc_params
->iv_size
);
2115 _gnutls_pkcs12_string_to_key (1 /*KEY*/, kdf_params
->salt
,
2116 kdf_params
->salt_size
,
2117 kdf_params
->iter_count
, password
,
2118 kdf_params
->key_size
, key
->data
);
2125 /* Now generate the IV
2127 if (enc_params
->iv_size
)
2130 _gnutls_pkcs12_string_to_key (2 /*IV*/, kdf_params
->salt
,
2131 kdf_params
->salt_size
,
2132 kdf_params
->iter_count
, password
,
2133 enc_params
->iv_size
,
2148 /* Encodes the parameters to be written in the encryptionAlgorithm.parameters
2152 write_schema_params (schema_id schema
, ASN1_TYPE pkcs8_asn
,
2154 const struct pbkdf2_params
*kdf_params
,
2155 const struct pbe_enc_params
*enc_params
)
2158 ASN1_TYPE pbes2_asn
= ASN1_TYPE_EMPTY
;
2167 asn1_create_element (_gnutls_get_pkix (),
2168 "PKIX1.pkcs-5-PBES2-params",
2169 &pbes2_asn
)) != ASN1_SUCCESS
)
2172 return _gnutls_asn2err (result
);
2175 result
= write_pbkdf2_params (pbes2_asn
, kdf_params
);
2182 result
= write_pbe_enc_params (pbes2_asn
, enc_params
);
2189 result
= _gnutls_x509_der_encode_and_copy (pbes2_asn
, "",
2190 pkcs8_asn
, where
, 0);
2197 asn1_delete_structure (&pbes2_asn
);
2203 asn1_create_element (_gnutls_get_pkix (),
2204 "PKIX1.pkcs-12-PbeParams",
2205 &pbes2_asn
)) != ASN1_SUCCESS
)
2208 result
= _gnutls_asn2err (result
);
2212 result
= write_pkcs12_kdf_params (pbes2_asn
, kdf_params
);
2219 result
= _gnutls_x509_der_encode_and_copy (pbes2_asn
, "",
2220 pkcs8_asn
, where
, 0);
2227 asn1_delete_structure (&pbes2_asn
);
2234 asn1_delete_structure (&pbes2_asn
);
2240 encrypt_data (const gnutls_datum_t
* plain
,
2241 const struct pbe_enc_params
*enc_params
,
2242 gnutls_datum_t
* key
, gnutls_datum_t
* encrypted
)
2246 uint8_t *data
= NULL
;
2247 gnutls_datum_t d_iv
;
2250 uint8_t pad
, pad_size
;
2252 pad_size
= gnutls_cipher_get_block_size (enc_params
->cipher
);
2254 if (pad_size
== 1) /* stream */
2257 data
= gnutls_malloc (plain
->size
+ pad_size
);
2261 return GNUTLS_E_MEMORY_ERROR
;
2264 memcpy (data
, plain
->data
, plain
->size
);
2268 pad
= pad_size
- (plain
->size
% pad_size
);
2271 memset (&data
[plain
->size
], pad
, pad
);
2276 data_size
= plain
->size
+ pad
;
2278 d_iv
.data
= (uint8_t *) enc_params
->iv
;
2279 d_iv
.size
= enc_params
->iv_size
;
2280 result
= _gnutls_cipher_init (&ch
, enc_params
->cipher
, key
, &d_iv
, 1);
2290 result
= _gnutls_cipher_encrypt (&ch
, data
, data_size
);
2297 encrypted
->data
= data
;
2298 encrypted
->size
= data_size
;
2300 _gnutls_cipher_deinit (&ch
);
2307 _gnutls_cipher_deinit (&ch
);
2311 /* Decrypts a PKCS #7 encryptedData. The output is allocated
2312 * and stored in dec.
2315 _gnutls_pkcs7_decrypt_data (const gnutls_datum_t
* data
,
2316 const char *password
, gnutls_datum_t
* dec
)
2321 ASN1_TYPE pbes2_asn
= ASN1_TYPE_EMPTY
, pkcs7_asn
= ASN1_TYPE_EMPTY
;
2322 int params_start
, params_end
, params_len
;
2323 struct pbkdf2_params kdf_params
;
2324 struct pbe_enc_params enc_params
;
2328 asn1_create_element (_gnutls_get_pkix (),
2329 "PKIX1.pkcs-7-EncryptedData",
2330 &pkcs7_asn
)) != ASN1_SUCCESS
)
2333 result
= _gnutls_asn2err (result
);
2337 result
= asn1_der_decoding (&pkcs7_asn
, data
->data
, data
->size
, NULL
);
2338 if (result
!= ASN1_SUCCESS
)
2341 result
= _gnutls_asn2err (result
);
2345 /* Check the encryption schema OID
2347 len
= sizeof (enc_oid
);
2349 asn1_read_value (pkcs7_asn
,
2350 "encryptedContentInfo.contentEncryptionAlgorithm.algorithm",
2352 if (result
!= ASN1_SUCCESS
)
2355 result
= _gnutls_asn2err (result
);
2359 if ((result
= check_schema (enc_oid
)) < 0)
2366 /* Get the DER encoding of the parameters.
2369 asn1_der_decoding_startEnd (pkcs7_asn
, data
->data
, data
->size
,
2370 "encryptedContentInfo.contentEncryptionAlgorithm.parameters",
2371 ¶ms_start
, ¶ms_end
);
2372 if (result
!= ASN1_SUCCESS
)
2375 result
= _gnutls_asn2err (result
);
2378 params_len
= params_end
- params_start
+ 1;
2381 read_pkcs_schema_params (&schema
, password
,
2382 &data
->data
[params_start
],
2383 params_len
, &kdf_params
, &enc_params
);
2384 if (result
< ASN1_SUCCESS
)
2387 result
= _gnutls_asn2err (result
);
2391 /* Parameters have been decoded. Now
2392 * decrypt the EncryptedData.
2396 decrypt_data (schema
, pkcs7_asn
,
2397 "encryptedContentInfo.encryptedContent", password
,
2398 &kdf_params
, &enc_params
, &tmp
);
2405 asn1_delete_structure (&pkcs7_asn
);
2412 asn1_delete_structure (&pbes2_asn
);
2413 asn1_delete_structure (&pkcs7_asn
);
2417 /* Encrypts to a PKCS #7 encryptedData. The output is allocated
2418 * and stored in enc.
2421 _gnutls_pkcs7_encrypt_data (schema_id schema
,
2422 const gnutls_datum_t
* data
,
2423 const char *password
, gnutls_datum_t
* enc
)
2426 gnutls_datum_t key
= { NULL
, 0 };
2427 gnutls_datum_t tmp
= { NULL
, 0 };
2428 ASN1_TYPE pkcs7_asn
= ASN1_TYPE_EMPTY
;
2429 struct pbkdf2_params kdf_params
;
2430 struct pbe_enc_params enc_params
;
2431 const char *str_oid
;
2434 asn1_create_element (_gnutls_get_pkix (),
2435 "PKIX1.pkcs-7-EncryptedData",
2436 &pkcs7_asn
)) != ASN1_SUCCESS
)
2439 result
= _gnutls_asn2err (result
);
2443 /* Write the encryption schema OID
2445 result
= schema_to_oid (schema
, &str_oid
);
2453 asn1_write_value (pkcs7_asn
,
2454 "encryptedContentInfo.contentEncryptionAlgorithm.algorithm",
2457 if (result
!= ASN1_SUCCESS
)
2460 result
= _gnutls_asn2err (result
);
2464 /* Generate a symmetric key.
2467 result
= generate_key (schema
, password
, &kdf_params
, &enc_params
, &key
);
2474 result
= write_schema_params (schema
, pkcs7_asn
,
2475 "encryptedContentInfo.contentEncryptionAlgorithm.parameters",
2476 &kdf_params
, &enc_params
);
2483 /* Parameters have been encoded. Now
2486 result
= encrypt_data (data
, &enc_params
, &key
, &tmp
);
2493 /* write the encrypted data.
2496 asn1_write_value (pkcs7_asn
,
2497 "encryptedContentInfo.encryptedContent", tmp
.data
,
2499 if (result
!= ASN1_SUCCESS
)
2502 result
= _gnutls_asn2err (result
);
2506 _gnutls_free_datum (&tmp
);
2507 _gnutls_free_datum (&key
);
2509 /* Now write the rest of the pkcs-7 stuff.
2512 result
= _gnutls_x509_write_uint32 (pkcs7_asn
, "version", 0);
2520 asn1_write_value (pkcs7_asn
, "encryptedContentInfo.contentType",
2522 if (result
!= ASN1_SUCCESS
)
2525 result
= _gnutls_asn2err (result
);
2529 result
= asn1_write_value (pkcs7_asn
, "unprotectedAttrs", NULL
, 0);
2530 if (result
!= ASN1_SUCCESS
)
2533 result
= _gnutls_asn2err (result
);
2537 /* Now encode and copy the DER stuff.
2539 result
= _gnutls_x509_der_encode (pkcs7_asn
, "", enc
, 0);
2541 asn1_delete_structure (&pkcs7_asn
);
2551 _gnutls_free_datum (&key
);
2552 _gnutls_free_datum (&tmp
);
2553 asn1_delete_structure (&pkcs7_asn
);