-
Notifications
You must be signed in to change notification settings - Fork 3
/
ed25519.go
97 lines (91 loc) · 2.87 KB
/
ed25519.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright 2020 Django Cass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cryptoutils
import (
"bytes"
"crypto/ed25519"
"crypto/x509"
"encoding/pem"
"errors"
"github.com/go-logr/logr"
)
var (
// ErrMissingOrBadBlock is thrown when the PEM cannot be correctly decoded
ErrMissingOrBadBlock = errors.New("failed to parse PEM block containing the key")
// ErrKeyUnknown is thrown when the key type is not ed25519
ErrKeyUnknown = errors.New("unexpected or unknown type of key")
)
// MarshalED25519PublicKey converts a crypto.ed25519 PublicKey into a string
func MarshalED25519PublicKey(log logr.Logger, pk *ed25519.PublicKey) (string, error) {
asn1Bytes, err := x509.MarshalPKIXPublicKey(*pk)
if err != nil {
log.Error(err, "failed to marshal public key")
return "", err
}
pemKey := &pem.Block{
Type: "PUBLIC KEY",
Bytes: asn1Bytes,
}
buf := new(bytes.Buffer)
err = pem.Encode(buf, pemKey)
if err != nil {
log.Error(err, "failed to encode pem key")
return "", err
}
return buf.String(), err
}
// ParseED25519PrivateKey attempts to parse a crypto.ed25519 PrivateKey from a given byte slice
func ParseED25519PrivateKey(log logr.Logger, raw []byte) (*ed25519.PrivateKey, error) {
// decode the pem
block, _ := pem.Decode(raw)
if block == nil || block.Type != "PRIVATE KEY" {
return nil, ErrMissingOrBadBlock
}
log.V(1).Info("successfully decoded PEM key", "Type", block.Type)
priv, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
// convert the resulting key into ed25519 or fail
switch priv := priv.(type) {
case ed25519.PrivateKey:
return &priv, nil
default:
return nil, ErrKeyUnknown
}
}
// ParseED25519PublicKey attempts to parse a crypto.ed25519 PublicKey from a given byte slice
func ParseED25519PublicKey(log logr.Logger, raw []byte) (*ed25519.PublicKey, error) {
// decode the pem
block, _ := pem.Decode(raw)
if block == nil || block.Type != "PUBLIC KEY" {
log.Error(ErrMissingOrBadBlock, "block is nil or unexpected type")
return nil, ErrMissingOrBadBlock
}
log.V(1).Info("successfully decoded PEM key", "Type", block.Type)
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// convert the resulting key into ed25519 or fail
switch pub := pub.(type) {
case ed25519.PublicKey:
return &pub, nil
default:
return nil, ErrKeyUnknown
}
}