Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/cmp/cmp_status.c
Line
Count
Source
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
/* CMP functions for PKIStatusInfo handling and PKIMessage decomposition */
13
14
#include "cmp_local.h"
15
16
/* CMP functions related to PKIStatus */
17
18
int ossl_cmp_pkisi_get_status(const OSSL_CMP_PKISI *si)
19
0
{
20
0
    int res;
21
22
0
    if (!ossl_assert(si != NULL && si->status != NULL))
23
0
        return -1;
24
0
    res = ossl_cmp_asn1_get_int(si->status);
25
0
    return res == -2 ? -1 : res;
26
0
}
27
28
const char *ossl_cmp_PKIStatus_to_string(int status)
29
0
{
30
0
    switch (status) {
31
0
    case OSSL_CMP_PKISTATUS_accepted:
32
0
        return "PKIStatus: accepted";
33
0
    case OSSL_CMP_PKISTATUS_grantedWithMods:
34
0
        return "PKIStatus: granted with modifications";
35
0
    case OSSL_CMP_PKISTATUS_rejection:
36
0
        return "PKIStatus: rejection";
37
0
    case OSSL_CMP_PKISTATUS_waiting:
38
0
        return "PKIStatus: waiting";
39
0
    case OSSL_CMP_PKISTATUS_revocationWarning:
40
0
        return "PKIStatus: revocation warning - a revocation of the cert is imminent";
41
0
    case OSSL_CMP_PKISTATUS_revocationNotification:
42
0
        return "PKIStatus: revocation notification - a revocation of the cert has occurred";
43
0
    case OSSL_CMP_PKISTATUS_keyUpdateWarning:
44
0
        return "PKIStatus: key update warning - update already done for the cert";
45
0
    default:
46
0
        ERR_raise_data(ERR_LIB_CMP, CMP_R_ERROR_PARSING_PKISTATUS,
47
0
            "PKIStatus: invalid=%d", status);
48
0
        return NULL;
49
0
    }
50
0
}
51
52
OSSL_CMP_PKIFREETEXT *ossl_cmp_pkisi_get0_statusString(const OSSL_CMP_PKISI *si)
53
0
{
54
0
    if (!ossl_assert(si != NULL))
55
0
        return NULL;
56
0
    return si->statusString;
57
0
}
58
59
int ossl_cmp_pkisi_get_pkifailureinfo(const OSSL_CMP_PKISI *si)
60
0
{
61
0
    int i;
62
0
    int res = 0;
63
64
0
    if (!ossl_assert(si != NULL))
65
0
        return -1;
66
0
    if (si->failInfo != NULL)
67
0
        for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++)
68
0
            if (ASN1_BIT_STRING_get_bit(si->failInfo, i))
69
0
                res |= 1 << i;
70
0
    return res;
71
0
}
72
73
/*-
74
 * convert PKIFailureInfo number to human-readable string
75
 * returns pointer to static string, or NULL on error
76
 */
