xref: /freebsd/crypto/openssh/openbsd-compat/openssl-compat.c (revision 3d9fd9fcb432750f3716b28f6ccb0104cd9d351a)
1043840dfSDag-Erling Smørgrav /*
2043840dfSDag-Erling Smørgrav  * Copyright (c) 2005 Darren Tucker <dtucker@zip.com.au>
3043840dfSDag-Erling Smørgrav  *
4043840dfSDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
5043840dfSDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
6043840dfSDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
7043840dfSDag-Erling Smørgrav  *
8043840dfSDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9043840dfSDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10043840dfSDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11043840dfSDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12043840dfSDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13043840dfSDag-Erling Smørgrav  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14043840dfSDag-Erling Smørgrav  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15043840dfSDag-Erling Smørgrav  */
16043840dfSDag-Erling Smørgrav 
17a0ee8cc6SDag-Erling Smørgrav #define SSH_DONT_OVERLOAD_OPENSSL_FUNCS
18043840dfSDag-Erling Smørgrav #include "includes.h"
19043840dfSDag-Erling Smørgrav 
20bc5531deSDag-Erling Smørgrav #ifdef WITH_OPENSSL
21bc5531deSDag-Erling Smørgrav 
224a421b63SDag-Erling Smørgrav #include <stdarg.h>
234a421b63SDag-Erling Smørgrav #include <string.h>
244a421b63SDag-Erling Smørgrav 
25761efaa7SDag-Erling Smørgrav #ifdef USE_OPENSSL_ENGINE
26761efaa7SDag-Erling Smørgrav # include <openssl/engine.h>
274a421b63SDag-Erling Smørgrav # include <openssl/conf.h>
28761efaa7SDag-Erling Smørgrav #endif
29761efaa7SDag-Erling Smørgrav 
304a421b63SDag-Erling Smørgrav #include "log.h"
314a421b63SDag-Erling Smørgrav 
32043840dfSDag-Erling Smørgrav #include "openssl-compat.h"
33043840dfSDag-Erling Smørgrav 
34a0ee8cc6SDag-Erling Smørgrav /*
35a0ee8cc6SDag-Erling Smørgrav  * OpenSSL version numbers: MNNFFPPS: major minor fix patch status
36535af610SEd Maste  * Versions >=3 require only major versions to match.
37535af610SEd Maste  * For versions <3, we accept compatible fix versions (so we allow 1.0.1
38535af610SEd Maste  * to work with 1.0.0). Going backwards is only allowed within a patch series.
39535af610SEd Maste  * See https://www.openssl.org/policies/releasestrat.html
40a0ee8cc6SDag-Erling Smørgrav  */
41a0ee8cc6SDag-Erling Smørgrav 
42043840dfSDag-Erling Smørgrav int
ssh_compatible_openssl(long headerver,long libver)43a0ee8cc6SDag-Erling Smørgrav ssh_compatible_openssl(long headerver, long libver)
44043840dfSDag-Erling Smørgrav {
45a0ee8cc6SDag-Erling Smørgrav 	long mask, hfix, lfix;
46a0ee8cc6SDag-Erling Smørgrav 
47a0ee8cc6SDag-Erling Smørgrav 	/* exact match is always OK */
48a0ee8cc6SDag-Erling Smørgrav 	if (headerver == libver)
49043840dfSDag-Erling Smørgrav 		return 1;
50a0ee8cc6SDag-Erling Smørgrav 
51535af610SEd Maste 	/*
52535af610SEd Maste 	 * For versions >= 3.0, only the major and status must match.
53535af610SEd Maste 	 */
54535af610SEd Maste 	if (headerver >= 0x3000000f) {
55535af610SEd Maste 		mask = 0xf000000fL; /* major,status */
56a0ee8cc6SDag-Erling Smørgrav 		return (headerver & mask) == (libver & mask);
57043840dfSDag-Erling Smørgrav 	}
58043840dfSDag-Erling Smørgrav 
59a0ee8cc6SDag-Erling Smørgrav 	/*
60535af610SEd Maste 	 * For versions >= 1.0.0, but <3, major,minor,status must match and
61535af610SEd Maste 	 * library fix version must be equal to or newer than the header.
62a0ee8cc6SDag-Erling Smørgrav 	 */
63a0ee8cc6SDag-Erling Smørgrav 	mask = 0xfff0000fL; /* major,minor,status */
64a0ee8cc6SDag-Erling Smørgrav 	hfix = (headerver & 0x000ff000) >> 12;
65a0ee8cc6SDag-Erling Smørgrav 	lfix = (libver & 0x000ff000) >> 12;
66a0ee8cc6SDag-Erling Smørgrav 	if ( (headerver & mask) == (libver & mask) && lfix >= hfix)
67043840dfSDag-Erling Smørgrav 		return 1;
684a421b63SDag-Erling Smørgrav 	return 0;
694a421b63SDag-Erling Smørgrav }
704a421b63SDag-Erling Smørgrav 
71761efaa7SDag-Erling Smørgrav void
ssh_libcrypto_init(void)7219261079SEd Maste ssh_libcrypto_init(void)
73761efaa7SDag-Erling Smørgrav {
7419261079SEd Maste #if defined(HAVE_OPENSSL_INIT_CRYPTO) && \
7519261079SEd Maste       defined(OPENSSL_INIT_ADD_ALL_CIPHERS) && \
7619261079SEd Maste       defined(OPENSSL_INIT_ADD_ALL_DIGESTS)
7719261079SEd Maste 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS |
7819261079SEd Maste 	    OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
7919261079SEd Maste #elif defined(HAVE_OPENSSL_ADD_ALL_ALGORITHMS)
80e146993eSDag-Erling Smørgrav 	OpenSSL_add_all_algorithms();
8119261079SEd Maste #endif
82761efaa7SDag-Erling Smørgrav 
8319261079SEd Maste #ifdef	USE_OPENSSL_ENGINE
84761efaa7SDag-Erling Smørgrav 	/* Enable use of crypto hardware */
85761efaa7SDag-Erling Smørgrav 	ENGINE_load_builtin_engines();
86761efaa7SDag-Erling Smørgrav 	ENGINE_register_all_complete();
872f513db7SEd Maste 
8819261079SEd Maste 	/* Load the libcrypto config file to pick up engines defined there */
8919261079SEd Maste # if defined(HAVE_OPENSSL_INIT_CRYPTO) && defined(OPENSSL_INIT_LOAD_CONFIG)
902f513db7SEd Maste 	OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS |
912f513db7SEd Maste 	    OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, NULL);
9219261079SEd Maste # else
9319261079SEd Maste 	OPENSSL_config(NULL);
942f513db7SEd Maste # endif
9519261079SEd Maste #endif /* USE_OPENSSL_ENGINE */
96761efaa7SDag-Erling Smørgrav }
97bc5531deSDag-Erling Smørgrav 
98*3d9fd9fcSEd Maste #ifndef HAVE_EVP_DIGESTSIGN
99*3d9fd9fcSEd Maste int
EVP_DigestSign(EVP_MD_CTX * ctx,unsigned char * sigret,size_t * siglen,const unsigned char * tbs,size_t tbslen)100*3d9fd9fcSEd Maste EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
101*3d9fd9fcSEd Maste     const unsigned char *tbs, size_t tbslen)
102*3d9fd9fcSEd Maste {
103*3d9fd9fcSEd Maste 	if (sigret != NULL) {
104*3d9fd9fcSEd Maste 		if (EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
105*3d9fd9fcSEd Maste 			return 0;
106*3d9fd9fcSEd Maste 	}
107*3d9fd9fcSEd Maste 
108*3d9fd9fcSEd Maste 	return EVP_DigestSignFinal(ctx, sigret, siglen);
109*3d9fd9fcSEd Maste }
110*3d9fd9fcSEd Maste #endif
111*3d9fd9fcSEd Maste 
112*3d9fd9fcSEd Maste #ifndef HAVE_EVP_DIGESTVERIFY
113*3d9fd9fcSEd Maste int
EVP_DigestVerify(EVP_MD_CTX * ctx,const unsigned char * sigret,size_t siglen,const unsigned char * tbs,size_t tbslen)114*3d9fd9fcSEd Maste EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, size_t siglen,
115*3d9fd9fcSEd Maste     const unsigned char *tbs, size_t tbslen)
116*3d9fd9fcSEd Maste {
117*3d9fd9fcSEd Maste 	if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
118*3d9fd9fcSEd Maste 		return -1;
119*3d9fd9fcSEd Maste 
120*3d9fd9fcSEd Maste 	return EVP_DigestVerifyFinal(ctx, sigret, siglen);
121*3d9fd9fcSEd Maste }
122*3d9fd9fcSEd Maste #endif
123*3d9fd9fcSEd Maste 
124bc5531deSDag-Erling Smørgrav #endif /* WITH_OPENSSL */
125