1 /*
2  * Copyright (C) 2003-2016 Free Software Foundation, Inc.
3  * Copyright (C) 2016 Red Hat, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
9  * The GnuTLS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program.  If not, see <https://www.gnu.org/licenses/>
21  *
22  */
23 
24 #ifndef GNUTLS_LIB_X509_PKCS7_INT_H
25 #define GNUTLS_LIB_X509_PKCS7_INT_H
26 
27 #include <gnutls/x509.h>
28 
29 /* PKCS #7
30  */
31 #define DATA_OID "1.2.840.113549.1.7.1"
32 #define ENC_DATA_OID "1.2.840.113549.1.7.6"
33 
34 #define SIGNED_DATA_OID "1.2.840.113549.1.7.2"
35 #define DIGESTED_DATA_OID "1.2.840.113549.1.7.5"
36 
37 
38 typedef enum schema_id {
39 	PBES2_GENERIC=1,	/* when the algorithm is unknown, temporal use when reading only */
40 	PBES2_DES,		/* the stuff in PKCS #5 */
41 	PBES2_3DES,
42 	PBES2_AES_128,
43 	PBES2_AES_192,
44 	PBES2_AES_256,
45 	PBES2_GOST28147_89_TC26Z,
46 	PBES2_GOST28147_89_CPA,
47 	PBES2_GOST28147_89_CPB,
48 	PBES2_GOST28147_89_CPC,
49 	PBES2_GOST28147_89_CPD,
50 	PKCS12_3DES_SHA1,	/* the stuff in PKCS #12 */
51 	PKCS12_ARCFOUR_SHA1,
52 	PKCS12_RC2_40_SHA1,
53 	PBES1_DES_MD5		/* openssl before 1.1.0 uses that by default */
54 } schema_id;
55 
56 struct pkcs_cipher_schema_st {
57 	unsigned int schema;
58 	const char *name;
59 	unsigned int flag;
60 	unsigned int cipher;
61 	unsigned pbes2;
62 	const char *cipher_oid;
63 	const char *write_oid;
64 	const char *desc;
65 	const char *iv_name;
66 	unsigned decrypt_only;
67 };
68 
69 const struct pkcs_cipher_schema_st *_gnutls_pkcs_schema_get(schema_id schema);
70 
71 struct pbe_enc_params {
72 	gnutls_cipher_algorithm_t cipher;
73 	uint8_t iv[MAX_CIPHER_BLOCK_SIZE];
74 	int iv_size;
75 	char pbes2_oid[MAX_OID_SIZE]; /* when reading params, the OID is stored for info purposes */
76 };
77 
78 int
79 _gnutls_decrypt_pbes1_des_md5_data(const char *password,
80 			   unsigned password_len,
81 			   const struct pbkdf2_params *kdf_params,
82 			   const struct pbe_enc_params *enc_params,
83 			   const gnutls_datum_t *encrypted_data,
84 			   gnutls_datum_t *decrypted_data);
85 
86 int _gnutls_check_pkcs_cipher_schema(const char *oid);
87 
88 int
89 _gnutls_pkcs_raw_decrypt_data(schema_id schema, ASN1_TYPE pkcs8_asn,
90 	     const char *root, const char *password,
91 	     const struct pbkdf2_params *kdf_params,
92 	     const struct pbe_enc_params *enc_params,
93 	     gnutls_datum_t *decrypted_data);
94 
95 int
96 _gnutls_pkcs_raw_encrypt_data(const gnutls_datum_t * plain,
97 	     const struct pbe_enc_params *enc_params,
98 	     const gnutls_datum_t * key, gnutls_datum_t * encrypted);
99 
100 int _gnutls_pkcs7_decrypt_data(const gnutls_datum_t * data,
101 			       const char *password, gnutls_datum_t * dec);
102 
103 int _gnutls_read_pbkdf1_params(const uint8_t * data, int data_size,
104 		       struct pbkdf2_params *kdf_params,
105 		       struct pbe_enc_params *enc_params);
106 
107 int
108 _gnutls_read_pkcs_schema_params(schema_id * schema, const char *password,
109 			const uint8_t * data, int data_size,
110 			struct pbkdf2_params *kdf_params,
111 			struct pbe_enc_params *enc_params);
112 
113 int
114 _gnutls_pkcs_write_schema_params(schema_id schema, ASN1_TYPE pkcs8_asn,
115 		    const char *where,
116 		    const struct pbkdf2_params *kdf_params,
117 		    const struct pbe_enc_params *enc_params);
118 
119 int
120 _gnutls_pkcs_generate_key(schema_id schema,
121 	     const char *password,
122 	     struct pbkdf2_params *kdf_params,
123 	     struct pbe_enc_params *enc_params, gnutls_datum_t * key);
124 
125 int _gnutls_pkcs_flags_to_schema(unsigned int flags);
126 int _gnutls_pkcs7_encrypt_data(schema_id schema,
127 			       const gnutls_datum_t * data,
128 			       const char *password, gnutls_datum_t * enc);
129 
130 int
131 _gnutls_pkcs7_data_enc_info(const gnutls_datum_t * data, const struct pkcs_cipher_schema_st **p,
132 	struct pbkdf2_params *kdf_params, char **oid);
133 
134 #endif /* GNUTLS_LIB_X509_PKCS7_INT_H */
135