pam_pkcs11 0.6.14
pam-pkcs11-ossl-compat.h
Go to the documentation of this file.
1/*
2 * sc-ossl-compat.h: OpenSC ecompatability 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef _PAM_PKCS11_OSSL_COMPAT_H
22#define _PAM_PKCS11_OSSL_COMPAT_H
23
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#include <openssl/opensslv.h>
30#include <openssl/opensslconf.h>
31/*
32 * Provide backward compatibility to older versions of OpenSSL
33 * while using most of OpenSSL 1.1 API
34 */
35
36/*
37 * EVP_CIPHER_CTX functions:
38 * EVP_CIPHER_CTX_new not in 0.9.7
39 * EVP_CIPHER_CTX_free not in 0.9.7
40 * EVP_CIPHER_CTX_init in 0.9.7 to 1.0.2. defined in 1.1 as EVP_CIPHER_CTX_reset
41 * EVP_CIPHER_CTX_cleanup in 0.9.7 to 1.0.2, defined in 1.1 as EVP_CIPHER_CTX_reset
42 * EVP_CIPHER_CTX_reset only in 1.1
43 *
44 * EVP_CIPHER_CTX_new does a EVP_CIPHER_CTX_init
45 * EVP_CIPHER_CTX_free does a EVP_CIPHER_CTX_cleanup
46 * EVP_CIPHER_CTX_cleanup does equivalent of a EVP_CIPHER_CTX_init
47 * Use EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_free, and EVP_CIPHER_CTX_cleanup between operations
48 */
49
50#if OPENSSL_VERSION_NUMBER <= 0x009070dfL
51
52/* in 0.9.7 EVP_CIPHER_CTX was always allocated inline or in other structures */
53
54#define EVP_CIPHER_CTX_new() ({ \
55 EVP_CIPHER_CTX * tmp = NULL; \
56 tmp = OPENSSL_malloc(sizeof(struct evp_cipher_ctx_st)); \
57 if (tmp) { \
58 EVP_CIPHER_CTX_init(tmp); \
59 } \
60 tmp; \
61 })
62
63#define EVP_CIPHER_CTX_free(x) ({ \
64 if (x) { \
65 EVP_CIPHER_CTX_cleanup(x); \
66 OPENSSL_free(x); \
67 } \
68 })
69#endif /* OPENSSL_VERSION_NUMBER =< 0x00907000L */
70
71/*
72 * 1.1 renames RSA_PKCS1_SSLeay to RSA_PKCS1_OpenSSL
73 * use RSA_PKCS1_OpenSSL
74 * Previous versions are missing a number of functions to access
75 * some hidden structures. Define them here:
76 */
77
78/* EVP_PKEY_base_id introduced in 1.0.1 */
79#if OPENSSL_VERSION_NUMBER < 0x10001000L
80#define EVP_PKEY_base_id(x) (x->type)
81#endif
82
83#if OPENSSL_VERSION_NUMBER < 0x10100000L
84#define RSA_PKCS1_OpenSSL RSA_PKCS1_SSLeay
85#define OPENSSL_malloc_init CRYPTO_malloc_init
86
87#define EVP_PKEY_get0_RSA(x) (x->pkey.rsa)
88#define EVP_PKEY_get0_DSA(x) (x->pkey.dsa)
89#define X509_get_extension_flags(x) (x->ex_flags)
90#define X509_get_key_usage(x) (x->ex_kusage)
91#define X509_get_extended_key_usage(x) (x->ex_xkusage)
92#define EVP_MD_CTX_new EVP_MD_CTX_create
93#define EVP_MD_CTX_free EVP_MD_CTX_destroy
94#define EVP_PKEY_up_ref(user_key) CRYPTO_add(&user_key->references, 1, CRYPTO_LOCK_EVP_PKEY)
95#define X509_up_ref(cert) CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509)
96#define X509_get0_tbs_sigalg(x) (x->cert_info->key->algor)
97#define X509_OBJECT_get0_X509(x) (x->data.x509)
98#define X509_OBJECT_get0_X509_CRL(x) (x->data.crl)
99#define RSA_get0_e(x) (x->e)
100#define RSA_get0_n(x) (x->n)
101#define ECDSA_SIG_get0_r(x) (x->r)
102#define ECDSA_SIG_get0_s(x) (x->s)
103#define ASN1_STRING_get0_data(x) ASN1_STRING_data(x)
104
105#define X509_OBJECT_free(x) ({ \
106 if (x) { \
107 X509_OBJECT_free_contents(x); \
108 OPENSSL_free(x); \
109 } \
110 })
111#endif
112
113
114/*
115 * OpenSSL-1.1.0-pre5 has hidden the RSA and DSA structures
116 * One can no longer use statements like rsa->n = ...
117 * Macros and defines don't work on all systems, so use inline versions
118 * If that is not good enough, vsersions could be added to libopensc
119 */
120
121#if OPENSSL_VERSION_NUMBER < 0x10100000L
122/* based on OpenSSL-1.1.0 e_os2.h */
123/* pam_pkcs11_ossl_inline: portable inline definition usable in public headers */
124# if !defined(inline) && !defined(__cplusplus)
125# if defined(__STDC_VERSION__) && __STDC_VERSION__>=199901L
126 /* just use inline */
127# define pam_pkcs11_ossl_inline inline
128# elif defined(__GNUC__) && __GNUC__>=2
129# define pam_pkcs11_ossl_inline __inline__
130# elif defined(_MSC_VER)
131# define pam_pkcs11_ossl_inline __inline
132# else
133# define pam_pkcs11_ossl_inline
134# endif
135# else
136# define pam_pkcs11_ossl_inline inline
137# endif
138#endif
139
140#if OPENSSL_VERSION_NUMBER < 0x10100000L
141
142#define RSA_bits(R) (BN_num_bits(R->n))
143
144#include <openssl/bn.h>
145#ifndef OPENSSL_NO_RSA
146#include <openssl/rsa.h>
147#endif
148#ifndef OPENSSL_NO_DSA
149#include <openssl/dsa.h>
150#endif
151
152#if 1
153#ifndef OPENSSL_NO_RSA
154static pam_pkcs11_ossl_inline int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
155{
156 /* d is the private component and may be NULL */
157 if (n == NULL || e == NULL)
158 return 0;
159
160 BN_free(r->n);
161 BN_free(r->e);
162 BN_free(r->d);
163 r->n = n;
164 r->e = e;
165 r->d = d;
166
167 return 1;
168}
169
170static pam_pkcs11_ossl_inline int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
171{
172 if (p == NULL || q == NULL)
173 return 0;
174
175 BN_free(r->p);
176 BN_free(r->q);
177 r->p = p;
178 r->q = q;
179
180 return 1;
181}
182
183static pam_pkcs11_ossl_inline int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
184{
185 if (dmp1 == NULL || dmq1 == NULL || iqmp == NULL)
186 return 0;
187
188 BN_free(r->dmp1);
189 BN_free(r->dmq1);
190 BN_free(r->iqmp);
191 r->dmp1 = dmp1;
192 r->dmq1 = dmq1;
193 r->iqmp = iqmp;
194
195 return 1;
196}
197
198static pam_pkcs11_ossl_inline void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
199{
200 if (n != NULL)
201 *n = r->n;
202 if (e != NULL)
203 *e = r->e;
204 if (d != NULL)
205 *d = r->d;
206}
207
208static pam_pkcs11_ossl_inline void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
209{
210 if (p != NULL)
211 *p = r->p;
212 if (q != NULL)
213 *q = r->q;
214}
215
217 const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp)
218{
219 if (dmp1 != NULL)
220 *dmp1 = r->dmp1;
221 if (dmq1 != NULL)
222 *dmq1 = r->dmq1;
223 if (iqmp != NULL)
224 *iqmp = r->iqmp;
225}
226
227#endif /* OPENSSL_NO_RSA */
228
229#ifndef OPENSSL_NO_DSA
230static pam_pkcs11_ossl_inline void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
231{
232 if (p != NULL)
233 *p = d->p;
234 if (q != NULL)
235 *q = d->q;
236 if (g != NULL)
237 *g = d->g;
238}
239
240static pam_pkcs11_ossl_inline void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
241{
242 if (pub_key != NULL)
243 *pub_key = d->pub_key;
244 if (priv_key != NULL)
245 *priv_key = d->priv_key;
246}
247
248/* NOTE: DSA_set0_* functions not defined because they are not currently used in OpenSC */
249#endif /* OPENSSL_NO_DSA */
250
251#else /* if we used macros */
252
253#define RSA_set0_key(R, N, E, D) \
254 ({ \
255 int ret = 0; \
256 if (!(N) || !(E)) { \
257 ret = 0; \
258 } else { \
259 BN_free(R->n); \
260 BN_free(R->e); \
261 BN_free(R->d); \
262 R->n = (N); \
263 R->e = (E); \
264 R->d = (D); \
265 ret = 1; \
266 } \
267 ret; \
268 })
269
270#define RSA_set0_factors(R, P, Q) \
271 ({ \
272 int ret= 0; \
273 if (!P || !Q) { \
274 ret = 0; \
275 } else { \
276 BN_free(R->p); \
277 BN_free(R->q); \
278 R->p = P; \
279 R->q = Q; \
280 ret = 1; \
281 } \
282 ret; \
283 })
284
285#define RSA_set0_crt_params(R, DMP1, DMQ1, IQMP) \
286 ({ \
287 int ret = 0; \
288 if (!DMP1 || !DMQ1 || !IQMP) { \
289 ret = 0; \
290 } else { \
291 BN_free(R->dmp1); \
292 BN_free(R->dmq1); \
293 BN_free(R->iqmp); \
294 R->dmp1 = DMP1; \
295 R->dmq1 = DMQ1; \
296 R->iqmp = IQMP; \
297 ret = 1; \
298 } \
299 ret; \
300 })
301
302#define RSA_get0_key(R, N, E, D) { \
303 BIGNUM **n = N; \
304 BIGNUM **e = E; \
305 BIGNUM **d = D; \
306 if (n) *(n) = R->n; \
307 if (e) *(e) = R->e; \
308 if (d) *(d) = R->d; \
309 }
310
311#define RSA_get0_factors(R, P, Q) {\
312 BIGNUM **p = P; \
313 BIGNUM **q = Q; \
314 if (p) *(p) = R->p; \
315 if (q) *(q) = R->q; \
316 }
317
318#define RSA_get0_crt_params(R, DMP1, DMQ1, IQMP) { \
319 BIGNUM **dmp1 = DMP1; \
320 BIGNUM **dmq1 = DMQ1; \
321 BIGNUM **iqmp = IQMP; \
322 if (dmp1) *(dmp1) = R->dmp1; \
323 if (dmq1) *(dmq1) = R->dmq1; \
324 if (iqmp) *(iqmp) = R->iqmp; \
325 }
326
327#define DSA_get0_key(D, PUB, PRIV) { \
328 BIGNUM **pub = PUB; \
329 BIGNUM **priv = PRIV; \
330 if (pub) *(pub) = D->pub_key; \
331 if (priv) *(priv) = D->priv_key; \
332 }
333
334#define DSA_get0_pqg(D, P, Q, G) { \
335 BIGNUM **p = P; \
336 BIGNUM **q = Q; \
337 BIGNUM **g = G; \
338 if (p) *(p) = D->p; \
339 if (q) *(q) = D->q; \
340 if (g) *(g) = D->g; \
341 }
342
343/* NOTE: DSA_set0_* functions not defined because they are not used in OpenSC */
344#endif /* 0 */
345#endif
346
347#ifdef __cplusplus
348}
349#endif
350
351#endif
static pam_pkcs11_ossl_inline void DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
static pam_pkcs11_ossl_inline int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
static pam_pkcs11_ossl_inline int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
#define pam_pkcs11_ossl_inline
static pam_pkcs11_ossl_inline int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
static pam_pkcs11_ossl_inline void RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp)
static pam_pkcs11_ossl_inline void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
static pam_pkcs11_ossl_inline void RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
static pam_pkcs11_ossl_inline void DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)