This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
126 lines (101 loc) · 2.29 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package autocrypt
import (
"encoding/base64"
"errors"
"net/mail"
"strings"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
)
var (
ErrUnknownType = errors.New("unkown type")
ErrUnknownPrefer = errors.New("unkown prefer")
ErrUnknownAttr = errors.New("unkown attribute")
ErrParse = errors.New("parse error")
ErrNoHeader = errors.New("no AUTOCRYPT header found")
)
const (
attrTo = "to"
attrKey = "key"
attrType = "type"
attrPreferEncrypted = "prefer-encrypted"
autocryptHeader = "AUTOCRYPT"
)
func ParseHeader(mailHeader mail.Header) (*Header, error) {
header := mailHeader.Get(autocryptHeader)
if len(header) == 0 {
return nil, ErrNoHeader
}
var (
parsed Header
part string
)
for len(header) > 0 {
i := strings.Index(header, ";")
if i < 0 {
part = header
header = ""
} else {
part = header[:i]
header = header[i+1:]
}
part = strings.Trim(part, " \t\n\r;")
i = strings.Index(part, "=")
if i < 0 {
return nil, ErrParse
}
k := part[:i]
v := part[i+1:]
switch k {
case attrTo:
parsed.To = v
case attrKey:
//key, err := parseKey(v)
key, err := parseKey(strings.Replace(v, " ", "", -1))
if err != nil {
return nil, err
}
parsed.Key = key
case attrType:
t, err := parseType(v)
if err != nil {
return nil, err
}
parsed.Type = t
case attrPreferEncrypted:
pe, err := parsePreferEncrypted(v)
if err != nil {
return nil, err
}
parsed.PreferEncrypted = pe
default:
if k[0] != '_' {
return nil, ErrUnknownAttr
}
if parsed.Uncritical == nil {
parsed.Uncritical = make(map[string]string)
}
parsed.Uncritical[k] = v
}
}
return &parsed, nil
}
func parseKey(b64Key string) (*openpgp.Entity, error) {
r := packet.NewReader(base64.NewDecoder(base64.StdEncoding, strings.NewReader(b64Key)))
return openpgp.ReadEntity(r)
}
func parseType(opt string) (TypeOption, error) {
if opt == "" || opt == typeMap[TypeOpenPGP] {
return TypeOpenPGP, nil
}
return TypeInvalid, ErrUnknownType
}
func parsePreferEncrypted(opt string) (bool, error) {
if opt == preferEncryptedMap[true] {
return true, nil
}
if opt == "" || opt == preferEncryptedMap[false] {
return false, nil
}
return false, ErrUnknownPrefer
}