| 1 | #include "openssl_common.h" |
| 2 | |
| 3 | #include <openssl/ec.h> |
| 4 | #include <openssl/err.h> |
| 5 | #include <openssl/evp.h> |
| 6 | #include <openssl/hmac.h> |
| 7 | #include <openssl/x509.h> |
| 8 | #include <openssl/x509v3.h> |
| 9 | #if defined(WITH_OPENSSL3) |
| 10 | #include <openssl/param_build.h> |
| 11 | #endif |
| 12 | |
| 13 | namespace mlspp::hpke { |
| 14 | |
| 15 | template<> |
| 16 | void |
| 17 | typed_delete(EVP_CIPHER_CTX* ptr) |
| 18 | { |
| 19 | EVP_CIPHER_CTX_free(ptr); |
| 20 | } |
| 21 | |
| 22 | #if WITH_BORINGSSL |
| 23 | template<> |
| 24 | void |
| 25 | typed_delete(EVP_AEAD_CTX* ptr) |
| 26 | { |
| 27 | EVP_AEAD_CTX_free(ptr); |
| 28 | } |
| 29 | #endif |
| 30 | |
| 31 | template<> |
| 32 | void |
| 33 | typed_delete(EVP_PKEY_CTX* ptr) |
| 34 | { |
| 35 | EVP_PKEY_CTX_free(ptr); |
| 36 | } |
| 37 | |
| 38 | template<> |
| 39 | void |
| 40 | typed_delete(EVP_MD_CTX* ptr) |
| 41 | { |
| 42 | EVP_MD_CTX_free(ptr); |
| 43 | } |
| 44 | |
| 45 | #if !defined(WITH_OPENSSL3) |
| 46 | template<> |
| 47 | void |
| 48 | typed_delete(HMAC_CTX* ptr) |
| 49 | { |
| 50 | HMAC_CTX_free(ptr); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | template<> |
| 55 | void |
| 56 | typed_delete(EVP_PKEY* ptr) |
| 57 | { |
| 58 | EVP_PKEY_free(ptr); |
| 59 | } |
| 60 | |
| 61 | template<> |
| 62 | void |
| 63 | typed_delete(BIGNUM* ptr) |
| 64 | { |
| 65 | BN_free(ptr); |
| 66 | } |
| 67 | |
| 68 | template<> |
| 69 | void |
| 70 | typed_delete(EC_POINT* ptr) |
| 71 | { |
| 72 | EC_POINT_free(ptr); |
| 73 | } |
| 74 | |
| 75 | #if !defined(WITH_OPENSSL3) |
| 76 | template<> |
| 77 | void |
| 78 | typed_delete(EC_KEY* ptr) |
| 79 | { |
| 80 | EC_KEY_free(ptr); |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | #if defined(WITH_OPENSSL3) |
| 85 | template<> |
| 86 | void |
| 87 | typed_delete(EVP_MAC* ptr) |
| 88 | { |
| 89 | EVP_MAC_free(ptr); |
| 90 | } |
| 91 | |
| 92 | template<> |
| 93 | void |
| 94 | typed_delete(EVP_MAC_CTX* ptr) |
| 95 | { |
| 96 | EVP_MAC_CTX_free(ptr); |
| 97 | } |
| 98 | |
| 99 | template<> |
| 100 | void |
| 101 | typed_delete(EC_GROUP* ptr) |
| 102 | { |
| 103 | EC_GROUP_free(ptr); |
| 104 | } |
| 105 | |
| 106 | template<> |
| 107 | void |
| 108 | typed_delete(OSSL_PARAM_BLD* ptr) |
| 109 | { |
| 110 | OSSL_PARAM_BLD_free(ptr); |
| 111 | } |
| 112 | |
| 113 | template<> |
| 114 | void |
| 115 | typed_delete(OSSL_PARAM* ptr) |
| 116 | { |
| 117 | OSSL_PARAM_free(ptr); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | template<> |
| 122 | void |
| 123 | typed_delete(X509* ptr) |
| 124 | { |
| 125 | X509_free(ptr); |
| 126 | } |
| 127 | |
| 128 | template<> |
| 129 | void |
| 130 | typed_delete(STACK_OF(GENERAL_NAME) * ptr) |
| 131 | { |
| 132 | sk_GENERAL_NAME_pop_free(ptr, GENERAL_NAME_free); |
| 133 | } |
| 134 | |
| 135 | template<> |
| 136 | void |
| 137 | typed_delete(BIO* ptr) |
| 138 | { |
| 139 | BIO_vfree(ptr); |
| 140 | } |
| 141 | |
| 142 | template<> |
| 143 | void |
| 144 | typed_delete(ASN1_TIME* ptr) |
| 145 | { |
| 146 | ASN1_TIME_free(ptr); |
| 147 | } |
| 148 | |
| 149 | /// |
| 150 | /// Map OpenSSL errors to C++ exceptions |
| 151 | /// |
| 152 | |
| 153 | std::runtime_error |
| 154 | openssl_error() |
| 155 | { |
| 156 | auto code = ERR_get_error(); |
| 157 | return std::runtime_error(ERR_error_string(code, nullptr)); |
| 158 | } |
| 159 | |
| 160 | } // namespace mlspp::hpke |