1 /**
2 Access to advanced cryptographic functionalities.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.crypto;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.classdb;
24 import godot.reference;
25 import godot.cryptokey;
26 import godot.x509certificate;
27 /**
28 Access to advanced cryptographic functionalities.
29 
30 The Crypto class allows you to access some more advanced cryptographic functionalities in Godot.
31 For now, this includes generating cryptographically secure random bytes, and RSA keys and self-signed X509 certificates generation. More functionalities are planned for future releases.
32 
33 
34 extends Node
35 
36 var crypto = Crypto.new()
37 var key = CryptoKey.new()
38 var cert = X509Certificate.new()
39 
40 func _ready():
41     # Generate new RSA key.
42     key = crypto.generate_rsa(4096)
43     # Generate new self-signed certificate with the given key.
44     cert = crypto.generate_self_signed_certificate(key, "CN=mydomain.com,O=My Game Company,C=IT")
45     # Save key and certificate in the user folder.
46     key.save("user://generated.key")
47     cert.save("user://generated.crt")
48 
49 
50 $(B Note:) Not available in HTML5 exports.
51 */
52 @GodotBaseClass struct Crypto
53 {
54 	package(godot) enum string _GODOT_internal_name = "Crypto";
55 public:
56 @nogc nothrow:
57 	union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; }
58 	alias _GODOT_base this;
59 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
60 	package(godot) __gshared bool _classBindingInitialized = false;
61 	package(godot) static struct GDNativeClassBinding
62 	{
63 		__gshared:
64 		@GodotName("generate_random_bytes") GodotMethod!(PoolByteArray, long) generateRandomBytes;
65 		@GodotName("generate_rsa") GodotMethod!(CryptoKey, long) generateRsa;
66 		@GodotName("generate_self_signed_certificate") GodotMethod!(X509Certificate, CryptoKey, String, String, String) generateSelfSignedCertificate;
67 	}
68 	/// 
69 	pragma(inline, true) bool opEquals(in Crypto other) const
70 	{ return _godot_object.ptr is other._godot_object.ptr; }
71 	/// 
72 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
73 	{ _godot_object.ptr = n; return null; }
74 	/// 
75 	pragma(inline, true) bool opEquals(typeof(null) n) const
76 	{ return _godot_object.ptr is n; }
77 	/// 
78 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
79 	mixin baseCasts;
80 	/// Construct a new instance of Crypto.
81 	/// Note: use `memnew!Crypto` instead.
82 	static Crypto _new()
83 	{
84 		static godot_class_constructor constructor;
85 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Crypto");
86 		if(constructor is null) return typeof(this).init;
87 		return cast(Crypto)(constructor());
88 	}
89 	@disable new(size_t s);
90 	/**
91 	Generates a $(D PoolByteArray) of cryptographically secure random bytes with given `size`.
92 	*/
93 	PoolByteArray generateRandomBytes(in long size)
94 	{
95 		checkClassBinding!(typeof(this))();
96 		return ptrcall!(PoolByteArray)(GDNativeClassBinding.generateRandomBytes, _godot_object, size);
97 	}
98 	/**
99 	Generates an RSA $(D CryptoKey) that can be used for creating self-signed certificates and passed to $(D StreamPeerSSL.acceptStream).
100 	*/
101 	Ref!CryptoKey generateRsa(in long size)
102 	{
103 		checkClassBinding!(typeof(this))();
104 		return ptrcall!(CryptoKey)(GDNativeClassBinding.generateRsa, _godot_object, size);
105 	}
106 	/**
107 	Generates a self-signed $(D X509Certificate) from the given $(D CryptoKey) and `issuer_name`. The certificate validity will be defined by `not_before` and `not_after` (first valid date and last valid date). The `issuer_name` must contain at least "CN=" (common name, i.e. the domain name), "O=" (organization, i.e. your company name), "C=" (country, i.e. 2 lettered ISO-3166 code of the country the organization is based in).
108 	A small example to generate an RSA key and a X509 self-signed certificate.
109 	
110 	
111 	var crypto = Crypto.new()
112 	# Generate 4096 bits RSA key.
113 	var key = crypto.generate_rsa(4096)
114 	# Generate self-signed certificate using the given key.
115 	var cert = crypto.generate_self_signed_certificate(key, "CN=example.com,O=A Game Company,C=IT")
116 	
117 	
118 	*/
119 	Ref!X509Certificate generateSelfSignedCertificate(CryptoKey key, in String issuer_name = gs!"CN=myserver,O=myorganisation,C=IT", in String not_before = gs!"20140101000000", in String not_after = gs!"20340101000000")
120 	{
121 		checkClassBinding!(typeof(this))();
122 		return ptrcall!(X509Certificate)(GDNativeClassBinding.generateSelfSignedCertificate, _godot_object, key, issuer_name, not_before, not_after);
123 	}
124 }