forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_keys.go
39 lines (35 loc) · 941 Bytes
/
load_keys.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package security
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
"path/filepath"
)
// LoadKeypair loads the keypair in the given location
func LoadKeypair(location string) (*ecdsa.PrivateKey, error) {
priv, err := ioutil.ReadFile(filepath.Clean(location + "/server.key"))
if err != nil {
return nil, err
}
privBlock, _ := pem.Decode(priv)
if privBlock == nil {
return nil, errors.New("No private key data found")
}
privKey, err := x509.ParseECPrivateKey(privBlock.Bytes)
if err != nil {
return nil, err
}
return privKey, nil
}
// LoadCert loads the certificate in the given location
func LoadCert(location string) (cert []byte, err error) {
cert, err = ioutil.ReadFile(filepath.Clean(location + "/server.cert"))
if err != nil {
return
}
return
}