crypto_mbedtls.h 6.1 KB

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