|
| 1 | +// Copyright (c) 2023 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0 |
| 3 | + |
| 4 | +import * as asn1js from 'asn1js'; |
| 5 | + |
| 6 | +// Converts a RSA-PSS key into a RSA Encryption key. |
| 7 | +// Required because WebCrypto do not support importing keys with `RSASSA-PSS` OID, |
| 8 | +// |
| 9 | +// Chromium: https://www.chromium.org/blink/webcrypto/#supported-key-formats |
| 10 | +// Firefox: https://github.com/mozilla/pkipolicy/blob/master/rootstore/policy.md#511-rsa |
| 11 | +// WebCrypto: https://github.com/w3c/webcrypto/pull/325 |
| 12 | +// |
| 13 | +// Documentation: https://www.rfc-editor.org/rfc/rfc4055#section-6 |
| 14 | +export function convertRSASSAPSSToEnc(keyRSAPSSEncSpki: Uint8Array): Uint8Array { |
| 15 | + const RSAEncryptionAlgID = '1.2.840.113549.1.1.1'; |
| 16 | + const schema = new asn1js.Sequence({ |
| 17 | + value: [ |
| 18 | + new asn1js.Sequence({ name: 'algorithm' }), |
| 19 | + new asn1js.BitString({ name: 'subjectPublicKey' }), |
| 20 | + ], |
| 21 | + }); |
| 22 | + const cmp = asn1js.verifySchema(keyRSAPSSEncSpki, schema); |
| 23 | + if (!cmp.verified) { |
| 24 | + throw new Error('bad parsing RSA-PSS key'); |
| 25 | + } |
| 26 | + |
| 27 | + const keyASN = new asn1js.Sequence({ |
| 28 | + value: [ |
| 29 | + new asn1js.Sequence({ |
| 30 | + value: [ |
| 31 | + new asn1js.ObjectIdentifier({ value: RSAEncryptionAlgID }), |
| 32 | + new asn1js.Null(), |
| 33 | + ], |
| 34 | + }), |
| 35 | + cmp.result.subjectPublicKey, |
| 36 | + ], |
| 37 | + }); |
| 38 | + |
| 39 | + return new Uint8Array(keyASN.toBER()); |
| 40 | +} |
| 41 | + |
| 42 | +function algorithm_RSASSA_PSS() { |
| 43 | + const RSAPSSAlgID = '1.2.840.113549.1.1.10'; |
| 44 | + const publicKeyParams = new asn1js.Sequence({ |
| 45 | + value: [ |
| 46 | + new asn1js.Constructed({ |
| 47 | + idBlock: { |
| 48 | + tagClass: 3, // CONTEXT-SPECIFIC |
| 49 | + tagNumber: 0, // [0] |
| 50 | + }, |
| 51 | + value: [ |
| 52 | + new asn1js.Sequence({ |
| 53 | + value: [ |
| 54 | + new asn1js.ObjectIdentifier({ value: '2.16.840.1.101.3.4.2.2' }), // sha-384 |
| 55 | + ], |
| 56 | + }), |
| 57 | + ], |
| 58 | + }), |
| 59 | + new asn1js.Constructed({ |
| 60 | + idBlock: { |
| 61 | + tagClass: 3, // CONTEXT-SPECIFIC |
| 62 | + tagNumber: 1, // [1] |
| 63 | + }, |
| 64 | + value: [ |
| 65 | + new asn1js.Sequence({ |
| 66 | + value: [ |
| 67 | + new asn1js.ObjectIdentifier({ value: '1.2.840.113549.1.1.8' }), // pkcs1-MGF |
| 68 | + new asn1js.Sequence({ |
| 69 | + value: [ |
| 70 | + new asn1js.ObjectIdentifier({ |
| 71 | + value: '2.16.840.1.101.3.4.2.2', |
| 72 | + }), // sha-384 |
| 73 | + ], |
| 74 | + }), |
| 75 | + ], |
| 76 | + }), |
| 77 | + ], |
| 78 | + }), |
| 79 | + new asn1js.Constructed({ |
| 80 | + idBlock: { |
| 81 | + tagClass: 3, // CONTEXT-SPECIFIC |
| 82 | + tagNumber: 2, // [2] |
| 83 | + }, |
| 84 | + value: [ |
| 85 | + new asn1js.Integer({ value: 48 }), // sLen = 48 |
| 86 | + ], |
| 87 | + }), |
| 88 | + ], |
| 89 | + }); |
| 90 | + |
| 91 | + return new asn1js.Sequence({ |
| 92 | + value: [new asn1js.ObjectIdentifier({ value: RSAPSSAlgID }), publicKeyParams], |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +// Exports a RSA-PSS key as RSASSA-PSS. |
| 97 | +// This is required because browsers do not support exporting RSA-PSS keys. |
| 98 | +// |
| 99 | +// Chromium: https://www.chromium.org/blink/webcrypto/#supported-key-formats |
| 100 | +// Firefox: https://github.com/mozilla/pkipolicy/blob/master/rootstore/policy.md#511-rsa |
| 101 | +// |
| 102 | +// Documentation: https://www.rfc-editor.org/rfc/rfc4055#section-6 |
| 103 | +export function convertEncToRSASSAPSS(keyEncRSAPSSSpki: Uint8Array): Uint8Array { |
| 104 | + const schema = new asn1js.Sequence({ |
| 105 | + value: [ |
| 106 | + new asn1js.Sequence({ name: 'algorithm' }), |
| 107 | + new asn1js.BitString({ name: 'subjectPublicKey' }), |
| 108 | + ], |
| 109 | + }); |
| 110 | + |
| 111 | + const cmp = asn1js.verifySchema(keyEncRSAPSSSpki, schema); |
| 112 | + if (!cmp.verified) { |
| 113 | + throw new Error('bad parsing EncRSA key'); |
| 114 | + } |
| 115 | + |
| 116 | + const algorithmID = algorithm_RSASSA_PSS(); |
| 117 | + const asn = new asn1js.Sequence({ |
| 118 | + value: [algorithmID, cmp.result.subjectPublicKey], |
| 119 | + }); |
| 120 | + |
| 121 | + return new Uint8Array(asn.toBER()); |
| 122 | +} |
0 commit comments