-
Notifications
You must be signed in to change notification settings - Fork 2
/
email2certs.go
144 lines (126 loc) · 3.34 KB
/
email2certs.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
// Copyright (C) 2017 Opsmate, Inc.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License, v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This software is distributed WITHOUT A WARRANTY OF ANY KIND.
// See the Mozilla Public License for details.
package main
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"flag"
"io"
"io/ioutil"
"log"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"os"
"strings"
"sync/atomic"
"github.com/fullsailor/pkcs7"
)
var verbose = flag.Bool("v", false, "Enable verbose output")
var numErrors uint32
func extractCertsFromPKCS7(data []byte) ([]*x509.Certificate, error) {
p7, err := pkcs7.Parse(data)
if err != nil {
return nil, err
}
certs := make([]*x509.Certificate, 0, len(p7.Certificates))
for _, cert := range p7.Certificates {
certs = append(certs, cert)
}
return certs, nil
}
func discardReader(r io.Reader) error {
_, err := io.Copy(ioutil.Discard, r)
return err
}
func processPart(contentType string, cte string, body io.Reader, certsOut chan<- *x509.Certificate) error {
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
log.Printf("Ignoring part of email because of bad MIME type: %s\n", err)
atomic.AddUint32(&numErrors, 1)
return discardReader(body)
}
if cte == "quoted-printable" {
body = quotedprintable.NewReader(body)
} else if cte == "base64" {
body = base64.NewDecoder(base64.StdEncoding, body)
}
if strings.HasPrefix(mediaType, "multipart/") {
return processMultipart(body, params["boundary"], certsOut)
} else if mediaType == "application/pkcs7-signature" {
data, err := ioutil.ReadAll(body)
if err != nil {
return err
}
certs, err := extractCertsFromPKCS7(data)
if err != nil {
log.Printf("Ignoring part of email because could not extract PKCS7 certs: %s\n", err)
atomic.AddUint32(&numErrors, 1)
} else {
for _, cert := range certs {
certsOut <- cert
}
}
return nil
} else {
return discardReader(body)
}
}
func processMultipart(r io.Reader, boundary string, certsOut chan<- *x509.Certificate) error {
reader := multipart.NewReader(r, boundary)
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
return err
}
err = processPart(part.Header.Get("Content-Type"), part.Header.Get("Content-Transfer-Encoding"), part, certsOut)
if err != nil {
return err
}
}
return nil
}
func processEmail(r io.Reader, certsOut chan<- *x509.Certificate) error {
mesg, err := mail.ReadMessage(r)
if err != nil {
return err
}
return processPart(mesg.Header.Get("Content-Type"), mesg.Header.Get("Content-Transfer-Encoding"), mesg.Body, certsOut)
}
func main() {
flag.Parse()
log.SetPrefix("email2certs: ")
certsChan := make(chan *x509.Certificate, 16)
go func() {
err := processEmail(os.Stdin, certsChan)
if err != nil {
log.Fatalf("Error parsing email from stdin: %s", err)
}
close(certsChan)
}()
numCerts := 0
for cert := range certsChan {
numCerts++
if err := pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
log.Fatalf("Error writing to stdout: %s", err)
}
}
if *verbose {
log.Printf("%d certificates extracted", numCerts)
}
if numErrors > 0 {
log.Printf("%d parse errors", numErrors)
os.Exit(3)
}
}