/
mit-pgp-server.go
123 lines (110 loc) · 2.85 KB
/
mit-pgp-server.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
// Copyright Banrai LLC. All rights reserved. Use of this source code is
// governed by the license that can be found in the LICENSE file.
package keyservers
import (
"encoding/xml"
"fmt"
"github.com/Banrai/TeamWork.io/server/httputil"
"html"
"io"
"strings"
)
/* For accessing public keys from the
MIT PGP Public Key Server
http://pgp.mit.edu/
*/
const MIT_SOURCE = "http://pgp.mit.edu/"
// parse the specific key links from a request of the form:
// http://pgp.mit.edu/pks/lookup?search=me@example.org excluding revoked/not
// verified keys
func parseKeyResult(in io.Reader) (string, error) {
dec := xml.NewDecoder(in)
captureData := false
for {
tok, err := dec.Token()
if err == io.EOF {
break
} else if err != nil {
return "", err
}
switch tok := tok.(type) {
case xml.StartElement:
if "pre" == tok.Name.Local {
captureData = true
}
case xml.EndElement:
captureData = false
case xml.CharData:
if captureData {
return strings.TrimSpace(fmt.Sprintf("%s", tok)), nil
}
}
}
return "", nil
}
// parse the specific key links from a request of the form:
// http://pgp.mit.edu/pks/lookup?search=me@example.org
// excluding revoked/not verified keys
func parseMatchResult(in io.Reader) ([]string, error) {
dec := xml.NewDecoder(in)
var results []string
captureData := false
for {
tok, err := dec.Token()
if err == io.EOF {
break
} else if err != nil {
return results, err
}
switch tok := tok.(type) {
case xml.StartElement:
if "a" == tok.Name.Local {
for _, attr := range tok.Attr {
if "href" == attr.Name.Local {
if strings.HasPrefix(attr.Value, "/pks/lookup?op=get&search=") {
captureData = true
results = append(results, html.UnescapeString(attr.Value))
}
}
}
}
case xml.CharData:
if captureData {
text := strings.TrimSpace(fmt.Sprintf("%s", tok))
if strings.Index(text, "not verified") > -1 ||
strings.Index(text, "REVOKED") > -1 {
// pop the most recent link, since it is invalid
results = results[:len(results)-1]
}
captureData = false
}
}
}
return results, nil
}
// search the MIT PGP Server for all public keys corresponding to this
// email address, returning them as armored strings, excluding revoked/not
// verified keys
func MITSearch(email string) ([]string, error) {
var keys []string
in, inErr := httputil.URLFetchAsReader(fmt.Sprintf("http://pgp.mit.edu/pks/lookup?search=%s", email))
if inErr != nil {
return keys, inErr
}
links, linkErr := parseMatchResult(in)
if linkErr != nil {
return keys, linkErr
}
for _, link := range links {
pkIn, pkInErr := httputil.URLFetchAsReader(fmt.Sprintf("http://pgp.mit.edu%s", link))
if pkInErr != nil {
return keys, pkInErr
}
key, keyErr := parseKeyResult(pkIn)
if keyErr != nil {
return keys, keyErr
}
keys = append(keys, key)
}
return keys, nil
}