Coverage Report

Created: 2025-08-28 07:07

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