/
keyfile.go
38 lines (34 loc) · 1001 Bytes
/
keyfile.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
// keyfile.go - operations with onion keyfiles.
//
// To the extent possible under law, Ivan Markin waived all copyright
// and related or neighboring rights to this module of onionutil, using the creative
// commons "cc0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package onionutil
import (
"crypto"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
)
func LoadPrivateKeyFile(filename string) (crypto.PrivateKey, crypto.PublicKey, error) {
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, nil, err
}
block, rest := pem.Decode(fileContent)
if len(rest) == len(fileContent) {
return nil, nil, fmt.Errorf("No vailid PEM blocks found")
}
switch block.Type {
case "RSA PRIVATE KEY":
sk, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, nil, err
}
return sk, sk.Public(), err
default:
return nil, nil, fmt.Errorf("Unrecognized type of PEM block")
}
}