forked from apple/swift-nio-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNIOSSHSignature.swift
More file actions
373 lines (320 loc) · 15.7 KB
/
NIOSSHSignature.swift
File metadata and controls
373 lines (320 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@preconcurrency import Crypto
import Foundation
import NIOCore
import NIOFoundationCompat
/// A representation of an SSH signature.
///
/// This type is intentionally highly opaque: we don't expect users to do anything with this directly.
/// Instead, we expect them to work with other APIs available on our opaque types.
public struct NIOSSHSignature: Hashable, Sendable {
internal var backingSignature: BackingSignature
internal init(backingSignature: BackingSignature) {
self.backingSignature = backingSignature
}
}
extension NIOSSHSignature {
/// The various signature types that can be used with NIOSSH.
internal enum BackingSignature: Sendable {
case ed25519(RawBytes) // There is no structured Signature type for Curve25519, and we may want Data or ByteBuffer.
case ecdsaP256(P256.Signing.ECDSASignature)
case ecdsaP384(P384.Signing.ECDSASignature)
case ecdsaP521(P521.Signing.ECDSASignature)
internal enum RawBytes {
case byteBuffer(ByteBuffer)
case data(Data)
}
}
/// The prefix of an Ed25519 signature.
fileprivate static let ed25519SignaturePrefix = "ssh-ed25519".utf8
/// The prefix of a P256 ECDSA public key.
fileprivate static let ecdsaP256SignaturePrefix = "ecdsa-sha2-nistp256".utf8
/// The prefix of a P384 ECDSA public key.
fileprivate static let ecdsaP384SignaturePrefix = "ecdsa-sha2-nistp384".utf8
/// The prefix of a P521 ECDSA public key.
fileprivate static let ecdsaP521SignaturePrefix = "ecdsa-sha2-nistp521".utf8
}
extension NIOSSHSignature.BackingSignature.RawBytes: Equatable {
public static func == (lhs: NIOSSHSignature.BackingSignature.RawBytes, rhs: NIOSSHSignature.BackingSignature.RawBytes) -> Bool {
switch (lhs, rhs) {
case (.byteBuffer(let lhs), .byteBuffer(let rhs)):
return lhs == rhs
case (.data(let lhs), .data(let rhs)):
return lhs == rhs
case (.byteBuffer(let lhs), .data(let rhs)):
return lhs.readableBytesView.elementsEqual(rhs)
case (.data(let lhs), .byteBuffer(let rhs)):
return rhs.readableBytesView.elementsEqual(lhs)
}
}
}
extension NIOSSHSignature.BackingSignature.RawBytes: Hashable {}
extension NIOSSHSignature.BackingSignature: Equatable {
static func == (lhs: NIOSSHSignature.BackingSignature, rhs: NIOSSHSignature.BackingSignature) -> Bool {
// We implement equatable in terms of the key representation.
switch (lhs, rhs) {
case (.ed25519(let lhs), .ed25519(let rhs)):
return lhs == rhs
case (.ecdsaP256(let lhs), .ecdsaP256(let rhs)):
return lhs.rawRepresentation == rhs.rawRepresentation
case (.ecdsaP384(let lhs), .ecdsaP384(let rhs)):
return lhs.rawRepresentation == rhs.rawRepresentation
case (.ecdsaP521(let lhs), .ecdsaP521(let rhs)):
return lhs.rawRepresentation == rhs.rawRepresentation
case (.ed25519, _),
(.ecdsaP256, _),
(.ecdsaP384, _),
(.ecdsaP521, _):
return false
}
}
}
extension NIOSSHSignature.BackingSignature: Hashable {
func hash(into hasher: inout Hasher) {
switch self {
case .ed25519(let bytes):
hasher.combine(0)
hasher.combine(bytes)
case .ecdsaP256(let sig):
hasher.combine(1)
hasher.combine(sig.rawRepresentation)
case .ecdsaP384(let sig):
hasher.combine(2)
hasher.combine(sig.rawRepresentation)
case .ecdsaP521(let sig):
hasher.combine(3)
hasher.combine(sig.rawRepresentation)
}
}
}
extension ByteBuffer {
/// Writes an SSH host key to this `ByteBuffer`.
@discardableResult
mutating func writeSSHSignature(_ sig: NIOSSHSignature) -> Int {
switch sig.backingSignature {
case .ed25519(let sig):
return self.writeEd25519Signature(signatureBytes: sig)
case .ecdsaP256(let sig):
return self.writeECDSAP256Signature(baseSignature: sig)
case .ecdsaP384(let sig):
return self.writeECDSAP384Signature(baseSignature: sig)
case .ecdsaP521(let sig):
return self.writeECDSAP521Signature(baseSignature: sig)
}
}
private mutating func writeEd25519Signature(signatureBytes: NIOSSHSignature.BackingSignature.RawBytes) -> Int {
// The Ed25519 signature format is easy: the ed25519 signature prefix, followed by
// the raw signature bytes.
var writtenLength = self.writeSSHString(NIOSSHSignature.ed25519SignaturePrefix)
switch signatureBytes {
case .byteBuffer(var buf):
writtenLength += self.writeSSHString(&buf)
case .data(let d):
writtenLength += self.writeSSHString(d)
}
return writtenLength
}
private mutating func writeECDSAP256Signature(baseSignature: P256.Signing.ECDSASignature) -> Int {
var writtenLength = self.writeSSHString(NIOSSHSignature.ecdsaP256SignaturePrefix)
// For ECDSA-P256, the key format is `mpint r` followed by `mpint s`. In this context, `r` is the
// first 32 bytes, and `s` is the second.
let rawRepresentation = baseSignature.rawRepresentation
precondition(rawRepresentation.count == 64, "Unexpected size for P256 key")
let rBytes: Data = rawRepresentation.prefix(32)
let sBytes: Data = rawRepresentation.dropFirst(32)
writtenLength += self.writeCompositeSSHString { buffer in
var written = 0
written += buffer.writePositiveMPInt(rBytes)
written += buffer.writePositiveMPInt(sBytes)
return written
}
return writtenLength
}
private mutating func writeECDSAP384Signature(baseSignature: P384.Signing.ECDSASignature) -> Int {
var writtenLength = self.writeSSHString(NIOSSHSignature.ecdsaP384SignaturePrefix)
// For ECDSA-P384, the key format is `mpint r` followed by `mpint s`. In this context, `r` is the
// first 48 bytes, and `s` is the second.
let rawRepresentation = baseSignature.rawRepresentation
precondition(rawRepresentation.count == 96, "Unexpected size for P384 key")
let rBytes: Data = rawRepresentation.prefix(48)
let sBytes: Data = rawRepresentation.dropFirst(48)
writtenLength += self.writeCompositeSSHString { buffer in
var written = 0
written += buffer.writePositiveMPInt(rBytes)
written += buffer.writePositiveMPInt(sBytes)
return written
}
return writtenLength
}
private mutating func writeECDSAP521Signature(baseSignature: P521.Signing.ECDSASignature) -> Int {
var writtenLength = self.writeSSHString(NIOSSHSignature.ecdsaP521SignaturePrefix)
// For ECDSA-P521, the key format is `mpint r` followed by `mpint s`. In this context, `r` is the
// first 66 bytes, and `s` is the second.
let rawRepresentation = baseSignature.rawRepresentation
precondition(rawRepresentation.count == 132, "Unexpected size for P521 key")
let rBytes: Data = rawRepresentation.prefix(66)
let sBytes: Data = rawRepresentation.dropFirst(66)
writtenLength += self.writeCompositeSSHString { buffer in
var written = 0
written += buffer.writePositiveMPInt(rBytes)
written += buffer.writePositiveMPInt(sBytes)
return written
}
return writtenLength
}
mutating func readSSHSignature() throws -> NIOSSHSignature? {
try self.rewindOnNilOrError { buffer in
// The wire format always begins with an SSH string containing the signature format identifier. Let's grab that.
guard var signatureIdentifierBytes = buffer.readSSHString() else {
return nil
}
// Now we need to check if they match our supported signature algorithms.
let bytesView = signatureIdentifierBytes.readableBytesView
if bytesView.elementsEqual(NIOSSHSignature.ed25519SignaturePrefix) {
return try buffer.readEd25519Signature()
} else if bytesView.elementsEqual(NIOSSHSignature.ecdsaP256SignaturePrefix) {
return try buffer.readECDSAP256Signature()
} else if bytesView.elementsEqual(NIOSSHSignature.ecdsaP384SignaturePrefix) {
return try buffer.readECDSAP384Signature()
} else if bytesView.elementsEqual(NIOSSHSignature.ecdsaP521SignaturePrefix) {
return try buffer.readECDSAP521Signature()
} else {
// We don't know this signature type.
let signature = signatureIdentifierBytes.readString(length: signatureIdentifierBytes.readableBytes) ?? "<unknown signature>"
throw NIOSSHError.unknownSignature(algorithm: signature)
}
}
}
/// A helper function that reads an Ed25519 signature.
///
/// Not safe to call from arbitrary code as this does not return the reader index on failure: it relies on the caller performing
/// the rewind.
private mutating func readEd25519Signature() throws -> NIOSSHSignature? {
// For ed25519 the signature is just r||s encoded as a String.
guard let sigBytes = self.readSSHString() else {
return nil
}
return NIOSSHSignature(backingSignature: .ed25519(.byteBuffer(sigBytes)))
}
/// A helper function that reads an ECDSA P-256 signature.
///
/// Not safe to call from arbitrary code as this does not return the reader index on failure: it relies on the caller performing
/// the rewind.
private mutating func readECDSAP256Signature() throws -> NIOSSHSignature? {
// For ECDSA-P256, the key format is `mpint r` followed by `mpint s`.
// We don't need them as mpints, so let's treat them as strings instead.
guard var signatureBytes = self.readSSHString(),
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString() else {
return nil
}
// Time to put these into the raw format that CryptoKit wants. This is r || s, with each
// integer explicitly left-padded with zeros.
return try NIOSSHSignature(backingSignature: .ecdsaP256(ECDSASignatureHelper.toECDSASignature(r: rBytes, s: sBytes)))
}
/// A helper function that reads an ECDSA P-384 signature.
///
/// Not safe to call from arbitrary code as this does not return the reader index on failure: it relies on the caller performing
/// the rewind.
private mutating func readECDSAP384Signature() throws -> NIOSSHSignature? {
// For ECDSA-P384, the key format is `mpint r` followed by `mpint s`.
// We don't need them as mpints, so let's treat them as strings instead.
guard var signatureBytes = self.readSSHString(),
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString() else {
return nil
}
// Time to put these into the raw format that CryptoKit wants. This is r || s, with each
// integer explicitly left-padded with zeros.
return try NIOSSHSignature(backingSignature: .ecdsaP384(ECDSASignatureHelper.toECDSASignature(r: rBytes, s: sBytes)))
}
/// A helper function that reads an ECDSA P-521 signature.
///
/// Not safe to call from arbitrary code as this does not return the reader index on failure: it relies on the caller performing
/// the rewind.
private mutating func readECDSAP521Signature() throws -> NIOSSHSignature? {
// For ECDSA-P521, the key format is `mpint r` followed by `mpint s`.
// We don't need them as mpints, so let's treat them as strings instead.
guard var signatureBytes = self.readSSHString(),
let rBytes = signatureBytes.readSSHString(),
let sBytes = signatureBytes.readSSHString() else {
return nil
}
// Time to put these into the raw format that CryptoKit wants. This is r || s, with each
// integer explicitly left-padded with zeros.
return try NIOSSHSignature(backingSignature: .ecdsaP521(ECDSASignatureHelper.toECDSASignature(r: rBytes, s: sBytes)))
}
}
/// A structure that helps store ECDSA signatures on the stack temporarily to avoid unnecessary memory allocation.
///
/// CryptoKit would like to receive ECDSA signatures in the form of `r || s`, where `r` and `s` are both left-padded
/// with zeros. We know that for P256 the ECDSA signature size is going to be 64 bytes, as each of the P256 points are
/// 32 bytes wide. Similar logic applies up to P521, whose signatures are 132 bytes in size.
///
/// To avoid an unnecessary memory allocation, we use this data structure to provide some heap space to store these in.
/// This structure is wide enough for any of these signatures, and just uses the appropriate amount of space for whatever
/// algorithm is actually in use.
private struct ECDSASignatureHelper {
private var storage: (
UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64,
UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64,
UInt64
) = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
private init(r: ByteBuffer, s: ByteBuffer, pointSize: Int) {
precondition(MemoryLayout<ECDSASignatureHelper>.size >= pointSize, "Invalid width for ECDSA signature helper.")
let rByteView = r.mpIntView
let sByteView = s.mpIntView
let rByteStartingOffset = pointSize - rByteView.count
let sByteStartingOffset = pointSize - sByteView.count
withUnsafeMutableBytes(of: &self.storage) { storagePtr in
let rPtr = UnsafeMutableRawBufferPointer(rebasing: storagePtr[rByteStartingOffset ..< pointSize])
let sPtr = UnsafeMutableRawBufferPointer(rebasing: storagePtr[(sByteStartingOffset + pointSize) ..< (pointSize * 2)])
precondition(rPtr.count == rByteView.count)
precondition(sPtr.count == sByteView.count)
rPtr.copyBytes(from: rByteView)
sPtr.copyBytes(from: sByteView)
}
}
static func toECDSASignature<Signature: ECDSASignatureProtocol>(r: ByteBuffer, s: ByteBuffer) throws -> Signature {
let helper = ECDSASignatureHelper(r: r, s: s, pointSize: Signature.pointSize)
return try withUnsafeBytes(of: helper.storage) { storagePtr in
try Signature(rawRepresentation: UnsafeRawBufferPointer(rebasing: storagePtr.prefix(Signature.pointSize * 2)))
}
}
}
extension ByteBuffer {
// A view onto the mpInt bytes. Strips off a leading 0 if it is present for
// size reasons.
fileprivate var mpIntView: ByteBufferView {
var baseView = self.readableBytesView
if baseView.first == 0 {
baseView = baseView.dropFirst()
}
return baseView
}
}
protocol ECDSASignatureProtocol {
init<D>(rawRepresentation: D) throws where D: DataProtocol
static var pointSize: Int { get }
}
extension P256.Signing.ECDSASignature: ECDSASignatureProtocol {
static var pointSize: Int { 32 }
}
extension P384.Signing.ECDSASignature: ECDSASignatureProtocol {
static var pointSize: Int { 48 }
}
extension P521.Signing.ECDSASignature: ECDSASignatureProtocol {
static var pointSize: Int { 66 }
}