1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/crypto/openpgp/errors"
15 // OpaquePacket represents an OpenPGP packet as raw, unparsed data. This is
16 // useful for splitting and storing the original packet contents separately,
17 // handling unsupported packet types or accessing parts of the packet not yet
18 // implemented by this package.
19 type OpaquePacket struct {
22 // Reason why the packet was parsed opaquely
24 // Binary contents of the packet data
28 func (op *OpaquePacket) parse(r io.Reader) (err error) {
29 op.Contents, err = ioutil.ReadAll(r)
33 // Serialize marshals the packet to a writer in its original form, including
35 func (op *OpaquePacket) Serialize(w io.Writer) (err error) {
36 err = serializeHeader(w, packetType(op.Tag), len(op.Contents))
38 _, err = w.Write(op.Contents)
43 // Parse attempts to parse the opaque contents into a structure supported by
44 // this package. If the packet is not known then the result will be another
46 func (op *OpaquePacket) Parse() (p Packet, err error) {
47 hdr := bytes.NewBuffer(nil)
48 err = serializeHeader(hdr, packetType(op.Tag), len(op.Contents))
53 p, err = Read(io.MultiReader(hdr, bytes.NewBuffer(op.Contents)))
61 // OpaqueReader reads OpaquePackets from an io.Reader.
62 type OpaqueReader struct {
66 func NewOpaqueReader(r io.Reader) *OpaqueReader {
67 return &OpaqueReader{r: r}
70 // Read the next OpaquePacket.
71 func (or *OpaqueReader) Next() (op *OpaquePacket, err error) {
72 tag, _, contents, err := readHeader(or.r)
76 op = &OpaquePacket{Tag: uint8(tag), Reason: err}
77 err = op.parse(contents)
84 // OpaqueSubpacket represents an unparsed OpenPGP subpacket,
85 // as found in signature and user attribute packets.
86 type OpaqueSubpacket struct {
91 // OpaqueSubpackets extracts opaque, unparsed OpenPGP subpackets from
92 // their byte representation.
93 func OpaqueSubpackets(contents []byte) (result []*OpaqueSubpacket, err error) {
96 subPacket *OpaqueSubpacket
98 for len(contents) > 0 {
99 subHeaderLen, subPacket, err = nextSubpacket(contents)
103 result = append(result, subPacket)
104 contents = contents[subHeaderLen+len(subPacket.Contents):]
109 func nextSubpacket(contents []byte) (subHeaderLen int, subPacket *OpaqueSubpacket, err error) {
110 // RFC 4880, section 5.2.3.1
112 if len(contents) < 1 {
115 subPacket = &OpaqueSubpacket{}
117 case contents[0] < 192:
118 subHeaderLen = 2 // 1 length byte, 1 subtype byte
119 if len(contents) < subHeaderLen {
122 subLen = uint32(contents[0])
123 contents = contents[1:]
124 case contents[0] < 255:
125 subHeaderLen = 3 // 2 length bytes, 1 subtype
126 if len(contents) < subHeaderLen {
129 subLen = uint32(contents[0]-192)<<8 + uint32(contents[1]) + 192
130 contents = contents[2:]
132 subHeaderLen = 6 // 5 length bytes, 1 subtype
133 if len(contents) < subHeaderLen {
136 subLen = uint32(contents[1])<<24 |
137 uint32(contents[2])<<16 |
138 uint32(contents[3])<<8 |
140 contents = contents[5:]
142 if subLen > uint32(len(contents)) || subLen == 0 {
145 subPacket.SubType = contents[0]
146 subPacket.Contents = contents[1:subLen]
149 err = errors.StructuralError("subpacket truncated")
153 func (osp *OpaqueSubpacket) Serialize(w io.Writer) (err error) {
154 buf := make([]byte, 6)
155 n := serializeSubpacketLength(buf, len(osp.Contents)+1)
157 if _, err = w.Write(buf[:n+1]); err != nil {
160 _, err = w.Write(osp.Contents)