Skip to content
This repository has been archived by the owner on Jul 17, 2021. It is now read-only.

Commit

Permalink
fix: add used nonce to data submission request
Browse files Browse the repository at this point in the history
  • Loading branch information
BerniWittmann committed Mar 21, 2021
1 parent 7d01f33 commit 936fb10
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/lib/Iris.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const defaultOptions: IrisOptions = {
};

export default class Iris {
axiosInstance: AxiosInstance;
codeKeyMap: IrisCodeKeyMap;
private axiosInstance: AxiosInstance;
private codeKeyMap: IrisCodeKeyMap;

constructor(options: Partial<IrisOptions>) {
this.codeKeyMap = new Map();
Expand Down Expand Up @@ -52,12 +52,13 @@ export default class Iris {
throw new Error("Code could not be found in key map. Did you perform 'getDataRequest' before?");
}
const keys = this.codeKeyMap.get(code);
const { dataToTransport, keyToTransport } = encryptData(keys.keyOfHealthDepartment, data);
const { dataToTransport, keyToTransport, nonce } = encryptData(keys.keyOfHealthDepartment, data);
const response = await this.axiosInstance.post(`/data-submissions/${code}/contacts_events`, {
checkCode: [ getNameCheckHash(user.firstName, user.lastName), getBirthDateCheckHash(user.birthDate) ].filter(c => !!c),
secret: keyToTransport,
keyReferenz: keys.keyReferenz,
encryptedData: dataToTransport,
nonce
} as IrisContactsEventsSubmissionDTO);
if (response.status !== 200) {
console.error('IRIS Gateway responded the following data', response.data);
Expand Down
19 changes: 10 additions & 9 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import * as crypto from 'crypto';

export function encryptData(keyOfHealthDepartment: string, data): { dataToTransport: string; keyToTransport: string } {
const publicKey = crypto.createPublicKey(keyOfHealthDepartment);
const iv = crypto.randomBytes(16);
export function encryptData(
keyOfHealthDepartment: string,
data,
): { dataToTransport: string; keyToTransport: string; nonce: string } {
const nonce = crypto.randomBytes(16);
const key = crypto.randomBytes(32);
const cipher = crypto.createCipheriv('aes-256', key, iv);
const encryptedData = Buffer.concat([cipher.update(JSON.stringify(data), 'utf8'), cipher.final()]);
const encryptedKey = crypto.publicEncrypt(
{ key: publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha3' },
key,
);
const cipher = crypto.createCipheriv('AES-256-CBC', key, nonce);
const dataString = JSON.stringify(data);
const encryptedData = Buffer.concat([cipher.update(dataString, 'utf8'), cipher.final()]);
const encryptedKey = crypto.publicEncrypt({ key: keyOfHealthDepartment }, key);
return {
dataToTransport: encryptedData.toString('base64'),
keyToTransport: encryptedKey.toString('base64'),
nonce: nonce.toString('base64'),
};
}
1 change: 1 addition & 0 deletions src/types/dto/IrisContactsEventsSubmissionDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export default interface IrisContactsEventsSubmissionDTO {
secret: string;
keyReferenz: string;
encryptedData: string;
nonce: string;
}

0 comments on commit 936fb10

Please sign in to comment.