forked from margelo/react-native-quick-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMGLKeys.h
More file actions
123 lines (97 loc) · 3.52 KB
/
MGLKeys.h
File metadata and controls
123 lines (97 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// MGLCipherKeys.h
// react-native-quick-crypto
//
// Created by Oscar on 20.06.22.
//
#ifndef MGLCipherKeys_h
#define MGLCipherKeys_h
#include <jsi/jsi.h>
#include <openssl/evp.h>
#include <memory>
#include <optional>
#include <string>
#ifdef ANDROID
#include "Utils/MGLUtils.h"
#else
#include "MGLUtils.h"
#endif
namespace margelo {
namespace jsi = facebook::jsi;
enum PKEncodingType {
// RSAPublicKey / RSAPrivateKey according to PKCS#1.
kKeyEncodingPKCS1,
// PrivateKeyInfo or EncryptedPrivateKeyInfo according to PKCS#8.
kKeyEncodingPKCS8,
// SubjectPublicKeyInfo according to X.509.
kKeyEncodingSPKI,
// ECPrivateKey according to SEC1.
kKeyEncodingSEC1
};
enum PKFormatType { kKeyFormatDER, kKeyFormatPEM, kKeyFormatJWK };
enum KeyType { kKeyTypeSecret, kKeyTypePublic, kKeyTypePrivate };
enum KeyEncodingContext {
kKeyContextInput,
kKeyContextExport,
kKeyContextGenerate
};
enum class ParseKeyResult {
kParseKeyOk,
kParseKeyNotRecognized,
kParseKeyNeedPassphrase,
kParseKeyFailed
};
struct AsymmetricKeyEncodingConfig {
bool output_key_object_ = false;
PKFormatType format_ = kKeyFormatDER;
std::optional<PKEncodingType> type_ = std::nullopt;
};
using PublicKeyEncodingConfig = AsymmetricKeyEncodingConfig;
struct PrivateKeyEncodingConfig : public AsymmetricKeyEncodingConfig {
const EVP_CIPHER *cipher_;
// The ByteSource alone is not enough to distinguish between "no passphrase"
// and a zero-length passphrase (which can be a null pointer), therefore, we
// use a NonCopyableMaybe.
NonCopyableMaybe<ByteSource> passphrase_;
};
// Here node uses extends MemoryRetainer no clue what that is, something with
// Snapshots stripped it for our implementation but if something doesn't work,
// you know why
class ManagedEVPPKey {
public:
ManagedEVPPKey() {}
explicit ManagedEVPPKey(EVPKeyPointer &&pkey);
ManagedEVPPKey(const ManagedEVPPKey &that);
ManagedEVPPKey &operator=(const ManagedEVPPKey &that);
operator bool() const;
EVP_PKEY *get() const;
static PublicKeyEncodingConfig GetPublicKeyEncodingFromJs(
jsi::Runtime &runtime, const jsi::Value *arguments, unsigned int *offset,
KeyEncodingContext context);
static NonCopyableMaybe<PrivateKeyEncodingConfig> GetPrivateKeyEncodingFromJs(
jsi::Runtime &runtime, const jsi::Value *arguments, unsigned int *offset,
KeyEncodingContext context);
//
static ManagedEVPPKey GetParsedKey(jsi::Runtime &runtime,
EVPKeyPointer &&pkey, ParseKeyResult ret,
const char *default_msg);
static ManagedEVPPKey GetPublicOrPrivateKeyFromJs(jsi::Runtime &runtime,
const jsi::Value *args,
unsigned int *offset);
static ManagedEVPPKey GetPrivateKeyFromJs(jsi::Runtime &runtime,
const jsi::Value *args,
unsigned int *offset,
bool allow_key_object);
static std::optional<StringOrBuffer> ToEncodedPublicKey(
jsi::Runtime &runtime, ManagedEVPPKey key,
const PublicKeyEncodingConfig &config);
static std::optional<StringOrBuffer> ToEncodedPrivateKey(
jsi::Runtime &runtime, ManagedEVPPKey key,
const PrivateKeyEncodingConfig &config);
private:
// size_t size_of_private_key() const;
// size_t size_of_public_key() const;
EVPKeyPointer pkey_;
};
} // namespace margelo
#endif /* MGLCipherKeys_h */