Skip to content

Commit da18f56

Browse files
committed
Add Discover
1 parent dd926f6 commit da18f56

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# go-openpgp-wks
2-
A Go library for OpenPGP Web Key Service
2+
3+
A Go library for [OpenPGP Web Key Service](https://tools.ietf.org/html/draft-koch-openpgp-webkey-service-04).
4+
5+
## License
6+
7+
MIT

discovery.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package wks
2+
3+
import (
4+
"crypto/sha1"
5+
"errors"
6+
"io"
7+
"net/http"
8+
"strings"
9+
10+
"github.com/tv42/zbase32"
11+
"golang.org/x/crypto/openpgp"
12+
"golang.org/x/crypto/openpgp/packet"
13+
)
14+
15+
func splitAddress(addr string) (local, domain string, err error) {
16+
parts := strings.Split(addr, "@")
17+
if len(parts) != 2 {
18+
return "", "", errors.New("wks: invalid email address")
19+
}
20+
return parts[0], parts[1], nil
21+
}
22+
23+
// Discover retrieves keys associated to an email address.
24+
func Discover(addr string) ([]*openpgp.Entity, error) {
25+
local, domain, err := splitAddress(strings.ToLower(addr))
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
// TODO: SRV record
31+
32+
hashedLocal := sha1.Sum([]byte(local))
33+
url := "https://"+domain+"/.well-known/openpgpkey/hu/" + zbase32.EncodeToString(hashedLocal[:])
34+
resp, err := http.Get(url)
35+
if err != nil {
36+
return nil, err
37+
}
38+
defer resp.Body.Close()
39+
40+
r := packet.NewReader(resp.Body)
41+
var entities []*openpgp.Entity
42+
for {
43+
e, err := openpgp.ReadEntity(r)
44+
if err == io.EOF {
45+
break
46+
} else if err != nil {
47+
return entities, err
48+
}
49+
50+
entities = append(entities, e)
51+
}
52+
53+
return entities, nil
54+
}

wks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// wks implements OpenPGP Web Key Service, defined in
2+
// https://tools.ietf.org/html/draft-koch-openpgp-webkey-service-04
3+
package wks

0 commit comments

Comments
 (0)