This commit is contained in:
Gianmarco Pettinato 2021-09-25 12:08:47 +02:00
commit b2630bab9b
8 changed files with 1509 additions and 187 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
node_modules node_modules
rules.json
certificate_collection.json

File diff suppressed because one or more lines are too long

1577
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,8 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --exec \"npx ts-node\" ./src/server.ts"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -16,6 +17,7 @@
"dcc-utils": "^0.2.0", "dcc-utils": "^0.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"jsrsasign": "^10.4.0", "jsrsasign": "^10.4.0",
"nodemon": "^2.0.13",
"typescript": "^4.4.3" "typescript": "^4.4.3"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1 +1 @@
{"rules":[{"name":"vaccine_end_day_complete","type":"EU/1/20/1525","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/20/1525","value":"15"},{"name":"vaccine_end_day_not_complete","type":"EU/1/20/1525","value":"365"},{"name":"vaccine_start_day_not_complete","type":"EU/1/20/1525","value":"15"},{"name":"vaccine_end_day_complete","type":"EU/1/21/1529","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/21/1529","value":"0"},{"name":"vaccine_end_day_not_complete","type":"EU/1/21/1529","value":"84"},{"name":"vaccine_start_day_not_complete","type":"EU/1/21/1529","value":"15"},{"name":"vaccine_end_day_complete","type":"EU/1/20/1507","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/20/1507","value":"0"},{"name":"vaccine_end_day_not_complete","type":"EU/1/20/1507","value":"42"},{"name":"vaccine_start_day_not_complete","type":"EU/1/20/1507","value":"15"},{"name":"vaccine_end_day_complete","type":"EU/1/20/1528","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/20/1528","value":"0"},{"name":"vaccine_end_day_not_complete","type":"EU/1/20/1528","value":"42"},{"name":"vaccine_start_day_not_complete","type":"EU/1/20/1528","value":"15"},{"name":"rapid_test_start_hours","type":"GENERIC","value":"0"},{"name":"rapid_test_end_hours","type":"GENERIC","value":"48"},{"name":"molecular_test_start_hours","type":"GENERIC","value":"0"},{"name":"molecular_test_end_hours","type":"GENERIC","value":"48"},{"name":"recovery_cert_start_day","type":"GENERIC","value":"0"},{"name":"recovery_cert_end_day","type":"GENERIC","value":"180"},{"name":"ios","type":"APP_MIN_VERSION","value":"1.1.0"},{"name":"android","type":"APP_MIN_VERSION","value":"1.1.0"}],"lastupdateDate":1632564259533} {"rules":[{"name":"vaccine_end_day_complete","type":"EU/1/20/1525","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/20/1525","value":"15"},{"name":"vaccine_end_day_not_complete","type":"EU/1/20/1525","value":"365"},{"name":"vaccine_start_day_not_complete","type":"EU/1/20/1525","value":"15"},{"name":"vaccine_end_day_complete","type":"EU/1/21/1529","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/21/1529","value":"0"},{"name":"vaccine_end_day_not_complete","type":"EU/1/21/1529","value":"84"},{"name":"vaccine_start_day_not_complete","type":"EU/1/21/1529","value":"15"},{"name":"vaccine_end_day_complete","type":"EU/1/20/1507","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/20/1507","value":"0"},{"name":"vaccine_end_day_not_complete","type":"EU/1/20/1507","value":"42"},{"name":"vaccine_start_day_not_complete","type":"EU/1/20/1507","value":"15"},{"name":"vaccine_end_day_complete","type":"EU/1/20/1528","value":"365"},{"name":"vaccine_start_day_complete","type":"EU/1/20/1528","value":"0"},{"name":"vaccine_end_day_not_complete","type":"EU/1/20/1528","value":"42"},{"name":"vaccine_start_day_not_complete","type":"EU/1/20/1528","value":"15"},{"name":"rapid_test_start_hours","type":"GENERIC","value":"0"},{"name":"rapid_test_end_hours","type":"GENERIC","value":"48"},{"name":"molecular_test_start_hours","type":"GENERIC","value":"0"},{"name":"molecular_test_end_hours","type":"GENERIC","value":"48"},{"name":"recovery_cert_start_day","type":"GENERIC","value":"0"},{"name":"recovery_cert_end_day","type":"GENERIC","value":"180"},{"name":"ios","type":"APP_MIN_VERSION","value":"1.1.0"},{"name":"android","type":"APP_MIN_VERSION","value":"1.1.0"}],"lastupdateDate":1632564259533}

View File

@ -4,7 +4,7 @@ import cors from 'cors';
const app = express(); const app = express();
app.set('port', process.env.PORT || 3001); app.set('port', process.env.PORT || 5050);
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded()); app.use(express.urlencoded());

View File

