From 58b09dec0e34ecb38f4d724f6ccd3b3b745f7029 Mon Sep 17 00:00:00 2001 From: Casey Marshall Date: Wed, 4 Sep 2013 10:21:45 -0400 Subject: [PATCH] openpgp/packet: Compact UserAttribute subpacket lengths, fixes signatures. Necessary to reproduce the same hash for verifying a UserAttribute signature. Reuse and fix defect in serializeSubpacketLength, RFC 4880, Section 4.2.2.2. R=agl CC=golang-dev https://golang.org/cl/13464044 --- openpgp/packet/opaque.go | 9 +++------ openpgp/packet/signature.go | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/openpgp/packet/opaque.go b/openpgp/packet/opaque.go index 7c4d813..5f9ad44 100644 --- a/openpgp/packet/opaque.go +++ b/openpgp/packet/opaque.go @@ -7,7 +7,6 @@ package packet import ( "bytes" "code.google.com/p/go.crypto/openpgp/errors" - "encoding/binary" "io" "io/ioutil" ) @@ -152,11 +151,9 @@ Truncated: func (osp *OpaqueSubpacket) Serialize(w io.Writer) (err error) { buf := make([]byte, 6) - buf[0] = 0xff - // Header length includes the subtype byte - binary.BigEndian.PutUint32(buf[1:5], uint32(len(osp.Contents)+1)) - buf[5] = osp.SubType - if _, err = w.Write(buf); err != nil { + n := serializeSubpacketLength(buf, len(osp.Contents)+1) + buf[n] = osp.SubType + if _, err = w.Write(buf[:n+1]); err != nil { return } _, err = w.Write(osp.Contents) diff --git a/openpgp/packet/signature.go b/openpgp/packet/signature.go index d0d6890..dbed36e 100644 --- a/openpgp/packet/signature.go +++ b/openpgp/packet/signature.go @@ -344,13 +344,14 @@ func subpacketLengthLength(length int) int { // serializeSubpacketLength marshals the given length into to. func serializeSubpacketLength(to []byte, length int) int { + // RFC 4880, Section 4.2.2. if length < 192 { to[0] = byte(length) return 1 } if length < 16320 { length -= 192 - to[0] = byte(length >> 8) + to[0] = byte((length >> 8) + 192) to[1] = byte(length) return 2 }