Skip to content

Commit 5500fba

Browse files
author
Jake Champion
committed
feat: Add support for SubtleCrypto
This adds an implementation of a subset of SubtleCrypto, specifically for JSON Web Token signing and validating. - SubtleCrypto.prototype.generateKey - SubtleCrypto.prototype.importKey - SubtleCrypto.prototype.sign - SubtleCrypto.prototype.verify with the following algorithms: - RSASSA_PKCS1_v1_5 - RSA_OAEP and the following digest algorithms: - SHA_1 - SHA_224 - SHA_256 - SHA_384 - SHA_512 Work in the future will be done to add the remaining algorithms and SubtleCrypto method implementations
1 parent 334c28b commit 5500fba

File tree

72 files changed

+70671
-708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+70671
-708
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ jobs:
173173
- async-select
174174
- btoa
175175
- byte-repeater
176+
- crypto
176177
- config-store
177178
- console
178179
- dynamic-backend
@@ -311,6 +312,7 @@ jobs:
311312
- 'async-select'
312313
- 'byte-repeater'
313314
- 'cache-override'
315+
- 'crypto'
314316
- 'edge-dictionary'
315317
- 'error'
316318
- 'geoip'
@@ -323,7 +325,7 @@ jobs:
323325
- 'request-upstream'
324326
- 'response'
325327
- 'response-headers'
326-
- secret-store
328+
- 'secret-store'
327329
- 'status'
328330
- 'timers'
329331
steps:

.vscode/c_cpp_properties.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"includePath": [
66
"${workspaceFolder}/c-dependencies/spidermonkey/debug/include",
77
"/opt/wasi-sdk/share/wasi-sysroot/include/",
8-
"${workspaceFolder}/c-dependencies/js-compute-runtime"
8+
"${workspaceFolder}/c-dependencies/js-compute-runtime",
9+
"${workspaceFolder}/c-dependencies/js-compute-runtime/build/openssl-3.0.7/include"
910
],
1011
"defines": [
1112
"__wasi__"

.vscode/settings.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"files.associations": {
3+
"*.mdx": "mdx",
34
"cstddef": "c",
45
"limits": "c",
56
"type_traits": "c",
@@ -87,7 +88,35 @@
8788
"__functional_03": "cpp",
8889
"__functional_base_03": "cpp",
8990
"memory_resource": "cpp",
90-
"numeric": "cpp"
91+
"numeric": "cpp",
92+
"future": "cpp",
93+
"__bits": "cpp",
94+
"__verbose_abort": "cpp",
95+
"any": "cpp",
96+
"cfenv": "cpp",
97+
"cinttypes": "cpp",
98+
"codecvt": "cpp",
99+
"complex": "cpp",
100+
"condition_variable": "cpp",
101+
"csignal": "cpp",
102+
"cuchar": "cpp",
103+
"format": "cpp",
104+
"forward_list": "cpp",
105+
"queue": "cpp",
106+
"scoped_allocator": "cpp",
107+
"shared_mutex": "cpp",
108+
"typeindex": "cpp",
109+
"valarray": "cpp",
110+
"variant": "cpp",
111+
"secmodi.h": "c",
112+
"secpkcs5.h": "c",
113+
"hash_map": "cpp",
114+
"hash_set": "cpp",
115+
"*.def": "cpp",
116+
"span": "cpp",
117+
"self_test.h": "c",
118+
"unistd.h": "c",
119+
"valuearray.h": "c"
91120
},
92121
"git.ignoreLimitWarning": true
93122
}

c-dependencies/js-compute-runtime/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ openssl: $(BUILD)/openssl/token
168168
$(BUILD)/openssl-$(OPENSSL_VERSION)/token: $(BUILD)/openssl-$(OPENSSL_VERSION).tar.gz $(FSM_SRC)/getuid.patch
169169
$Q tar -C $(BUILD) -xf $<
170170
$Q patch -d $(BUILD)/openssl-$(OPENSSL_VERSION) -p1 < $(FSM_SRC)/getuid.patch
171+
$Q patch -d $(BUILD)/openssl-$(OPENSSL_VERSION) -p1 < $(FSM_SRC)/rand.patch
171172
$Q touch $@
172173