77
static const char *CMP_PKIFAILUREINFO_to_string(int number)
78
0
{
79
0
    switch (number) {
80
0
    case OSSL_CMP_PKIFAILUREINFO_badAlg:
81
0
        return "badAlg";
82
0
    case OSSL_CMP_PKIFAILUREINFO_badMessageCheck:
83
0
        return "badMessageCheck";
84
0
    case OSSL_CMP_PKIFAILUREINFO_badRequest:
85
0
        return "badRequest";
86
0
    case OSSL_CMP_PKIFAILUREINFO_badTime:
87
0
        return "badTime";
88
0
    case OSSL_CMP_PKIFAILUREINFO_badCertId:
89
0
        return "badCertId";
90
0
    case OSSL_CMP_PKIFAILUREINFO_badDataFormat:
91
0
        return "badDataFormat";
92
0
    case OSSL_CMP_PKIFAILUREINFO_wrongAuthority:
93
0
        return "wrongAuthority";
94
0
    case OSSL_CMP_PKIFAILUREINFO_incorrectData:
95
0
        return "incorrectData";
96
0
    case OSSL_CMP_PKIFAILUREINFO_missingTimeStamp:
97
0
        return "missingTimeStamp";
98
0
    case OSSL_CMP_PKIFAILUREINFO_badPOP:
99
0
        return "badPOP";
100
0
    case OSSL_CMP_PKIFAILUREINFO_certRevoked:
101
0
        return "certRevoked";
102
0
    case OSSL_CMP_PKIFAILUREINFO_certConfirmed:
103
0
        return "certConfirmed";
104
0
    case OSSL_CMP_PKIFAILUREINFO_wrongIntegrity:
105
0
        return "wrongIntegrity";
106
0
    case OSSL_CMP_PKIFAILUREINFO_badRecipientNonce:
107
0
        return "badRecipientNonce";
108
0
    case OSSL_CMP_PKIFAILUREINFO_timeNotAvailable:
109
0
        return "timeNotAvailable";
110
0
    case OSSL_CMP_PKIFAILUREINFO_unacceptedPolicy:
111
0
        return "unacceptedPolicy";
112
0
    case OSSL_CMP_PKIFAILUREINFO_unacceptedExtension:
113
0
        return "unacceptedExtension";
114
0
    case OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable:
115
0
        return "addInfoNotAvailable";
116
0
    case OSSL_CMP_PKIFAILUREINFO_badSenderNonce:
117
0
        return "badSenderNonce";
118
0
    case OSSL_CMP_PKIFAILUREINFO_badCertTemplate:
119
0
        return "badCertTemplate";
120
0
    case OSSL_CMP_PKIFAILUREINFO_signerNotTrusted:
121
0
        return "signerNotTrusted";
122
0
    case OSSL_CMP_PKIFAILUREINFO_transactionIdInUse:
123
0
        return "transactionIdInUse";
124
0
    case OSSL_CMP_PKIFAILUREINFO_unsupportedVersion:
125
0
        return "unsupportedVersion";
126
0
    case OSSL_CMP_PKIFAILUREINFO_notAuthorized:
127
0
        return "notAuthorized";
128
0
    case OSSL_CMP_PKIFAILUREINFO_systemUnavail:
129
0
        return "systemUnavail";
130
0
    case OSSL_CMP_PKIFAILUREINFO_systemFailure:
131
0
        return "systemFailure";
132
0
    case OSSL_CMP_PKIFAILUREINFO_duplicateCertReq:
133
0
        return "duplicateCertReq";
134
0
    default:
135
0
        return NULL; /* illegal failure number */
136
0
    }
137
0
}
138
139
int ossl_cmp_pkisi_check_pkifailureinfo(const OSSL_CMP_PKISI *si, int bit_index)
140
0
{
141
0
    if (!ossl_assert(si != NULL && si->failInfo != NULL))
142
0
        return -1;
143
0
    if (bit_index < 0 || bit_index > OSSL_CMP_PKIFAILUREINFO_MAX) {
144
0
        ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS);
145
0
        return -1;
146
0
    }
147
148
0
    return ASN1_BIT_STRING_get_bit(si->failInfo, bit_index);
149
0
}
150
151
/*-
152
 * place human-readable error string created from PKIStatusInfo in given buffer
153
 * returns pointer to the same buffer containing the string, or NULL on error
154
 */
155
static char *snprint_PKIStatusInfo_parts(int status, int fail_info,
156
    const OSSL_CMP_PKIFREETEXT *status_strings,
157
    char *buf, size_t bufsize)
158
0
{
159
0
    int failure;
160
0
    const char *status_string, *failure_string;
161
0
    ASN1_UTF8STRING *text;
162
0
    int i;
163
0
    int printed_chars;
164
0
    int failinfo_found = 0;
165
0
    int n_status_strings;
166
0
    char *write_ptr = buf;
167
168
0
    if (buf == NULL
169
0
        || status < 0
170
0
        || (status_string = ossl_cmp_PKIStatus_to_string(status)) == NULL)
171
0
        return NULL;
172
173
0
#define ADVANCE_BUFFER                                         \
174
0
    if (printed_chars < 0 || (size_t)printed_chars >= bufsize) \
175
0
        return NULL;                                           \
176
0
    write_ptr += printed_chars;                                \
177
0
    bufsize -= printed_chars;
178
179
0
    printed_chars = BIO_snprintf(write_ptr, bufsize, "%s", status_string);
180
0
    ADVANCE_BUFFER;
181
182
    /*
183
     * failInfo is optional and may be empty;
184
     * if present, print failInfo before statusString because it is more concise
185
     */
186
0
    if (fail_info != -1 && fail_info != 0) {
187
0
        printed_chars = BIO_snprintf(write_ptr, bufsize, "; PKIFailureInfo: ");
188
0
        ADVANCE_BUFFER;
189
0
        for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
190
0
            if ((fail_info & (1 << failure)) != 0) {
191
0
                failure_string = CMP_PKIFAILUREINFO_to_string(failure);
192
0
                if (failure_string != NULL) {
193
0
                    printed_chars = BIO_snprintf(write_ptr, bufsize, "%s%s",
194
0
                        failinfo_found ? ", " : "",
195
0
                        failure_string);
196
0
                    ADVANCE_BUFFER;
197
0
                    failinfo_found = 1;
198
0
                }
199
0
            }
200
0
        }
201
0
    }
