|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +class DERLite{ |
| 4 | + constructor() {} |
| 5 | + |
| 6 | + /* Simplified DER export and import is provided because a large number of |
| 7 | + * libraries and languages understand DER as a key exchange and storage |
| 8 | + * format. DER is NOT required for VAPID, however, the key you may |
| 9 | + * generate here (or in a different library) may be in this format. |
| 10 | + * |
| 11 | + * A fully featured DER library is available at |
| 12 | + * https://github.com/indutny/asn1.js |
| 13 | + */ |
| 14 | + |
| 15 | + export_private_der(key) { |
| 16 | + /* Generate a DER sequence. |
| 17 | + * |
| 18 | + * This can be read in via something like |
| 19 | + * python's |
| 20 | + * ecdsa.keys.SigningKey |
| 21 | + * .from_der(base64.urlsafe_b64decode("MHc...")) |
| 22 | + * |
| 23 | + * :param key: CryptoKey containing private key info |
| 24 | + */ |
| 25 | + return webCrypto.exportKey("jwk", key) |
| 26 | + .then(k => { |
| 27 | + // verifying key |
| 28 | + let xv = mzcc.fromUrlBase64(k.x); |
| 29 | + let yv = mzcc.fromUrlBase64(k.y); |
| 30 | + // private key |
| 31 | + let dv = mzcc.fromUrlBase64(k.d); |
| 32 | + |
| 33 | + // verifying key (public) |
| 34 | + let vk = '\x00\x04' + xv + yv; |
| 35 | + // \x02 is integer |
| 36 | + let int1 = '\x02\x01\x01'; // integer 1 |
| 37 | + // \x04 is octet string |
| 38 | + let dvstr = '\x04' + mzcc.chr(dv.length) + dv; |
| 39 | + let curve_oid = "\x06\x08" + |
| 40 | + "\x2a\x86\x48\xce\x3d\x03\x01\x07"; |
| 41 | + // \xaX is a construct, low byte is order. |
| 42 | + let curve_oid_const = '\xa0' + mzcc.chr(curve_oid.length) + |
| 43 | + curve_oid; |
| 44 | + // \x03 is a bitstring |
| 45 | + let vk_enc = '\x03' + mzcc.chr(vk.length) + vk; |
| 46 | + let vk_const = '\xa1' + mzcc.chr(vk_enc.length) + vk_enc; |
| 47 | + // \x30 is a sequence start. |
| 48 | + let seq = int1 + dvstr + curve_oid_const + vk_const; |
| 49 | + let rder = "\x30" + mzcc.chr(seq.length) + seq; |
| 50 | + return mzcc.toUrlBase64(rder); |
| 51 | + }) |
| 52 | + .catch(err => console.error(err)) |
| 53 | + } |
| 54 | + |
| 55 | + import_private_der(der_str) { |
| 56 | + /* Import a Private Key stored in DER format. This allows a key |
| 57 | + * to be generated outside of this script. |
| 58 | + * |
| 59 | + * :param der_str: URL safe base64 formatted DER string. |
| 60 | + * :returns: Promise containing the imported private key |
| 61 | + */ |
| 62 | + let der = mzcc._strToArray(mzcc.fromUrlBase64(der_str)); |
| 63 | + // quick guantlet to see if this is a valid DER |
| 64 | + let cmp = new Uint8Array([2,1,1,4]); |
| 65 | + if (der[0] != 48 || |
| 66 | + ! der.slice(2, 6).every(function(v, i){return cmp[i] == v})){ |
| 67 | + throw new Error("Invalid import key") |
| 68 | + } |
| 69 | + let dv = der.slice(7, 7+der[6]); |
| 70 | + // HUGE cheat to get the x y values |
| 71 | + let xv = der.slice(-64, -32); |
| 72 | + let yv = der.slice(-32); |
| 73 | + let key_ops = ['sign']; |
| 74 | + |
| 75 | + let jwk = { |
| 76 | + crv: "P-256", |
| 77 | + ext: true, |
| 78 | + key_ops: key_ops, |
| 79 | + kty: "EC", |
| 80 | + x: mzcc.toUrlBase64(String.fromCharCode.apply(null, xv)), |
| 81 | + y: mzcc.toUrlBase64(String.fromCharCode.apply(null, yv)), |
| 82 | + d: mzcc.toUrlBase64(String.fromCharCode.apply(null, dv)), |
| 83 | + }; |
| 84 | + |
| 85 | + console.debug(JSON.stringify(jwk)); |
| 86 | + return webCrypto.importKey('jwk', jwk, 'ECDSA', true, key_ops); |
| 87 | + } |
| 88 | + |
| 89 | + export_public_der(key) { |
| 90 | + /* Generate a DER sequence containing just the public key info. |
| 91 | + * |
| 92 | + * :param key: CryptoKey containing public key information |
| 93 | + * :returns: a URL safe base64 encoded string containing the |
| 94 | + * public key |
| 95 | + */ |
| 96 | + return webCrypto.exportKey("jwk", key) |
| 97 | + .then(k => { |
| 98 | + // raw keys always begin with a 4 |
| 99 | + let xv = mzcc._strToArray(mzcc.fromUrlBase64(k.x)); |
| 100 | + let yv = mzcc._strToArray(mzcc.fromUrlBase64(k.y)); |
| 101 | + |
| 102 | + let point = "\x00\x04" + |
| 103 | + String.fromCharCode.apply(null, xv) + |
| 104 | + String.fromCharCode.apply(null, yv); |
| 105 | + window.Kpoint = point; |
| 106 | + // a combination of the oid_ecPublicKey + p256 encoded oid |
| 107 | + let prefix = "\x30\x13" + // sequence + length |
| 108 | + "\x06\x07" + "\x2a\x86\x48\xce\x3d\x02\x01" + |
| 109 | + "\x06\x08" + "\x2a\x86\x48\xce\x3d\x03\x01\x07" |
| 110 | + let encPoint = "\x03" + mzcc.chr(point.length) + point |
| 111 | + let rder = "\x30" + mzcc.chr(prefix.length + encPoint.length) + |
| 112 | + prefix + encPoint; |
| 113 | + let der = mzcc.toUrlBase64(rder); |
| 114 | + return der; |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + import_public_der(derArray) { |
| 119 | + /* Import a DER formatted public key string. |
| 120 | + * |
| 121 | + * The Crypto-Key p256ecdsa=... key is such a thing. |
| 122 | + * Returns a promise containing the public key. |
| 123 | + * |
| 124 | + * :param derArray: the DER array containing the public key. |
| 125 | + * NOTE: This may also be a URL safe base64 encoded version |
| 126 | + * of the DER array. |
| 127 | + * :returns: A promise containing the imported public key. |
| 128 | + * |
| 129 | + */ |
| 130 | + if (typeof(derArray) == "string") { |
| 131 | + derArray = mzcc._strToArray(mzcc.fromUrlBase64(derArray)); |
| 132 | + } |
| 133 | + /* Super light weight public key import function */ |
| 134 | + let err = new Error(this.lang.errs.ERR_PUB_D_KEY); |
| 135 | + // Does the record begin with "\x30" |
| 136 | + if (derArray[0] != 48) { throw err} |
| 137 | + // is this an ECDSA record? (looking for \x2a and \x86 |
| 138 | + if (derArray[6] != 42 && derArray[7] != 134) { throw err} |
| 139 | + if (derArray[15] != 42 && derArray[16] != 134) { throw err} |
| 140 | + // Public Key Record usually beings @ offset 23. |
| 141 | + if (derArray[23] != 3 && derArray[24] != 40 && |
| 142 | + derArray[25] != 0 && derArray[26] != 4) { |
| 143 | + throw err; |
| 144 | + } |
| 145 | + // pubkey offset starts at byte 25 |
| 146 | + let x = mzcc.toUrlBase64(String.fromCharCode |
| 147 | + .apply(null, derArray.slice(27, 27+32))); |
| 148 | + let y = mzcc.toUrlBase64(String.fromCharCode |
| 149 | + .apply(null, derArray.slice(27+32, 27+64))); |
| 150 | + |
| 151 | + // Convert to a JWK and import it. |
| 152 | + let jwk = { |
| 153 | + crv: "P-256", |
| 154 | + ext: true, |
| 155 | + key_ops: ["verify"], |
| 156 | + kty: "EC", |
| 157 | + x: x, |
| 158 | + y, y |
| 159 | + }; |
| 160 | + |
| 161 | + return webCrypto.importKey('jwk', jwk, 'ECDSA', true, ["verify"]) |
| 162 | + |
| 163 | + } |
| 164 | +} |
| 165 | + |
0 commit comments