]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/cmp/cmp_protect.c
Use in CMP+CRMF libctx and propq param added to sign/verify/HMAC/decrypt
[thirdparty/openssl.git] / crypto / cmp / cmp_protect.c
CommitLineData
3dbc5156 1/*
33388b44 2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3dbc5156
DDO
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
14/* explicit #includes not strictly needed since implied by the above: */
15#include <openssl/asn1t.h>
16#include <openssl/cmp.h>
17#include <openssl/crmf.h>
18#include <openssl/err.h>
19#include <openssl/x509.h>
20
852c2ed2
RS
21DEFINE_STACK_OF(X509)
22
3dbc5156 23/*
6d1f50b5 24 * This function is also used by the internal verify_PBMAC() in cmp_vfy.c.
3dbc5156 25 *
6d1f50b5
DDO
26 * Calculate protection for given PKImessage according to
27 * the algorithm and parameters in the message header's protectionAlg
28 * using the credentials, library context, and property criteria in the ctx.
3dbc5156 29 *
6d1f50b5 30 * returns ASN1_BIT_STRING representing the protection on success, else NULL
3dbc5156 31 */
6d1f50b5
DDO
32ASN1_BIT_STRING *ossl_cmp_calc_protection(const OSSL_CMP_CTX *ctx,
33 const OSSL_CMP_MSG *msg)
3dbc5156
DDO
34{
35 ASN1_BIT_STRING *prot = NULL;
642f60d8 36 OSSL_CMP_PROTECTEDPART prot_part;
3dbc5156 37 const ASN1_OBJECT *algorOID = NULL;
3dbc5156
DDO
38 const void *ppval = NULL;
39 int pptype = 0;
3dbc5156 40
6d1f50b5 41 if (!ossl_assert(ctx != NULL && msg != NULL))
3dbc5156
DDO
42 return NULL;
43
44 /* construct data to be signed */
45 prot_part.header = msg->header;
46 prot_part.body = msg->body;
47
3dbc5156
DDO
48 if (msg->header->protectionAlg == NULL) {
49 CMPerr(0, CMP_R_UNKNOWN_ALGORITHM_ID);
6d1f50b5 50 return NULL;
3dbc5156
DDO
51 }
52 X509_ALGOR_get0(&algorOID, &pptype, &ppval, msg->header->protectionAlg);
53
6d1f50b5
DDO
54 if (OBJ_obj2nid(algorOID) == NID_id_PasswordBasedMAC) {
55 int len;
56 size_t prot_part_der_len;
57 unsigned char *prot_part_der = NULL;
58 size_t sig_len;
59 unsigned char *protection = NULL;
60 OSSL_CRMF_PBMPARAMETER *pbm = NULL;
61 ASN1_STRING *pbm_str = NULL;
62 const unsigned char *pbm_str_uc = NULL;
63
64 if (ctx->secretValue == NULL) {
65 CMPerr(0, CMP_R_MISSING_PBM_SECRET);
66 return NULL;
67 }
3dbc5156
DDO
68 if (ppval == NULL) {
69 CMPerr(0, CMP_R_ERROR_CALCULATING_PROTECTION);
6d1f50b5 70 return NULL;
3dbc5156 71 }
6d1f50b5
DDO
72
73 len = i2d_OSSL_CMP_PROTECTEDPART(&prot_part, &prot_part_der);
74 if (len < 0 || prot_part_der == NULL) {
75 CMPerr(0, CMP_R_ERROR_CALCULATING_PROTECTION);
3dbc5156
DDO
76 goto end;
77 }
6d1f50b5
DDO
78 prot_part_der_len = (size_t)len;
79
3dbc5156
DDO
80 pbm_str = (ASN1_STRING *)ppval;
81 pbm_str_uc = pbm_str->data;
82 pbm = d2i_OSSL_CRMF_PBMPARAMETER(NULL, &pbm_str_uc, pbm_str->length);
83 if (pbm == NULL) {
84 CMPerr(0, CMP_R_WRONG_ALGORITHM_OID);
85 goto end;
86 }
87
6d1f50b5
DDO
88 if (!OSSL_CRMF_pbm_new(ctx->libctx, ctx->propq,
89 pbm, prot_part_der, prot_part_der_len,
90 ctx->secretValue->data, ctx->secretValue->length,
3dbc5156
DDO
91 &protection, &sig_len))
92 goto end;
3dbc5156 93
6d1f50b5
DDO
94 if ((prot = ASN1_BIT_STRING_new()) == NULL)
95 return NULL;
96 /* OpenSSL defaults all bit strings to be encoded as ASN.1 NamedBitList */
97 prot->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
98 prot->flags |= ASN1_STRING_FLAG_BITS_LEFT;
99 if (!ASN1_BIT_STRING_set(prot, protection, sig_len)) {
100 ASN1_BIT_STRING_free(prot);
101 prot = NULL;
3dbc5156 102 }
6d1f50b5
DDO
103 end:
104 OSSL_CRMF_PBMPARAMETER_free(pbm);
105 OPENSSL_free(protection);
106 OPENSSL_free(prot_part_der);
107 return prot;
3dbc5156 108 } else {
6d1f50b5
DDO
109 int md_nid;
110 const EVP_MD *md = NULL;
3dbc5156 111
6d1f50b5
DDO
112 if (ctx->pkey == NULL) {
113 CMPerr(0, CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION);
114 return NULL;
115 }
116 if (!OBJ_find_sigid_algs(OBJ_obj2nid(algorOID), &md_nid, NULL)
117 || (md = EVP_get_digestbynid(md_nid)) == NULL) {
118 CMPerr(0, CMP_R_UNKNOWN_ALGORITHM_ID);
119 return NULL;
120 }
121
122 if ((prot = ASN1_BIT_STRING_new()) == NULL)
123 return NULL;
124 if (ASN1_item_sign_with_libctx(ASN1_ITEM_rptr(OSSL_CMP_PROTECTEDPART),
125 NULL, NULL, prot, &prot_part, NULL,
126 ctx->pkey, md, ctx->libctx, ctx->propq))
127 return prot;
3dbc5156 128 ASN1_BIT_STRING_free(prot);
6d1f50b5 129 return NULL;
3dbc5156 130 }
3dbc5156
DDO
131}
132
133int ossl_cmp_msg_add_extraCerts(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
134{
135 if (!ossl_assert(ctx != NULL && msg != NULL))
136 return 0;
137
138 if (msg->extraCerts == NULL
139 && (msg->extraCerts = sk_X509_new_null()) == NULL)
140 return 0;
141
63f1883d 142 if (ctx->cert != NULL && ctx->pkey != NULL) {
143be474 143 /* make sure that our own cert is included in the first position */
eeccc237
DDO
144 if (!X509_add_cert(msg->extraCerts, ctx->cert,
145 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
146 | X509_ADD_FLAG_PREPEND))
3dbc5156 147 return 0;
143be474 148 /* if we have untrusted certs, try to add intermediate certs */
3dbc5156
DDO
149 if (ctx->untrusted_certs != NULL) {
150 STACK_OF(X509) *chain =
28e9f62b
DDO
151 ossl_cmp_build_cert_chain(ctx->libctx, ctx->propq,
152 ctx->untrusted_certs, ctx->cert);
eeccc237
DDO
153 int res = X509_add_certs(msg->extraCerts, chain,
154 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP
155 | X509_ADD_FLAG_NO_SS);
143be474 156
3dbc5156
DDO
157 sk_X509_pop_free(chain, X509_free);
158 if (res == 0)
159 return 0;
160 }
161 }
162
163 /* add any additional certificates from ctx->extraCertsOut */
eeccc237
DDO
164 if (!X509_add_certs(msg->extraCerts, ctx->extraCertsOut,
165 X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP))
3dbc5156
DDO
166 return 0;
167
168 /* if none was found avoid empty ASN.1 sequence */
169 if (sk_X509_num(msg->extraCerts) == 0) {
170 sk_X509_free(msg->extraCerts);
171 msg->extraCerts = NULL;
172 }
173 return 1;
174}
175
176/*
177 * Create an X509_ALGOR structure for PasswordBasedMAC protection based on
178 * the pbm settings in the context
3dbc5156 179 */
6d1f50b5 180static int set_pbmac_algor(const OSSL_CMP_CTX *ctx, X509_ALGOR **alg)
3dbc5156 181{
3dbc5156
DDO
182 OSSL_CRMF_PBMPARAMETER *pbm = NULL;
183 unsigned char *pbm_der = NULL;
184 int pbm_der_len;
185 ASN1_STRING *pbm_str = NULL;
186
187 if (!ossl_assert(ctx != NULL))
6d1f50b5 188 return 0;
3dbc5156 189
97e00da9 190 pbm = OSSL_CRMF_pbmp_new(ctx->libctx, ctx->pbm_slen,
6d1f50b5
DDO
191 EVP_MD_type(ctx->pbm_owf), ctx->pbm_itercnt,
192 ctx->pbm_mac);
3dbc5156 193 pbm_str = ASN1_STRING_new();
6d1f50b5 194 if (pbm == NULL || pbm_str == NULL)
3dbc5156
DDO
195 goto err;
196
197 if ((pbm_der_len = i2d_OSSL_CRMF_PBMPARAMETER(pbm, &pbm_der)) < 0)
198 goto err;
199
200 if (!ASN1_STRING_set(pbm_str, pbm_der, pbm_der_len))
201 goto err;
6d1f50b5
DDO
202 if (*alg == NULL && (*alg = X509_ALGOR_new()) == NULL)
203 goto err;
3dbc5156
DDO
204 OPENSSL_free(pbm_der);
205
6d1f50b5 206 X509_ALGOR_set0(*alg, OBJ_nid2obj(NID_id_PasswordBasedMAC),
3dbc5156
DDO
207 V_ASN1_SEQUENCE, pbm_str);
208 OSSL_CRMF_PBMPARAMETER_free(pbm);
6d1f50b5 209 return 1;
3dbc5156
DDO
210
211 err:
212 ASN1_STRING_free(pbm_str);
3dbc5156
DDO
213 OPENSSL_free(pbm_der);
214 OSSL_CRMF_PBMPARAMETER_free(pbm);
6d1f50b5
DDO
215 return 0;
216}
217
218static int set_sig_algor(const OSSL_CMP_CTX *ctx, X509_ALGOR **alg)
219{
220 int nid = 0;
221 ASN1_OBJECT *algo = NULL;
222
223 if (!OBJ_find_sigid_by_algs(&nid, EVP_MD_type(ctx->digest),
224 EVP_PKEY_id(ctx->pkey))) {
225 CMPerr(0, CMP_R_UNSUPPORTED_KEY_TYPE);
226 return 0;
227 }
228 if ((algo = OBJ_nid2obj(nid)) == NULL)
229 return 0;
230 if (*alg == NULL && (*alg = X509_ALGOR_new()) == NULL)
231 return 0;
232
233 if (X509_ALGOR_set0(*alg, algo, V_ASN1_UNDEF, NULL))
234 return 1;
235 ASN1_OBJECT_free(algo);
236 return 0;
237}
238
239static int set_senderKID(const OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg,
240 const ASN1_OCTET_STRING *id)
241{
242 if (id == NULL)
243 id = ctx->referenceValue; /* standard for PBM, fallback for sig-based */
244 return id == NULL || ossl_cmp_hdr_set1_senderKID(msg->header, id);
3dbc5156
DDO
245}
246
247int ossl_cmp_msg_protect(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
248{
249 if (!ossl_assert(ctx != NULL && msg != NULL))
250 return 0;
251
143be474
DDO
252 /*
253 * For the case of re-protection remove pre-existing protection.
254 * TODO: Consider also removing any pre-existing extraCerts.
255 */
256 X509_ALGOR_free(msg->header->protectionAlg);
257 msg->header->protectionAlg = NULL;
258 ASN1_BIT_STRING_free(msg->protection);
259 msg->protection = NULL;
260
3dbc5156
DDO
261 if (ctx->unprotectedSend)
262 return 1;
263
264 /* use PasswordBasedMac according to 5.1.3.1 if secretValue is given */
265 if (ctx->secretValue != NULL) {
6d1f50b5 266 if (!set_pbmac_algor(ctx, &msg->header->protectionAlg))
3dbc5156 267 goto err;
6d1f50b5 268 if (!set_senderKID(ctx, msg, NULL))
3dbc5156 269 goto err;
6d1f50b5 270
3dbc5156 271 /*
6d1f50b5
DDO
272 * will add any additional certificates from ctx->extraCertsOut
273 * while not needed to validate the protection certificate,
274 * the option to do this might be handy for certain use cases
3dbc5156 275 */
6d1f50b5
DDO
276 } else if (ctx->cert != NULL && ctx->pkey != NULL) {
277 /* use MSG_SIG_ALG according to 5.1.3.3 if client cert and key given */
3dbc5156 278
143be474 279 /* make sure that key and certificate match */
63f1883d 280 if (!X509_check_private_key(ctx->cert, ctx->pkey)) {
143be474
DDO
281 CMPerr(0, CMP_R_CERT_AND_KEY_DO_NOT_MATCH);
282 goto err;
283 }
3dbc5156 284
6d1f50b5 285 if (!set_sig_algor(ctx, &msg->header->protectionAlg))
143be474 286 goto err;
6d1f50b5
DDO
287 /* set senderKID to keyIdentifier of the cert according to 5.1.1 */
288 if (!set_senderKID(ctx, msg, X509_get0_subject_key_id(ctx->cert)))
143be474 289 goto err;
143be474
DDO
290
291 /*
6d1f50b5
DDO
292 * will add ctx->cert followed, if possible, by its chain built
293 * from ctx->untrusted_certs, and then ctx->extraCertsOut
143be474 294 */
143be474
DDO
295 } else {
296 CMPerr(0, CMP_R_MISSING_KEY_INPUT_FOR_CREATING_PROTECTION);
297 goto err;
3dbc5156 298 }
6d1f50b5 299 if ((msg->protection = ossl_cmp_calc_protection(ctx, msg)) == NULL)
143be474
DDO
300 goto err;
301
302 /*
63f1883d 303 * If present, add ctx->cert followed by its chain as far as possible.
143be474
DDO
304 * Finally add any additional certificates from ctx->extraCertsOut;
305 * even if not needed to validate the protection
306 * the option to do this might be handy for certain use cases.
307 */
308 if (!ossl_cmp_msg_add_extraCerts(ctx, msg))
309 goto err;
3dbc5156 310
cfca56df
DDO
311 /*
312 * As required by RFC 4210 section 5.1.1., if the sender name is not known
313 * to the client it set to NULL-DN. In this case for identification at least
314 * the senderKID must be set, where we took the referenceValue as fallback.
315 */
cfca56df
DDO
316 if (ossl_cmp_general_name_is_NULL_DN(msg->header->sender)
317 && msg->header->senderKID == NULL)
318 CMPerr(0, CMP_R_MISSING_SENDER_IDENTIFICATION);
319 else
320 return 1;
321
3dbc5156
DDO
322 err:
323 CMPerr(0, CMP_R_ERROR_PROTECTING_MESSAGE);
324 return 0;
325}