-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comply with RFC 5753 when computing ECDH keys in EnvelopedData
Fixes #334
- Loading branch information
Showing
2 changed files
with
143 additions
and
56 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
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,77 @@ | ||
/* eslint-disable no-undef */ | ||
import * as asn1js from "asn1js"; | ||
import * as pkijs from "../src/index.js"; | ||
|
||
const { Crypto } = require("@peculiar/webcrypto"); | ||
const crypto = new Crypto(); | ||
|
||
const assert = require("assert"); | ||
pkijs.setEngine("newEngine", crypto, new pkijs.CryptoEngine({ name: "", crypto: crypto, subtle: crypto.subtle })); | ||
|
||
const recipientPrivateKeyPem = ` | ||
-----BEGIN PRIVATE KEY----- | ||
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgSgh0H70tbGuageLjXJ8+OhH6wzoSQJ96 | ||
qJ4PQ8RP2jagCgYIKoZIzj0DAQehRANCAAToW17Szlrc7F6JXyn3HggMkHx1TluBrteZ0WAQHV31u4yi | ||
LaaR70atxlhCdMaTpFey+lnnjfSns3TipH47meUO | ||
-----END PRIVATE KEY----- | ||
`; | ||
const recipientCertificatePem = ` | ||
-----BEGIN CERTIFICATE----- | ||
MIIDrDCCAmCgAwIBAgIICTX8CVCpVZcwQQYJKoZIhvcNAQEKMDSgDzANBglghkgBZQMEAgEFAKEcMBoG | ||
CSqGSIb3DQEBCDANBglghkgBZQMEAgEFAKIDAgEgMIGQMYGNMIGKBgNVBAMegYIAMAA2ADQAMABjAGYA | ||
NQAyADMAMgA4ADkAZQBiAGYAMgBiADkAOAA4ADI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F+FDk86b1rykY=== | ||
-----END CERTIFICATE----- | ||
`; | ||
const envelopedDataPem = ` | ||
-----BEGIN CMS----- | ||
MIIB/wYJKoZIhvcNAQcDoIIB8DCCAewCAQIxggF3oYIBcwIBA6BRoU8wCQYHKoZIzj0CAQNCAATqtUjV | ||
kFlUhI0eYHRpRUFYeCJL7OXFTDGbFzLo75544lkdQ6QqUOAdjlCctmY1aURutiyp8ClU8S5B1rLPBJuZ | ||
oUIEQO0Gsde4OdIqXuwrBdW+b+5JU4b4LmUJZCzEVpsmtAUBZ3wd7TsjuHbHtSvRpGUyw8Hu8hfyDefA | ||
FQzaMnZjCoYwFwYGK4EEAQsDMA0GCWCGSAFlAwQBLQUAMIG9MIG6MIGdMIGQMYGNMIGKBgNVBAMegYIA | ||
MAA2ADQAMABjAGYANQAyADMAMgA4ADkAZQBiAGYAMgBiADkAOAA4ADIAYwA2AGIAYQAxADgAYQAxAGIA | ||
OQBkAGMAYwAwAGEAMABmADMAMABiADcAOAA2ADIAZABjADgAMgAxADEAZQA4ADAAZAAyAGIANAA1AGEA | ||
NQBmAGEANQBkAggJNfwJUKlVlwQYMK8dCPR+rZ+p8f9JoB2+ns5mGIV45F7MMIAGCSqGSIb3DQEHATAd | ||
BglghkgBZQMEAQIEEFlT3Jb453LG9SVYa7MaWiCggAQgXNT1MFuyL+mH3XORYWrmsk9a1qui+48NZZbJ | ||
qH9W/zoAAAAAoRgwFgYIBAB/ABEAAQAxCgIIMltJu53L4Og=== | ||
-----END CMS----- | ||
`; | ||
|
||
it("EnvelopedData with an ECCCMSSharedInfo containing algorithmParams should be decrypted", async () => { | ||
const recipientPrivateKey = pemToDer(recipientPrivateKeyPem); | ||
|
||
const recipientCertificate = new pkijs.Certificate({schema: pemToAsn1(recipientCertificatePem)}); | ||
|
||
const contentInfo = new pkijs.ContentInfo({schema: pemToAsn1(envelopedDataPem)}); | ||
const envelopedData = new pkijs.EnvelopedData({schema: contentInfo.content}); | ||
|
||
const plaintext = await envelopedData.decrypt(0, {recipientCertificate, recipientPrivateKey}); | ||
assert.equal("Hi. My name is Alice.", Buffer.from(plaintext).toString()); | ||
}); | ||
|
||
function pemToDer(pemString) { | ||
const derBase64 = pemString.replace(/(-----(BEGIN|END) [\w ]+-----|\n)/g, ""); | ||
const der = Buffer.from(derBase64, "base64"); | ||
return der.buffer.slice(der.byteOffset, der.byteOffset + der.byteLength); | ||
} | ||
|
||
function pemToAsn1(pemString) { | ||
const der = pemToDer(pemString); | ||
const asn1 = asn1js.fromBER(der); | ||
if (asn1.offset === -1) { | ||
throw new Error("Value is not DER-encoded"); | ||
} | ||
return asn1.result; | ||
} |