]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/crmf/crmf_pbm.c
OSSL_PARAM_construct_utf8_string computes the string length.
[thirdparty/openssl.git] / crypto / crmf / crmf_pbm.c
1 /*-
2 * Copyright 2007-2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
5 *
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
10 *
11 * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb.
12 */
13
14
15 #include <string.h>
16
17 #include <openssl/rand.h>
18 #include <openssl/evp.h>
19
20 #include "crmf_int.h"
21
22 /* explicit #includes not strictly needed since implied by the above: */
23 #include <openssl/asn1t.h>
24 #include <openssl/crmf.h>
25 #include <openssl/err.h>
26 #include <openssl/evp.h>
27 #include <openssl/params.h>
28 #include <openssl/core_names.h>
29
30 /*-
31 * creates and initializes OSSL_CRMF_PBMPARAMETER (section 4.4)
32 * |slen| SHOULD be > 8 (16 is common)
33 * |owfnid| e.g., NID_sha256
34 * |itercnt| MUST be > 100 (500 is common)
35 * |macnid| e.g., NID_hmac_sha1
36 * returns pointer to OSSL_CRMF_PBMPARAMETER on success, NULL on error
37 */
38 OSSL_CRMF_PBMPARAMETER *OSSL_CRMF_pbmp_new(size_t slen, int owfnid,
39 int itercnt, int macnid)
40 {
41 OSSL_CRMF_PBMPARAMETER *pbm = NULL;
42 unsigned char *salt = NULL;
43
44 if ((pbm = OSSL_CRMF_PBMPARAMETER_new()) == NULL) {
45 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, ERR_R_MALLOC_FAILURE);
46 goto err;
47 }
48
49 /*
50 * salt contains a randomly generated value used in computing the key
51 * of the MAC process. The salt SHOULD be at least 8 octets (64
52 * bits) long.
53 */
54 if ((salt = OPENSSL_malloc(slen)) == NULL) {
55 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, ERR_R_MALLOC_FAILURE);
56 goto err;
57 }
58 if (RAND_bytes(salt, (int)slen) <= 0) {
59 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_FAILURE_OBTAINING_RANDOM);
60 goto err;
61 }
62 if (!ASN1_OCTET_STRING_set(pbm->salt, salt, (int)slen))
63 goto err;
64
65 /*
66 * owf identifies the hash algorithm and associated parameters used to
67 * compute the key used in the MAC process. All implementations MUST
68 * support SHA-1.
69 */
70 if (!X509_ALGOR_set0(pbm->owf, OBJ_nid2obj(owfnid), V_ASN1_UNDEF, NULL)) {
71 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_SETTING_OWF_ALGOR_FAILURE);
72 goto err;
73 }
74
75 /*
76 * iterationCount identifies the number of times the hash is applied
77 * during the key computation process. The iterationCount MUST be a
78 * minimum of 100. Many people suggest using values as high as 1000
79 * iterations as the minimum value. The trade off here is between
80 * protection of the password from attacks and the time spent by the
81 * server processing all of the different iterations in deriving
82 * passwords. Hashing is generally considered a cheap operation but
83 * this may not be true with all hash functions in the future.
84 */
85 if (itercnt < 100) {
86 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_ITERATIONCOUNT_BELOW_100);
87 goto err;
88 }
89
90 if (!ASN1_INTEGER_set(pbm->iterationCount, itercnt)) {
91 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_CRMFERROR);
92 goto err;
93 }
94
95 /*
96 * mac identifies the algorithm and associated parameters of the MAC
97 * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
98 * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
99 */
100 if (!X509_ALGOR_set0(pbm->mac, OBJ_nid2obj(macnid), V_ASN1_UNDEF, NULL)) {
101 CRMFerr(CRMF_F_OSSL_CRMF_PBMP_NEW, CRMF_R_SETTING_MAC_ALGOR_FAILURE);
102 goto err;
103 }
104
105 OPENSSL_free(salt);
106 return pbm;
107 err:
108 OPENSSL_free(salt);
109 OSSL_CRMF_PBMPARAMETER_free(pbm);
110 return NULL;
111 }
112
113 /*-
114 * calculates the PBM based on the settings of the given OSSL_CRMF_PBMPARAMETER
115 * |pbmp| identifies the algorithms, salt to use
116 * |msg| message to apply the PBM for
117 * |msglen| length of the message
118 * |sec| key to use
119 * |seclen| length of the key
120 * |mac| pointer to the computed mac, will be set on success
121 * |maclen| if not NULL, will set variable to the length of the mac on success
122 * returns 1 on success, 0 on error
123 */
124 int OSSL_CRMF_pbm_new(const OSSL_CRMF_PBMPARAMETER *pbmp,
125 const unsigned char *msg, size_t msglen,
126 const unsigned char *sec, size_t seclen,
127 unsigned char **out, size_t *outlen)
128 {
129 int mac_nid, hmac_md_nid = NID_undef;
130 const char *mdname = NULL;
131 const EVP_MD *m = NULL;
132 EVP_MD_CTX *ctx = NULL;
133 unsigned char basekey[EVP_MAX_MD_SIZE];
134 unsigned int bklen = EVP_MAX_MD_SIZE;
135 int64_t iterations;
136 unsigned char *mac_res = 0;
137 int ok = 0;
138 EVP_MAC *mac = NULL;
139 EVP_MAC_CTX *mctx = NULL;
140 OSSL_PARAM macparams[3] =
141 { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
142
143 if (out == NULL || pbmp == NULL || pbmp->mac == NULL
144 || pbmp->mac->algorithm == NULL || msg == NULL || sec == NULL) {
145 CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_NULL_ARGUMENT);
146 goto err;
147 }
148 if ((mac_res = OPENSSL_malloc(EVP_MAX_MD_SIZE)) == NULL) {
149 CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, ERR_R_MALLOC_FAILURE);
150 goto err;
151 }
152
153 /*
154 * owf identifies the hash algorithm and associated parameters used to
155 * compute the key used in the MAC process. All implementations MUST
156 * support SHA-1.
157 */
158 if ((m = EVP_get_digestbyobj(pbmp->owf->algorithm)) == NULL) {
159 CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_UNSUPPORTED_ALGORITHM);
160 goto err;
161 }
162
163 if ((ctx = EVP_MD_CTX_new()) == NULL) {
164 CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, ERR_R_MALLOC_FAILURE);
165 goto err;
166 }
167
168 /* compute the basekey of the salted secret */
169 if (!EVP_DigestInit_ex(ctx, m, NULL))
170 goto err;
171 /* first the secret */
172 if (!EVP_DigestUpdate(ctx, sec, seclen))
173 goto err;
174 /* then the salt */
175 if (!EVP_DigestUpdate(ctx, pbmp->salt->data, pbmp->salt->length))
176 goto err;
177 if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
178 goto err;
179 if (!ASN1_INTEGER_get_int64(&iterations, pbmp->iterationCount)
180 || iterations < 100 /* min from RFC */
181 || iterations > OSSL_CRMF_PBM_MAX_ITERATION_COUNT) {
182 CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_BAD_PBM_ITERATIONCOUNT);
183 goto err;
184 }
185
186 /* the first iteration was already done above */
187 while (--iterations > 0) {
188 if (!EVP_DigestInit_ex(ctx, m, NULL))
189 goto err;
190 if (!EVP_DigestUpdate(ctx, basekey, bklen))
191 goto err;
192 if (!EVP_DigestFinal_ex(ctx, basekey, &bklen))
193 goto err;
194 }
195
196 /*
197 * mac identifies the algorithm and associated parameters of the MAC
198 * function to be used. All implementations MUST support HMAC-SHA1 [HMAC].
199 * All implementations SHOULD support DES-MAC and Triple-DES-MAC [PKCS11].
200 */
201 mac_nid = OBJ_obj2nid(pbmp->mac->algorithm);
202
203 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, mac_nid, NULL, &hmac_md_nid, NULL)
204 || ((mdname = OBJ_nid2sn(hmac_md_nid)) == NULL)) {
205 CRMFerr(CRMF_F_OSSL_CRMF_PBM_NEW, CRMF_R_UNSUPPORTED_ALGORITHM);
206 goto err;
207 }
208
209 macparams[0] =
210 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
211 (char *)mdname, 0);
212 macparams[1] =
213 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, basekey, bklen);
214 if ((mac = EVP_MAC_fetch(NULL, "HMAC", NULL)) == NULL
215 || (mctx = EVP_MAC_CTX_new(mac)) == NULL
216 || !EVP_MAC_CTX_set_params(mctx, macparams)
217 || !EVP_MAC_init(mctx)
218 || !EVP_MAC_update(mctx, msg, msglen)
219 || !EVP_MAC_final(mctx, mac_res, outlen, EVP_MAX_MD_SIZE))
220 goto err;
221
222 ok = 1;
223
224 err:
225 /* cleanup */
226 OPENSSL_cleanse(basekey, bklen);
227 EVP_MAC_CTX_free(mctx);
228 EVP_MAC_free(mac);
229 EVP_MD_CTX_free(ctx);
230
231 if (ok == 1) {
232 *out = mac_res;
233 return 1;
234 }
235
236 OPENSSL_free(mac_res);
237
238 if (pbmp != NULL && pbmp->mac != NULL) {
239 char buf[128];
240
241 if (OBJ_obj2txt(buf, sizeof(buf), pbmp->mac->algorithm, 0))
242 ERR_add_error_data(1, buf);
243 }
244 return 0;
245 }