crypto_mbedtls.h 5.7 KB

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