Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/cmp/cmp_util.c
Line
Count
Source
1
/*
2
 * Copyright 2007-2022 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 <string.h>
13
#include <openssl/cmp_util.h>
14
#include "cmp_local.h" /* just for decls of internal functions defined here */
15
#include <openssl/cmperr.h>
16
#include <openssl/err.h> /* should be implied by cmperr.h */
17
#include <openssl/x509v3.h>
18
19
/*
20
 * use trace API for CMP-specific logging, prefixed by "CMP " and severity
21
 */
22
23
int OSSL_CMP_log_open(void) /* is designed to be idempotent */
24
0
{
25
0
#ifdef OPENSSL_NO_TRACE
26
0
    return 1;
27
#else
28
#ifndef OPENSSL_NO_STDIO
29
    BIO *bio = BIO_new_fp(stdout, BIO_NOCLOSE);
30
31
    if (bio != NULL && OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, bio))
32
        return 1;
33
    BIO_free(bio);
34
#endif
35
    ERR_raise(ERR_LIB_CMP, CMP_R_NO_STDIO);
36
    return 0;
37
#endif
38
0
}
39
40
void OSSL_CMP_log_close(void) /* is designed to be idempotent */
41
220
{
42
220
    (void)OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_CMP, NULL);
43
220
}
44
45
/* return >= 0 if level contains logging level, possibly preceded by "CMP " */
46
0
#define max_level_len 5 /* = max length of the below strings, e.g., "EMERG" */
47
static OSSL_CMP_severity parse_level(const char *level)
48
0
{
49
0
    const char *end_level = strchr(level, ':');
50
0
    int len;
51
0
    char level_copy[max_level_len + 1];
52
53
0
    if (end_level == NULL)
54
0
        return -1;
55
56
0
    if (strncmp(level, OSSL_CMP_LOG_PREFIX,
57
0
            strlen(OSSL_CMP_LOG_PREFIX))
58
0
        == 0)
59
0
        level += strlen(OSSL_CMP_LOG_PREFIX);
60
0
    len = end_level - level;
61
0
    if (len > max_level_len)
62
0
        return -1;
63
0
    OPENSSL_strlcpy(level_copy, level, len + 1);
64
0
    return strcmp(level_copy, "EMERG") == 0 ? OSSL_CMP_LOG_EMERG : strcmp(level_copy, "ALERT") == 0 ? OSSL_CMP_LOG_ALERT
65
0
        : strcmp(level_copy, "CRIT") == 0                                                           ? OSSL_CMP_LOG_CRIT
66
0
        : strcmp(level_copy, "ERROR") == 0                                                          ? OSSL_CMP_LOG_ERR
67
0
        : strcmp(level_copy, "WARN") == 0                                                           ? OSSL_CMP_LOG_WARNING
68
0
        : strcmp(level_copy, "NOTE") == 0                                                           ? OSSL_CMP_LOG_NOTICE
69
0
        : strcmp(level_copy, "INFO") == 0                                                           ? OSSL_CMP_LOG_INFO
70
0
        : strcmp(level_copy, "DEBUG") == 0                                                          ? OSSL_CMP_LOG_DEBUG
71
0
                                                                                                    : -1;
72
0
}
73
74
const char *ossl_cmp_log_parse_metadata(const char *buf,
75
    OSSL_CMP_severity *level,
76
    char **func, char **file, int *line)
77
0
{
78
0
    const char *p_func = buf;
79
0
    const char *p_file = buf == NULL ? NULL : strchr(buf, ':');
80
0
    const char *p_level = buf;
81
0
    const char *msg = buf;
82
83
0
    *level = -1;
84
0
    *func = NULL;
85
0
    *file = NULL;
86
0
    *line = 0;
87
88
0
    if (p_file != NULL) {
89
0
        const char *p_line = strchr(++p_file, ':');
90
91
0
        if ((*level = parse_level(buf)) < 0 && p_line != NULL) {
92
            /* check if buf contains location info and logging level */
93
0
            char *p_level_tmp = (char *)p_level;
94
0
            const long line_number = strtol(++p_line, &p_level_tmp, 10);
95
96
0
            p_level = p_level_tmp;
97
0
            if (p_level > p_line && *(p_level++) == ':') {
98
0
                if ((*level = parse_level(p_level)) >= 0) {
99
0
                    *func = OPENSSL_strndup(p_func, p_file - 1 - p_func);
100
0
                    *file = OPENSSL_strndup(p_file, p_line - 1 - p_file);
101
                    /* no real problem if OPENSSL_strndup() returns NULL */
102
0
                    *line = (int)line_number;
103
0
                    msg = strchr(p_level, ':');
104
0
                    if (msg != NULL && *++msg == ' ')
105
0
                        msg++;
106
0
                }
107
0
            }
108
0
        }
109
0
    }
110
0
    return msg;
111
0
}
112
113
258k
#define UNKNOWN_FUNC "(unknown function)" /* the default for OPENSSL_FUNC */
114
/*
115
 * substitute fallback if component/function name is NULL or empty or contains
116
 * just pseudo-information "(unknown function)" due to -pedantic and macros.h
117
 */
