-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
209 lines (174 loc) · 4.15 KB
/
key.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dkim
import (
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"fmt"
"strings"
"time"
)
// Key represent a DKIM key record.
type Key struct {
// REQUIRED fields.
// Public contains public key data.
// ("p=", base64, REQUIRED)
Public []byte
// RECOMMENDED fields.
// Version of DKIM key record.
// ("v=", plain-text, RECOMMENDED, default is "DKIM1")
Version []byte
// OPTIONAL fields.
// Type of key.
// ("k=", plain-text, OPTIONAL, default is "rsa").
Type *KeyType
// HashAlgs contains list of hash algorithm that might be used.
// ("h=", plain-text colon-separated, OPTIONAL, defaults to allowing
// all algorithms)
HashAlgs []HashAlg
// Notes that might be interest to human.
// ("n=", qp-section, OPTIONAL, default is empty)
Notes []byte
// Services contains list of service types to which this record
// applies.
// ("s=", plain-text colon-separated, OPTIONAL, default is "*").
Services [][]byte
// Flags contains list of flags.
// ("t=", plain-text colon-separated, OPTIONAL, default is no flags set)
Flags []KeyFlag
// RSA contains parsed Public key.
RSA *rsa.PublicKey
// ExpiredAt define time when the key will be expired.
// This is a local value derived from lookup time + RR TTL.
ExpiredAt int64
}
// LookupKey DKIM (public) key using specific query method and DKIM domain
// name (selector plus SDID).
func LookupKey(qmethod QueryMethod, dname string) (key *Key, err error) {
if len(dname) == 0 {
return nil, nil
}
if qmethod.Type == QueryTypeDNS {
key, err = lookupDNS(qmethod.Option, dname)
}
return key, err
}
// ParseTXT parse DNS TXT resource record into Key.
func ParseTXT(txt []byte, ttl uint32) (key *Key, err error) {
if len(txt) == 0 {
return nil, nil
}
p := newParser(txt)
key = &Key{}
for {
tag, err := p.fetchTag()
if err != nil {
return nil, err
}
if tag == nil {
break
}
err = key.set(tag)
if err != nil {
return nil, err
}
}
key.ExpiredAt = time.Now().Unix() + int64(ttl)
return key, nil
}
// Pack the key to be used in DNS TXT record.
func (key *Key) Pack() string {
if key == nil {
return ""
}
var bb strings.Builder
sep := []byte{';', ' '}
if len(key.Version) > 0 {
bb.WriteString("v=")
bb.Write(key.Version)
}
if len(key.Public) > 0 {
if bb.Len() > 0 {
bb.Write(sep)
}
bb.WriteString("p=")
bb.Write(key.Public)
}
if key.Type != nil {
if bb.Len() > 0 {
bb.Write(sep)
}
bb.WriteString("k=")
bb.Write(keyTypeNames[*key.Type])
}
if len(key.HashAlgs) > 0 {
if bb.Len() > 0 {
bb.Write(sep)
}
bb.WriteString("h=")
bb.Write(packHashAlgs(key.HashAlgs))
}
if len(key.Notes) > 0 {
if bb.Len() > 0 {
bb.Write(sep)
}
bb.WriteString("n=")
bb.Write(key.Notes)
}
if len(key.Services) > 0 {
if bb.Len() > 0 {
bb.Write(sep)
}
bb.WriteString("s=")
bb.Write(bytes.Join(key.Services, sepColon))
}
if len(key.Flags) > 0 {
if bb.Len() > 0 {
bb.Write(sep)
}
bb.WriteString("t=")
bb.Write(packKeyFlags(key.Flags))
}
return bb.String()
}
// IsExpired will return true if key ExpiredAt time is less than current time;
// otherwise it will return false.
func (key *Key) IsExpired() bool {
return key.ExpiredAt < time.Now().Unix()
}
func (key *Key) set(t *tag) (err error) {
if t == nil {
return nil
}
switch t.key {
case tagDNSPublicKey:
pkey, err := base64.RawStdEncoding.DecodeString(string(t.value))
if err != nil {
err = fmt.Errorf("dkim: error decode public key: %w", err)
return err
}
pk, err := x509.ParsePKIXPublicKey(pkey)
if err != nil {
err = fmt.Errorf("dkim: error parsing public key: %w", err)
return err
}
key.RSA = pk.(*rsa.PublicKey)
key.Public = t.value
case tagDNSVersion:
key.Version = t.value
case tagDNSHashAlgs:
key.HashAlgs = unpackHashAlgs(t.value)
case tagDNSKeyType:
key.Type = parseKeyType(t.value)
case tagDNSNotes:
key.Notes = t.value
case tagDNSServices:
key.Services = bytes.Split(t.value, sepColon)
case tagDNSFlags:
key.Flags = unpackKeyFlags(t.value)
}
return nil
}