|
| 1 | +#include"ncrypto.h" |
| 2 | +#include<algorithm> |
| 3 | +#include<cstring> |
| 4 | +#include"openssl/bn.h" |
| 5 | +#if OPENSSL_VERSION_MAJOR >= 3 |
| 6 | +#include"openssl/provider.h" |
| 7 | +#endif |
| 8 | + |
| 9 | +namespacencrypto{ |
| 10 | + |
| 11 | +// ============================================================================ |
| 12 | + |
| 13 | +ClearErrorOnReturn::ClearErrorOnReturn(CryptoErrorList* errors) : errors_(errors){ |
| 14 | +ERR_clear_error(); |
| 15 | +} |
| 16 | + |
| 17 | +ClearErrorOnReturn::~ClearErrorOnReturn(){ |
| 18 | +if (errors_ != nullptr) errors_->capture(); |
| 19 | +ERR_clear_error(); |
| 20 | +} |
| 21 | + |
| 22 | +intClearErrorOnReturn::peeKError(){returnERR_peek_error()} |
| 23 | + |
| 24 | +MarkPopErrorOnReturn::MarkPopErrorOnReturn(CryptoErrorList* errors) : errors_(errors){ |
| 25 | +ERR_set_mark(); |
| 26 | +} |
| 27 | + |
| 28 | +MarkPopErrorOnReturn::~MarkPopErrorOnReturn(){ |
| 29 | +if (errors_ != nullptr) errors_->capture(); |
| 30 | +ERR_pop_to_mark(); |
| 31 | +} |
| 32 | + |
| 33 | +intMarkPopErrorOnReturn::peekError(){returnERR_peek_error()} |
| 34 | + |
| 35 | +CryptoErrorList::CryptoErrorList(CryptoErrorList::Option option){ |
| 36 | +if (option == Option::CAPTURE_ON_CONSTRUCT) capture(); |
| 37 | +} |
| 38 | + |
| 39 | +voidCryptoErrorList::capture(){ |
| 40 | + errors_.clear(); |
| 41 | +while(constauto err = ERR_get_error()){ |
| 42 | +char buf[256]; |
| 43 | +ERR_error_string_n(err, buf, sizeof(buf)); |
| 44 | + errors_.emplace_front(buf); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +voidCryptoErrorList::add(std::string error){ |
| 49 | + errors_.push_back(error); |
| 50 | +} |
| 51 | + |
| 52 | +std::optional<std::string> CryptoErrorList::pop_back(){ |
| 53 | +if (errors_.empty()) return std::nullopt; |
| 54 | + std::string error = errors_.back(); |
| 55 | + errors_.pop_back(); |
| 56 | +return error; |
| 57 | +} |
| 58 | + |
| 59 | +std::optional<std::string> CryptoErrorList::pop_front(){ |
| 60 | +if (errors_.empty()) return std::nullopt; |
| 61 | + std::string error = errors_.front(); |
| 62 | + errors_.pop_front(); |
| 63 | +return error; |
| 64 | +} |
| 65 | + |
| 66 | +// ============================================================================ |
| 67 | +boolisFipsEnabled(){ |
| 68 | +#if OPENSSL_VERSION_MAJOR >= 3 |
| 69 | +returnEVP_default_properties_is_fips_enabled(nullptr) == 1; |
| 70 | +#else |
| 71 | +returnFIPS_mode() == 1; |
| 72 | +#endif |
| 73 | +} |
| 74 | + |
| 75 | +boolsetFipsEnabled(bool enable, CryptoErrorList* errors){ |
| 76 | +if (isFipsEnabled() == enable) returntrue; |
| 77 | + ClearErrorOnReturn clearErrorOnReturn(errors); |
| 78 | +#if OPENSSL_VERSION_MAJOR >= 3 |
| 79 | +returnEVP_default_properties_enable_fips(nullptr, enable ? 1 : 0) == 1; |
| 80 | +#else |
| 81 | +returnFIPS_mode_set(enable ? 1 : 0) == 1; |
| 82 | +#endif |
| 83 | +} |
| 84 | + |
| 85 | +booltestFipsEnabled(){ |
| 86 | +#if OPENSSL_VERSION_MAJOR >= 3 |
| 87 | + OSSL_PROVIDER* fips_provider = nullptr; |
| 88 | +if (OSSL_PROVIDER_available(nullptr, "fips")){ |
| 89 | + fips_provider = OSSL_PROVIDER_load(nullptr, "fips"); |
| 90 | + } |
| 91 | +constauto enabled = fips_provider == nullptr ? 0 : |
| 92 | +OSSL_PROVIDER_self_test(fips_provider) ? 1 : 0; |
| 93 | +#else |
| 94 | +#ifdef OPENSSL_FIPS |
| 95 | +constauto enabled = FIPS_selftest() ? 1 : 0; |
| 96 | +#else// OPENSSL_FIPS |
| 97 | +constauto enabled = 0; |
| 98 | +#endif// OPENSSL_FIPS |
| 99 | +#endif |
| 100 | + |
| 101 | +return enabled; |
| 102 | +} |
| 103 | + |
| 104 | +// ============================================================================ |
| 105 | +// Bignum |
| 106 | +BignumPointer::BignumPointer(BIGNUM* bignum) : bn_(bignum){} |
| 107 | + |
| 108 | +BignumPointer::BignumPointer(BignumPointer&& other) noexcept |
| 109 | + : bn_(other.release()){} |
| 110 | + |
| 111 | +BignumPointer& BignumPointer::operator=(BignumPointer&& other) noexcept{ |
| 112 | +if (this == &other) return *this; |
| 113 | +this->~BignumPointer(); |
| 114 | +return *new (this) BignumPointer(std::move(other)); |
| 115 | +} |
| 116 | + |
| 117 | +BignumPointer::~BignumPointer(){reset()} |
| 118 | + |
| 119 | +voidBignumPointer::reset(BIGNUM* bn){ |
| 120 | + bn_.reset(bn); |
| 121 | +} |
| 122 | + |
| 123 | +BIGNUM* BignumPointer::release(){ |
| 124 | +return bn_.release(); |
| 125 | +} |
| 126 | + |
| 127 | +size_tBignumPointer::byteLength(){ |
| 128 | +if (bn_ == nullptr) return0; |
| 129 | +returnBN_num_bytes(bn_.get()); |
| 130 | +} |
| 131 | + |
| 132 | +std::vector<uint8_t> BignumPointer::encode(){ |
| 133 | +returnencodePadded(bn_.get(), byteLength()); |
| 134 | +} |
| 135 | + |
| 136 | +std::vector<uint8_t> BignumPointer::encodePadded(size_t size){ |
| 137 | +returnencodePadded(bn_.get(), size); |
| 138 | +} |
| 139 | + |
| 140 | +std::vector<uint8_t> BignumPointer::encode(const BIGNUM* bn){ |
| 141 | +returnencodePadded(bn, bn != nullptr ? BN_num_bytes(bn) : 0); |
| 142 | +} |
| 143 | + |
| 144 | +std::vector<uint8_t> BignumPointer::encodePadded(const BIGNUM* bn, size_t s){ |
| 145 | +if (bn == nullptr) return std::vector<uint8_t>(0); |
| 146 | +size_t size = std::max(s, static_cast<size_t>(BN_num_bytes(bn))); |
| 147 | + std::vector<uint8_t> buf(size); |
| 148 | +BN_bn2binpad(bn, buf.data(), size); |
| 149 | +return buf; |
| 150 | +} |
| 151 | + |
| 152 | +bool BignumPointer::operator==(const BignumPointer& other) noexcept{ |
| 153 | +if (bn_ == nullptr && other.bn_ != nullptr) returnfalse; |
| 154 | +if (bn_ != nullptr && other.bn_ == nullptr) returnfalse; |
| 155 | +if (bn_ == nullptr && other.bn_ == nullptr) returntrue; |
| 156 | +returnBN_cmp(bn_.get(), other.bn_.get()) == 0; |
| 157 | +} |
| 158 | + |
| 159 | +bool BignumPointer::operator==(const BIGNUM* other) noexcept{ |
| 160 | +if (bn_ == nullptr && other != nullptr) returnfalse; |
| 161 | +if (bn_ != nullptr && other == nullptr) returnfalse; |
| 162 | +if (bn_ == nullptr && other == nullptr) returntrue; |
| 163 | +returnBN_cmp(bn_.get(), other) == 0; |
| 164 | +} |
| 165 | + |
| 166 | +// ============================================================================ |
| 167 | +// Utility methods |
| 168 | + |
| 169 | +boolCSPRNG(void* buffer, size_t length){ |
| 170 | +auto buf = reinterpret_cast<unsignedchar*>(buffer); |
| 171 | +do{ |
| 172 | +if (1 == RAND_status()){ |
| 173 | +#if OPENSSL_VERSION_MAJOR >= 3 |
| 174 | +if (1 == RAND_bytes_ex(nullptr, buf, length, 0)){ |
| 175 | +returntrue; |
| 176 | + } |
| 177 | +#else |
| 178 | +while (length > INT_MAX && 1 == RAND_bytes(buf, INT_MAX)){ |
| 179 | + buf += INT_MAX; |
| 180 | + length -= INT_MAX; |
| 181 | + } |
| 182 | +if (length <= INT_MAX && 1 == RAND_bytes(buf, static_cast<int>(length))) |
| 183 | +returntrue; |
| 184 | +#endif |
| 185 | + } |
| 186 | +#if OPENSSL_VERSION_MAJOR >= 3 |
| 187 | +constauto code = ERR_peek_last_error(); |
| 188 | +// A misconfigured OpenSSL 3 installation may report 1 from RAND_poll() |
| 189 | +// and RAND_status() but fail in RAND_bytes() if it cannot look up |
| 190 | +// a matching algorithm for the CSPRNG. |
| 191 | +if (ERR_GET_LIB(code) == ERR_LIB_RAND){ |
| 192 | +constauto reason = ERR_GET_REASON(code); |
| 193 | +if (reason == RAND_R_ERROR_INSTANTIATING_DRBG || |
| 194 | + reason == RAND_R_UNABLE_TO_FETCH_DRBG || |
| 195 | + reason == RAND_R_UNABLE_TO_CREATE_DRBG){ |
| 196 | +returnfalse; |
| 197 | + } |
| 198 | + } |
| 199 | +#endif |
| 200 | + } while (1 == RAND_poll()); |
| 201 | + |
| 202 | +returnfalse; |
| 203 | +} |
| 204 | + |
| 205 | +intNoPasswordCallback(char* buf, int size, int rwflag, void* u){ |
| 206 | +return0; |
| 207 | +} |
| 208 | + |
| 209 | +intPasswordCallback(char* buf, int size, int rwflag, void* u){ |
| 210 | +const Buffer* passphrase = static_cast<const Buffer*>(u); |
| 211 | +if (passphrase != nullptr){ |
| 212 | +size_t buflen = static_cast<size_t>(size); |
| 213 | +size_t len = passphrase->len; |
| 214 | +if (buflen < len) |
| 215 | +return -1; |
| 216 | +memcpy(buf, reinterpret_cast<constchar*>(passphrase->data), len); |
| 217 | +return len; |
| 218 | + } |
| 219 | + |
| 220 | +return -1; |
| 221 | +} |
| 222 | + |
| 223 | +} // namespace ncrypto |
0 commit comments