1 /* $NetBSD: openssl_shim.c,v 1.5 2021/04/29 17:26:12 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 9 * 10 * See the COPYRIGHT file distributed with this work for additional 11 * information regarding copyright ownership. 12 */ 13 14 #include <inttypes.h> 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include <openssl/crypto.h> 19 #include <openssl/engine.h> 20 #include <openssl/evp.h> 21 #include <openssl/hmac.h> 22 #include <openssl/opensslv.h> 23 #include <openssl/ssl.h> 24 25 #include "openssl_shim.h" 26 27 #if !HAVE_CRYPTO_ZALLOC 28 void * 29 CRYPTO_zalloc(size_t num, const char *file, int line) { 30 void *ret = CRYPTO_malloc(num, file, line); 31 if (ret != NULL) { 32 memset(ret, 0, num); 33 } 34 return (ret); 35 } 36 #endif /* if !HAVE_CRYPTO_ZALLOC */ 37 38 #if !HAVE_EVP_CIPHER_CTX_NEW 39 EVP_CIPHER_CTX * 40 EVP_CIPHER_CTX_new(void) { 41 EVP_CIPHER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); 42 return (ctx); 43 } 44 #endif /* if !HAVE_EVP_CIPHER_CTX_NEW */ 45 46 #if !HAVE_EVP_CIPHER_CTX_FREE 47 void 48 EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) { 49 if (ctx != NULL) { 50 EVP_CIPHER_CTX_cleanup(ctx); 51 OPENSSL_free(ctx); 52 } 53 } 54 #endif /* if !HAVE_EVP_CIPHER_CTX_FREE */ 55 56 #if !HAVE_EVP_MD_CTX_NEW 57 EVP_MD_CTX * 58 EVP_MD_CTX_new(void) { 59 EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(*ctx)); 60 if (ctx != NULL) { 61 memset(ctx, 0, sizeof(*ctx)); 62 } 63 return (ctx); 64 } 65 #endif /* if !HAVE_EVP_MD_CTX_NEW */ 66 67 #if !HAVE_EVP_MD_CTX_FREE 68 void 69 EVP_MD_CTX_free(EVP_MD_CTX *ctx) { 70 if (ctx != NULL) { 71 EVP_MD_CTX_cleanup(ctx); 72 OPENSSL_free(ctx); 73 } 74 } 75 #endif /* if !HAVE_EVP_MD_CTX_FREE */ 76 77 #if !HAVE_EVP_MD_CTX_RESET 78 int 79 EVP_MD_CTX_reset(EVP_MD_CTX *ctx) { 80 return (EVP_MD_CTX_cleanup(ctx)); 81 } 82 #endif /* if !HAVE_EVP_MD_CTX_RESET */ 83 84 #if !HAVE_HMAC_CTX_NEW 85 HMAC_CTX * 86 HMAC_CTX_new(void) { 87 HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); 88 if (ctx != NULL) { 89 if (!HMAC_CTX_reset(ctx)) { 90 HMAC_CTX_free(ctx); 91 return (NULL); 92 } 93 } 94 return (ctx); 95 } 96 #endif /* if !HAVE_HMAC_CTX_NEW */ 97 98 #if !HAVE_HMAC_CTX_FREE 99 void 100 HMAC_CTX_free(HMAC_CTX *ctx) { 101 if (ctx != NULL) { 102 HMAC_CTX_cleanup(ctx); 103 OPENSSL_free(ctx); 104 } 105 } 106 #endif /* if !HAVE_HMAC_CTX_FREE */ 107 108 #if !HAVE_HMAC_CTX_RESET 109 int 110 HMAC_CTX_reset(HMAC_CTX *ctx) { 111 HMAC_CTX_cleanup(ctx); 112 return (1); 113 } 114 #endif /* if !HAVE_HMAC_CTX_RESET */ 115 116 #if !HAVE_HMAC_CTX_GET_MD 117 const EVP_MD * 118 HMAC_CTX_get_md(const HMAC_CTX *ctx) { 119 return (ctx->md); 120 } 121 #endif /* if !HAVE_HMAC_CTX_GET_MD */ 122 123 #if !HAVE_SSL_READ_EX 124 int 125 SSL_read_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes) { 126 int rv = SSL_read(ssl, buf, num); 127 if (rv > 0) { 128 *readbytes = rv; 129 rv = 1; 130 } 131 132 return (rv); 133 } 134 #endif 135 136 #if !HAVE_SSL_PEEK_EX 137 int 138 SSL_peek_ex(SSL *ssl, void *buf, size_t num, size_t *readbytes) { 139 int rv = SSL_peek(ssl, buf, num); 140 if (rv > 0) { 141 *readbytes = rv; 142 rv = 1; 143 } 144 145 return (rv); 146 } 147 #endif 148 149 #if !HAVE_SSL_WRITE_EX 150 int 151 SSL_write_ex(SSL *ssl, const void *buf, size_t num, size_t *written) { 152 int rv = SSL_write(ssl, buf, num); 153 if (rv > 0) { 154 *written = rv; 155 rv = 1; 156 } 157 158 return (rv); 159 } 160 #endif 161 162 #if !HAVE_BIO_READ_EX 163 int 164 BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes) { 165 int rv = BIO_read(b, data, dlen); 166 if (rv > 0) { 167 *readbytes = rv; 168 rv = 1; 169 } 170 171 return (rv); 172 } 173 #endif 174 175 #if !HAVE_BIO_WRITE_EX 176 int 177 BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written) { 178 int rv = BIO_write(b, data, dlen); 179 if (rv > 0) { 180 *written = rv; 181 rv = 1; 182 } 183 184 return (rv); 185 } 186 #endif 187 188 #if !HAVE_OPENSSL_INIT_CRYPTO 189 int 190 OPENSSL_init_crypto(uint64_t opts, const void *settings) { 191 (void)settings; 192 193 if ((opts & OPENSSL_INIT_NO_LOAD_CRYPTO_STRINGS) == 0) { 194 ERR_load_crypto_strings(); 195 } 196 197 if ((opts & (OPENSSL_INIT_NO_ADD_ALL_CIPHERS | 198 OPENSSL_INIT_NO_ADD_ALL_CIPHERS)) == 0) 199 { 200 OpenSSL_add_all_algorithms(); 201 } else if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) == 0) { 202 OpenSSL_add_all_digests(); 203 } else if ((opts & OPENSSL_INIT_NO_ADD_ALL_CIPHERS) == 0) { 204 OpenSSL_add_all_ciphers(); 205 } 206 207 return (1); 208 } 209 #endif 210 211 #if !HAVE_OPENSSL_INIT_SSL 212 int 213 OPENSSL_init_ssl(uint64_t opts, const void *settings) { 214 OPENSSL_init_crypto(opts, settings); 215 216 SSL_library_init(); 217 218 if ((opts & OPENSSL_INIT_NO_LOAD_SSL_STRINGS) == 0) { 219 SSL_load_error_strings(); 220 } 221 222 return (1); 223 } 224 #endif 225