118
static const char *improve_location_name(const char *func, const char *fallback)
119
258k
{
120
258k
    if (fallback == NULL)
121
0
        return func == NULL ? UNKNOWN_FUNC : func;
122
123
258k
    return func == NULL || *func == '\0' || strcmp(func, UNKNOWN_FUNC) == 0
124
258k
        ? fallback
125
258k
        : func;
126
258k
}
127
128
int OSSL_CMP_print_to_bio(BIO *bio, const char *component, const char *file,
129
    int line, OSSL_CMP_severity level, const char *msg)
130
0
{
131
0
    const char *level_string = level == OSSL_CMP_LOG_EMERG ? "EMERG" : level == OSSL_CMP_LOG_ALERT ? "ALERT"
132
0
        : level == OSSL_CMP_LOG_CRIT                                                               ? "CRIT"
133
0
        : level == OSSL_CMP_LOG_ERR                                                                ? "error"
134
0
        : level == OSSL_CMP_LOG_WARNING                                                            ? "warning"
135
0
        : level == OSSL_CMP_LOG_NOTICE                                                             ? "NOTE"
136
0
        : level == OSSL_CMP_LOG_INFO                                                               ? "info"
137
0
        : level == OSSL_CMP_LOG_DEBUG                                                              ? "DEBUG"
138
0
                                                                                                   : "(unknown level)";
139
140
0
#ifndef NDEBUG
141
0
    if (BIO_printf(bio, "%s:%s:%d:", improve_location_name(component, "CMP"),
142
0
            file, line)
143
0
        < 0)
144
0
        return 0;
145
0
#endif
146
0
    return BIO_printf(bio, OSSL_CMP_LOG_PREFIX "%s: %s\n",
147
0
               level_string, msg)
148
0
        >= 0;
149
0
}
150
151
#define ERR_PRINT_BUF_SIZE 4096
152
/* this is similar to ERR_print_errors_cb, but uses the CMP-specific cb type */
153
void OSSL_CMP_print_errors_cb(OSSL_CMP_log_cb_t log_fn)
154
44.3k
{
155
44.3k
    unsigned long err;
156
44.3k
    char msg[ERR_PRINT_BUF_SIZE];
157
44.3k
    const char *file = NULL, *func = NULL, *data = NULL;
158
44.3k
    int line, flags;
159
160
302k
    while ((err = ERR_get_error_all(&file, &line, &func, &data, &flags)) != 0) {
161
258k
        const char *component = improve_location_name(func, ERR_lib_error_string(err));
162
258k
        unsigned long reason = ERR_GET_REASON(err);
163
258k
        const char *rs = NULL;
164
258k
        char rsbuf[256];
165
166
258k
#ifndef OPENSSL_NO_ERR
167
258k
        if (ERR_SYSTEM_ERROR(err)) {
168
0
            if (openssl_strerror_r(reason, rsbuf, sizeof(rsbuf)))
169
0
                rs = rsbuf;
170
258k
        } else {
171
258k
            rs = ERR_reason_error_string(err);
172
258k
        }
173
258k
#endif
174
258k
        if (rs == NULL) {
175
128
            BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", reason);
176
128
            rs = rsbuf;
177
128
        }
178
258k
        if (data != NULL && (flags & ERR_TXT_STRING) != 0)
179
12.8k
            BIO_snprintf(msg, sizeof(msg), "%s:%s", rs, data);
180
245k
        else
181
245k
            BIO_snprintf(msg, sizeof(msg), "%s", rs);
182
183
258k
        if (log_fn == NULL) {
184
0
#ifndef OPENSSL_NO_STDIO
185
0
            BIO *bio = BIO_new_fp(stderr, BIO_NOCLOSE);
186
187
0
            if (bio != NULL) {
188
0
                OSSL_CMP_print_to_bio(bio, component, file, line,
189
0
                    OSSL_CMP_LOG_ERR, msg);
190
0
                BIO_free(bio);
191
0
            }
192
#else
193
            /* ERR_raise(ERR_LIB_CMP, CMP_R_NO_STDIO) makes no sense during error printing */
194
#endif
195
258k
        } else {
196
258k
            if (log_fn(component, file, line, OSSL_CMP_LOG_ERR, msg) <= 0)
197
0
                break; /* abort outputting the error report */
198
258k
        }
199
258k
    }
200
44.3k
}
201
202
int ossl_cmp_X509_STORE_add1_certs(X509_STORE *store, STACK_OF(X509) *certs,
203
    int only_self_signed)