202
0
    if (!failinfo_found && status != OSSL_CMP_PKISTATUS_accepted
203
0
        && status != OSSL_CMP_PKISTATUS_grantedWithMods) {
204
0
        printed_chars = BIO_snprintf(write_ptr, bufsize, "; <no failure info>");
205
0
        ADVANCE_BUFFER;
206
0
    }
207
208
    /* statusString sequence is optional and may be empty */
209
0
    n_status_strings = sk_ASN1_UTF8STRING_num(status_strings);
210
0
    if (n_status_strings > 0) {
211
0
        printed_chars = BIO_snprintf(write_ptr, bufsize, "; StatusString%s: ",
212
0
            n_status_strings > 1 ? "s" : "");
213
0
        ADVANCE_BUFFER;
214
0
        for (i = 0; i < n_status_strings; i++) {
215
0
            text = sk_ASN1_UTF8STRING_value(status_strings, i);
216
0
            printed_chars = BIO_snprintf(write_ptr, bufsize, "\"%.*s\"%s",
217
0
                ASN1_STRING_length(text),
218
0
                ASN1_STRING_get0_data(text),
219
0
                i < n_status_strings - 1 ? ", " : "");
220
0
            ADVANCE_BUFFER;
221
0
        }
222
0
    }
223
0
#undef ADVANCE_BUFFER
224
0
    return buf;
225
0
}
226
227
char *OSSL_CMP_snprint_PKIStatusInfo(const OSSL_CMP_PKISI *statusInfo,
228
    char *buf, size_t bufsize)
229
0
{
230
0
    int failure_info;
231
232
0
    if (statusInfo == NULL) {
233
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
234
0
        return NULL;
235
0
    }
236
237
0
    failure_info = ossl_cmp_pkisi_get_pkifailureinfo(statusInfo);
238
239
0
    return snprint_PKIStatusInfo_parts(ASN1_INTEGER_get(statusInfo->status),
240
0
        failure_info,
241
0
        statusInfo->statusString, buf, bufsize);
242
0
}
243
244
char *OSSL_CMP_CTX_snprint_PKIStatus(const OSSL_CMP_CTX *ctx, char *buf,
245
    size_t bufsize)
246
0
{
247
0
    if (ctx == NULL) {
248
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
249
0
        return NULL;
250
0
    }
251
252
0
    return snprint_PKIStatusInfo_parts(OSSL_CMP_CTX_get_status(ctx),
253
0
        OSSL_CMP_CTX_get_failInfoCode(ctx),
254
0
        OSSL_CMP_CTX_get0_statusString(ctx),
255
0
        buf, bufsize);
256
0
}
257
258
/*-
259
 * Creates a new PKIStatusInfo structure and fills it in
260
 * returns a pointer to the structure on success, NULL on error
261
 * note: strongly overlaps with TS_RESP_CTX_set_status_info()
262
 * and TS_RESP_CTX_add_failure_info() in ../ts/ts_rsp_sign.c
263
 */
264
OSSL_CMP_PKISI *OSSL_CMP_STATUSINFO_new(int status, int fail_info,
265
    const char *text)
266
39.3k
{
267
39.3k
    OSSL_CMP_PKISI *si = OSSL_CMP_PKISI_new();
268
39.3k
    ASN1_UTF8STRING *utf8_text = NULL;
269
39.3k
    int failure;
270
271
39.3k
    if (si == NULL)
272
0
        goto err;
273
39.3k
    if (!ASN1_INTEGER_set(si->status, status))
274
0
        goto err;
275
276
39.3k
    if (text != NULL) {
277
39.1k
        if ((utf8_text = ASN1_UTF8STRING_new()) == NULL
278
39.1k
            || !ASN1_STRING_set(utf8_text, text, -1))
279
0
            goto err;
280
39.1k
        if ((si->statusString = sk_ASN1_UTF8STRING_new_null()) == NULL)
281
0
            goto err;
282
39.1k
        if (!sk_ASN1_UTF8STRING_push(si->statusString, utf8_text))
283
0
            goto err;
284
        /* Ownership is lost. */
285
39.1k
        utf8_text = NULL;
286
39.1k
    }
287
288
1.10M
    for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
289
1.06M
        if ((fail_info & (1 << failure)) != 0) {
290
39.3k
            if (si->failInfo == NULL
291
39.3k
                && (si->failInfo = ASN1_BIT_STRING_new()) == NULL)
292
0
                goto err;
293
39.3k
            if (!ASN1_BIT_STRING_set_bit(si->failInfo, failure, 1))
294
0
                goto err;
295
39.3k
        }
296
1.06M
    }
297
39.3k
    return si;
298
299
0
err:
300
0
    OSSL_CMP_PKISI_free(si);
301
0
    ASN1_UTF8STRING_free(utf8_text);
302
    return NULL;
303
39.3k
}