/
crypto.go
70 lines (61 loc) · 1.67 KB
/
crypto.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
package byzq
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
)
// See https://golang.org/src/crypto/tls/generate_cert.go
var curve = elliptic.P256()
// ParseKey takes a PEM formatted string and returns a private key.
func ParseKey(pemKey string) (*ecdsa.PrivateKey, error) {
block, _ := pem.Decode([]byte(pemKey))
//TODO(hein) Is this correct way? Does Decode return err?
if block == nil {
return nil, fmt.Errorf("no block to decode")
}
key, err := x509.ParseECPrivateKey(block.Bytes)
//TODO(Hein) Do we need to create a new error here?
if err != nil {
return nil, fmt.Errorf("failed to parse key from pem block: %v\n %v", err, key)
}
return key, nil
}
// ReadKeyfile reads the provided keyFile and returns the private key
// stored in the file.
func ReadKeyfile(keyFile string) (*ecdsa.PrivateKey, error) {
b, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, err
}
return ParseKey(string(b))
}
// GenerateKeyfile generates a private key and writes it to the given keyFile.
// Note that if the file exists it will be overwritten.
func GenerateKeyfile(keyFile string) error {
f, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
key, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
return err
}
ec, err := x509.MarshalECPrivateKey(key)
if err != nil {
return fmt.Errorf("failed to marshal key to pem block: %v\n %v", err, key)
}
err = pem.Encode(f, &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: ec,
})
if err != nil {
return fmt.Errorf("failed to PEM encode key: %v\n %v", err, key)
}
return nil
}