jose 4.12.0 → 4.12.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/README.md +1 -1
- package/dist/browser/key/import.js +5 -85
- package/dist/browser/runtime/asn1.js +79 -1
- package/dist/browser/runtime/jwk_to_key.js +3 -0
- package/dist/node/cjs/key/import.js +2 -82
- package/dist/node/cjs/runtime/asn1.js +7 -1
- package/dist/node/esm/key/import.js +5 -85
- package/dist/node/esm/runtime/asn1.js +5 -0
- package/dist/types/key/generate_key_pair.d.ts +2 -2
- package/dist/types/key/generate_secret.d.ts +3 -3
- package/dist/types/key/import.d.ts +10 -6
- package/dist/types/types.d.ts +1 -1
- package/package.json +1 -1
- package/dist/node/cjs/lib/format_pem.js +0 -6
- package/dist/node/esm/lib/format_pem.js +0 -4
package/README.md
CHANGED
|
@@ -1,102 +1,25 @@
|
|
|
1
|
-
import { decode as decodeBase64URL
|
|
2
|
-
import { fromSPKI
|
|
3
|
-
import { fromPKCS8 as importPrivate } from '../runtime/asn1.js';
|
|
1
|
+
import { decode as decodeBase64URL } from '../runtime/base64url.js';
|
|
2
|
+
import { fromSPKI, fromPKCS8, fromX509 } from '../runtime/asn1.js';
|
|
4
3
|
import asKeyObject from '../runtime/jwk_to_key.js';
|
|
5
4
|
import { JOSENotSupported } from '../util/errors.js';
|
|
6
|
-
import formatPEM from '../lib/format_pem.js';
|
|
7
5
|
import isObject from '../lib/is_object.js';
|
|
8
|
-
function getElement(seq) {
|
|
9
|
-
let result = [];
|
|
10
|
-
let next = 0;
|
|
11
|
-
while (next < seq.length) {
|
|
12
|
-
let nextPart = parseElement(seq.subarray(next));
|
|
13
|
-
result.push(nextPart);
|
|
14
|
-
next += nextPart.byteLength;
|
|
15
|
-
}
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
function parseElement(bytes) {
|
|
19
|
-
let position = 0;
|
|
20
|
-
let tag = bytes[0] & 0x1f;
|
|
21
|
-
position++;
|
|
22
|
-
if (tag === 0x1f) {
|
|
23
|
-
tag = 0;
|
|
24
|
-
while (bytes[position] >= 0x80) {
|
|
25
|
-
tag = tag * 128 + bytes[position] - 0x80;
|
|
26
|
-
position++;
|
|
27
|
-
}
|
|
28
|
-
tag = tag * 128 + bytes[position] - 0x80;
|
|
29
|
-
position++;
|
|
30
|
-
}
|
|
31
|
-
let length = 0;
|
|
32
|
-
if (bytes[position] < 0x80) {
|
|
33
|
-
length = bytes[position];
|
|
34
|
-
position++;
|
|
35
|
-
}
|
|
36
|
-
else if (length === 0x80) {
|
|
37
|
-
length = 0;
|
|
38
|
-
while (bytes[position + length] !== 0 || bytes[position + length + 1] !== 0) {
|
|
39
|
-
if (length > bytes.byteLength) {
|
|
40
|
-
throw new TypeError('invalid indefinite form length');
|
|
41
|
-
}
|
|
42
|
-
length++;
|
|
43
|
-
}
|
|
44
|
-
const byteLength = position + length + 2;
|
|
45
|
-
return {
|
|
46
|
-
byteLength,
|
|
47
|
-
contents: bytes.subarray(position, position + length),
|
|
48
|
-
raw: bytes.subarray(0, byteLength),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
let numberOfDigits = bytes[position] & 0x7f;
|
|
53
|
-
position++;
|
|
54
|
-
length = 0;
|
|
55
|
-
for (let i = 0; i < numberOfDigits; i++) {
|
|
56
|
-
length = length * 256 + bytes[position];
|
|
57
|
-
position++;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const byteLength = position + length;
|
|
61
|
-
return {
|
|
62
|
-
byteLength,
|
|
63
|
-
contents: bytes.subarray(position, byteLength),
|
|
64
|
-
raw: bytes.subarray(0, byteLength),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
function spkiFromX509(buf) {
|
|
68
|
-
const tbsCertificate = getElement(getElement(parseElement(buf).contents)[0].contents);
|
|
69
|
-
return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw);
|
|
70
|
-
}
|
|
71
|
-
function getSPKI(x509) {
|
|
72
|
-
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '');
|
|
73
|
-
const raw = decodeBase64(pem);
|
|
74
|
-
return formatPEM(spkiFromX509(raw), 'PUBLIC KEY');
|
|
75
|
-
}
|
|
76
6
|
export async function importSPKI(spki, alg, options) {
|
|
77
7
|
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
|
|
78
8
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
79
9
|
}
|
|
80
|
-
return
|
|
10
|
+
return fromSPKI(spki, alg, options);
|
|
81
11
|
}
|
|
82
12
|
export async function importX509(x509, alg, options) {
|
|
83
13
|
if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
|
|
84
14
|
throw new TypeError('"x509" must be X.509 formatted string');
|
|
85
15
|
}
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
spki = getSPKI(x509);
|
|
89
|
-
}
|
|
90
|
-
catch (cause) {
|
|
91
|
-
throw new TypeError('failed to parse the X.509 certificate', { cause });
|
|
92
|
-
}
|
|
93
|
-
return importPublic(spki, alg, options);
|
|
16
|
+
return fromX509(x509, alg, options);
|
|
94
17
|
}
|
|
95
18
|
export async function importPKCS8(pkcs8, alg, options) {
|
|
96
19
|
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
|
|
97
20
|
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
|
98
21
|
}
|
|
99
|
-
return
|
|
22
|
+
return fromPKCS8(pkcs8, alg, options);
|
|
100
23
|
}
|
|
101
24
|
export async function importJWK(jwk, alg, octAsKeyObject) {
|
|
102
25
|
var _a;
|
|
@@ -104,9 +27,6 @@ export async function importJWK(jwk, alg, octAsKeyObject) {
|
|
|
104
27
|
throw new TypeError('JWK must be an object');
|
|
105
28
|
}
|
|
106
29
|
alg || (alg = jwk.alg);
|
|
107
|
-
if (typeof alg !== 'string' || !alg) {
|
|
108
|
-
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
109
|
-
}
|
|
110
30
|
switch (jwk.kty) {
|
|
111
31
|
case 'oct':
|
|
112
32
|
if (typeof jwk.k !== 'string' || !jwk.k) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isCloudflareWorkers } from './env.js';
|
|
2
2
|
import crypto, { isCryptoKey } from './webcrypto.js';
|
|
3
3
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
4
|
-
import { encodeBase64 } from './base64url.js';
|
|
4
|
+
import { encodeBase64, decodeBase64 } from './base64url.js';
|
|
5
5
|
import formatPEM from '../lib/format_pem.js';
|
|
6
6
|
import { JOSENotSupported } from '../util/errors.js';
|
|
7
7
|
import { types } from './is_key_like.js';
|
|
@@ -134,3 +134,81 @@ export const fromPKCS8 = (pem, alg, options) => {
|
|
|
134
134
|
export const fromSPKI = (pem, alg, options) => {
|
|
135
135
|
return genericImport(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, 'spki', pem, alg, options);
|
|
136
136
|
};
|
|
137
|
+
function getElement(seq) {
|
|
138
|
+
let result = [];
|
|
139
|
+
let next = 0;
|
|
140
|
+
while (next < seq.length) {
|
|
141
|
+
let nextPart = parseElement(seq.subarray(next));
|
|
142
|
+
result.push(nextPart);
|
|
143
|
+
next += nextPart.byteLength;
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
function parseElement(bytes) {
|
|
148
|
+
let position = 0;
|
|
149
|
+
let tag = bytes[0] & 0x1f;
|
|
150
|
+
position++;
|
|
151
|
+
if (tag === 0x1f) {
|
|
152
|
+
tag = 0;
|
|
153
|
+
while (bytes[position] >= 0x80) {
|
|
154
|
+
tag = tag * 128 + bytes[position] - 0x80;
|
|
155
|
+
position++;
|
|
156
|
+
}
|
|
157
|
+
tag = tag * 128 + bytes[position] - 0x80;
|
|
158
|
+
position++;
|
|
159
|
+
}
|
|
160
|
+
let length = 0;
|
|
161
|
+
if (bytes[position] < 0x80) {
|
|
162
|
+
length = bytes[position];
|
|
163
|
+
position++;
|
|
164
|
+
}
|
|
165
|
+
else if (length === 0x80) {
|
|
166
|
+
length = 0;
|
|
167
|
+
while (bytes[position + length] !== 0 || bytes[position + length + 1] !== 0) {
|
|
168
|
+
if (length > bytes.byteLength) {
|
|
169
|
+
throw new TypeError('invalid indefinite form length');
|
|
170
|
+
}
|
|
171
|
+
length++;
|
|
172
|
+
}
|
|
173
|
+
const byteLength = position + length + 2;
|
|
174
|
+
return {
|
|
175
|
+
byteLength,
|
|
176
|
+
contents: bytes.subarray(position, position + length),
|
|
177
|
+
raw: bytes.subarray(0, byteLength),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
let numberOfDigits = bytes[position] & 0x7f;
|
|
182
|
+
position++;
|
|
183
|
+
length = 0;
|
|
184
|
+
for (let i = 0; i < numberOfDigits; i++) {
|
|
185
|
+
length = length * 256 + bytes[position];
|
|
186
|
+
position++;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const byteLength = position + length;
|
|
190
|
+
return {
|
|
191
|
+
byteLength,
|
|
192
|
+
contents: bytes.subarray(position, byteLength),
|
|
193
|
+
raw: bytes.subarray(0, byteLength),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function spkiFromX509(buf) {
|
|
197
|
+
const tbsCertificate = getElement(getElement(parseElement(buf).contents)[0].contents);
|
|
198
|
+
return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw);
|
|
199
|
+
}
|
|
200
|
+
function getSPKI(x509) {
|
|
201
|
+
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '');
|
|
202
|
+
const raw = decodeBase64(pem);
|
|
203
|
+
return formatPEM(spkiFromX509(raw), 'PUBLIC KEY');
|
|
204
|
+
}
|
|
205
|
+
export const fromX509 = (pem, alg, options) => {
|
|
206
|
+
let spki;
|
|
207
|
+
try {
|
|
208
|
+
spki = getSPKI(pem);
|
|
209
|
+
}
|
|
210
|
+
catch (cause) {
|
|
211
|
+
throw new TypeError('failed to parse the X.509 certificate', { cause });
|
|
212
|
+
}
|
|
213
|
+
return fromSPKI(spki, alg, options);
|
|
214
|
+
};
|
|
@@ -124,6 +124,9 @@ function subtleMapping(jwk) {
|
|
|
124
124
|
}
|
|
125
125
|
const parse = async (jwk) => {
|
|
126
126
|
var _a, _b;
|
|
127
|
+
if (!jwk.alg) {
|
|
128
|
+
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
129
|
+
}
|
|
127
130
|
const { algorithm, keyUsages } = subtleMapping(jwk);
|
|
128
131
|
const rest = [
|
|
129
132
|
algorithm,
|
|
@@ -3,79 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.importJWK = exports.importPKCS8 = exports.importX509 = exports.importSPKI = void 0;
|
|
4
4
|
const base64url_js_1 = require("../runtime/base64url.js");
|
|
5
5
|
const asn1_js_1 = require("../runtime/asn1.js");
|
|
6
|
-
const asn1_js_2 = require("../runtime/asn1.js");
|
|
7
6
|
const jwk_to_key_js_1 = require("../runtime/jwk_to_key.js");
|
|
8
7
|
const errors_js_1 = require("../util/errors.js");
|
|
9
|
-
const format_pem_js_1 = require("../lib/format_pem.js");
|
|
10
8
|
const is_object_js_1 = require("../lib/is_object.js");
|
|
11
|
-
function getElement(seq) {
|
|
12
|
-
let result = [];
|
|
13
|
-
let next = 0;
|
|
14
|
-
while (next < seq.length) {
|
|
15
|
-
let nextPart = parseElement(seq.subarray(next));
|
|
16
|
-
result.push(nextPart);
|
|
17
|
-
next += nextPart.byteLength;
|
|
18
|
-
}
|
|
19
|
-
return result;
|
|
20
|
-
}
|
|
21
|
-
function parseElement(bytes) {
|
|
22
|
-
let position = 0;
|
|
23
|
-
let tag = bytes[0] & 0x1f;
|
|
24
|
-
position++;
|
|
25
|
-
if (tag === 0x1f) {
|
|
26
|
-
tag = 0;
|
|
27
|
-
while (bytes[position] >= 0x80) {
|
|
28
|
-
tag = tag * 128 + bytes[position] - 0x80;
|
|
29
|
-
position++;
|
|
30
|
-
}
|
|
31
|
-
tag = tag * 128 + bytes[position] - 0x80;
|
|
32
|
-
position++;
|
|
33
|
-
}
|
|
34
|
-
let length = 0;
|
|
35
|
-
if (bytes[position] < 0x80) {
|
|
36
|
-
length = bytes[position];
|
|
37
|
-
position++;
|
|
38
|
-
}
|
|
39
|
-
else if (length === 0x80) {
|
|
40
|
-
length = 0;
|
|
41
|
-
while (bytes[position + length] !== 0 || bytes[position + length + 1] !== 0) {
|
|
42
|
-
if (length > bytes.byteLength) {
|
|
43
|
-
throw new TypeError('invalid indefinite form length');
|
|
44
|
-
}
|
|
45
|
-
length++;
|
|
46
|
-
}
|
|
47
|
-
const byteLength = position + length + 2;
|
|
48
|
-
return {
|
|
49
|
-
byteLength,
|
|
50
|
-
contents: bytes.subarray(position, position + length),
|
|
51
|
-
raw: bytes.subarray(0, byteLength),
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
let numberOfDigits = bytes[position] & 0x7f;
|
|
56
|
-
position++;
|
|
57
|
-
length = 0;
|
|
58
|
-
for (let i = 0; i < numberOfDigits; i++) {
|
|
59
|
-
length = length * 256 + bytes[position];
|
|
60
|
-
position++;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
const byteLength = position + length;
|
|
64
|
-
return {
|
|
65
|
-
byteLength,
|
|
66
|
-
contents: bytes.subarray(position, byteLength),
|
|
67
|
-
raw: bytes.subarray(0, byteLength),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
function spkiFromX509(buf) {
|
|
71
|
-
const tbsCertificate = getElement(getElement(parseElement(buf).contents)[0].contents);
|
|
72
|
-
return (0, base64url_js_1.encodeBase64)(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw);
|
|
73
|
-
}
|
|
74
|
-
function getSPKI(x509) {
|
|
75
|
-
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '');
|
|
76
|
-
const raw = (0, base64url_js_1.decodeBase64)(pem);
|
|
77
|
-
return (0, format_pem_js_1.default)(spkiFromX509(raw), 'PUBLIC KEY');
|
|
78
|
-
}
|
|
79
9
|
async function importSPKI(spki, alg, options) {
|
|
80
10
|
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
|
|
81
11
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
@@ -87,21 +17,14 @@ async function importX509(x509, alg, options) {
|
|
|
87
17
|
if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
|
|
88
18
|
throw new TypeError('"x509" must be X.509 formatted string');
|
|
89
19
|
}
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
spki = getSPKI(x509);
|
|
93
|
-
}
|
|
94
|
-
catch (cause) {
|
|
95
|
-
throw new TypeError('failed to parse the X.509 certificate', { cause });
|
|
96
|
-
}
|
|
97
|
-
return (0, asn1_js_1.fromSPKI)(spki, alg, options);
|
|
20
|
+
return (0, asn1_js_1.fromX509)(x509, alg, options);
|
|
98
21
|
}
|
|
99
22
|
exports.importX509 = importX509;
|
|
100
23
|
async function importPKCS8(pkcs8, alg, options) {
|
|
101
24
|
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
|
|
102
25
|
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
|
103
26
|
}
|
|
104
|
-
return (0,
|
|
27
|
+
return (0, asn1_js_1.fromPKCS8)(pkcs8, alg, options);
|
|
105
28
|
}
|
|
106
29
|
exports.importPKCS8 = importPKCS8;
|
|
107
30
|
async function importJWK(jwk, alg, octAsKeyObject) {
|
|
@@ -110,9 +33,6 @@ async function importJWK(jwk, alg, octAsKeyObject) {
|
|
|
110
33
|
throw new TypeError('JWK must be an object');
|
|
111
34
|
}
|
|
112
35
|
alg || (alg = jwk.alg);
|
|
113
|
-
if (typeof alg !== 'string' || !alg) {
|
|
114
|
-
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
115
|
-
}
|
|
116
36
|
switch (jwk.kty) {
|
|
117
37
|
case 'oct':
|
|
118
38
|
if (typeof jwk.k !== 'string' || !jwk.k) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fromSPKI = exports.fromPKCS8 = exports.toPKCS8 = exports.toSPKI = void 0;
|
|
3
|
+
exports.fromX509 = exports.fromSPKI = exports.fromPKCS8 = exports.toPKCS8 = exports.toSPKI = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const buffer_1 = require("buffer");
|
|
6
6
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
@@ -46,3 +46,9 @@ const fromSPKI = (pem) => (0, crypto_1.createPublicKey)({
|
|
|
46
46
|
format: 'der',
|
|
47
47
|
});
|
|
48
48
|
exports.fromSPKI = fromSPKI;
|
|
49
|
+
const fromX509 = (pem) => (0, crypto_1.createPublicKey)({
|
|
50
|
+
key: pem,
|
|
51
|
+
type: 'spki',
|
|
52
|
+
format: 'pem',
|
|
53
|
+
});
|
|
54
|
+
exports.fromX509 = fromX509;
|
|
@@ -1,102 +1,25 @@
|
|
|
1
|
-
import { decode as decodeBase64URL
|
|
2
|
-
import { fromSPKI
|
|
3
|
-
import { fromPKCS8 as importPrivate } from '../runtime/asn1.js';
|
|
1
|
+
import { decode as decodeBase64URL } from '../runtime/base64url.js';
|
|
2
|
+
import { fromSPKI, fromPKCS8, fromX509 } from '../runtime/asn1.js';
|
|
4
3
|
import asKeyObject from '../runtime/jwk_to_key.js';
|
|
5
4
|
import { JOSENotSupported } from '../util/errors.js';
|
|
6
|
-
import formatPEM from '../lib/format_pem.js';
|
|
7
5
|
import isObject from '../lib/is_object.js';
|
|
8
|
-
function getElement(seq) {
|
|
9
|
-
let result = [];
|
|
10
|
-
let next = 0;
|
|
11
|
-
while (next < seq.length) {
|
|
12
|
-
let nextPart = parseElement(seq.subarray(next));
|
|
13
|
-
result.push(nextPart);
|
|
14
|
-
next += nextPart.byteLength;
|
|
15
|
-
}
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
function parseElement(bytes) {
|
|
19
|
-
let position = 0;
|
|
20
|
-
let tag = bytes[0] & 0x1f;
|
|
21
|
-
position++;
|
|
22
|
-
if (tag === 0x1f) {
|
|
23
|
-
tag = 0;
|
|
24
|
-
while (bytes[position] >= 0x80) {
|
|
25
|
-
tag = tag * 128 + bytes[position] - 0x80;
|
|
26
|
-
position++;
|
|
27
|
-
}
|
|
28
|
-
tag = tag * 128 + bytes[position] - 0x80;
|
|
29
|
-
position++;
|
|
30
|
-
}
|
|
31
|
-
let length = 0;
|
|
32
|
-
if (bytes[position] < 0x80) {
|
|
33
|
-
length = bytes[position];
|
|
34
|
-
position++;
|
|
35
|
-
}
|
|
36
|
-
else if (length === 0x80) {
|
|
37
|
-
length = 0;
|
|
38
|
-
while (bytes[position + length] !== 0 || bytes[position + length + 1] !== 0) {
|
|
39
|
-
if (length > bytes.byteLength) {
|
|
40
|
-
throw new TypeError('invalid indefinite form length');
|
|
41
|
-
}
|
|
42
|
-
length++;
|
|
43
|
-
}
|
|
44
|
-
const byteLength = position + length + 2;
|
|
45
|
-
return {
|
|
46
|
-
byteLength,
|
|
47
|
-
contents: bytes.subarray(position, position + length),
|
|
48
|
-
raw: bytes.subarray(0, byteLength),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
let numberOfDigits = bytes[position] & 0x7f;
|
|
53
|
-
position++;
|
|
54
|
-
length = 0;
|
|
55
|
-
for (let i = 0; i < numberOfDigits; i++) {
|
|
56
|
-
length = length * 256 + bytes[position];
|
|
57
|
-
position++;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const byteLength = position + length;
|
|
61
|
-
return {
|
|
62
|
-
byteLength,
|
|
63
|
-
contents: bytes.subarray(position, byteLength),
|
|
64
|
-
raw: bytes.subarray(0, byteLength),
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
function spkiFromX509(buf) {
|
|
68
|
-
const tbsCertificate = getElement(getElement(parseElement(buf).contents)[0].contents);
|
|
69
|
-
return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw);
|
|
70
|
-
}
|
|
71
|
-
function getSPKI(x509) {
|
|
72
|
-
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '');
|
|
73
|
-
const raw = decodeBase64(pem);
|
|
74
|
-
return formatPEM(spkiFromX509(raw), 'PUBLIC KEY');
|
|
75
|
-
}
|
|
76
6
|
export async function importSPKI(spki, alg, options) {
|
|
77
7
|
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
|
|
78
8
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
79
9
|
}
|
|
80
|
-
return
|
|
10
|
+
return fromSPKI(spki, alg, options);
|
|
81
11
|
}
|
|
82
12
|
export async function importX509(x509, alg, options) {
|
|
83
13
|
if (typeof x509 !== 'string' || x509.indexOf('-----BEGIN CERTIFICATE-----') !== 0) {
|
|
84
14
|
throw new TypeError('"x509" must be X.509 formatted string');
|
|
85
15
|
}
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
spki = getSPKI(x509);
|
|
89
|
-
}
|
|
90
|
-
catch (cause) {
|
|
91
|
-
throw new TypeError('failed to parse the X.509 certificate', { cause });
|
|
92
|
-
}
|
|
93
|
-
return importPublic(spki, alg, options);
|
|
16
|
+
return fromX509(x509, alg, options);
|
|
94
17
|
}
|
|
95
18
|
export async function importPKCS8(pkcs8, alg, options) {
|
|
96
19
|
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
|
|
97
20
|
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
|
98
21
|
}
|
|
99
|
-
return
|
|
22
|
+
return fromPKCS8(pkcs8, alg, options);
|
|
100
23
|
}
|
|
101
24
|
export async function importJWK(jwk, alg, octAsKeyObject) {
|
|
102
25
|
var _a;
|
|
@@ -104,9 +27,6 @@ export async function importJWK(jwk, alg, octAsKeyObject) {
|
|
|
104
27
|
throw new TypeError('JWK must be an object');
|
|
105
28
|
}
|
|
106
29
|
alg || (alg = jwk.alg);
|
|
107
|
-
if (typeof alg !== 'string' || !alg) {
|
|
108
|
-
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
109
|
-
}
|
|
110
30
|
switch (jwk.kty) {
|
|
111
31
|
case 'oct':
|
|
112
32
|
if (typeof jwk.k !== 'string' || !jwk.k) {
|
|
@@ -17,7 +17,7 @@ export interface GenerateKeyPairOptions {
|
|
|
17
17
|
*/
|
|
18
18
|
modulusLength?: number;
|
|
19
19
|
/**
|
|
20
|
-
* (Web
|
|
20
|
+
* (Only effective in Web Crypto API runtimes) The value to use as
|
|
21
21
|
* [SubtleCrypto.generateKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey)
|
|
22
22
|
* `extractable` argument. Default is false.
|
|
23
23
|
*/
|
|
@@ -27,7 +27,7 @@ export interface GenerateKeyPairOptions {
|
|
|
27
27
|
* Generates a private and a public key for a given JWA algorithm identifier. This can only generate
|
|
28
28
|
* asymmetric key pairs. For symmetric secrets use the `generateSecret` function.
|
|
29
29
|
*
|
|
30
|
-
* Note: Under Web
|
|
30
|
+
* Note: Under Web Crypto API runtime the `privateKey` is generated with `extractable` set to
|
|
31
31
|
* `false` by default.
|
|
32
32
|
*
|
|
33
33
|
* @example Usage
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { KeyLike } from '../types';
|
|
2
2
|
export interface GenerateSecretOptions {
|
|
3
3
|
/**
|
|
4
|
-
* (Web
|
|
4
|
+
* (Only effective in Web Crypto API runtimes) The value to use as
|
|
5
5
|
* [SubtleCrypto.generateKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey)
|
|
6
6
|
* `extractable` argument. Default is false.
|
|
7
7
|
*/
|
|
@@ -10,8 +10,8 @@ export interface GenerateSecretOptions {
|
|
|
10
10
|
/**
|
|
11
11
|
* Generates a symmetric secret key for a given JWA algorithm identifier.
|
|
12
12
|
*
|
|
13
|
-
* Note: Under Web
|
|
14
|
-
*
|
|
13
|
+
* Note: Under Web Crypto API runtime the secret key is generated with `extractable` set to `false`
|
|
14
|
+
* by default.
|
|
15
15
|
*
|
|
16
16
|
* @example Usage
|
|
17
17
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { JWK, KeyLike } from '../types';
|
|
2
2
|
export interface PEMImportOptions {
|
|
3
3
|
/**
|
|
4
|
-
* (Web
|
|
4
|
+
* (Only effective in Web Crypto API runtimes) The value to use as
|
|
5
5
|
* [SubtleCrypto.importKey()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey)
|
|
6
6
|
* `extractable` argument. Default is false.
|
|
7
7
|
*/
|
|
@@ -24,7 +24,8 @@ export interface PEMImportOptions {
|
|
|
24
24
|
* ```
|
|
25
25
|
*
|
|
26
26
|
* @param pem PEM-encoded SPKI string
|
|
27
|
-
* @param alg JSON Web Algorithm identifier to be used
|
|
27
|
+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
|
|
28
|
+
* with the imported key, its presence is only enforced in Web Crypto API runtimes.
|
|
28
29
|
*/
|
|
29
30
|
export declare function importSPKI(spki: string, alg: string, options?: PEMImportOptions): Promise<KeyLike>;
|
|
30
31
|
/**
|
|
@@ -51,7 +52,8 @@ export declare function importSPKI(spki: string, alg: string, options?: PEMImpor
|
|
|
51
52
|
* ```
|
|
52
53
|
*
|
|
53
54
|
* @param pem X.509 certificate string
|
|
54
|
-
* @param alg JSON Web Algorithm identifier to be used
|
|
55
|
+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
|
|
56
|
+
* with the imported key, its presence is only enforced in Web Crypto API runtimes.
|
|
55
57
|
*/
|
|
56
58
|
export declare function importX509(x509: string, alg: string, options?: PEMImportOptions): Promise<KeyLike>;
|
|
57
59
|
/**
|
|
@@ -72,7 +74,8 @@ export declare function importX509(x509: string, alg: string, options?: PEMImpor
|
|
|
72
74
|
* ```
|
|
73
75
|
*
|
|
74
76
|
* @param pem PEM-encoded PKCS#8 string
|
|
75
|
-
* @param alg JSON Web Algorithm identifier to be used
|
|
77
|
+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
|
|
78
|
+
* with the imported key, its presence is only enforced in Web Crypto API runtimes.
|
|
76
79
|
*/
|
|
77
80
|
export declare function importPKCS8(pkcs8: string, alg: string, options?: PEMImportOptions): Promise<KeyLike>;
|
|
78
81
|
/**
|
|
@@ -107,8 +110,9 @@ export declare function importPKCS8(pkcs8: string, alg: string, options?: PEMImp
|
|
|
107
110
|
* ```
|
|
108
111
|
*
|
|
109
112
|
* @param jwk JSON Web Key.
|
|
110
|
-
* @param alg JSON Web Algorithm identifier to be used
|
|
111
|
-
* property on the JWK
|
|
113
|
+
* @param alg (Only effective in Web Crypto API runtimes) JSON Web Algorithm identifier to be used
|
|
114
|
+
* with the imported key. Default is the "alg" property on the JWK, its presence is only enforced
|
|
115
|
+
* in Web Crypto API runtimes.
|
|
112
116
|
* @param octAsKeyObject Forces a symmetric key to be imported to a KeyObject or CryptoKey. Default
|
|
113
117
|
* is true unless JWK "ext" (Extractable) is true.
|
|
114
118
|
*/
|
package/dist/types/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = (b64, descriptor) => {
|
|
4
|
-
const newlined = (b64.match(/.{1,64}/g) || []).join('\n');
|
|
5
|
-
return `-----BEGIN ${descriptor}-----\n${newlined}\n-----END ${descriptor}-----`;
|
|
6
|
-
};
|