This repository was archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdkim_signer.go
145 lines (128 loc) · 2.84 KB
/
dkim_signer.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
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"github.com/emersion/go-dkim"
"io/ioutil"
"log"
"os"
)
type dkimSigner struct {
pk *rsa.PrivateKey
next MsgProcessor
}
func (d dkimSigner) Process(msg *ReceivedMsg) error {
options := &dkim.SignOptions{
Domain: GetString(DomainKey),
Selector: "mx",
Signer: d.pk,
}
var b bytes.Buffer
e := dkim.Sign(&b, bytes.NewReader(msg.Content), options)
if e != nil {
return e
}
msg.Content = b.Bytes()
return d.next.Process(msg)
}
func NewDkimSigner(pk *rsa.PrivateKey, next MsgProcessor) MsgProcessor {
return &dkimSigner{
pk: pk,
next: next,
}
}
func GetOrCreateDkim() *rsa.PrivateKey {
var privKey *rsa.PrivateKey
var e error
privKeyBytes, e1 := ioutil.ReadFile(GetString(DkimPrivateKeyFileKey))
pubKeyBytes, e2 := ioutil.ReadFile(GetString(DkimPublicKeyFileKey))
if os.IsNotExist(e1) && os.IsNotExist(e2) {
privKey, e = rsa.GenerateKey(rand.Reader, GetInt(DkimKeyBitsKey))
if e != nil {
log.Fatal(e)
}
e = ioutil.WriteFile(GetString(DkimPrivateKeyFileKey),
ExportRsaPrivateKeyAsPem(privKey),
0700)
if e != nil {
log.Fatal(e)
}
pubKey, e := ExportRsaPublicKeyAsPem(&privKey.PublicKey)
if e != nil {
log.Fatal(e)
}
e = ioutil.WriteFile(GetString(DkimPublicKeyFileKey), pubKey, 0700)
if e != nil {
log.Fatal(e)
}
} else if e1 != nil {
log.Fatal(e1)
} else if e2 != nil {
log.Fatal(e2)
} else {
privKey, e = ParseRsaPrivateKeyFromPem(privKeyBytes)
if e != nil {
log.Fatal(e)
}
publicKey, e := ParseRsaPublicKeyFromPem(pubKeyBytes)
if e != nil {
log.Fatal(e)
}
privKey.PublicKey = *publicKey
}
return privKey
}
func ExportRsaPrivateKeyAsPem(pk *rsa.PrivateKey) []byte {
pkb := x509.MarshalPKCS1PrivateKey(pk)
pkp := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: pkb,
},
)
return pkp
}
func ParseRsaPrivateKeyFromPem(pkp []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(pkp)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return priv, nil
}
func ExportRsaPublicKeyAsPem(pk *rsa.PublicKey) ([]byte, error) {
pkb, err := x509.MarshalPKIXPublicKey(pk)
if err != nil {
return nil, err
}
pkp := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pkb,
},
)
return pkp, nil
}
func ParseRsaPublicKeyFromPem(pkp []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(pkp)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
switch pub := pub.(type) {
case *rsa.PublicKey:
return pub, nil
default:
return nil, errors.New("key type is not RSA")
}
}