forked from diggsweden/DGCValidator
-
Notifications
You must be signed in to change notification settings - Fork 7
/
VerificationService.cs
101 lines (89 loc) · 3.29 KB
/
VerificationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.IO;
using System.Text;
using DGCValidator.Services.CWT;
using DGCValidator.Services;
using DGCValidator.Services.DGC;
using DGCValidator.Services.DGC.V1;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using PeterO.Cbor;
namespace DGCValidator.Services
{
/**
* A Crypto support class for the reading of the European Digital Green Certificate.
*
* @author Henrik Bengtsson (henrik@sondaica.se)
* @author Martin Lindström (martin@idsec.se)
* @author Henric Norlander (extern.henric.norlander@digg.se)
*/
public class VerificationService
{
public VerificationService()
{
}
public static SignedDGC VerifyData(String codeData)
{
try {
// The base45 encoded data shoudl begin with HC1
if( codeData.StartsWith("HC1:"))
{
string base45CodedData = codeData.Substring(4);
// Base 45 decode data
byte[] base45DecodedData = Base45Decoding(Encoding.GetEncoding("UTF-8").GetBytes(base45CodedData));
// zlib decompression
byte[] uncompressedData = ZlibDecompression(base45DecodedData);
SignedDGC vacProof = new SignedDGC();
// Sign and encrypt data
byte[] signedData = VerifySignedData(uncompressedData, vacProof);
// Get json from CBOR representation of ProofCode
EU_DGC eU_DGC = GetVaccinationProofFromCbor(signedData);
vacProof.Dgc = eU_DGC;
return vacProof;
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
throw e;
}
return null;
}
protected static byte[] ZlibDecompression(byte[] compressedData)
{
if( compressedData[0] == 0x78 )
{
var outputStream = new MemoryStream();
using (var compressedStream = new MemoryStream(compressedData))
using (var inputStream = new InflaterInputStream(compressedStream))
{
inputStream.CopyTo(outputStream);
outputStream.Position = 0;
return outputStream.ToArray();
}
}
else
{
// The data is not compressed
return compressedData;
}
}
protected static byte[] VerifySignedData(byte[] signedData, SignedDGC vacProof)
{
DGCVerifier verifier = new DGCVerifier(App.CertificateManager);
return verifier.Verify(signedData, vacProof);
}
protected static byte[] Base45Decoding(byte[] encodedData)
{
byte[] uncodedData = Base45.Decode(encodedData);
return uncodedData;
}
protected static EU_DGC GetVaccinationProofFromCbor(byte[] cborData)
{
CBORObject cbor = CBORObject.DecodeFromBytes(cborData, CBOREncodeOptions.Default);
string json = cbor.ToJSONString();
EU_DGC vacProof = EU_DGC.FromJson(cbor.ToJSONString());
return vacProof;
}
}
}