173174
OPENSSL_OPTS := -static -no-sock -no-asm -no-ui-console -no-egd
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "crypto-key-rsa-components.h"
2+
3+
CryptoKeyRSAComponents::CryptoKeyRSAComponents(std::string modulus, std::string exponent)
4+
: _type(Type::Public), _modulus(modulus), _exponent(exponent) {}
5+
6+
CryptoKeyRSAComponents::CryptoKeyRSAComponents(std::string modulus, std::string exponent,
7+
std::string privateExponent)
8+
: _type(Type::Private), _modulus(modulus), _exponent(exponent),
9+
_privateExponent(privateExponent), _hasAdditionalPrivateKeyParameters(false) {}
10+
11+
CryptoKeyRSAComponents::CryptoKeyRSAComponents(std::string modulus, std::string exponent,
12+
std::string privateExponent,
13+
std::optional<PrimeInfo> firstPrimeInfo,
14+
std::optional<PrimeInfo> secondPrimeInfo,
15+
std::vector<PrimeInfo> otherPrimeInfos)
16+
: _type(Type::Private), _modulus(modulus), _exponent(exponent),
17+
_privateExponent(privateExponent), _hasAdditionalPrivateKeyParameters(true),
18+
_firstPrimeInfo(firstPrimeInfo), _secondPrimeInfo(secondPrimeInfo),
19+
_otherPrimeInfos(otherPrimeInfos) {}
20+
21+
CryptoKeyRSAComponents::~CryptoKeyRSAComponents() = default;
22+
23+
CryptoKeyRSAComponents CryptoKeyRSAComponents::createPrivateWithAdditionalData(
24+
std::string modulus, std::string exponent, std::string privateExponent,
25+
std::optional<PrimeInfo> firstPrimeInfo, std::optional<PrimeInfo> secondPrimeInfo,
26+
std::vector<PrimeInfo> otherPrimeInfos) {
27+
return CryptoKeyRSAComponents(modulus, exponent, privateExponent, firstPrimeInfo, secondPrimeInfo,
28+
otherPrimeInfos);
29+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <list>
4+
#include <span>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <strings.h>
9+
#include <vector>
10+
11+
class PrimeInfo {
12+
public:
13+
std::string primeFactor;
14+
std::string factorCRTExponent;
15+
std::string factorCRTCoefficient;
16+
PrimeInfo(
17+
std::string primeFactor,
18+
std::string factorCRTExponent,
19+
std::string factorCRTCoefficient):
20+
primeFactor{primeFactor},
21+
factorCRTExponent{factorCRTExponent},
22+
factorCRTCoefficient{factorCRTCoefficient}
23+
{};
24+
PrimeInfo(
25+
std::string primeFactor,
26+
std::string factorCRTExponent
27+
):
28+
primeFactor{primeFactor},
29+
factorCRTExponent{factorCRTExponent} {};
30+
PrimeInfo(
31+
std::string primeFactor
32+
):
33+
primeFactor{primeFactor} {};
34+
};
35+
36+
class CryptoKeyRSAComponents {
37+
public:
38+
enum class Type { Public, Private };
39+
40+
static CryptoKeyRSAComponents createPublic(std::string modulus, std::string exponent) {
41+
return CryptoKeyRSAComponents(modulus, exponent);
42+
}
43+
44+
static CryptoKeyRSAComponents createPrivate(std::string modulus, std::string exponent,
45+
std::string privateExponent) {
46+
return CryptoKeyRSAComponents(modulus, exponent, privateExponent);
47+
}
48+
49+
50+
static CryptoKeyRSAComponents createPrivateWithAdditionalData(
51+
std::string modulus, std::string exponent, std::string privateExponent,
52+
std::optional<PrimeInfo> firstPrimeInfo, std::optional<PrimeInfo> secondPrimeInfo, std::vector<PrimeInfo> otherPrimeInfos);
53+
54+
virtual ~CryptoKeyRSAComponents();
55+
56+
Type type() { return _type; }
57+
58+
// Private and public keys.
59+
std::string modulus() { return _modulus; }
60+
std::string exponent() { return _exponent; }
61+
62+
// Only private keys.
63+
std::string privateExponent() { return _privateExponent; }
64+
bool hasAdditionalPrivateKeyParameters() { return _hasAdditionalPrivateKeyParameters; }
65+
std::optional<PrimeInfo> firstPrimeInfo() { return _firstPrimeInfo; }
66+
std::optional<PrimeInfo> secondPrimeInfo() { return _secondPrimeInfo; }
67+
std::vector<PrimeInfo> otherPrimeInfos() { return _otherPrimeInfos; }
68+
CryptoKeyRSAComponents(std::string modulus, std::string exponent);
69+
70+
CryptoKeyRSAComponents(std::string modulus, std::string exponent, std::string privateExponent);
71+
72+
CryptoKeyRSAComponents(std::string modulus, std::string exponent, std::string privateExponent,
73+
std::optional<PrimeInfo> firstPrimeInfo, std::optional<PrimeInfo> secondPrimeInfo,
74+
std::vector<PrimeInfo> otherPrimeInfos);
75+
76+
Type _type;
77+
78+
// Private and public keys.
79+
std::string _modulus;
80+
std::string _exponent;
81+
82+
// Only private keys.
83+
std::string _privateExponent;
84+
bool _hasAdditionalPrivateKeyParameters;
85+
std::optional<PrimeInfo> _firstPrimeInfo;
86+
std::optional<PrimeInfo> _secondPrimeInfo;
87+
std::vector<PrimeInfo>
88+
_otherPrimeInfos; // When three or more primes have been used, the number of array elements
89+
// is be the number of primes used minus two.
90+
};

0 commit comments

Comments
 (0)