Coverage Report

Created: 2026-08-31 06:56

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