gematik / ref-OpenHealthCardKit

Controlling/Use-case framework for accessing smart cards of the telematic infrastructure. API Documentation: https://swiftpackageindex.com/gematik/ref-OpenHealthCardKit/main/documentation/healthcardaccess
Apache License 2.0
16 stars 6 forks source link

Issue reading EF.PD and EF.VD #24

Closed etjenymeraj closed 6 months ago

etjenymeraj commented 6 months ago

In the current project Im reading a lot of values from root folder such as gdo atr etc. This reading works just fine.

However when I switch to HCA folder (DF.HCA) and I use the already built command methods from the library to read (EF.PD) the data bytes I receive or the hex code is incomplete.

My EF.PD command printed

First PD command - cla: 00 ins: B0 p1: 82 p2: 00 ne: 256 Second PD command - cla: 00 ins: B0 p1: 00 p2: 08 ne: 65536

I tried running commands in this order but also just running second command directly. The result is the same.

Can you advise any potential solutions here?

etjenymeraj commented 6 months ago
func selectHcaCommand() -> CommandType {
    let applicationIdentifier = EgkFileSystem.DF.HCA.aid
    return HealthCardCommand.Select.selectFile(with: applicationIdentifier)
}
etjenymeraj commented 6 months ago
func readHCAPDCommandShort() throws -> CommandType {
    guard let shortFileIdentifier = EgkFileSystem.EF.hcaPD.sfid else { throw HealthCardCommandError.hcaPDUnavailable  }
    return try HealthCardCommand.Read.readFileCommand(with: shortFileIdentifier, ne: APDU.expectedLengthWildcardShort)
}

func readHCAPDCommand() throws -> CommandType {
    try HealthCardCommand.Read.readFileCommand(ne: APDU.expectedLengthWildcardExtended, offset: 2)
}
etjenymeraj commented 6 months ago
    try await card.selectHca()

    let hcaPDDataShort = try await card.readHCAPDShort()
    print("##########")
    print("NFC DATA PD Hex \(hcaPDDataShort!.hexString)")
    print("##########")
    print("NFC DATA PD Bytes \([UInt8](hcaPDDataShort!))")

    let hcaPDData     = try await card.readHCAPD()
    print("##########")
    print("NFC DATA PD Hex \(hcaPDData!.hexString)")
    print("##########")
    print("NFC DATA PD Bytes \([UInt8](hcaPDData!))")
etjenymeraj commented 6 months ago

extension Data { var hexString: String { Array(self) .map { $0.hexString } .joined(separator: "") } }

etjenymeraj commented 6 months ago

extension UInt8 { var hexString: String { var result = String(self, radix: 16, uppercase: true)

    while result.count < 2 {
        result = "0" + result
    }

    return result
}

}

etjenymeraj commented 6 months ago
func selectHca() async throws {
    let command = selectHcaCommand()
    return try await send(command: command)
}

func readHCAPDShort() async throws -> Data? {
    let command = try readHCAPDCommandShort()
    return try await send(command: command)
}

func readHCAPD() async throws -> Data? {
    let command = try readHCAPDCommand()
    return try await send(command: command)
}
etjenymeraj commented 6 months ago

Leaving this for anyone else here. The above commands work fine. The format you get is gzip. So you need to decompress the gzip. Then you get data which you need to convert to string. Not using utf 8 but rather

String(data: unzippedData!, encoding: .windowsCP1250))")

priska96 commented 1 month ago

@etjenymeraj Hey can I ask how you decoded the hexString that is returned from the PD file?