export/import keys
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-01-08 13:30:53 +01:00
parent 4c42a682d5
commit d6a40871c4
5 changed files with 84 additions and 19 deletions

View File

@ -49,7 +49,7 @@ class Authority {
key,
(info) => {
try {
return keystore.get_key (info.iat / 1000);
return keystore.get_key (info.iat / 1000, info.iss);
}
catch {
return '';
@ -89,6 +89,7 @@ class Authority {
const attributes = {
id: create_salt (),
iat: time,
iss: keystore.instance_id,
type,
valid_for,
next_module: options?.next_module

View File

@ -5,9 +5,10 @@
* Created by Timo Hocker <timo@scode.ovh>, December 2020
*/
import { generate_keypair } from '@sapphirecode/crypto-helper';
import { generate_keypair, random_hex } from '@sapphirecode/crypto-helper';
import { to_b58 } from '@sapphirecode/encoding-helper';
const renew_interval = 60;
const renew_interval = 3600;
interface Key {
key: string;
@ -21,11 +22,6 @@ interface KeyPair {
type KeyStoreData = Record<string, KeyPair>;
function get_index (iat: number): string {
return Math.floor (iat / renew_interval)
.toFixed (0);
}
async function create_key (valid_for: number) {
const time = (new Date)
.getTime ();
@ -45,11 +41,22 @@ async function create_key (valid_for: number) {
class KeyStore {
private _keys: KeyStoreData = {};
private _interval: NodeJS.Timeout;
private _instance: string;
public get instance_id (): string {
return this._instance;
}
public constructor () {
this._interval = setInterval (() => {
this.garbage_collect ();
}, renew_interval);
this._instance = to_b58 (random_hex (16), 'hex');
}
private get_index (iat: number, instance = this._instance): string {
return instance + Math.floor (iat / renew_interval)
.toFixed (0);
}
private garbage_collect (set: KeyStoreData = this._keys): void {
@ -68,7 +75,11 @@ class KeyStore {
}
}
public async get_sign_key (iat: number, valid_for: number): Promise<string> {
public async get_sign_key (
iat: number,
valid_for: number,
instance?: string
): Promise<string> {
if (valid_for <= 0)
throw new Error ('cannot create infinitely valid key');
@ -76,7 +87,7 @@ class KeyStore {
.getTime ())
throw new Error ('cannot access already expired keys');
const index = get_index (iat);
const index = this.get_index (iat, instance);
const valid_until = (new Date)
.getTime () + (valid_for * 1000);
@ -86,6 +97,9 @@ class KeyStore {
if (key.public_key.valid_until < valid_until)
key.public_key.valid_until = valid_until;
if (typeof key.private_key === 'undefined')
throw new Error ('cannot access already expired keys');
return key.private_key?.key as string;
}
@ -93,8 +107,8 @@ class KeyStore {
return this._keys[index].private_key?.key as string;
}
public get_key (iat: number): string {
const index = get_index (iat);
public get_key (iat: number, instance?: string): string {
const index = this.get_index (iat, instance);
if (typeof this._keys[index] === 'undefined')
throw new Error ('key could not be found');
@ -115,8 +129,12 @@ class KeyStore {
public import_verification_data (data: KeyStoreData): void {
const import_set = { ...data };
this.garbage_collect (import_set);
// TODO: import
for (const key of Object.keys (import_set)) {
if (typeof this._keys[key] !== 'undefined')
throw new Error ('cannot import to the same instance');
this._keys[key] = import_set[key];
}
this.garbage_collect ();
}
}