0.2.0-dev Download ZIP
165 lines ·6.9 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* crypto_mbedtls.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "core/crypto/crypto.h" |
| 34 | |
| 35 | #include <mbedtls/ssl.h> |
| 36 | |
| 37 | #if MBEDTLS_VERSION_MAJOR < 4 |
| 38 | int godot_mbedtls_random_compat(void *p_rng, unsigned char *r_output, size_t p_output_len); |
| 39 | #define GODOT_MBEDTLS_COMPAT_ARGS(x) x, godot_mbedtls_random_compat, nullptr |
| 40 | #define GODOT_MBEDTLS_COMPAT_SSL_CONF mbedtls_ssl_conf_rng(&conf, godot_mbedtls_random_compat, nullptr); |
| 41 | #else |
| 42 | #define GODOT_MBEDTLS_COMPAT_ARGS(x) x |
| 43 | #define GODOT_MBEDTLS_COMPAT_SSL_CONF |
| 44 | #endif |
| 45 | |
| 46 | class CryptoMbedTLS; |
| 47 | class TLSContextMbedTLS; |
| 48 | class CryptoKeyMbedTLS : public CryptoKey { |
| 49 | GDSOFTCLASS(CryptoKeyMbedTLS, CryptoKey); |
| 50 | |
| 51 | private: |
| 52 | psa_key_id_t pk_slot = PSA_KEY_ID_NULL; |
| 53 | psa_key_type_t pk_type = PSA_KEY_TYPE_NONE; |
| 54 | size_t pk_size = 0; |
| 55 | |
| 56 | bool public_only = true; |
| 57 | |
| 58 | void _clear(); |
| 59 | int _parse_key(const uint8_t *p_buf, int p_size, bool p_public_only); |
| 60 | |
| 61 | public: |
| 62 | static CryptoKey *create(bool p_notify_postinitialize = true); |
| 63 | static void make_default() { CryptoKey::_create = create; } |
| 64 | static void finalize() { CryptoKey::_create = nullptr; } |
| 65 | |
| 66 | static Ref<CryptoKeyMbedTLS> generate(psa_key_type_t p_type, size_t p_size); |
| 67 | |
| 68 | psa_key_id_t get_key_id() const { return pk_slot; } |
| 69 | psa_key_type_t get_key_type() const { return pk_type; } |
| 70 | size_t get_key_size() const { return pk_size; } |
| 71 | |
| 72 | psa_key_id_t reimport(psa_key_usage_t p_usage, psa_algorithm_t p_alg); // You *MUST* call psa_destroy_key when you are done with the key. |
| 73 | |
| 74 | Error load(const String &p_path, bool p_public_only) override; |
| 75 | Error save(const String &p_path, bool p_public_only) override; |
| 76 | String save_to_string(bool p_public_only) override; |
| 77 | Error load_from_string(const String &p_string_key, bool p_public_only) override; |
| 78 | bool is_public_only() const override { return public_only; } |
| 79 | |
| 80 | CryptoKeyMbedTLS() {} |
| 81 | ~CryptoKeyMbedTLS() override { |
| 82 | _clear(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | class X509CertificateMbedTLS : public X509Certificate { |
| 87 | GDSOFTCLASS(X509CertificateMbedTLS, X509Certificate); |
| 88 | |
| 89 | private: |
| 90 | mbedtls_x509_crt cert; |
| 91 | int locks; |
| 92 | |
| 93 | public: |
| 94 | static X509Certificate *create(bool p_notify_postinitialize = true); |
| 95 | static void make_default() { X509Certificate::_create = create; } |
| 96 | static void finalize() { X509Certificate::_create = nullptr; } |
| 97 | |
| 98 | mbedtls_x509_crt *get_context() { return &cert; } |
| 99 | |
| 100 | Error load(const String &p_path) override; |
| 101 | Error load_from_memory(const uint8_t *p_buffer, int p_len) override; |
| 102 | Error save(const String &p_path) override; |
| 103 | String save_to_string() override; |
| 104 | Error load_from_string(const String &p_string_key) override; |
| 105 | |
| 106 | X509CertificateMbedTLS() { |
| 107 | mbedtls_x509_crt_init(&cert); |
| 108 | locks = 0; |
| 109 | } |
| 110 | ~X509CertificateMbedTLS() override { |
| 111 | mbedtls_x509_crt_free(&cert); |
| 112 | ERR_FAIL_COND(locks); |
| 113 | } |
| 114 | |
| 115 | _FORCE_INLINE_ void lock() { locks++; } |
| 116 | _FORCE_INLINE_ void unlock() { locks--; } |
| 117 | }; |
| 118 | |
| 119 | class HMACContextMbedTLS : public HMACContext { |
| 120 | private: |
| 121 | psa_mac_operation_t mac_op = PSA_MAC_OPERATION_INIT; |
| 122 | psa_key_id_t key_slot = PSA_KEY_ID_NULL; |
| 123 | int hash_len = 0; |
| 124 | |
| 125 | void _clear(); |
| 126 | |
| 127 | public: |
| 128 | static HMACContext *create(bool p_notify_postinitialize = true); |
| 129 | static void make_default() { HMACContext::_create = create; } |
| 130 | static void finalize() { HMACContext::_create = nullptr; } |
| 131 | |
| 132 | static bool is_hash_type_allowed(HashingContext::HashType p_hash_type); |
| 133 | |
| 134 | Error start(HashingContext::HashType p_hash_type, const PackedByteArray &p_key) override; |
| 135 | Error update(const PackedByteArray &p_data) override; |
| 136 | PackedByteArray finish() override; |
| 137 | |
| 138 | HMACContextMbedTLS() {} |
| 139 | ~HMACContextMbedTLS() override; |
| 140 | }; |
| 141 | |
| 142 | class CryptoMbedTLS : public Crypto { |
| 143 | private: |
| 144 | static Ref<X509CertificateMbedTLS> default_certs; |
| 145 | |
| 146 | public: |
| 147 | static Crypto *create(bool p_notify_postinitialize = true); |
| 148 | static void initialize_crypto(); |
| 149 | static void finalize_crypto(); |
| 150 | static Ref<X509CertificateMbedTLS> get_default_certificates(); |
| 151 | static void load_default_certificates(const String &p_path); |
| 152 | static psa_algorithm_t psa_alg_from_hashtype(HashingContext::HashType p_hash_type, int &r_size); |
| 153 | static mbedtls_md_type_t md_type_from_hashtype(HashingContext::HashType p_hash_type, int &r_size); |
| 154 | |
| 155 | PackedByteArray generate_random_bytes(int p_bytes) override; |
| 156 | Ref<CryptoKey> generate_rsa(int p_bits) override; |
| 157 | Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, const String &p_issuer_name, const String &p_not_before, const String &p_not_after) override; |
| 158 | Vector<uint8_t> sign(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, Ref<CryptoKey> p_key) override; |
| 159 | bool verify(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, const Vector<uint8_t> &p_signature, Ref<CryptoKey> p_key) override; |
| 160 | Vector<uint8_t> encrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_plaintext) override; |
| 161 | Vector<uint8_t> decrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_ciphertext) override; |
| 162 | |
| 163 | CryptoMbedTLS(); |
| 164 | ~CryptoMbedTLS() override; |
| 165 | }; |