crypto_mbedtls.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef CRYPTO_MBEDTLS_H
  31. #define CRYPTO_MBEDTLS_H
  32. #include "core/crypto/crypto.h"
  33. #include "core/io/resource.h"
  34. #include <mbedtls/ctr_drbg.h>
  35. #include <mbedtls/entropy.h>
  36. #include <mbedtls/ssl.h>
  37. class CryptoMbedTLS;
  38. class TLSContextMbedTLS;
  39. class CryptoKeyMbedTLS : public CryptoKey {
  40. private:
  41. mbedtls_pk_context pkey;
  42. int locks = 0;
  43. bool public_only = true;
  44. int _parse_key(const uint8_t *p_buf, int p_size);
  45. public:
  46. static CryptoKey *create(bool p_notify_postinitialize = true);
  47. static void make_default() { CryptoKey::_create = create; }
  48. static void finalize() { CryptoKey::_create = nullptr; }
  49. virtual Error load(const String &p_path, bool p_public_only);
  50. virtual Error save(const String &p_path, bool p_public_only);
  51. virtual String save_to_string(bool p_public_only);
  52. virtual Error load_from_string(const String &p_string_key, bool p_public_only);
  53. virtual bool is_public_only() const { return public_only; }
  54. CryptoKeyMbedTLS() {
  55. mbedtls_pk_init(&pkey);
  56. locks = 0;
  57. }
  58. ~CryptoKeyMbedTLS() {
  59. mbedtls_pk_free(&pkey);
  60. }
  61. _FORCE_INLINE_ void lock() { locks++; }
  62. _FORCE_INLINE_ void unlock() { locks--; }
  63. friend class CryptoMbedTLS;
  64. friend class TLSContextMbedTLS;
  65. };
  66. class X509CertificateMbedTLS : public X509Certificate {
  67. private:
  68. mbedtls_x509_crt cert;
  69. int locks;
  70. public:
  71. static X509Certificate *create(bool p_notify_postinitialize = true);
  72. static void make_default() { X509Certificate::_create = create; }
  73. static void finalize() { X509Certificate::_create = nullptr; }
  74. virtual Error load(const String &p_path);
  75. virtual Error load_from_memory(const uint8_t *p_buffer, int p_len);
  76. virtual Error save(const String &p_path);
  77. virtual String save_to_string();
  78. virtual Error load_from_string(const String &p_string_key);
  79. X509CertificateMbedTLS() {
  80. mbedtls_x509_crt_init(&cert);
  81. locks = 0;
  82. }
  83. ~X509CertificateMbedTLS() {
  84. mbedtls_x509_crt_free(&cert);
  85. }
  86. _FORCE_INLINE_ void lock() { locks++; }
  87. _FORCE_INLINE_ void unlock() { locks--; }
  88. friend class CryptoMbedTLS;
  89. friend class TLSContextMbedTLS;
  90. };
  91. class HMACContextMbedTLS : public HMACContext {
  92. private:
  93. HashingContext::HashType hash_type;
  94. int hash_len = 0;
  95. void *ctx = nullptr;
  96. public:
  97. static HMACContext *create(bool p_notify_postinitialize = true);
  98. static void make_default() { HMACContext::_create = create; }
  99. static void finalize() { HMACContext::_create = nullptr; }
  100. static bool is_md_type_allowed(mbedtls_md_type_t p_md_type);
  101. virtual Error start(HashingContext::HashType p_hash_type, const PackedByteArray &p_key);
  102. virtual Error update(const PackedByteArray &p_data);
  103. virtual PackedByteArray finish();
  104. HMACContextMbedTLS() {}
  105. ~HMACContextMbedTLS();
  106. };
  107. class CryptoMbedTLS : public Crypto {
  108. private:
  109. mbedtls_entropy_context entropy;
  110. mbedtls_ctr_drbg_context ctr_drbg;
  111. static X509CertificateMbedTLS *default_certs;
  112. public:
  113. static Crypto *create(bool p_notify_postinitialize = true);
  114. static void initialize_crypto();
  115. static void finalize_crypto();
  116. static X509CertificateMbedTLS *get_default_certificates();
  117. static void load_default_certificates(const String &p_path);
  118. static mbedtls_md_type_t md_type_from_hashtype(HashingContext::HashType p_hash_type, int &r_size);
  119. virtual PackedByteArray generate_random_bytes(int p_bytes);
  120. virtual Ref<CryptoKey> generate_rsa(int p_bytes);
  121. virtual 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);
  122. virtual Vector<uint8_t> sign(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, Ref<CryptoKey> p_key);
  123. virtual bool verify(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, const Vector<uint8_t> &p_signature, Ref<CryptoKey> p_key);
  124. virtual Vector<uint8_t> encrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_plaintext);
  125. virtual Vector<uint8_t> decrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_ciphertext);
  126. CryptoMbedTLS();
  127. ~CryptoMbedTLS();
  128. };
  129. #endif // CRYPTO_MBEDTLS_H