| ... | @@ -2391,6 +2391,7 @@ pub const Signature = union(enum) { | ... | @@ -2391,6 +2391,7 @@ pub const Signature = union(enum) { |
| 2391 | // https://pkg.go.dev/golang.org/x/crypto/ssh#pkg-constants | 2391 | // https://pkg.go.dev/golang.org/x/crypto/ssh#pkg-constants |
| 2392 | // https://datatracker.ietf.org/doc/html/rfc9580#section-4 | 2392 | // https://datatracker.ietf.org/doc/html/rfc9580#section-4 |
| 2393 | // https://datatracker.ietf.org/doc/html/rfc9580#signature-packet | 2393 | // https://datatracker.ietf.org/doc/html/rfc9580#signature-packet |
| | 2394 | // https://datatracker.ietf.org/doc/html/rfc5656 |
| 2394 | pub fn fromReader(kind: NonVoidUnionFieldEnum(Signature), allocator: std.mem.Allocator, b64r: anytype, message: []const u8) !Signature { | 2395 | pub fn fromReader(kind: NonVoidUnionFieldEnum(Signature), allocator: std.mem.Allocator, b64r: anytype, message: []const u8) !Signature { |
| 2395 | if (kind == .pgp) { | 2396 | if (kind == .pgp) { |
| 2396 | var signed_data: nio.AllocatingWriter = .init(allocator); | 2397 | var signed_data: nio.AllocatingWriter = .init(allocator); |
| ... | @@ -2552,11 +2553,10 @@ pub const Signature = union(enum) { | ... | @@ -2552,11 +2553,10 @@ pub const Signature = union(enum) { |
| 2552 | | 2553 | |
| 2553 | var valid: ?bool = null; | 2554 | var valid: ?bool = null; |
| 2554 | var sigfixed: nio.FixedBufferStream([]const u8) = .init(signature); | 2555 | var sigfixed: nio.FixedBufferStream([]const u8) = .init(signature); |
| 2555 | const sigformat = try sigfixed.readAlloc(allocator, try sigfixed.readInt(u32, .big)); | 2556 | const sigformat = try sigfixed.readSlice(try sigfixed.readInt(u32, .big)); |
| 2556 | defer allocator.free(sigformat); | | |
| 2557 | var pkfixed: nio.FixedBufferStream([]const u8) = .init(publickey); | 2557 | var pkfixed: nio.FixedBufferStream([]const u8) = .init(publickey); |
| 2558 | const pkformat = try pkfixed.readAlloc(allocator, try pkfixed.readInt(u32, .big)); | 2558 | const pkformat = try pkfixed.readSlice(try pkfixed.readInt(u32, .big)); |
| 2559 | defer allocator.free(pkformat); | 2559 | _ = pkformat; |
| 2560 | | 2560 | |
| 2561 | if (std.mem.eql(u8, sigformat, "ssh-rsa")) { | 2561 | if (std.mem.eql(u8, sigformat, "ssh-rsa")) { |
| 2562 | // intentionally skipped | 2562 | // intentionally skipped |
| ... | @@ -2573,8 +2573,30 @@ pub const Signature = union(enum) { | ... | @@ -2573,8 +2573,30 @@ pub const Signature = union(enum) { |
| 2573 | const sig = ed.Signature.fromBytes(sig_bytes); | 2573 | const sig = ed.Signature.fromBytes(sig_bytes); |
| 2574 | valid = if (sig.verifyStrict(signed_data.items, pk)) true else |_| false; | 2574 | valid = if (sig.verifyStrict(signed_data.items, pk)) true else |_| false; |
| 2575 | } | 2575 | } |
| | 2576 | if (std.mem.eql(u8, sigformat, "ecdsa-sha2-nistp256")) blk: { |
| | 2577 | const C = std.crypto.ecc.P256; |
| | 2578 | const H = std.crypto.hash.sha2.Sha256; |
| | 2579 | const ns = std.crypto.sign.ecdsa.Ecdsa(C, H); |
| | 2580 | const ident_len = pkfixed.readInt(u32, .big) catch break :blk; |
| | 2581 | if (ident_len != "nistp256".len) break :blk; |
| | 2582 | if (!(pkfixed.readExpected("nistp256") catch break :blk)) break :blk; |
| | 2583 | const q_len = pkfixed.readInt(u32, .big) catch break :blk; |
| | 2584 | const q = pkfixed.readSlice(q_len) catch break :blk; |
| | 2585 | const pk = ns.PublicKey.fromSec1(q) catch break :blk; |
| | 2586 | const sigblob_len = sigfixed.readInt(u32, .big) catch break :blk; |
| | 2587 | _ = sigblob_len; |
| | 2588 | const r_len = sigfixed.readInt(u32, .big) catch break :blk; |
| | 2589 | var r = sigfixed.readSlice(r_len) catch break :blk; |
| | 2590 | if (r[0] == 0) r = r[1..]; |
| | 2591 | if (r.len != C.scalar.encoded_length) break :blk; |
| | 2592 | const s_len = sigfixed.readInt(u32, .big) catch break :blk; |
| | 2593 | var s = sigfixed.readSlice(s_len) catch break :blk; |
| | 2594 | if (s[0] == 0) s = s[1..]; |
| | 2595 | if (s.len != C.scalar.encoded_length) break :blk; |
| | 2596 | const sig = ns.Signature.fromBytes((r[0..C.scalar.encoded_length] ++ s[0..C.scalar.encoded_length]).*); |
| | 2597 | valid = if (sig.verify(signed_data.items, pk)) true else |_| false; |
| | 2598 | } |
| 2576 | // ssh-dss (dsa) | 2599 | // ssh-dss (dsa) |
| 2577 | // ecdsa-sha2-nistp256 | | |
| 2578 | // sk-ecdsa-sha2-nistp256@openssh.com | 2600 | // sk-ecdsa-sha2-nistp256@openssh.com |
| 2579 | // ecdsa-sha2-nistp384 | 2601 | // ecdsa-sha2-nistp384 |
| 2580 | // ecdsa-sha2-nistp521 | 2602 | // ecdsa-sha2-nistp521 |