crypto-helper/test/spec/signatures.js

255 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-10-04 12:18:39 +02:00
'use strict';
const crypto = require ('../../index');
const rsa = require ('../../lib/rsa');
2020-10-04 12:18:39 +02:00
// eslint-disable-next-line max-lines-per-function
describe ('signatures', () => {
2020-12-30 17:16:29 +01:00
beforeEach (() => {
jasmine.clock ()
.install ();
const base_time = (new Date);
jasmine.clock ()
.mockDate (base_time);
});
afterEach (() => {
jasmine.clock ()
.uninstall ();
});
2020-10-04 12:18:39 +02:00
it ('sign_object', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz');
expect (typeof str)
.toEqual ('string');
}).not.toThrow ();
});
it ('sign_object with rsa key', async () => {
const obj = { foo: 'bar' };
const k = await rsa.generate_keypair ();
expect (() => {
const str = crypto.sign_object (obj, k.private_key);
expect (typeof str)
.toEqual ('string');
}).not.toThrow ();
});
it ('verify_signature with rsa key', () => {
const obj = { foo: 'bar' };
const str = 'U1GcsN3yZzSKxPH8jhCGTKiswfazB9rMfUtE5351LT11t6EmS7xfPjnt'
+ '.5ytniC6q2ovoF7ZqbD8qk9r2kjjAcA9EYhLwC3wwJKPPsKdHSTFd7d9TzBP1skQ98X'
+ 'LjRUkc2M8M84LmWLg76EvcY2pw6HwsFvCUoZYcKAJp3vkp9MQVrVYdHKMPkBjQKyy2V'
+ 'KtEZiBsomBVJd6Hudd1YLMQ4J4s52iHsegDswKE9djYVEmgKkJUAiZJ2viFHw3fBbp2'
+ 'Abo2Dm5oqYtw7Nn9RFstW3CcNQHV1PzHDKD56Uw3opuYwVZhQth8ux2CdkC2yMvgVsT'
+ 'dUyCuu78ugaGvzsMXCbe2BzaPFDTE9JYtMcDFFP43nUGHNd6cWwzoKTZBX852Exz6Rb'
+ 'VjcWUvL81dLPBLJV.2';
const key = '-----BEGIN RSA PUBLIC KEY-----\n'
+ 'MIIBCgKCAQEA4LCEoJYNwwksuzPESpmPziHp98WhY5Qml6RiN9uxrKGPV6QwwmDQ\n'
+ 'ks6C+ZfYbFG9NCx1MEuWL0Tvp/6ZBhMyaJrI5iwo0CmSX3WdFcbXmdl0l6N1+5r7\n'
+ 'l3SkKsr/AX4gwcDor4dYuLEv5KawGdfcP0IxsoAcIN1UJ5HJ+eheB3fVcSh/IIBf\n'
+ 'O+cL/4Chw8eAaDBG5mZ1Xgd4gIjJGYAxgUNvaShGzs8k1y+jqjD5IkZ1h9dgoGJG\n'
+ 'dUmjCLWrOzx8SqdqJYmQJX+6GNswnvVF30bkW+/MJZF/P2jLFtSa24Monh7axIqx\n'
+ '8HG0xDw1Z98WV9oQh/vDP/KAs1cPp0AJlwIDAQAB\n'
+ '-----END RSA PUBLIC KEY-----\n';
const ver = crypto.verify_signature (str, key);
expect (ver)
.toEqual (obj);
});
it ('verify_signature reject with rsa key', () => {
const str = 'U1GcsN3yZzSKxPH8jhCGTKiswfazB9rMfUtE5351LT11t6EmS7xfPjnt'
+ '.5ytniC6q2ovoF7ZqbD8qk9r2kjjAcA9EYhLwC3wwJKPPsKdHSTFd7d9TzBP1skQ98X'
+ 'LjRUkc2M8M84LmWLg76EvcY2pw6HwsFvCUoZYcKAJp3vkp9MQVrVYdHKMPkBjQKyy2V'
+ 'KtEZiBsomBVJd6Hudd1YLMQ4J4s52iHsegDswKE9djYVEmgKkJUAiZJ2viFHw3fBbp2'
+ 'Abo2Dm5oqYtw7Nn9RFstW3CcNQHV1PzHDKD56Uw3opuYwVZhQth8ux2CdkC2yMvgVsT'
+ 'dUyCuu78ugaGvzsMXCbe2BzaPFDTE9JYtMcDFFP43nUGHNd6cWwzoKTZBX852Exz6Rb'
+ 'VjcWUvL81dLPBLJA.2';
const key = '-----BEGIN RSA PUBLIC KEY-----\n'
+ 'MIIBCgKCAQEA4LCEoJYNwwksuzPESpmPziHp98WhY5Qml6RiN9uxrKGPV6QwwmDQ\n'
+ 'ks6C+ZfYbFG9NCx1MEuWL0Tvp/6ZBhMyaJrI5iwo0CmSX3WdFcbXmdl0l6N1+5r7\n'
+ 'l3SkKsr/AX4gwcDor4dYuLEv5KawGdfcP0IxsoAcIN1UJ5HJ+eheB3fVcSh/IIBf\n'
+ 'O+cL/4Chw8eAaDBG5mZ1Xgd4gIjJGYAxgUNvaShGzs8k1y+jqjD5IkZ1h9dgoGJG\n'
+ 'dUmjCLWrOzx8SqdqJYmQJX+6GNswnvVF30bkW+/MJZF/P2jLFtSa24Monh7axIqx\n'
+ '8HG0xDw1Z98WV9oQh/vDP/KAs1cPp0AJlwIDAQAB\n'
+ '-----END RSA PUBLIC KEY-----\n';
const ver = crypto.verify_signature (str, key);
expect (ver)
.toBeNull ();
});
2020-12-13 13:09:34 +01:00
it ('should sign object with key info', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz', 'baz');
const res = crypto.get_signature_info (str);
expect (res.key_info)
.toEqual ('baz');
}).not.toThrow ();
});
it ('should sign object with custom properties', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz', { bar: 'baz' });
const res = crypto.get_signature_info (str);
expect (res.bar)
.toEqual ('baz');
}).not.toThrow ();
});
it ('should sign object with custom override properties', () => {
const obj = { foo: 'bar' };
expect (() => {
const str = crypto.sign_object (obj, 'baz', { iat: 'baz' });
const res = crypto.get_signature_info (str);
expect (res.iat)
.toEqual ('baz');
}).not.toThrow ();
});
2020-10-04 12:18:39 +02:00
it ('decode_signed', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.decode_signed (str);
expect (obj)
.toEqual (dec);
});
it ('verify_signature', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, 'baz');
expect (obj)
.toEqual (dec);
});
2020-12-13 13:09:34 +01:00
it ('should verify and return all info', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz', { iat: 'baz' });
const dec = crypto.verify_signature_get_info (str, 'baz');
expect (dec.obj)
.toEqual (obj);
expect (dec.iat)
.toEqual ('baz');
});
it ('should verify signature using function retrieved key', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, () => 'baz');
expect (obj)
.toEqual (dec);
});
2020-12-13 13:31:48 +01:00
it ('should verify signature using function retrieved timeout 0', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, 'baz', () => 0);
expect (obj)
.toEqual (dec);
});
2020-11-29 12:01:43 +01:00
it ('should reject tampered signatures', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
const dec = crypto.verify_signature (str, 'foo');
expect (dec)
.toEqual (null);
});
2020-10-04 12:18:39 +02:00
it ('should return null on invalid input', () => {
const ver = crypto.verify_signature (null, 'foo');
expect (ver)
.toEqual (null);
const dec = crypto.decode_signed (null, 'foo');
expect (dec)
.toEqual (null);
});
2020-11-29 12:05:51 +01:00
it ('should not fail verification if timeout unspecified', () => {
2020-10-04 12:18:39 +02:00
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.tick (36e5);
2020-10-04 12:18:39 +02:00
const dec = crypto.verify_signature (str, 'baz');
expect (obj)
.toEqual (dec);
});
2020-11-29 12:05:51 +01:00
it ('should reject old signatures', () => {
2020-10-04 12:18:39 +02:00
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.tick (50);
2020-10-04 12:18:39 +02:00
const dec = crypto.verify_signature (str, 'baz', 1);
expect (dec)
.toEqual (null);
});
2020-11-29 12:05:51 +01:00
it ('should not reject valid signatures', () => {
2020-10-04 12:18:39 +02:00
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
2020-11-29 12:01:43 +01:00
jasmine.clock ()
.tick (50);
2020-10-04 12:18:39 +02:00
const dec = crypto.verify_signature (str, 'baz', 100);
expect (obj)
.toEqual (dec);
2020-11-29 12:01:43 +01:00
});
2020-12-13 13:31:48 +01:00
it ('should verify signature using function retrieved timeout', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz', { to: 100 });
jasmine.clock ()
.tick (50);
const dec = crypto.verify_signature (str, 'baz', (info) => info.to);
expect (obj)
.toEqual (dec);
});
2020-11-29 12:01:43 +01:00
it ('verify_signature on almost timed out packet', () => {
const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz');
jasmine.clock ()
.tick (10);
const dec = crypto.verify_signature (str, 'baz', 10);
expect (obj)
.toEqual (dec);
2020-10-04 12:18:39 +02:00
});
it ('should decode problematic token', () => {
2020-12-30 17:16:29 +01:00
// eslint-disable-next-line max-len
const str = 'wEJbzvUywiaiGWZUG6CtCXNkNmRGyVoi9icytpTe4gZhsb8Gk.5PZbhGL525mdV7EmYomTwUei6qULpLaZwSXy92eaUDNgbyXPHsr9dfUCeEBpTqmzuq3VtmmV43epUyWRoHocAsV3.2';
const obj = crypto.decode_signed (str);
expect (obj)
.toEqual ({ id: 1 });
});
it ('should automatically reencode b64 tokens', () => {
// eslint-disable-next-line max-len
2020-10-04 12:18:39 +02:00
const str = 'eyJpYXQiOjE1ODE0NDAwMTIyODgsIm9iaiI6eyJpZCI6MX19.24ZOsWrnfkNe%2FbM0r7DaVJMqE2bfn2aAM%2BZSzWeSf31OCTlXXNWD34RBL2X5v3UliYQ4IIsLNBFbaW9texPHug%3D%3D';
const obj = crypto.decode_signed (str);
expect (obj)
.toEqual ({ id: 1 });
});
2020-12-30 17:16:29 +01:00
it ('verify_signature on b64 string', () => {
// eslint-disable-next-line max-len
const str = 'eyJpYXQiOjE2MDkzNDQ4MDMyMjcsIm9iaiI6eyJpZCI6MX19.N762xuMaNbT%2Fqb0uTKST68BZgSnmNxXaHl4GY7iAKqaDDEwZn3biYfg5DgJ45QgPZrndchczDjUqLkyXoqw4KQ%3D%3D';
const obj = crypto.verify_signature (str, 'baz');
expect (obj)
.toEqual ({ id: 1 });
});
2020-10-04 12:18:39 +02:00
});