|
| 1 | +// Copyright 2019-2020 Gohilla. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:js_interop'; |
| 16 | + |
| 17 | +import 'package:cryptography/cryptography.dart'; |
| 18 | +import 'package:cryptography/dart.dart'; |
| 19 | +import 'package:meta/meta.dart'; |
| 20 | + |
| 21 | +import '_javascript_bindings.dart' as web_crypto; |
| 22 | + |
| 23 | +class BrowserEd25519 extends Ed25519 { |
| 24 | + static final _jsAlgorithm = web_crypto.AlgorithmNameParams( |
| 25 | + name: 'Ed25519'.toJS, |
| 26 | + ).jsObject; |
| 27 | + |
| 28 | + final Ed25519? _fallback; |
| 29 | + |
| 30 | + @literal |
| 31 | + const BrowserEd25519({required Ed25519? fallback}) |
| 32 | + : _fallback = fallback, |
| 33 | + super.constructor(); |
| 34 | + |
| 35 | + @override |
| 36 | + Future<SimpleKeyPair> newKeyPair() async { |
| 37 | + late web_crypto.Jwk jwk; |
| 38 | + try { |
| 39 | + final jsCryptoKey = await web_crypto.generateKeyWhenKeyPair( |
| 40 | + _jsAlgorithm, true.toJS, ['sign'.toJS, 'verify'.toJS].toJS); |
| 41 | + jwk = await web_crypto.exportKeyWhenJwk(jsCryptoKey.privateKey); |
| 42 | + } catch (e) { |
| 43 | + final fallback = _fallback; |
| 44 | + if (fallback != null) { |
| 45 | + return fallback.newKeyPair(); |
| 46 | + } |
| 47 | + throw StateError('$runtimeType.newKeyPair(...) failed: $e'); |
| 48 | + } |
| 49 | + return SimpleKeyPairData( |
| 50 | + web_crypto.base64UrlDecode(jwk.d!.toDart), |
| 51 | + publicKey: SimplePublicKey( |
| 52 | + web_crypto.base64UrlDecode(jwk.x!.toDart), |
| 53 | + type: KeyPairType.ed25519, |
| 54 | + ), |
| 55 | + type: KeyPairType.ed25519, |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + @override |
| 60 | + Future<SimpleKeyPair> newKeyPairFromSeed(List<int> seed) { |
| 61 | + KeyPairType.ed25519.checkPrivateKeyBytesFormat(seed); |
| 62 | + return DartEd25519().newKeyPairFromSeed(seed); |
| 63 | + } |
| 64 | + |
| 65 | + @override |
| 66 | + Future<Signature> sign(List<int> message, {required KeyPair keyPair}) async { |
| 67 | + try { |
| 68 | + final publicKeyFuture = keyPair.extractPublicKey(); |
| 69 | + final keyPairData = await (keyPair as SimpleKeyPair).extract(); |
| 70 | + final jsCryptoKey = await web_crypto.importKeyWhenJwk( |
| 71 | + web_crypto.Jwk( |
| 72 | + kty: 'OKP'.toJS, |
| 73 | + crv: 'Ed25519'.toJS, |
| 74 | + d: web_crypto.base64UrlEncode(keyPairData.bytes).toJS, |
| 75 | + x: web_crypto.base64UrlEncode(keyPairData.publicKey.bytes).toJS, |
| 76 | + ), |
| 77 | + _jsAlgorithm, |
| 78 | + false.toJS, |
| 79 | + ['sign'.toJS].toJS, |
| 80 | + ); |
| 81 | + final signature = await web_crypto.sign( |
| 82 | + _jsAlgorithm, |
| 83 | + jsCryptoKey, |
| 84 | + web_crypto.jsUint8ListFrom(message), |
| 85 | + ); |
| 86 | + return Signature( |
| 87 | + signature, |
| 88 | + publicKey: await publicKeyFuture, |
| 89 | + ); |
| 90 | + } catch (e) { |
| 91 | + final fallback = _fallback; |
| 92 | + if (fallback != null) { |
| 93 | + return fallback.sign(message, keyPair: keyPair); |
| 94 | + } |
| 95 | + throw StateError('$runtimeType.sign(...) failed: $e'); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @override |
| 100 | + Future<bool> verify(List<int> message, {required Signature signature}) async { |
| 101 | + Ed25519.checkSignatureLength(signature.bytes.length); |
| 102 | + final simplePublicKey = signature.publicKey as SimplePublicKey; |
| 103 | + KeyPairType.ed25519.checkPublicKeyBytesFormat(simplePublicKey.bytes); |
| 104 | + try { |
| 105 | + final jsPublicKey = await web_crypto.importKeyWhenRaw( |
| 106 | + web_crypto.jsUint8ListFrom(simplePublicKey.bytes), |
| 107 | + _jsAlgorithm, |
| 108 | + true.toJS, |
| 109 | + ['verify'.toJS].toJS, |
| 110 | + ); |
| 111 | + return web_crypto.verify( |
| 112 | + _jsAlgorithm, |
| 113 | + jsPublicKey, |
| 114 | + web_crypto.jsUint8ListFrom(signature.bytes), |
| 115 | + web_crypto.jsUint8ListFrom(message), |
| 116 | + ); |
| 117 | + } catch (e) { |
| 118 | + final fallback = _fallback; |
| 119 | + if (fallback != null) { |
| 120 | + return fallback.verify(message, signature: signature); |
| 121 | + } |
| 122 | + throw StateError('$runtimeType.verify(...) failed: $e'); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments