From 528a53f7f76f08af5902f4cfa8235380b3434ba0 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Mon, 25 Apr 2022 00:43:06 +0200 Subject: drafty types refactor with simple ascii package types.go compiles but that is about it, here be dragons. Pushing so that we can get an idea of what this refactor would roughly look like. --- pkg/types/binary/ssh/ssh.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 pkg/types/binary/ssh/ssh.go (limited to 'pkg/types/binary/ssh') diff --git a/pkg/types/binary/ssh/ssh.go b/pkg/types/binary/ssh/ssh.go new file mode 100644 index 0000000..9693476 --- /dev/null +++ b/pkg/types/binary/ssh/ssh.go @@ -0,0 +1,34 @@ +// package ssh provides selected parts of the SSH data format, see: +// +// - https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.sshsig +// - https://datatracker.ietf.org/doc/html/rfc4251#section-5 +// +package ssh + +import ( + "bytes" + "encoding/binary" +) + +// ToSignBlob outputs the raw bytes to be signed for a given namespace and +// message. The reserved string is empty and the specified hash is SHA256. +func ToSignBlob(namespace string, hashedMessage []byte) []byte { + buf := bytes.NewBuffer(nil) + + buf.Write([]byte("SSHSIG")) + addString(buf, namespace) + addString(buf, "") + addString(buf, "sha256") + addString(buf, string(hashedMessage[:])) + + return buf.Bytes() +} + +func addUint32(buf *bytes.Buffer, num uint32) { + binary.Write(buf, binary.BigEndian, num) +} + +func addString(buf *bytes.Buffer, str string) { + addUint32(buf, uint32(len(str))) + buf.Write([]byte(str)) +} -- cgit v1.2.3