crypto_mbedtls.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*************************************************************************/
  2. /* crypto_mbedtls.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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;
  43. public:
  44. static CryptoKey *create();
  45. static void make_default() { CryptoKey::_create = create; }
  46. static void finalize() { CryptoKey::_create = nullptr; }
  47. virtual Error load(String p_path);
  48. virtual Error save(String p_path);
  49. CryptoKeyMbedTLS() {
  50. mbedtls_pk_init(&pkey);
  51. locks = 0;
  52. }
  53. ~CryptoKeyMbedTLS() {
  54. mbedtls_pk_free(&pkey);
  55. }
  56. _FORCE_INLINE_ void lock() { locks++; }
  57. _FORCE_INLINE_ void unlock() { locks--; }
  58. friend class CryptoMbedTLS;
  59. friend class SSLContextMbedTLS;
  60. };
  61. class X509CertificateMbedTLS : public X509Certificate {
  62. private:
  63. mbedtls_x509_crt cert;
  64. int locks;
  65. public:
  66. static X509Certificate *create();
  67. static void make_default() { X509Certificate::_create = create; }
  68. static void finalize() { X509Certificate::_create = nullptr; }
  69. virtual Error load(String p_path);
  70. virtual Error load_from_memory(const uint8_t *p_buffer, int p_len);
  71. virtual Error save(String p_path);
  72. X509CertificateMbedTLS() {
  73. mbedtls_x509_crt_init(&cert);
  74. locks = 0;
  75. }
  76. ~X509CertificateMbedTLS() {
  77. mbedtls_x509_crt_free(&cert);
  78. }
  79. _FORCE_INLINE_ void lock() { locks++; }
  80. _FORCE_INLINE_ void unlock() { locks--; }
  81. friend class CryptoMbedTLS;
  82. friend class SSLContextMbedTLS;
  83. };
  84. class CryptoMbedTLS : public Crypto {
  85. private:
  86. mbedtls_entropy_context entropy;
  87. mbedtls_ctr_drbg_context ctr_drbg;
  88. static X509CertificateMbedTLS *default_certs;
  89. public:
  90. static Crypto *create();
  91. static void initialize_crypto();
  92. static void finalize_crypto();
  93. static X509CertificateMbedTLS *get_default_certificates();
  94. static void load_default_certificates(String p_path);
  95. virtual PackedByteArray generate_random_bytes(int p_bytes);
  96. virtual Ref<CryptoKey> generate_rsa(int p_bytes);
  97. virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after);
  98. CryptoMbedTLS();
  99. ~CryptoMbedTLS();
  100. };
  101. #endif // CRYPTO_MBEDTLS_H