Cleanup and consolidation

This commit is contained in:
Max Goedjen 2025-12-14 10:16:07 -08:00
parent c5de2a9d5d
commit 2807ca33ad
No known key found for this signature in database
2 changed files with 10 additions and 36 deletions

View File

@ -43,7 +43,7 @@ let package = Package(
),
.testTarget(
name: "SecretKitTests",
dependencies: ["SecretKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
dependencies: ["SecretKit", "SecretAgentKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
swiftSettings: swiftSettings,
),
.target(

View File

@ -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)
}