aboutsummaryrefslogtreecommitdiff
path: root/pkg/types/binary
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus@mullvad.net>2022-04-25 00:43:06 +0200
committerRasmus Dahlberg <rasmus@mullvad.net>2022-04-25 00:43:06 +0200
commit528a53f7f76f08af5902f4cfa8235380b3434ba0 (patch)
tree662b7834d5ce15627554e9307a4e00f7364fba11 /pkg/types/binary
parent4fc0ff2ec2f48519ee245d6d7edee1921cb3b8bc (diff)
drafty types refactor with simple ascii packagergdd/sketch
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.
Diffstat (limited to 'pkg/types/binary')
-rw-r--r--pkg/types/binary/ssh/ssh.go34
-rw-r--r--pkg/types/binary/trunnel/trunnel.go33
2 files changed, 67 insertions, 0 deletions
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))
+}
diff --git a/pkg/types/binary/trunnel/trunnel.go b/pkg/types/binary/trunnel/trunnel.go
new file mode 100644
index 0000000..fbf41f9
--- /dev/null
+++ b/pkg/types/binary/trunnel/trunnel.go
@@ -0,0 +1,33 @@
+// package trunnel provides selected Trunnel primitives, see:
+//
+// - https://gitlab.torproject.org/tpo/core/trunnel/-/blob/main/doc/trunnel.md
+package trunnel
+
+import (
+ "bytes"
+ "encoding/binary"
+ "fmt"
+ "io"
+)
+
+func Uint64(buf *bytes.Buffer, num *uint64) error {
+ if err := binary.Read(buf, binary.BigEndian, num); err != nil {
+ return fmt.Errorf("uint64: %w", err)
+ }
+ return nil
+}
+
+func Array(buf *bytes.Buffer, arr []byte) error {
+ if _, err := io.ReadFull(buf, arr); err != nil {
+ return fmt.Errorf("array[%d]: %w", len(arr), err)
+ }
+ return nil
+}
+
+func AddUint64(buf *bytes.Buffer, num uint64) {
+ binary.Write(buf, binary.BigEndian, num)
+}
+
+func AddArray(buf *bytes.Buffer, arr []byte) {
+ buf.Write(arr[:])
+}