Coverage Report

Created: 2025-07-01 06:08

/src/opensc/src/libopensc/sc-ossl-compat.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * sc-ossl-compat.h: OpenSC compatibility for older OpenSSL versions
3
 *
4
 * Copyright (C) 2016 Douglas E. Engert <deengert@gmail.com>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifndef _SC_OSSL_COMPAT_H
22
#define _SC_OSSL_COMPAT_H
23
24
#ifdef ENABLE_OPENSSL
25
26
#ifdef __cplusplus
27
extern "C" {
28
#endif
29
30
#include <openssl/opensslv.h>
31
#include <openssl/opensslconf.h>
32
/*
33
 * Provide compatibility OpenSSL 1.1.1, 3.0.1 and LibreSSL 3.4.2
34
 *
35
 * LibreSSL is a fork of OpenSSL from 2014
36
 * In its version of openssl/opensslv.h it defines:
37
 * OPENSSL_VERSION_NUMBER  0x20000000L (Will not change)
38
 * LIBRESSL_VERSION_NUMBER  0x3040200fL (changes with its versions)
39
 */
40
41
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30500000L
42
#define X509_get_extension_flags(x) (x->ex_flags)
43
#define X509_get_key_usage(x)   (x->ex_kusage)
44
#define X509_get_extended_key_usage(x)  (x->ex_xkusage)
45
#define EVP_MD_CTX_md_data(x)          (x->md_data)
46
#endif
47
48
#if defined(LIBRESSL_VERSION_NUMBER)
49
#define OPENSSL_malloc_init()     while(0) continue
50
#if LIBRESSL_VERSION_NUMBER < 0x30500000L
51
#define FIPS_mode()                             (0)
52
#endif
53
/* OpenSSL 1.1.1 has EVP_sha3_* */
54
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x30800000L
55
#define EVP_sha3_224()                          (NULL)
56
#define EVP_sha3_256()                          (NULL)
57
#define EVP_sha3_384()                          (NULL)
58
#define EVP_sha3_512()                          (NULL)
59
#endif
60
#if LIBRESSL_VERSION_NUMBER < 0x3070000fL
61
#define EVP_PKEY_new_raw_public_key(t, e, p, l) (NULL)
62
#define EVP_PKEY_get_raw_public_key(p, pu, l)   (0)
63
#endif
64
#endif
65
66
/* OpenSSL 1.1.1 has FIPS_mode function */
67
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
68
#define FIPS_mode()                             EVP_default_properties_is_fips_enabled(NULL)
69
#endif
70
71
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
72
73
#define USE_OPENSSL3_LIBCTX
74
75
#include <openssl/provider.h>
76
#include <openssl/crypto.h>
77
#include <openssl/evp.h>
78
79
typedef struct ossl3ctx {
80
  OSSL_LIB_CTX *libctx;
81
  OSSL_PROVIDER *defprov;
82
  OSSL_PROVIDER *legacyprov;
83
} ossl3ctx_t;
84
85
static inline EVP_MD *_sc_evp_md(ossl3ctx_t *ctx, const char *algorithm)
86
{
87
  return EVP_MD_fetch(ctx->libctx, algorithm, NULL);
88
}
89
#define sc_evp_md(ctx, alg) _sc_evp_md((ctx)->ossl3ctx, alg)
90
91
static inline void sc_evp_md_free(EVP_MD *md)
92
{
93
  EVP_MD_free(md);
94
}
95
96
static inline EVP_PKEY_CTX *_sc_evp_pkey_ctx_new(ossl3ctx_t *ctx,
97
             EVP_PKEY *pkey)
98
{
99
  return EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, NULL);
100
}
101
#define sc_evp_pkey_ctx_new(ctx, pkey) \
102
  _sc_evp_pkey_ctx_new((ctx)->ossl3ctx, pkey)
103
104
static inline EVP_CIPHER *_sc_evp_cipher(ossl3ctx_t *ctx, const char *algorithm)
105
{
106
  return EVP_CIPHER_fetch(ctx->libctx, algorithm, NULL);
107
}
108
#define sc_evp_cipher(ctx, alg) _sc_evp_cipher((ctx)->ossl3ctx, alg)
109
110
static inline void sc_evp_cipher_free(EVP_CIPHER *cipher)
111
{
112
  EVP_CIPHER_free(cipher);
113
}
114
115
#else /* OPENSSL < 3 */
116
117
#include <openssl/evp.h>
118
119
static inline EVP_MD *sc_evp_md(void *unused, const char *algorithm)
120
0
{
121
0
  return (EVP_MD *)EVP_get_digestbyname(algorithm);
122
0
}
Unexecuted instantiation: asn1.c:sc_evp_md
Unexecuted instantiation: pkcs15-algo.c:sc_evp_md
Unexecuted instantiation: sc.c:sc_evp_md
Unexecuted instantiation: log.c:sc_evp_md
123
124
static inline void sc_evp_md_free(EVP_MD *md)
125
0
{
126
0
  return;
127
0
}
Unexecuted instantiation: asn1.c:sc_evp_md_free
Unexecuted instantiation: pkcs15-algo.c:sc_evp_md_free
Unexecuted instantiation: sc.c:sc_evp_md_free
Unexecuted instantiation: log.c:sc_evp_md_free
128
129
static inline EVP_PKEY_CTX *sc_evp_pkey_ctx_new(void *unused, EVP_PKEY *pkey)
130
0
{
131
0
  return EVP_PKEY_CTX_new(pkey, NULL);
132
0
}
Unexecuted instantiation: asn1.c:sc_evp_pkey_ctx_new
Unexecuted instantiation: pkcs15-algo.c:sc_evp_pkey_ctx_new
Unexecuted instantiation: sc.c:sc_evp_pkey_ctx_new
Unexecuted instantiation: log.c:sc_evp_pkey_ctx_new
133
134
static inline EVP_CIPHER *sc_evp_cipher(void *unused, const char *algorithm)
135
0
{
136
0
  return (EVP_CIPHER *)EVP_get_cipherbyname(algorithm);
137
0
}
Unexecuted instantiation: asn1.c:sc_evp_cipher
Unexecuted instantiation: pkcs15-algo.c:sc_evp_cipher
Unexecuted instantiation: sc.c:sc_evp_cipher
Unexecuted instantiation: log.c:sc_evp_cipher
138
139
static inline void sc_evp_cipher_free(EVP_CIPHER *cipher)
140
0
{
141
0
  return;
142
0
}
Unexecuted instantiation: asn1.c:sc_evp_cipher_free
Unexecuted instantiation: pkcs15-algo.c:sc_evp_cipher_free
Unexecuted instantiation: sc.c:sc_evp_cipher_free
Unexecuted instantiation: log.c:sc_evp_cipher_free
143
144
#endif
145
146
147
#ifdef __cplusplus
148
}
149
#endif /* __cplusplus */
150
151
#endif /* ENABLE_OPENSSL */
152
#endif /* _SC_OSSL_COMPAT_H */