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( .testTarget(
name: "SecretKitTests", name: "SecretKitTests",
dependencies: ["SecretKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"], dependencies: ["SecretKit", "SecretAgentKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
swiftSettings: swiftSettings, swiftSettings: swiftSettings,
), ),
.target( .target(

View File

@ -1,6 +1,7 @@
import Foundation import Foundation
import Testing import Testing
@testable import SecretKit @testable import SecretKit
@testable import SecretAgentKit
@testable import SmartCardSecretKit @testable import SmartCardSecretKit
@Suite struct OpenSSHSignatureWriterTests { @Suite struct OpenSSHSignatureWriterTests {
@ -59,50 +60,23 @@ private extension OpenSSHSignatureWriterTests {
enum ParseError: Error { enum ParseError: Error {
case eof case eof
case invalidLength
case invalidAlgorithm 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) { 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() // Prefix
guard outerLength == (openSSHSignedData.count - 4) else { throw ParseError.invalidLength } _ = try reader.readNextBytes(as: UInt32.self)
let algorithm = try reader.readString() let algorithm = try reader.readNextChunkAsString()
guard String(data: algorithm, encoding: .utf8) == "ecdsa-sha2-nistp256" else { guard algorithm == "ecdsa-sha2-nistp256" else {
throw ParseError.invalidAlgorithm throw ParseError.invalidAlgorithm
} }
let signatureChunk = try reader.readString() let sigReader = try reader.readNextChunkAsSubReader()
var sigReader = Reader(data: signatureChunk) let r = try sigReader.readNextChunk()
let r = try sigReader.readString() let s = try sigReader.readNextChunk()
let s = try sigReader.readString()
return (r, s) return (r, s)
} }