204
0
{
205
0
    int i;
206
207
0
    if (store == NULL) {
208
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
209
0
        return 0;
210
0
    }
211
0
    if (certs == NULL)
212
0
        return 1;
213
0
    for (i = 0; i < sk_X509_num(certs); i++) {
214
0
        X509 *cert = sk_X509_value(certs, i);
215
216
0
        if (!only_self_signed || X509_self_signed(cert, 0) == 1)
217
0
            if (!X509_STORE_add_cert(store, cert)) /* ups cert ref counter */
218
0
                return 0;
219
0
    }
220
0
    return 1;
221
0
}
222
223
int ossl_cmp_sk_ASN1_UTF8STRING_push_str(STACK_OF(ASN1_UTF8STRING) *sk,
224
    const char *text, int len)
225
81.4k
{
226
81.4k
    ASN1_UTF8STRING *utf8string;
227
228
81.4k
    if (!ossl_assert(sk != NULL && text != NULL))
229
0
        return 0;
230
81.4k
    if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
231
0
        return 0;
232
81.4k
    if (!ASN1_STRING_set(utf8string, text, len))
233
0
        goto err;
234
81.4k
    if (!sk_ASN1_UTF8STRING_push(sk, utf8string))
235
0
        goto err;
236
81.4k
    return 1;
237
238
0
err:
239
0
    ASN1_UTF8STRING_free(utf8string);
240
0
    return 0;
241
81.4k
}
242
243
int ossl_cmp_asn1_octet_string_set1(ASN1_OCTET_STRING **tgt,
244
    const ASN1_OCTET_STRING *src)
245
336k
{
246
336k
    ASN1_OCTET_STRING *new;
247
336k
    if (tgt == NULL) {
248
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
249
0
        return 0;
250
0
    }
251
336k
    if (*tgt == src) /* self-assignment */
252
128k
        return 1;
253
254
208k
    if (src != NULL) {
255
129k
        if ((new = ASN1_OCTET_STRING_dup(src)) == NULL)
256
0
            return 0;
257
129k
    } else {
258
78.6k
        new = NULL;
259
78.6k
    }
260
261
208k
    ASN1_OCTET_STRING_free(*tgt);
262
208k
    *tgt = new;
263
208k
    return 1;
264
208k
}
265
266
int ossl_cmp_asn1_octet_string_set1_bytes(ASN1_OCTET_STRING **tgt,
267
    const unsigned char *bytes, int len)
268
115k
{
269
115k
    ASN1_OCTET_STRING *new = NULL;
270
271
115k
    if (tgt == NULL) {
272
0
        ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
273
0
        return 0;
274
0
    }
275
115k
    if (bytes != NULL) {
276
115k
        if ((new = ASN1_OCTET_STRING_new()) == NULL
277
115k
            || !(ASN1_OCTET_STRING_set(new, bytes, len))) {
278
0
            ASN1_OCTET_STRING_free(new);
279
0
            return 0;
280
0
        }
281
115k
    }
282
283
115k
    ASN1_OCTET_STRING_free(*tgt);
284
115k
    *tgt = new;
285
115k
    return 1;
286
115k
}