Or navigate to your Xcode project then select Swift Packages, click the “+” icon and search for EUDCCKit.
Usage
The EUDCCKit Swift Package is made of four distinct libraries to decode, verify and validate an EU Digital COVID Certificate.
EUDCC
The EUDCC library contains the model definition of the EU Digital COVID Certificate
import EUDCC
// The EU Digital COVID Certificate model
let eudcc: EUDCC
// Access content of EUDCC
switch eudcc.content {
case .vaccination(let vaccination):
print("Vaccination", vaccination)
case .test(let test):
print("Test", test)
case .recovery(let recovery):
print("Recovery", recovery)
}
The EUDCCDecoder library provides an EUDCCDecoder object which is capabale of decoding a Base-45 string reperesentation of the EU Digital COVID Certificate which is mostly embedded in a QR-Code.
import EUDCCDecoder
// Initialize an EUDCCDecoder
let decoder = EUDCCDecoder()
// The Base-45 encoded EU Digital COVID Certificate from a QR-Code
let qrCodeContent = "HC1:..."
// Decode EUDCC from QR-Code
let decodingResult = decoder.decode(from: qrCodeContent)
// Switch on decoding result
switch decodingResult {
case .success(let eudcc):
// Successfully decoded Digital COVID Certificate
print("EU Digital COVID Certificate", eudcc)
case .failure(let decodingError):
// Decoding failed with error
print("Failed to decode EUDCC", decodingError)
}
The EUDCCVerifier library provides an EUDCCVerifier object which can be used to verify the cryptographic signature of the EU Digital COVID Certificate.
import EUDCCVerifier
// Initialize an EUDDCCVerifier
let verifier = EUDCCVerifier(
trustService: EUCentralEUDCCTrustService()
)
// Verify EU Digital COVID Certificate
verifier.verify(eudcc: eudcc) { verificationResult in
// Switch on verification result
switch verificationResult {
case .success(let trustCertificate):
print("Cryptographically valid", trustCertificate)
case .invald:
print("Invalid EUDCC")
case .failure(let error):
print("Error occured during verification", error)
}
}
Beside the content property of an EUDCC you can make use of the following convenience properties to check if the EUDCC contains a vaccination, test or recovery object.
import EUDCC
// Vaccination
let vaccination: EUDCC.Vaccination? = eudcc.vaccination
// Test
let test: EUDCC.Test? = eudcc.test
// Recovery
let recovery: EUDCC.Recovery? = eudcc.recovery
Well-Known-Value
Each of the following objects are exposing a WellKnownValue enumeration which can be used to retrieve more detailed information about a certain value:
import EUDCC
let vaccineMedicinalProduct: EUDCC.Vaccination.VaccineMedicinalProduct
// Switch on WellKnownValue of VaccineMedicinalProduct
switch vaccineMedicinalProduct.wellKnownValue {
case .covid19VaccineModerna:
break
case .vaxzevria:
break
default:
break
}
Encoding
The EUDCC contains two properties cryptographicSignature and base45Representation which are convenience objects that are not an offical part of the EU Digital COVID Certificate JSON Schema.
If you wish to skip those properties when encoding an EUDCC you can set the following userInfo configuration to a JSONEncoder.
The EUDCCDecoder supports decoding a Base-45 encoded String and Data object.
import EUDCCDecoder
let eudccDecoder = EUDCCDecoder()
// Decode from Base-45 encoded String
let eudccBase45EncodedString: String
let stringDecodingResult = eudccDecoder.decode(
from: eudccBase45EncodedString
)
// Decode from Base-45 encoded Data
let eudccBase45EncodedData: Data
let dataDecodingResult = eudccDecoder.decode(
from: eudccBase45EncodedData
)
Convenience decoding
By importing the EUDCCDecoder library the EUDCC object will be extended with a static decode function.
import EUDCCDecoder
let decodingResult = EUDCC.decode(from: "HC1:...")
EUDCCVerifier
EUDCCTrustService
In order to verify an EUDCC the EUDCCVerifier needs to be instantiated with an instance of an EUDCCTrustService which is used to retrieve the trust certificates.
The EUDCCKit comes along with two pre defined EUDCCTrustService implementations:
EUCentralEUDCCTrustService
RobertKochInstituteEUDCCTrustService
If you wish to retrieve certificates from multiple EUDCCTrustService implementation you can make use of the GroupableEUDCCTrustService:
let trustService = GroupableEUDCCTrustService(
trustServices: [
EUCentralEUDCCTrustService(),
RobertKochInstituteEUDCCTrustService()
]
)
trustService.getTrustCertificates { certificates in
// ...
}
Convenience verification
By importing the EUDCCVerifier library the EUDCC object will be extended with a verify function.
import EUDCC
import EUDCCVerifier
let eudcc: EUDCC
eudcc.verify(
using: EUDCCVerifier(
trustService: EUCentralEUDCCTrustService()
)
) { verificationResult in
switch verificationResult {
case .success(let trustCertificate):
break
case .invald:
break
case .failure(let error):
break
}
}
EUDCCValidator
ValidationRule
An EUDCC can be validated by using an EUDCCValidator and a given EUDCC.ValidationRule. An EUDCC.ValidationRule can be initialized with a simple closure wich takes in an EUDCC and returns a Bool whether the validation succeed or failed.
import EUDCC
import EUDCCValidator
// Simple EUDCC ValidationRule instantiation
let validationRule = EUDCC.ValidationRule { eudcc in
// Process EUDCC and return Bool result
}
// EUDCC ValidationRule with Tag in order to uniquely identify a ValidationRule
let isVaccinationComplete = EUDCC.ValidationRule(
tag: "is-vaccination-complete"
) { eudcc in
eudcc.vaccination?.doseNumber == eudcc.vaccination?.totalSeriesOfDoses
}
The EUDCCKit comes along with many pre defined EUDCC.ValidationRule like the following ones.
import EUDCC
import EUDCCValidator
let eudcc: EUDCC
let validator = EUDCCValidator()
// Is fully immunized
validator.validate(
eudcc: eudcc,
rule: .isFullyImmunized(minimumDaysPast: 15)
)
// Is tested positive
validator.validate(
eudcc: eudcc,
rule: .isTestedPositive
)
Logical/Conditional operators
In order to create more complex rules each EUDCC.ValidationRule can be chained together by applying standard operators.
By importing the EUDCCValidator library the EUDCC object will be extended with a validate function.
import EUDCC
import EUDCCValidator
let eudcc: EUDCC
let validationRule = eudcc.validate(
rule: .isWellKnownVaccineMedicinalProduct
)
License
EUDCCKit
Copyright (c) 2021 Sven Tiigi sven.tiigi@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
EU Digital COVID Certificate Kit
A Swift Package to decode, verify and validate EU Digital COVID Certificates
for iOS, tvOS, watchOS and macOS
Disclaimer
Features
Installation
Swift Package Manager
To integrate using Apple’s Swift Package Manager, add the following as a dependency to your
Package.swift
:Or navigate to your Xcode project then select
Swift Packages
, click the “+” icon and search forEUDCCKit
.Usage
The
EUDCCKit
Swift Package is made of four distinct libraries to decode, verify and validate an EU Digital COVID Certificate.EUDCC
The
EUDCC
library contains the model definition of the EU Digital COVID CertificateEUDDCDecoder
The
EUDCCDecoder
library provides anEUDCCDecoder
object which is capabale of decoding a Base-45 string reperesentation of the EU Digital COVID Certificate which is mostly embedded in a QR-Code.EUDCCVerifier
The
EUDCCVerifier
library provides anEUDCCVerifier
object which can be used to verify the cryptographic signature of the EU Digital COVID Certificate.EUDCCValidator
The
EUDCCValidator
library provides anEUDCCValidator
object which can be used to validate the EU Digital COVID Certifiate based on given rules.Advanced
EUDCC
Content
Beside the
content
property of anEUDCC
you can make use of the following convenience properties to check if theEUDCC
contains a vaccination, test or recovery object.Well-Known-Value
Each of the following objects are exposing a
WellKnownValue
enumeration which can be used to retrieve more detailed information about a certain value:EUDCC.DiseaseAgentTargeted
EUDCC.Test.TestResult
EUDCC.Test.TestType
EUDCC.Vaccination.VaccineMarketingAuthorizationHolder
EUDCC.Vaccination.VaccineMedicinalProduct
EUDCC.Vaccination.VaccineOrProphylaxis
Encoding
The
EUDCC
contains two propertiescryptographicSignature
andbase45Representation
which are convenience objects that are not an offical part of the EU Digital COVID Certificate JSON Schema.If you wish to skip those properties when encoding an
EUDCC
you can set the followinguserInfo
configuration to aJSONEncoder
.EUDDCDecoder
Decoding
The
EUDCCDecoder
supports decoding a Base-45 encodedString
andData
object.Convenience decoding
By importing the
EUDCCDecoder
library theEUDCC
object will be extended with a staticdecode
function.EUDCCVerifier
EUDCCTrustService
In order to verify an
EUDCC
theEUDCCVerifier
needs to be instantiated with an instance of anEUDCCTrustService
which is used to retrieve the trust certificates.The
EUDCCKit
comes along with two pre definedEUDCCTrustService
implementations:EUCentralEUDCCTrustService
RobertKochInstituteEUDCCTrustService
If you wish to retrieve certificates from multiple
EUDCCTrustService
implementation you can make use of theGroupableEUDCCTrustService
:Convenience verification
By importing the
EUDCCVerifier
library theEUDCC
object will be extended with averify
function.EUDCCValidator
ValidationRule
An
EUDCC
can be validated by using anEUDCCValidator
and a givenEUDCC.ValidationRule
. AnEUDCC.ValidationRule
can be initialized with a simple closure wich takes in anEUDCC
and returns aBool
whether the validation succeed or failed.The
EUDCCKit
comes along with many pre definedEUDCC.ValidationRule
like the following ones.Logical/Conditional operators
In order to create more complex rules each
EUDCC.ValidationRule
can be chained together by applying standard operators.Convenience validation
By importing the
EUDCCValidator
library theEUDCC
object will be extended with avalidate
function.License