@webrtc-node/webrtc 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +177 -0
- package/LICENSE +373 -0
- package/README.md +87 -0
- package/index.d.ts +388 -0
- package/lib/index.js +4910 -0
- package/lib/load-native.js +42 -0
- package/package.json +91 -0
- package/scripts/install-native.js +154 -0
- package/scripts/prebuild-integrity.js +227 -0
- package/src/native/addon.cc +1632 -0
- package/src/native/certificate.cc +215 -0
- package/src/native/certificate.hpp +20 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
#include "certificate.hpp"
|
|
2
|
+
|
|
3
|
+
#include <chrono>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <iomanip>
|
|
6
|
+
#include <limits>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <sstream>
|
|
9
|
+
#include <stdexcept>
|
|
10
|
+
#include <string>
|
|
11
|
+
|
|
12
|
+
#include <openssl/bio.h>
|
|
13
|
+
#include <openssl/ec.h>
|
|
14
|
+
#include <openssl/err.h>
|
|
15
|
+
#include <openssl/evp.h>
|
|
16
|
+
#include <openssl/pem.h>
|
|
17
|
+
#include <openssl/rsa.h>
|
|
18
|
+
#include <openssl/x509.h>
|
|
19
|
+
|
|
20
|
+
namespace webrtc_node {
|
|
21
|
+
namespace {
|
|
22
|
+
|
|
23
|
+
std::string OpenSslErrorString() {
|
|
24
|
+
unsigned long error = ERR_get_error();
|
|
25
|
+
if (!error)
|
|
26
|
+
return "unknown OpenSSL error";
|
|
27
|
+
char buffer[256];
|
|
28
|
+
ERR_error_string_n(error, buffer, sizeof(buffer));
|
|
29
|
+
return buffer;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void CheckOpenSsl(int result, const char *message) {
|
|
33
|
+
if (result != 1)
|
|
34
|
+
throw std::runtime_error(std::string(message) + ": " + OpenSslErrorString());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void CheckOpenSslPositive(int result, const char *message) {
|
|
38
|
+
if (result <= 0)
|
|
39
|
+
throw std::runtime_error(std::string(message) + ": " + OpenSslErrorString());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
using BioPtr = std::unique_ptr<BIO, decltype(&BIO_free)>;
|
|
43
|
+
using EvpPkeyPtr = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>;
|
|
44
|
+
using EvpPkeyCtxPtr = std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)>;
|
|
45
|
+
using X509Ptr = std::unique_ptr<X509, decltype(&X509_free)>;
|
|
46
|
+
|
|
47
|
+
std::string BioToString(BIO *bio) {
|
|
48
|
+
BUF_MEM *memory = nullptr;
|
|
49
|
+
BIO_get_mem_ptr(bio, &memory);
|
|
50
|
+
if (!memory || !memory->data)
|
|
51
|
+
return {};
|
|
52
|
+
return std::string(memory->data, memory->length);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
EvpPkeyPtr GenerateEcKey() {
|
|
56
|
+
EvpPkeyCtxPtr parameterContext(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr),
|
|
57
|
+
EVP_PKEY_CTX_free);
|
|
58
|
+
if (!parameterContext)
|
|
59
|
+
throw std::runtime_error("Failed to allocate EC parameter context");
|
|
60
|
+
CheckOpenSsl(EVP_PKEY_paramgen_init(parameterContext.get()), "Failed to initialize EC parameters");
|
|
61
|
+
CheckOpenSsl(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(parameterContext.get(),
|
|
62
|
+
NID_X9_62_prime256v1),
|
|
63
|
+
"Failed to set EC curve");
|
|
64
|
+
|
|
65
|
+
EVP_PKEY *parametersRaw = nullptr;
|
|
66
|
+
CheckOpenSsl(EVP_PKEY_paramgen(parameterContext.get(), ¶metersRaw),
|
|
67
|
+
"Failed to generate EC parameters");
|
|
68
|
+
EvpPkeyPtr parameters(parametersRaw, EVP_PKEY_free);
|
|
69
|
+
|
|
70
|
+
EvpPkeyCtxPtr keyContext(EVP_PKEY_CTX_new(parameters.get(), nullptr), EVP_PKEY_CTX_free);
|
|
71
|
+
if (!keyContext)
|
|
72
|
+
throw std::runtime_error("Failed to allocate EC key context");
|
|
73
|
+
CheckOpenSsl(EVP_PKEY_keygen_init(keyContext.get()), "Failed to initialize EC key generation");
|
|
74
|
+
|
|
75
|
+
EVP_PKEY *keyRaw = nullptr;
|
|
76
|
+
CheckOpenSsl(EVP_PKEY_keygen(keyContext.get(), &keyRaw), "Failed to generate EC key");
|
|
77
|
+
return EvpPkeyPtr(keyRaw, EVP_PKEY_free);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
EvpPkeyPtr GenerateRsaKey(uint32_t modulusLength) {
|
|
81
|
+
EvpPkeyCtxPtr keyContext(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr), EVP_PKEY_CTX_free);
|
|
82
|
+
if (!keyContext)
|
|
83
|
+
throw std::runtime_error("Failed to allocate RSA key context");
|
|
84
|
+
CheckOpenSsl(EVP_PKEY_keygen_init(keyContext.get()), "Failed to initialize RSA key generation");
|
|
85
|
+
CheckOpenSsl(EVP_PKEY_CTX_set_rsa_keygen_bits(keyContext.get(), static_cast<int>(modulusLength)),
|
|
86
|
+
"Failed to set RSA modulus length");
|
|
87
|
+
|
|
88
|
+
EVP_PKEY *keyRaw = nullptr;
|
|
89
|
+
CheckOpenSsl(EVP_PKEY_keygen(keyContext.get(), &keyRaw), "Failed to generate RSA key");
|
|
90
|
+
return EvpPkeyPtr(keyRaw, EVP_PKEY_free);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
long ExpirationSecondsFromMilliseconds(double expiresMs) {
|
|
94
|
+
if (expiresMs <= 0)
|
|
95
|
+
return 0;
|
|
96
|
+
double seconds = std::ceil(expiresMs / 1000.0);
|
|
97
|
+
constexpr double maxLongSeconds = 2147483647.0;
|
|
98
|
+
if (seconds > maxLongSeconds)
|
|
99
|
+
seconds = maxLongSeconds;
|
|
100
|
+
return static_cast<long>(seconds);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
std::string FingerprintForCertificate(X509 *certificate) {
|
|
104
|
+
unsigned char digest[EVP_MAX_MD_SIZE];
|
|
105
|
+
unsigned int digestLength = 0;
|
|
106
|
+
CheckOpenSsl(X509_digest(certificate, EVP_sha256(), digest, &digestLength),
|
|
107
|
+
"Failed to compute certificate fingerprint");
|
|
108
|
+
|
|
109
|
+
std::ostringstream output;
|
|
110
|
+
output << std::hex << std::nouppercase << std::setfill('0');
|
|
111
|
+
for (unsigned int i = 0; i < digestLength; ++i) {
|
|
112
|
+
if (i)
|
|
113
|
+
output << ':';
|
|
114
|
+
output << std::setw(2) << static_cast<unsigned int>(digest[i]);
|
|
115
|
+
}
|
|
116
|
+
return output.str();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
double CertificateExpirationMilliseconds(X509 *certificate) {
|
|
120
|
+
int days = 0;
|
|
121
|
+
int seconds = 0;
|
|
122
|
+
if (ASN1_TIME_diff(&days, &seconds, nullptr, X509_get0_notAfter(certificate)) != 1)
|
|
123
|
+
throw std::runtime_error("Failed to read certificate expiration");
|
|
124
|
+
|
|
125
|
+
const auto now = std::chrono::system_clock::now().time_since_epoch();
|
|
126
|
+
const auto remaining = std::chrono::hours(days * 24LL) + std::chrono::seconds(seconds);
|
|
127
|
+
return static_cast<double>(
|
|
128
|
+
std::chrono::duration_cast<std::chrono::milliseconds>(now + remaining).count());
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
X509Ptr ReadCertificate(const std::string &certificatePem) {
|
|
132
|
+
if (certificatePem.size() > static_cast<size_t>(std::numeric_limits<int>::max()))
|
|
133
|
+
throw std::invalid_argument("Certificate PEM is too large");
|
|
134
|
+
BioPtr bio(BIO_new_mem_buf(certificatePem.data(), static_cast<int>(certificatePem.size())),
|
|
135
|
+
BIO_free);
|
|
136
|
+
if (!bio)
|
|
137
|
+
throw std::runtime_error("Failed to allocate certificate PEM BIO");
|
|
138
|
+
|
|
139
|
+
ERR_clear_error();
|
|
140
|
+
X509Ptr certificate(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr), X509_free);
|
|
141
|
+
if (!certificate)
|
|
142
|
+
throw std::runtime_error("Invalid certificate PEM: " + OpenSslErrorString());
|
|
143
|
+
return certificate;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
EvpPkeyPtr ReadPrivateKey(const std::string &keyPem) {
|
|
147
|
+
if (keyPem.size() > static_cast<size_t>(std::numeric_limits<int>::max()))
|
|
148
|
+
throw std::invalid_argument("Private key PEM is too large");
|
|
149
|
+
BioPtr bio(BIO_new_mem_buf(keyPem.data(), static_cast<int>(keyPem.size())), BIO_free);
|
|
150
|
+
if (!bio)
|
|
151
|
+
throw std::runtime_error("Failed to allocate private key PEM BIO");
|
|
152
|
+
|
|
153
|
+
ERR_clear_error();
|
|
154
|
+
EvpPkeyPtr key(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr), EVP_PKEY_free);
|
|
155
|
+
if (!key)
|
|
156
|
+
throw std::runtime_error("Invalid private key PEM: " + OpenSslErrorString());
|
|
157
|
+
return key;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
} // namespace
|
|
161
|
+
|
|
162
|
+
CertificateMaterial GenerateCertificateMaterial(const std::string &algorithm, uint32_t modulusLength,
|
|
163
|
+
double expiresMs) {
|
|
164
|
+
EvpPkeyPtr key = algorithm == "RSASSA-PKCS1-v1_5" ? GenerateRsaKey(modulusLength)
|
|
165
|
+
: GenerateEcKey();
|
|
166
|
+
X509Ptr certificate(X509_new(), X509_free);
|
|
167
|
+
if (!certificate)
|
|
168
|
+
throw std::runtime_error("Failed to allocate X509 certificate");
|
|
169
|
+
|
|
170
|
+
CheckOpenSsl(X509_set_version(certificate.get(), 2), "Failed to set certificate version");
|
|
171
|
+
auto serial = std::chrono::system_clock::now().time_since_epoch().count();
|
|
172
|
+
ASN1_INTEGER_set(X509_get_serialNumber(certificate.get()), static_cast<long>(serial & 0x7fffffff));
|
|
173
|
+
X509_gmtime_adj(X509_get_notBefore(certificate.get()), -3600);
|
|
174
|
+
X509_gmtime_adj(X509_get_notAfter(certificate.get()),
|
|
175
|
+
ExpirationSecondsFromMilliseconds(expiresMs));
|
|
176
|
+
CheckOpenSsl(X509_set_pubkey(certificate.get(), key.get()), "Failed to set certificate public key");
|
|
177
|
+
|
|
178
|
+
X509_NAME *name = X509_get_subject_name(certificate.get());
|
|
179
|
+
CheckOpenSsl(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
|
|
180
|
+
reinterpret_cast<const unsigned char *>("webrtc-node"),
|
|
181
|
+
-1, -1, 0),
|
|
182
|
+
"Failed to set certificate subject");
|
|
183
|
+
CheckOpenSsl(X509_set_issuer_name(certificate.get(), name), "Failed to set certificate issuer");
|
|
184
|
+
CheckOpenSslPositive(X509_sign(certificate.get(), key.get(), EVP_sha256()),
|
|
185
|
+
"Failed to sign certificate");
|
|
186
|
+
|
|
187
|
+
BioPtr certificateBio(BIO_new(BIO_s_mem()), BIO_free);
|
|
188
|
+
BioPtr keyBio(BIO_new(BIO_s_mem()), BIO_free);
|
|
189
|
+
if (!certificateBio || !keyBio)
|
|
190
|
+
throw std::runtime_error("Failed to allocate PEM BIO");
|
|
191
|
+
CheckOpenSsl(PEM_write_bio_X509(certificateBio.get(), certificate.get()),
|
|
192
|
+
"Failed to encode certificate PEM");
|
|
193
|
+
CheckOpenSsl(PEM_write_bio_PrivateKey(keyBio.get(), key.get(), nullptr, nullptr, 0, nullptr,
|
|
194
|
+
nullptr),
|
|
195
|
+
"Failed to encode private key PEM");
|
|
196
|
+
|
|
197
|
+
return {BioToString(certificateBio.get()), BioToString(keyBio.get()),
|
|
198
|
+
FingerprintForCertificate(certificate.get()),
|
|
199
|
+
CertificateExpirationMilliseconds(certificate.get())};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
CertificateMaterial ImportCertificateMaterial(const std::string &certificatePem,
|
|
203
|
+
const std::string &keyPem) {
|
|
204
|
+
auto certificate = ReadCertificate(certificatePem);
|
|
205
|
+
auto key = ReadPrivateKey(keyPem);
|
|
206
|
+
|
|
207
|
+
ERR_clear_error();
|
|
208
|
+
if (X509_check_private_key(certificate.get(), key.get()) != 1)
|
|
209
|
+
throw std::runtime_error("Certificate and private key do not match");
|
|
210
|
+
|
|
211
|
+
return {certificatePem, keyPem, FingerprintForCertificate(certificate.get()),
|
|
212
|
+
CertificateExpirationMilliseconds(certificate.get())};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
} // namespace webrtc_node
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstdint>
|
|
4
|
+
#include <string>
|
|
5
|
+
|
|
6
|
+
namespace webrtc_node {
|
|
7
|
+
|
|
8
|
+
struct CertificateMaterial {
|
|
9
|
+
std::string certificatePem;
|
|
10
|
+
std::string keyPem;
|
|
11
|
+
std::string fingerprint;
|
|
12
|
+
double expires = 0;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
CertificateMaterial GenerateCertificateMaterial(const std::string &algorithm,
|
|
16
|
+
uint32_t modulusLength, double expiresMs);
|
|
17
|
+
CertificateMaterial ImportCertificateMaterial(const std::string &certificatePem,
|
|
18
|
+
const std::string &keyPem);
|
|
19
|
+
|
|
20
|
+
} // namespace webrtc_node
|