mirror of
https://github.com/maxgoedjen/secretive.git
synced 2026-01-06 16:42:01 +01:00
Cleanup and consolidation
This commit is contained in:
parent
c5de2a9d5d
commit
2807ca33ad
@ -43,7 +43,7 @@ let package = Package(
|
||||
),
|
||||
.testTarget(
|
||||
name: "SecretKitTests",
|
||||
dependencies: ["SecretKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
|
||||
dependencies: ["SecretKit", "SecretAgentKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
|
||||
swiftSettings: swiftSettings,
|
||||
),
|
||||
.target(
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import SecretKit
|
||||
@testable import SecretAgentKit
|
||||
@testable import SmartCardSecretKit
|
||||
|
||||
@Suite struct OpenSSHSignatureWriterTests {
|
||||
@ -59,50 +60,23 @@ private extension OpenSSHSignatureWriterTests {
|
||||
|
||||
enum ParseError: Error {
|
||||
case eof
|
||||
case invalidLength
|
||||
case invalidAlgorithm
|
||||
}
|
||||
|
||||
struct Reader {
|
||||
var data: Data
|
||||
var offset: Int = 0
|
||||
|
||||
mutating func readU32() throws -> Int {
|
||||
guard offset + 4 <= data.count else { throw ParseError.eof }
|
||||
let value = data[offset..<offset + 4].reduce(0 as UInt32) { ($0 << 8) | UInt32($1) }
|
||||
offset += 4
|
||||
return Int(value)
|
||||
}
|
||||
|
||||
mutating func readBytes(count: Int) throws -> Data {
|
||||
guard count >= 0 else { throw ParseError.invalidLength }
|
||||
guard offset + count <= data.count else { throw ParseError.eof }
|
||||
let out = data[offset..<offset + count]
|
||||
offset += count
|
||||
return Data(out)
|
||||
}
|
||||
|
||||
mutating func readString() throws -> Data {
|
||||
let length = try readU32()
|
||||
return try readBytes(count: length)
|
||||
}
|
||||
}
|
||||
|
||||
func parseEcdsaSignatureMpints(from openSSHSignedData: Data) throws -> (r: Data, s: Data) {
|
||||
var reader = Reader(data: openSSHSignedData)
|
||||
let reader = OpenSSHReader(data: openSSHSignedData)
|
||||
|
||||
let outerLength = try reader.readU32()
|
||||
guard outerLength == (openSSHSignedData.count - 4) else { throw ParseError.invalidLength }
|
||||
// Prefix
|
||||
_ = try reader.readNextBytes(as: UInt32.self)
|
||||
|
||||
let algorithm = try reader.readString()
|
||||
guard String(data: algorithm, encoding: .utf8) == "ecdsa-sha2-nistp256" else {
|
||||
let algorithm = try reader.readNextChunkAsString()
|
||||
guard algorithm == "ecdsa-sha2-nistp256" else {
|
||||
throw ParseError.invalidAlgorithm
|
||||
}
|
||||
|
||||
let signatureChunk = try reader.readString()
|
||||
var sigReader = Reader(data: signatureChunk)
|
||||
let r = try sigReader.readString()
|
||||
let s = try sigReader.readString()
|
||||
let sigReader = try reader.readNextChunkAsSubReader()
|
||||
let r = try sigReader.readNextChunk()
|
||||
let s = try sigReader.readNextChunk()
|
||||
return (r, s)
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user