Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/crypto/crypto.h
9973 views
1
/**************************************************************************/
2
/* crypto.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/hashing_context.h"
34
#include "core/io/resource.h"
35
#include "core/io/resource_loader.h"
36
#include "core/io/resource_saver.h"
37
#include "core/object/ref_counted.h"
38
39
class CryptoKey : public Resource {
40
GDCLASS(CryptoKey, Resource);
41
42
protected:
43
static void _bind_methods();
44
static CryptoKey *(*_create)(bool p_notify_postinitialize);
45
46
public:
47
static CryptoKey *create(bool p_notify_postinitialize = true);
48
virtual Error load(const String &p_path, bool p_public_only = false) = 0;
49
virtual Error save(const String &p_path, bool p_public_only = false) = 0;
50
virtual String save_to_string(bool p_public_only = false) = 0;
51
virtual Error load_from_string(const String &p_string_key, bool p_public_only = false) = 0;
52
virtual bool is_public_only() const = 0;
53
};
54
55
class X509Certificate : public Resource {
56
GDCLASS(X509Certificate, Resource);
57
58
protected:
59
static void _bind_methods();
60
static X509Certificate *(*_create)(bool p_notify_postinitialize);
61
62
public:
63
static X509Certificate *create(bool p_notify_postinitialize = true);
64
virtual Error load(const String &p_path) = 0;
65
virtual Error load_from_memory(const uint8_t *p_buffer, int p_len) = 0;
66
virtual Error save(const String &p_path) = 0;
67
virtual String save_to_string() = 0;
68
virtual Error load_from_string(const String &string) = 0;
69
};
70
71
class TLSOptions : public RefCounted {
72
GDCLASS(TLSOptions, RefCounted);
73
74
private:
75
enum Mode {
76
MODE_CLIENT = 0,
77
MODE_CLIENT_UNSAFE = 1,
78
MODE_SERVER = 2,
79
};
80
81
Mode mode = MODE_CLIENT;
82
String common_name;
83
Ref<X509Certificate> trusted_ca_chain;
84
Ref<X509Certificate> own_certificate;
85
Ref<CryptoKey> private_key;
86
87
protected:
88
static void _bind_methods();
89
90
public:
91
static Ref<TLSOptions> client(Ref<X509Certificate> p_trusted_chain = Ref<X509Certificate>(), const String &p_common_name_override = String());
92
static Ref<TLSOptions> client_unsafe(Ref<X509Certificate> p_trusted_chain);
93
static Ref<TLSOptions> server(Ref<CryptoKey> p_own_key, Ref<X509Certificate> p_own_certificate);
94
95
String get_common_name_override() const { return common_name; }
96
Ref<X509Certificate> get_trusted_ca_chain() const { return trusted_ca_chain; }
97
Ref<X509Certificate> get_own_certificate() const { return own_certificate; }
98
Ref<CryptoKey> get_private_key() const { return private_key; }
99
bool is_server() const { return mode == MODE_SERVER; }
100
bool is_unsafe_client() const { return mode == MODE_CLIENT_UNSAFE; }
101
};
102
103
class HMACContext : public RefCounted {
104
GDCLASS(HMACContext, RefCounted);
105
106
protected:
107
static void _bind_methods();
108
static HMACContext *(*_create)(bool p_notify_postinitialize);
109
110
public:
111
static HMACContext *create(bool p_notify_postinitialize = true);
112
113
virtual Error start(HashingContext::HashType p_hash_type, const PackedByteArray &p_key) = 0;
114
virtual Error update(const PackedByteArray &p_data) = 0;
115
virtual PackedByteArray finish() = 0;
116
117
HMACContext() {}
118
virtual ~HMACContext() {}
119
};
120
121
class Crypto : public RefCounted {
122
GDCLASS(Crypto, RefCounted);
123
124
protected:
125
static void _bind_methods();
126
static Crypto *(*_create)(bool p_notify_postinitialize);
127
static void (*_load_default_certificates)(const String &p_path);
128
129
public:
130
static Crypto *create(bool p_notify_postinitialize = true);
131
static void load_default_certificates(const String &p_path);
132
133
virtual PackedByteArray generate_random_bytes(int p_bytes) = 0;
134
virtual Ref<CryptoKey> generate_rsa(int p_bytes) = 0;
135
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) = 0;
136
137
virtual Vector<uint8_t> sign(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, Ref<CryptoKey> p_key) = 0;
138
virtual bool verify(HashingContext::HashType p_hash_type, const Vector<uint8_t> &p_hash, const Vector<uint8_t> &p_signature, Ref<CryptoKey> p_key) = 0;
139
virtual Vector<uint8_t> encrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_plaintext) = 0;
140
virtual Vector<uint8_t> decrypt(Ref<CryptoKey> p_key, const Vector<uint8_t> &p_ciphertext) = 0;
141
142
PackedByteArray hmac_digest(HashingContext::HashType p_hash_type, const PackedByteArray &p_key, const PackedByteArray &p_msg);
143
144
// Compares two PackedByteArrays for equality without leaking timing information in order to prevent timing attacks.
145
// @see: https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy
146
bool constant_time_compare(const PackedByteArray &p_trusted, const PackedByteArray &p_received);
147
148
Crypto() {}
149
};
150
151
class ResourceFormatLoaderCrypto : public ResourceFormatLoader {
152
public:
153
virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
154
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
155
virtual bool handles_type(const String &p_type) const override;
156
virtual String get_resource_type(const String &p_path) const override;
157
158
// Treat certificates as text files, do not generate a `*.{crt,key,pub}.uid` file.
159
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; }
160
virtual bool has_custom_uid_support() const override { return true; }
161
};
162
163
class ResourceFormatSaverCrypto : public ResourceFormatSaver {
164
public:
165
virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
166
virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
167
virtual bool recognize(const Ref<Resource> &p_resource) const override;
168
};
169
170