This repository has been archived by the owner on Feb 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
78 lines (68 loc) · 1.62 KB
/
utils.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"io"
"io/ioutil"
"os"
"regexp"
)
// base64url encoding without padding
func b64(s []byte) string { return base64.RawURLEncoding.EncodeToString(s) }
// Parse HTTP Link header to get rel => link map
func parseLinks(links []string) map[string]string {
re := regexp.MustCompile(`<(.*)>;\s*rel="(.*)"`)
m := make(map[string]string)
for _, link := range links {
rs := re.FindStringSubmatch(link)
m[rs[2]] = rs[1]
}
return m
}
// Parse PEM-encoded RSA private key from r.
func parseKey(r io.Reader) (*rsa.PrivateKey, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
blk := new(pem.Block)
// find the first RSA private key
for {
blk, b = pem.Decode(b)
if blk != nil && blk.Type == "RSA PRIVATE KEY" {
return x509.ParsePKCS1PrivateKey(blk.Bytes)
}
}
return nil, errors.New("no RSA private key found")
}
// Open PEM-encoded RSA private key at the given path.
func openKey(path string) (*rsa.PrivateKey, error) {
r, err := os.Open(path)
if err != nil {
return nil, err
}
return parseKey(r)
}
// Parse PEM-encoded x509 certificate
func parseCert(r io.Reader) (*x509.Certificate, error) {
der, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return x509.ParseCertificate(der)
}
// Generate RSA private key and write to w in PEM format.
func genKey(w io.Writer, bits int) error {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
return pem.Encode(w, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
})
}