@ -7,9 +7,9 @@ export class CertificateDownloader{
private readonly updateApi = '/v1/dgc/signercertificate/update' private readonly updateApi = '/v1/dgc/signercertificate/update'
private readonly statusApi = '/v1/dgc/signercertificate/status' private readonly statusApi = '/v1/dgc/signercertificate/status'
private readonly keyStorage = './cerificate_collection.json'; private readonly keyStorage = './cerificate_collection.json';
// private readonly timeSpan = 86400000; private readonly timeSpan = 86400000;
private readonly timeSpan = 1; // private readonly timeSpan = 1;
private cerficateCollection:unknown = {}; private certificatesCollection:{kid:string,certificate:string}[] = [];
private currentValidKids:string[] = []; private currentValidKids:string[] = [];
public async getCertificates(): Promise<unknown> { public async getCertificates(): Promise<unknown> {
@ -22,9 +22,9 @@ export class CertificateDownloader{
if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){ if(savedData.lastupdateDate == null || Date.now() - savedData?.lastupdateDate > this.timeSpan){
await this.getAllCertificate(); await this.getAllCertificate();
} else { } else {
this.cerficateCollection = savedData.certificates; this.certificatesCollection = savedData.certificates;
} }
return this.cerficateCollection; return this.certificatesCollection;
} catch (error) { } catch (error) {
console.log(error); console.log(error);
if(error.errno == -2){ if(error.errno == -2){
@ -42,54 +42,52 @@ export class CertificateDownloader{
// return CertificateDownloader.instance; // return CertificateDownloader.instance;
// } // }
// async getAllCertificate(): Promise<void> {
// this.cerficateCollection = {};
// const response:AxiosResponse<JSON> = (await axios.get('https://raw.githubusercontent.com/lovasoa/sanipasse/master/src/assets/Digital_Green_Certificate_Signing_Keys.json'));
// if(response.status == 200){
// console.log(response.data);
// this.cerficateCollection = response.data;
// console.log(response);
// const lastupdateDate = Date.now();
// const file = await fs.open(this.keyStorage,'w');
// file.writeFile(JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}));
// console.log(this.cerficateCollection);
// await file.close();
// }else{
// throw new Error(response.statusText);
// }
// }
async getAllCertificate(): Promise<void> { async getAllCertificate(): Promise<void> {
this.cerficateCollection = {}; let exit = false;
const response:AxiosResponse<JSON> = (await axios.get('https://raw.githubusercontent.com/lovasoa/sanipasse/master/src/assets/Digital_Green_Certificate_Signing_Keys.json')); let headers = {};
if(response.status == 200){ this.certificatesCollection = [];
console.log(response.data); while(!exit){
this.cerficateCollection = response.data; const response:AxiosResponse = await axios.get(this.baseUrl+this.updateApi,{headers});
console.log(response); headers = {'X-RESUME-TOKEN': response.headers['x-resume-token']};
const lastupdateDate = Date.now(); const currentKid:string = response.headers['x-kid'];
const file = await fs.open(this.keyStorage,'w'); if(this.currentValidKids.includes(currentKid)){
file.writeFile(JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate})); // console.log('=========AGGIUNG===========');
console.log(this.cerficateCollection); const cert = {kid:currentKid, certificate: response.data};
await file.close(); // console.log(cert);
}else{ this.certificatesCollection.push(cert);
throw new Error(response.statusText); }
exit = (response.status !== 200);
} }
const lastupdateDate = Date.now();
const file = await fs.open(this.keyStorage,'w');
file.writeFile(JSON.stringify({'certificates':this.certificatesCollection, lastupdateDate}));
console.log(this.certificatesCollection);
await file.close();
} }
// async getAllCertificate(): Promise<void> { async updateKids(): Promise<void> {
// let exit = false; try {
// let headers = {}; const resp = await axios.get(this.baseUrl+this.statusApi);
// this.cerficateCollection = []; this.currentValidKids = await resp.data as string[];
// while(!exit){ } catch (error) {
// // const response = await fetch(this.baseUrl+this.updateApi,{headers}); console.log('could not get keyChild ', error);
// const response:AxiosResponse = await axios.get(this.baseUrl+this.updateApi,{headers}); }
// // console.log(response.headers); }
// headers = {'X-RESUME-TOKEN': response.headers['x-resume-token']};
// const currentKid:string = response.headers['x-kid'];
// if(this.currentValidKids.includes(currentKid)){
// // console.log('=========AGGIUNG===========');
// const cert = `-----BEGIN CERTIFICATE-----${response.data}-----END CERTIFICATE-----`;
// // console.log(cert);
// this.cerficateCollection.push(cert);
// }
// exit = (response.status !== 200);
// }
// const lastupdateDate = Date.now();
// const file = await fs.open(this.keyStorage,'w');
// file.writeFile(JSON.stringify({'certificates':this.cerficateCollection, lastupdateDate}));
// console.log(this.cerficateCollection);
// await file.close();
// }
// async updateKids(): Promise<void> {
// try {
// const resp = await axios.get(this.baseUrl+this.statusApi);
// this.currentValidKids = await resp.data as string[];
// } catch (error) {
// console.log('could not get keyChild ', error);
// }
// }
} }

View File

@ -25,9 +25,10 @@ export default class Verifier {
async checkCertificate(certificate:string): Promise<unknown>{ async checkCertificate(certificate:string): Promise<unknown>{
console.log(certificate); console.log(certificate);
const dcc = await DCC.fromRaw(certificate); const dcc = await DCC.fromRaw(certificate);
console.log(dcc.payload);
let result = await dcc.checkSignatureWithKeysList(await this.certDownloader.getCertificates()); let result = await dcc.checkSignatureWithKeysList(await this.certDownloader.getCertificates());
const vaccineVerifier = new VaccineVerifier(await this.ruleDownloader.getRules()); // const vaccineVerifier = new VaccineVerifier(await this.ruleDownloader.getRules());
result = {signature: result, valid:vaccineVerifier.checkCertifcate(dcc)}; // result = {signature: result, valid:vaccineVerifier.checkCertifcate(dcc)};
console.log(result); console.log(result);
return result; return result;
} }