2017-01-25 01:26:01 +03:00
|
|
|
//
|
|
|
|
// AuthenticationResponse.swift
|
2017-07-19 20:54:47 +03:00
|
|
|
// SoftU2F
|
2017-01-25 01:26:01 +03:00
|
|
|
//
|
|
|
|
// Created by Benjamin P Toews on 9/14/16.
|
|
|
|
//
|
|
|
|
|
2017-02-07 21:34:05 +03:00
|
|
|
import Foundation
|
2017-02-03 04:39:26 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
public struct AuthenticationResponse: RawConvertible {
|
2018-09-25 01:57:50 +03:00
|
|
|
public let body: Data
|
|
|
|
public let trailer: ResponseStatus
|
2017-07-19 22:21:00 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
public var userPresence: UInt8 {
|
|
|
|
return body[0]
|
|
|
|
}
|
2017-02-07 21:34:05 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
public var counter: UInt32 {
|
|
|
|
let lowerBound = MemoryLayout<UInt8>.size
|
|
|
|
let upperBound = lowerBound + MemoryLayout<UInt32>.size
|
|
|
|
let data = body.subdata(in: lowerBound..<upperBound)
|
2017-07-19 22:21:00 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
return data.withUnsafeBytes { (ptr: UnsafePointer<UInt32>) -> UInt32 in
|
|
|
|
return ptr.pointee.bigEndian
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 04:39:26 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
public var signature: Data {
|
|
|
|
let lowerBound = MemoryLayout<UInt8>.size + MemoryLayout<UInt32>.size
|
|
|
|
let upperBound = body.count
|
|
|
|
return body.subdata(in: lowerBound..<upperBound)
|
|
|
|
}
|
|
|
|
|
|
|
|
public init(userPresence: UInt8, counter: UInt32, signature: Data) {
|
|
|
|
let writer = DataWriter()
|
2017-01-25 01:26:01 +03:00
|
|
|
writer.write(userPresence)
|
|
|
|
writer.write(counter)
|
|
|
|
writer.writeData(signature)
|
2017-07-19 22:21:00 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
body = writer.buffer
|
|
|
|
trailer = .NoError
|
2017-01-25 01:26:01 +03:00
|
|
|
}
|
2017-02-08 03:25:44 +03:00
|
|
|
}
|
2017-02-03 04:39:26 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
extension AuthenticationResponse: Response {
|
2018-09-25 01:57:50 +03:00
|
|
|
public init(body: Data, trailer: ResponseStatus) {
|
2017-02-08 03:25:44 +03:00
|
|
|
self.body = body
|
|
|
|
self.trailer = trailer
|
|
|
|
}
|
2017-07-19 22:21:00 +03:00
|
|
|
|
2018-09-25 01:57:50 +03:00
|
|
|
public func validateBody() throws {
|
2017-02-08 03:25:44 +03:00
|
|
|
// TODO: minimum signature size?
|
|
|
|
if body.count < MemoryLayout<UInt8>.size + MemoryLayout<UInt32>.size + 1 {
|
|
|
|
throw ResponseError.BadSize
|
2017-01-25 01:26:01 +03:00
|
|
|
}
|
2017-07-19 22:21:00 +03:00
|
|
|
|
2017-02-08 03:25:44 +03:00
|
|
|
if trailer != .NoError {
|
|
|
|
throw ResponseError.BadStatus
|
2017-01-27 01:14:27 +03:00
|
|
|
}
|
2017-01-25 01:26:01 +03:00
|
|
|
}
|
2017-01-25 19:53:01 +03:00
|
|
|
}
|