keeweb/app/scripts/util/signature-verifier.js

64 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-05-13 22:36:07 +02:00
const Logger = require('./logger');
const publicKey = require('raw-loader!../../resources/public-key.pem');
const kdbxweb = require('kdbxweb');
const SignatureVerifier = {
logger: new Logger('signature-verifier'),
2017-05-14 17:14:21 +02:00
publicKey: null,
2017-05-14 00:24:06 +02:00
verify(data, signature, pk) {
2017-05-13 22:36:07 +02:00
return new Promise((resolve, reject) => {
2019-08-16 23:05:39 +02:00
const algo = { name: 'RSASSA-PKCS1-v1_5', hash: { name: 'SHA-256' } };
2017-05-13 22:36:07 +02:00
try {
2017-05-14 00:24:06 +02:00
if (!pk) {
pk = this.getPublicKey();
}
signature = kdbxweb.ByteUtils.base64ToBytes(signature);
2019-04-13 11:04:31 +02:00
const subtle = window.crypto.subtle;
const keyFormat = 'spki';
pk = kdbxweb.ByteUtils.base64ToBytes(pk);
2019-08-16 23:05:39 +02:00
subtle
.importKey(keyFormat, pk, algo, false, ['verify'])
.then(cryptoKey => {
try {
subtle
.verify(
algo,
cryptoKey,
kdbxweb.ByteUtils.arrayToBuffer(signature),
kdbxweb.ByteUtils.arrayToBuffer(data)
)
.then(isValid => {
resolve(isValid);
})
.catch(e => {
this.logger.error('Verify error', e);
reject();
});
} catch (e) {
this.logger.error('Signature verification error', e);
2017-05-14 17:04:14 +02:00
reject();
2019-08-16 23:05:39 +02:00
}
})
.catch(e => {
this.logger.error('ImportKey error', e);
2017-05-13 22:36:07 +02:00
reject();
2019-08-16 23:05:39 +02:00
});
2017-05-13 22:36:07 +02:00
} catch (e) {
2017-05-14 17:04:14 +02:00
this.logger.error('Signature key verification error', e);
2017-05-13 22:36:07 +02:00
reject();
}
});
2017-05-14 00:24:06 +02:00
},
getPublicKey() {
2017-05-14 17:14:21 +02:00
if (!this.publicKey) {
2019-08-16 23:05:39 +02:00
this.publicKey = publicKey.match(/-+BEGIN PUBLIC KEY-+([\s\S]+?)-+END PUBLIC KEY-+/)[1].replace(/\s+/g, '');
2017-05-14 17:14:21 +02:00
}
return this.publicKey;
2017-05-13 22:36:07 +02:00
}
};
module.exports = SignatureVerifier;