/** * Verifies the supplied signed DGC. If verification is successful the method returns the contained HCERT * (eu_hcert_v1) in its binary representation. * * @throws SignatureException * if signature validation fails * @throws CertificateExpiredException * if the DGC has expired */ public byte[] Verify(byte[] signedDGC, SignedDGC vacProof) { CoseSign1_Object obj = CoseSign1_Object.Decode(signedDGC); byte[] kid = obj.GetKeyIdentifier(); string country = obj.GetCwt().GetIssuer(); vacProof.IssuingCountry = country; if (kid == null && country == null) { throw new Exception("Signed object does not contain kid or country - cannot find certificate"); } List <AsymmetricKeyParameter> certs = this.certificateProvider.GetCertificates(country, kid); foreach (AsymmetricKeyParameter cert in certs) { Console.WriteLine("Attempting HCERT signature verification using certificate");// '{0}'", cert.Subject);//getSubjectX500Principal().getName()) ; try { byte[] key = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(cert).GetEncoded(); obj.VerifySignature(key); Console.WriteLine("HCERT signature verification succeeded using certificate");// '{0}'", cert.Subject); //getSubjectX500Principal().getName()); } catch (Exception e) { Console.WriteLine("HCERT signature verification failed using certificate '{0}' - {1}", cert, e.Message, e); continue; } // OK, before we are done - let's ensure that the HCERT hasn't expired. CWT cwt = obj.GetCwt(); DateTime expiration = cwt.GetExpiration(); if (expiration != null) { if (DateTime.UtcNow.CompareTo(expiration) >= 0) { throw new CertificateExpiredException("Signed HCERT has expired"); } } else { Console.WriteLine("Signed HCERT did not contain an expiration time - assuming it is valid"); } vacProof.ExpirationDate = expiration; vacProof.IssuedDate = cwt.GetIssuedAt(); // OK, everything looks fine - return the DGC return(cwt.GetDgcV1()); } if (certs.Count <= 0) { throw new Exception("No signer certificates could be found"); } else { throw new Exception("Signature verification failed for all attempted keys"); } }
private void UpdateFields(String scanResult) { try { if (scanResult != null) { var result = VerificationService.VerifyData(scanResult); if (result != null) { SignedDGC proof = result; if (proof != null) { Subject = new Models.SubjectModel { Name = proof.Dgc.Sub.Fn + " " + proof.Dgc.Sub.Gn, ConvertDateOfBirth = proof.Dgc.Sub.Dob, Identifier = proof.Dgc.Sub.Id[0].I }; Signature = new Models.SignatureModel { IssuedDate = proof.IssuedDate, ExpirationDate = proof.ExpirationDate, IssuerCountry = proof.IssuingCountry }; bool vaccinated = false; if (proof.Dgc.Vac != null) { foreach (Vac vac in proof.Dgc.Vac) { AddCertificate(new Models.VaccineCertModel { Type = Models.CertType.VACCINE, Adm = vac.Adm, Aut = vac.Aut, Cou = vac.Cou, Dat = vac.Dat, Dis = vac.Dis, Lot = vac.Lot, Mep = vac.Mep, Seq = vac.Seq, Tot = vac.Tot, Vap = vac.Vap }); vaccinated = true; } } bool tested = false; if (proof.Dgc.Tst != null) { foreach (Tst tst in proof.Dgc.Tst) { AddCertificate(new Models.TestCertModel { Type = Models.CertType.TEST, Cou = tst.Cou, Dis = tst.Dis, Dtr = Models.TestCertModel.ConvertFromSecondsEpoc(tst.Dtr), Dts = Models.TestCertModel.ConvertFromSecondsEpoc(tst.Dts), Fac = tst.Fac, Ori = tst.Ori, Res = tst.Res, Typ = tst.Typ, Tna = tst.Tna, Tma = tst.Tma }); tested = true; } } bool recovered = false; if (proof.Dgc.Rec != null) { foreach (Rec rec in proof.Dgc.Rec) { AddCertificate(new Models.RecoveredCertModel { Type = Models.CertType.RECOVERED, Cou = rec.Cou, Dat = rec.Dat, Dis = rec.Dis }); recovered = true; } } if (vaccinated) { ResultText = AppResources.VaccinatedText; IsResultOK = true; } else if (tested) { ResultText = AppResources.TestedText; IsResultOK = true; } else if (recovered) { ResultText = AppResources.RecoveredText; IsResultOK = true; } else { ResultText = AppResources.MissingDataText; IsResultOK = false; } } else { ResultText = AppResources.ErrorReadingText; IsResultOK = false; } } else { ResultText = AppResources.ErrorReadingText + ", " + scanResult; IsResultOK = false; } } } catch (Exception ex) { ResultText = AppResources.ErrorReadingText + ", " + ex.Message; IsResultOK = false; } }
private void UpdateFields(String scanResult) { try { if (scanResult != null) { var result = VerificationService.VerifyData(scanResult); if (result != null) { SignedDGC proof = result; if (proof != null) { Subject = new Models.SubjectModel { Name = proof.Dgc.Nam.Fn + " " + proof.Dgc.Nam.Gn, ConvertDateOfBirth = proof.Dgc.Dob, Identifier = ""/*proof.Dgc.Sub.Id[0].I*/ }; Signature = new Models.SignatureModel { IssuedDate = proof.IssuedDate, ExpirationDate = proof.ExpirationDate, IssuerCountry = proof.IssuingCountry }; bool vaccinated = false; if (proof.Dgc.V != null && proof.Dgc.V.Length > 0) { foreach (VElement vac in proof.Dgc.V) { AddCertificate(new Models.VaccineCertModel { Type = Models.CertType.VACCINE, Tg = CodeMapperUtil.GetDiseaseAgentTargeted(vac.Tg), Vp = CodeMapperUtil.GetVaccineOrProphylaxis(vac.Vp), Mp = CodeMapperUtil.GetVaccineMedicalProduct(vac.Mp), Ma = CodeMapperUtil.GetMarketingAuthHolder(vac.Ma), Dn = vac.Dn, Sd = vac.Sd, Dt = vac.Dt, Co = vac.Co, Is = vac.Is, Ci = vac.Ci }); vaccinated = true; } } bool tested = false; if (proof.Dgc.T != null && proof.Dgc.T.Length > 0) { foreach (TElement tst in proof.Dgc.T) { AddCertificate(new Models.TestCertModel { Type = Models.CertType.TEST, Tg = tst.Tg.ToString(), Tt = tst.Tt, Nm = tst.Nm, Sc = tst.Sc, /*Models.TestCertModel.ConvertFromSecondsEpoc(tst.Sc),*/ Dr = tst.Dr, /*Models.TestCertModel.ConvertFromSecondsEpoc(tst.Dr),*/ Tr = CodeMapperUtil.GetTestResult(tst.Tr), Tc = tst.Tc, Co = tst.Co, Is = tst.Is, Ci = tst.Ci }); tested = true; } } bool recovered = false; if (proof.Dgc.R != null && proof.Dgc.R.Length > 0) { foreach (RElement rec in proof.Dgc.R) { AddCertificate(new Models.RecoveredCertModel { Type = Models.CertType.RECOVERED, Tg = CodeMapperUtil.GetDiseaseAgentTargeted(rec.Tg), Fr = rec.Fr, Co = rec.Co, Is = rec.Is, Df = rec.Df, Du = rec.Du, Ci = rec.Ci }); recovered = true; } } if (vaccinated) { ResultText = AppResources.VaccinatedText; IsResultOK = true; } else if (tested) { ResultText = AppResources.TestedText; IsResultOK = true; } else if (recovered) { ResultText = AppResources.RecoveredText; IsResultOK = true; } else { ResultText = AppResources.MissingDataText; IsResultOK = false; } } else { ResultText = AppResources.ErrorReadingText; IsResultOK = false; } } else { ResultText = AppResources.ErrorReadingText + ", " + scanResult; IsResultOK = false; } } } catch (Exception ex) { ResultText = AppResources.ErrorReadingText + ", " + ex.Message; IsResultOK = false; } }