-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
95 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
"redirections", | ||
"rels", | ||
"setext", | ||
"spki", | ||
"subproperty", | ||
"superproperty", | ||
"unfollow", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,6 @@ | |
"@std/url": "jsr:@std/url@^0.220.1", | ||
"@std/yaml": "jsr:@std/yaml@^0.220.1", | ||
"fast-check": "npm:fast-check@^3.17.0", | ||
"jose": "npm:jose@^5.2.3", | ||
"jsonld": "npm:jsonld@^8.3.2", | ||
"mock_fetch": "https://deno.land/x/[email protected]/mod.ts", | ||
"uri-template-router": "npm:uri-template-router@^0.0.16", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ | |
"@std/url": "jsr:@std/url@^0.220.1", | ||
"@std/yaml": "jsr:@std/yaml@^0.220.1", | ||
"fast-check": "npm:fast-check@^3.17.0", | ||
"jose": "npm:jose@^5.2.3", | ||
"jsonld": "npm:jsonld@^8.3.2", | ||
"mock_fetch": "https://deno.land/x/[email protected]/mod.ts", | ||
"uri-template-router": "npm:uri-template-router@^0.0.16", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { exportJwk, importJwk } from "../httpsig/key.ts"; | ||
import { exportSpki, importSpki } from "./key.ts"; | ||
|
||
// cSpell: disable | ||
const pem = "-----BEGIN PUBLIC KEY-----\n" + | ||
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxsRuvCkgJtflBTl4OVsm\n" + | ||
"nt/J1mQfZasfJtN33dcZ3d1lJroxmgmMu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR\n" + | ||
"5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7WQnKQgQMI+ezn24GHsZ/v1JIo77lerX5\n" + | ||
"k4HNwTNVt+yaZVQWaOMR3+6FwziQR6kd0VuG9/a9dgAnz2cEoORRC1i4W7IZaB1s\n" + | ||
"Znh1WbHbevlGd72HSXll5rocPIHn8gq6xpBgpHwRphlRsgn4KHaJ6brXDIJjrnQh\n" + | ||
"Ie/YUBOGj/ImSEXhRwlFerKsoAVnZ0Hwbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2\n" + | ||
"uwIDAQAB\n" + | ||
"-----END PUBLIC KEY-----\n"; | ||
// cSpell: enable | ||
|
||
const jwk = { | ||
alg: "RS256", | ||
// cSpell: disable | ||
e: "AQAB", | ||
// cSpell: enable | ||
ext: true, | ||
key_ops: ["verify"], | ||
kty: "RSA", | ||
// cSpell: disable | ||
n: "xsRuvCkgJtflBTl4OVsmnt_J1mQfZasfJtN33dcZ3d1lJroxmgmMu69zjGEAwkNbMQaWN" + | ||
"LqC4eogkJaeJ4RR5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7WQnKQgQMI-ezn24GHsZ_v1J" + | ||
"Io77lerX5k4HNwTNVt-yaZVQWaOMR3-6FwziQR6kd0VuG9_a9dgAnz2cEoORRC1i4W7IZa" + | ||
"B1sZnh1WbHbevlGd72HSXll5rocPIHn8gq6xpBgpHwRphlRsgn4KHaJ6brXDIJjrnQhIe_" + | ||
"YUBOGj_ImSEXhRwlFerKsoAVnZ0Hwbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2uw", | ||
// cSpell: enable | ||
}; | ||
|
||
Deno.test("importSpki()", async () => { | ||
const key = await importSpki(pem); | ||
assertEquals(await exportJwk(key), jwk); | ||
}); | ||
|
||
Deno.test("exportSpki()", async () => { | ||
const key = await importJwk(jwk, "public"); | ||
const spki = await exportSpki(key); | ||
assertEquals(spki, pem); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { decodeBase64, encodeBase64 } from "@std/encoding/base64"; | ||
|
||
/** | ||
* Imports a PEM-SPKI formatted public key. | ||
* @param pem The PEM-SPKI formatted public key. | ||
* @returns The imported public key. | ||
*/ | ||
export async function importSpki(pem: string): Promise<CryptoKey> { | ||
pem = pem.replace(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, ""); | ||
const spki = decodeBase64(pem); | ||
// TODO: support other than RSASSA-PKCS1-v1_5: | ||
return await crypto.subtle.importKey( | ||
"spki", | ||
spki, | ||
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, | ||
true, | ||
["verify"], | ||
); | ||
} | ||
|
||
/** | ||
* Exports a public key in PEM-SPKI format. | ||
* @param key The public key to export. | ||
* @returns The exported public key in PEM-SPKI format. | ||
*/ | ||
export async function exportSpki(key: CryptoKey): Promise<string> { | ||
const spki = await crypto.subtle.exportKey("spki", key); | ||
let pem = encodeBase64(spki); | ||
pem = (pem.match(/.{1,64}/g) || []).join("\n"); | ||
return `-----BEGIN PUBLIC KEY-----\n${pem}\n-----END PUBLIC KEY-----\n`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
* @module | ||
*/ | ||
export * from "./docloader.ts"; | ||
export * from "./key.ts"; | ||
export * from "./langstr.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters