Coverage Report

Created: 2025-12-04 06:33

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
156
char *snprint_PKIStatusInfo_parts(int status, int fail_info,
157
                                  const OSSL_CMP_PKIFREETEXT *status_strings,
158
                                  char *buf, size_t bufsize)
159
0
{
160
0
    int failure;
161
0
    const char *status_string, *failure_string;
162
0
    ASN1_UTF8STRING *text;
163
0
    int i;
164
0
    int printed_chars;
165
0
    int failinfo_found = 0;
166
0
    int n_status_strings;
167
0
    char *write_ptr = buf;
168
169
0
    if (buf == NULL
170
0
            || status < 0
171
0
            || (status_string = ossl_cmp_PKIStatus_to_string(status)) == NULL)
172
0
        return NULL;
173
174
0
#define ADVANCE_BUFFER  \
175
0
    if (printed_chars < 0 || (size_t)printed_chars >= bufsize)  \
176
0
        return NULL; \
177
0
    write_ptr += printed_chars; \
178
0
    bufsize -= printed_chars;
179
180
0
    printed_chars = BIO_snprintf(write_ptr, bufsize, "%s", status_string);
181
0
    ADVANCE_BUFFER;
182
183
    /*
184
     * failInfo is optional and may be empty;
185
     * if present, print failInfo before statusString because it is more concise
186
     */
187
0
    if (fail_info != -1 && fail_info != 0) {
188
0
        printed_chars = BIO_snprintf(write_ptr, bufsize, "; PKIFailureInfo: ");
189
0
        ADVANCE_BUFFER;
190
0
        for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
191
0
            if ((fail_info & (1 << failure)) != 0) {
192
0
                failure_string = CMP_PKIFAILUREINFO_to_string(failure);
193
0
                if (failure_string != NULL) {
194
0
                    printed_chars = BIO_snprintf(write_ptr, bufsize, "%s%s",
195
0
                                                 failinfo_found ? ", " : "",
196
0
                                                 failure_string);
197
0
                    ADVANCE_BUFFER;
198
0
                    failinfo_found = 1;
199
0
                }
200
0
            }
201
0
        }
202
0
    }
203
0
    if (!failinfo_found && status != OSSL_CMP_PKISTATUS_accepted
204
0
            && status != OSSL_CMP_PKISTATUS_grantedWithMods) {
205
0
        printed_chars = BIO_snprintf(write_ptr, bufsize, "; <no failure info>");
206
0
        ADVANCE_BUFFER;
207
0
    }
208
209
    /* statusString sequence is optional and may be empty */
210
0
    n_status_strings = sk_ASN1_UTF8STRING_num(status_strings);
211
0
    if (n_status_strings > 0) {
212
0
        printed_chars = BIO_snprintf(write_ptr, bufsize, "; StatusString%s: ",
213
0
                                     n_status_strings > 1 ? "s" : "");
214
0
        ADVANCE_BUFFER;
215
0
        for (i = 0; i < n_status_strings; i++) {
216
0
            text = sk_ASN1_UTF8STRING_value(status_strings, i);
217
0
            printed_chars = BIO_snprintf(write_ptr, bufsize, "\"%.*s\"%s",
218
0
                                         ASN1_STRING_length(text),
219
0
                                         ASN1_STRING_get0_data(text),
220
0
                                         i < n_status_strings - 1 ? ", " : "");
221
0
            ADVANCE_BUFFER;
222
0
        }
223
0
    }
224
0
#undef ADVANCE_BUFFER
225
0
    return buf;
226
0
}
227
228
char *OSSL_CMP_snprint_PKIStatusInfo(const OSSL_CMP_PKISI *statusInfo,
229
                                     char *buf, size_t bufsize)
230
0
{
231
0
    int failure_info;
232
233
0
    if (statusInfo == NULL) {
234
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
235
0
        return NULL;
236
0
    }
237
238
0
    failure_info = ossl_cmp_pkisi_get_pkifailureinfo(statusInfo);
239
240
0
    return snprint_PKIStatusInfo_parts(ASN1_INTEGER_get(statusInfo->status),
241
0
                                       failure_info,
242
0
                                       statusInfo->statusString, buf, bufsize);
243
0
}
244
245
char *OSSL_CMP_CTX_snprint_PKIStatus(const OSSL_CMP_CTX *ctx, char *buf,
246
                                     size_t bufsize)
247
0
{
248
0
    if (ctx == NULL) {
249
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
250
0
        return NULL;
251
0
    }
252
253
0
    return snprint_PKIStatusInfo_parts(OSSL_CMP_CTX_get_status(ctx),
254
0
                                       OSSL_CMP_CTX_get_failInfoCode(ctx),
255
0
                                       OSSL_CMP_CTX_get0_statusString(ctx),
256
0
                                       buf, bufsize);
257
0
}
258
259
/*-
260
 * Creates a new PKIStatusInfo structure and fills it in
261
 * returns a pointer to the structure on success, NULL on error
262
 * note: strongly overlaps with TS_RESP_CTX_set_status_info()
263
 * and TS_RESP_CTX_add_failure_info() in ../ts/ts_rsp_sign.c
264
 */
265
OSSL_CMP_PKISI *OSSL_CMP_STATUSINFO_new(int status, int fail_info,
266
                                        const char *text)
267
42.7k
{
268
42.7k
    OSSL_CMP_PKISI *si = OSSL_CMP_PKISI_new();
269
42.7k
    ASN1_UTF8STRING *utf8_text = NULL;
270
42.7k
    int failure;
271
272
42.7k
    if (si == NULL)
273
0
        goto err;
274
42.7k
    if (!ASN1_INTEGER_set(si->status, status))
275
0
        goto err;
276
277
42.7k
    if (text != NULL) {
278
42.5k
        if ((utf8_text = ASN1_UTF8STRING_new()) == NULL
279
42.5k
                || !ASN1_STRING_set(utf8_text, text, -1))
280
0
            goto err;
281
42.5k
        if ((si->statusString = sk_ASN1_UTF8STRING_new_null()) == NULL)
282
0
            goto err;
283
42.5k
        if (!sk_ASN1_UTF8STRING_push(si->statusString, utf8_text))
284
0
            goto err;
285
        /* Ownership is lost. */
286
42.5k
        utf8_text = NULL;
287
42.5k
    }
288
289
1.19M
    for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) {
290
1.15M
        if ((fail_info & (1 << failure)) != 0) {
291
42.7k
            if (si->failInfo == NULL
292
42.7k
                    && (si->failInfo = ASN1_BIT_STRING_new()) == NULL)
293
0
                goto err;
294
42.7k
            if (!ASN1_BIT_STRING_set_bit(si->failInfo, failure, 1))
295
0
                goto err;
296
42.7k
        }
297
1.15M
    }
298
42.7k
    return si;
299
300
0
 err:
301
0
    OSSL_CMP_PKISI_free(si);
302
0
    ASN1_UTF8STRING_free(utf8_text);
303
    return NULL;
304
42.7k
}