/src/openssl40/crypto/cmp/cmp_protect.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2007-2026 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 | | |
12 | | #include "cmp_local.h" |
13 | | #include "crypto/asn1.h" /* for ossl_X509_ALGOR_from_nid() */ |
14 | | |
15 | | /* |
16 | | * This function is also used by the internal verify_PBMAC() in cmp_vfy.c. |
17 | | * |
18 | | * Calculate protection for |msg| according to |msg->header->protectionAlg| |
19 | | * using the credentials, library context, and property criteria in the ctx. |
20 | | * Unless |msg->header->protectionAlg| is PasswordBasedMAC, |
21 | | * its value is completed according to |ctx->pkey| and |ctx->digest|, |
22 | | * where the latter irrelevant in the case of Edwards curves. |
23 | | * |
24 | | * returns ASN1_BIT_STRING representing the protection on success, else NULL |
25 | | */ |
26 | | ASN1_BIT_STRING *ossl_cmp_calc_protection(const OSSL_CMP_CTX *ctx, |
27 | | const OSSL_CMP_MSG *msg) |
28 | 570 | { |
29 | 570 | ASN1_BIT_STRING *prot = NULL; |
30 | 570 | OSSL_CMP_PROTECTEDPART prot_part; |
31 | 570 | const ASN1_OBJECT *algorOID = NULL; |
32 | 570 | const void *ppval = NULL; |
33 | 570 | int pptype = 0; |
34 | | |
35 | 570 | if (!ossl_assert(ctx != NULL && msg != NULL)) |
36 | 0 | return NULL; |
37 | | |
38 | | /* construct data to be signed */ |
39 | 570 | prot_part.header = msg->header; |
40 | 570 | prot_part.body = msg->body; |
41 | | |
42 | 570 | if (msg->header->protectionAlg == NULL) { |
43 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_UNKNOWN_ALGORITHM_ID); |
44 | 0 | return NULL; |
45 | 0 | } |
46 | 570 | X509_ALGOR_get0(&algorOID, &pptype, &ppval, msg->header->protectionAlg); |
47 | | |
48 | 570 | if (OBJ_obj2nid(algorOID) == NID_id_PasswordBasedMAC) { |
49 | 570 | int len; |
50 | 570 | size_t prot_part_der_len; |
51 | 570 | unsigned char *prot_part_der = NULL; |
52 | 570 | size_t sig_len; |
53 | 570 | unsigned char *protection = NULL; |
54 | 570 | OSSL_CRMF_PBMPARAMETER *pbm = NULL; |
55 | 570 | ASN1_STRING *pbm_str = NULL; |
56 | 570 | const unsigned char *pbm_str_uc = NULL; |
57 | | |
58 | 570 | if (ctx->secretValue == NULL) { |
59 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PBM_SECRET); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | 570 | if (pptype != V_ASN1_SEQUENCE || ppval == NULL) { |
63 | 38 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CALCULATING_PROTECTION); |
64 | 38 | return NULL; |
65 | 38 | } |
66 | | |
67 | 532 | len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der); |
68 | 532 | if (len < 0 || prot_part_der == NULL) { |
69 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_CALCULATING_PROTECTION); |
70 | 0 | goto end; |
71 | 0 | } |
72 | 532 | prot_part_der_len = (size_t)len; |
73 | | |
74 | 532 | pbm_str = (ASN1_STRING *)ppval; |
75 | 532 | pbm_str_uc = ASN1_STRING_get0_data(pbm_str); |
76 | 532 | pbm = d2i_OSSL_CRMF_PBMPARAMETER(NULL, &pbm_str_uc, ASN1_STRING_length(pbm_str)); |
77 | 532 | if (pbm == NULL) { |
78 | 214 | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_ALGORITHM_OID); |
79 | 214 | goto end; |
80 | 214 | } |
81 | | |
82 | 318 | if (!OSSL_CRMF_pbm_new(ctx->libctx, ctx->propq, |
83 | 318 | pbm, prot_part_der, prot_part_der_len, |
84 | 318 | ASN1_STRING_get0_data(ctx->secretValue), ASN1_STRING_length(ctx->secretValue), |
85 | 318 | &protection, &sig_len)) |
86 | 150 | goto end; |
87 | | |
88 | 168 | if (sig_len > INT_MAX || (prot = ASN1_BIT_STRING_new()) == NULL) |
89 | 0 | goto end; |
90 | | /* OpenSSL by default encodes all bit strings as ASN.1 NamedBitList */ |
91 | 168 | ossl_asn1_string_set_bits_left(prot, 0); |
92 | 168 | if (!ASN1_BIT_STRING_set(prot, protection, (int)sig_len)) { |
93 | 0 | ASN1_BIT_STRING_free(prot); |
94 | 0 | prot = NULL; |
95 | 0 | } |
96 | 532 | end: |
97 | 532 | OSSL_CRMF_PBMPARAMETER_free(pbm); |
98 | 532 | OPENSSL_free(protection); |
99 | 532 | OPENSSL_free(prot_part_der); |
100 | 532 | return prot; |
101 | 168 | } else { |
102 | 0 | const EVP_MD *md = ctx->digest; |
103 | 0 | char name[80] = ""; |
104 | |
|
105 | 0 | if (ctx->pkey == NULL) { |
106 | 0 | ERR_raise(ERR_LIB_CMP, |
107 | 0 | CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | 0 | if (EVP_PKEY_get_default_digest_name(ctx->pkey, name, sizeof(name)) > 0 |
111 | 0 | && strcmp(name, "UNDEF") == 0) /* at least for Ed25519, Ed448 */ |
112 | 0 | md = NULL; |
113 | |
|
114 | 0 | if ((prot = ASN1_BIT_STRING_new()) == NULL) |
115 | 0 | return NULL; |
116 | 0 | if (ASN1_item_sign_ex(ASN1_ITEM_rptr(OSSL_CMP_PROTECTEDPART), |
117 | 0 | msg->header->protectionAlg, /* sets X509_ALGOR */ |
118 | 0 | NULL, prot, &prot_part, NULL, ctx->pkey, md, |
119 | 0 | ctx->libctx, ctx->propq) |
120 | 0 | != 0) |
121 | 0 | return prot; |
122 | 0 | ASN1_BIT_STRING_free(prot); |
123 | 0 | return NULL; |
124 | 0 | } |
125 | 570 | } |
126 | | |
127 | | void ossl_cmp_set_own_chain(OSSL_CMP_CTX *ctx) |
128 | 0 | { |
129 | 0 | if (!ossl_assert(ctx != NULL)) |
130 | 0 | return; |
131 | | /* if not yet done try to build chain using available untrusted certs */ |
132 | 0 | if (ctx->chain == NULL) { |
133 | 0 | ossl_cmp_debug(ctx, "trying to build chain for own CMP signer cert"); |
134 | 0 | ctx->chain = X509_build_chain(ctx->cert, ctx->untrusted, NULL, 0, |
135 | 0 | ctx->libctx, ctx->propq); |
136 | 0 | if (ctx->chain != NULL) { |
137 | 0 | ossl_cmp_debug(ctx, "success building chain for own CMP signer cert"); |
138 | 0 | } else { |
139 | | /* dump errors to avoid confusion when printing further ones */ |
140 | 0 | OSSL_CMP_CTX_print_errors(ctx); |
141 | 0 | ossl_cmp_warn(ctx, "could not build chain for own CMP signer cert"); |
142 | 0 | } |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* ctx is not const just because ctx->chain may get adapted */ |
147 | | int ossl_cmp_msg_add_extraCerts(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg) |
148 | 1.83k | { |
149 | 1.83k | if (!ossl_assert(ctx != NULL && msg != NULL)) |
150 | 0 | return 0; |
151 | | |
152 | | /* Add first ctx->cert and its chain if using signature-based protection */ |
153 | 1.83k | if (!ctx->unprotectedSend && ctx->secretValue == NULL |
154 | 0 | && ctx->cert != NULL && ctx->pkey != NULL) { |
155 | 0 | int prepend = X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP |
156 | 0 | | X509_ADD_FLAG_PREPEND | X509_ADD_FLAG_NO_SS; |
157 | |
|
158 | 0 | ossl_cmp_set_own_chain(ctx); |
159 | 0 | if (ctx->chain != NULL) { |
160 | 0 | if (!ossl_x509_add_certs_new(&msg->extraCerts, ctx->chain, prepend)) |
161 | 0 | return 0; |
162 | 0 | } else { |
163 | | /* make sure that at least our own signer cert is included first */ |
164 | 0 | if (!ossl_x509_add_cert_new(&msg->extraCerts, ctx->cert, prepend)) |
165 | 0 | return 0; |
166 | 0 | ossl_cmp_debug(ctx, "fallback: adding just own CMP signer cert"); |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | | /* add any additional certificates from ctx->extraCertsOut */ |
171 | 1.83k | if (!ossl_x509_add_certs_new(&msg->extraCerts, ctx->extraCertsOut, |
172 | 1.83k | X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP)) |
173 | 0 | return 0; |
174 | | |
175 | | /* in case extraCerts are empty list avoid empty ASN.1 sequence */ |
176 | 1.83k | if (sk_X509_num(msg->extraCerts) == 0) { |
177 | 0 | sk_X509_free(msg->extraCerts); |
178 | 0 | msg->extraCerts = NULL; |
179 | 0 | } |
180 | 1.83k | return 1; |
181 | 1.83k | } |
182 | | |
183 | | /* |
184 | | * Create an X509_ALGOR structure for PasswordBasedMAC protection based on |
185 | | * the pbm settings in the context |
186 | | */ |
187 | | static X509_ALGOR *pbmac_algor(const OSSL_CMP_CTX *ctx) |
188 | 0 | { |
189 | 0 | OSSL_CRMF_PBMPARAMETER *pbm = NULL; |
190 | 0 | unsigned char *pbm_der = NULL; |
191 | 0 | int pbm_der_len; |
192 | 0 | ASN1_STRING *pbm_str = NULL; |
193 | 0 | X509_ALGOR *alg = NULL; |
194 | |
|
195 | 0 | if (!ossl_assert(ctx != NULL)) |
196 | 0 | return NULL; |
197 | | |
198 | 0 | pbm = OSSL_CRMF_pbmp_new(ctx->libctx, ctx->pbm_slen, |
199 | 0 | EVP_MD_get_type(ctx->pbm_owf), ctx->pbm_itercnt, |
200 | 0 | ctx->pbm_mac); |
201 | 0 | pbm_str = ASN1_STRING_new(); |
202 | 0 | if (pbm == NULL || pbm_str == NULL) |
203 | 0 | goto err; |
204 | 0 | if ((pbm_der_len = i2d_OSSL_CRMF_PBMPARAMETER(pbm, &pbm_der)) < 0) |
205 | 0 | goto err; |
206 | 0 | if (!ASN1_STRING_set(pbm_str, pbm_der, pbm_der_len)) |
207 | 0 | goto err; |
208 | 0 | alg = ossl_X509_ALGOR_from_nid(NID_id_PasswordBasedMAC, |
209 | 0 | V_ASN1_SEQUENCE, pbm_str); |
210 | 0 | err: |
211 | 0 | if (alg == NULL) |
212 | 0 | ASN1_STRING_free(pbm_str); |
213 | 0 | OPENSSL_free(pbm_der); |
214 | 0 | OSSL_CRMF_PBMPARAMETER_free(pbm); |
215 | 0 | return alg; |
216 | 0 | } |
217 | | |
218 | | static int set_senderKID(const OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg, |
219 | | const ASN1_OCTET_STRING *id) |
220 | 2.37k | { |
221 | 2.37k | if (id == NULL) |
222 | 2.37k | id = ctx->referenceValue; /* standard for PBM, fallback for sig-based */ |
223 | 2.37k | return id == NULL || ossl_cmp_hdr_set1_senderKID(msg->header, id); |
224 | 2.37k | } |
225 | | |
226 | | /* ctx is not const just because ctx->chain may get adapted */ |
227 | | int ossl_cmp_msg_protect(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg) |
228 | 33.5k | { |
229 | 33.5k | if (!ossl_assert(ctx != NULL && msg != NULL)) |
230 | 0 | return 0; |
231 | | |
232 | | /* |
233 | | * For the case of re-protection remove pre-existing protection. |
234 | | * Does not remove any pre-existing extraCerts. |
235 | | */ |
236 | 33.5k | X509_ALGOR_free(msg->header->protectionAlg); |
237 | 33.5k | msg->header->protectionAlg = NULL; |
238 | 33.5k | ASN1_BIT_STRING_free(msg->protection); |
239 | 33.5k | msg->protection = NULL; |
240 | | |
241 | 33.5k | if (ctx->unprotectedSend) { |
242 | 2.37k | if (!set_senderKID(ctx, msg, NULL)) |
243 | 0 | goto err; |
244 | 31.1k | } else if (ctx->secretValue != NULL) { |
245 | | /* use PasswordBasedMac according to 5.1.3.1 if secretValue is given */ |
246 | 0 | if ((msg->header->protectionAlg = pbmac_algor(ctx)) == NULL) |
247 | 0 | goto err; |
248 | 0 | if (!set_senderKID(ctx, msg, NULL)) |
249 | 0 | goto err; |
250 | | |
251 | | /* |
252 | | * will add any additional certificates from ctx->extraCertsOut |
253 | | * while not needed to validate the protection certificate, |
254 | | * the option to do this might be handy for certain use cases |
255 | | */ |
256 | 31.1k | } else if (ctx->cert != NULL && ctx->pkey != NULL) { |
257 | | /* use MSG_SIG_ALG according to 5.1.3.3 if client cert and key given */ |
258 | | |
259 | | /* make sure that key and certificate match */ |
260 | 0 | if (!X509_check_private_key(ctx->cert, ctx->pkey)) { |
261 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_CERT_AND_KEY_DO_NOT_MATCH); |
262 | 0 | goto err; |
263 | 0 | } |
264 | | |
265 | 0 | if ((msg->header->protectionAlg = X509_ALGOR_new()) == NULL) |
266 | 0 | goto err; |
267 | | /* set senderKID to keyIdentifier of the cert according to 5.1.1 */ |
268 | 0 | if (!set_senderKID(ctx, msg, X509_get0_subject_key_id(ctx->cert))) |
269 | 0 | goto err; |
270 | | |
271 | | /* |
272 | | * will add ctx->cert followed, if possible, by its chain built |
273 | | * from ctx->untrusted, and then ctx->extraCertsOut |
274 | | */ |
275 | 31.1k | } else { |
276 | 31.1k | ERR_raise(ERR_LIB_CMP, |
277 | 31.1k | CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION); |
278 | 31.1k | goto err; |
279 | 31.1k | } |
280 | 2.37k | if (!ctx->unprotectedSend |
281 | | /* protect according to msg->header->protectionAlg partly set above */ |
282 | 0 | && ((msg->protection = ossl_cmp_calc_protection(ctx, msg)) == NULL)) |
283 | 0 | goto err; |
284 | | |
285 | | /* |
286 | | * For signature-based protection add ctx->cert followed by its chain. |
287 | | * Finally add any additional certificates from ctx->extraCertsOut; |
288 | | * even if not needed to validate the protection |
289 | | * the option to do this might be handy for certain use cases. |
290 | | */ |
291 | 2.37k | if (!ossl_cmp_msg_add_extraCerts(ctx, msg)) |
292 | 0 | goto err; |
293 | | |
294 | | /* |
295 | | * As required by RFC 9810 section 5.1.1., if the sender name is not known |
296 | | * to the client it set to NULL-DN. In this case for identification at least |
297 | | * the senderKID must be set, where we took the referenceValue as fallback. |
298 | | */ |
299 | 2.37k | if (!(ossl_cmp_general_name_is_NULL_DN(msg->header->sender) |
300 | 2.37k | && msg->header->senderKID == NULL)) |
301 | 0 | return 1; |
302 | 2.37k | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_SENDER_IDENTIFICATION); |
303 | | |
304 | 33.5k | err: |
305 | | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROTECTING_MESSAGE); |
306 | 33.5k | return 0; |
307 | 2.37k | } |