Repository navigation
Expand file tree
/
Copy pathcommon.go
More file actions
81 lines (70 loc) · 2.19 KB
/
Copy pathcommon.go
File metadata and controls
81 lines (70 loc) · 2.19 KB
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
// Copyright IBM Corp. 2022, 2025
// SPDX-License-Identifier: MPL-2.0
package nodeenrollment
import (
"crypto"
"crypto/sha256"
"crypto/x509"
"fmt"
"reflect"
"strings"
"github.com/sethvargo/go-diceware/diceware"
"golang.org/x/crypto/hkdf"
)
func IsNil(in any) bool {
if in == nil {
return true
}
switch reflect.TypeOf(in).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(in).IsNil()
}
return false
}
// ContainsKnownAlpnProto performs a simple check to see if one our defined
// ALPN protos is contained in the given set
func ContainsKnownAlpnProto(protos ...string) bool {
for _, p := range protos {
switch {
case strings.HasPrefix(p, FetchNodeCredsNextProtoV1Prefix),
strings.HasPrefix(p, AuthenticateNodeNextProtoV1Prefix),
strings.HasPrefix(p, CertificatePreferenceV1Prefix):
return true
}
}
return false
}
// SubjectKeyInfoAndKeyIdFromPubKey returns the PKIX-encoded public key and the
// library-specific key ID derived from it
func SubjectKeyInfoAndKeyIdFromPubKey(pubKey crypto.PublicKey) ([]byte, string, error) {
const op = "nodeenrollment.SubjectKeyInfoAndKeyIdFromPubKey"
if IsNil(pubKey) {
return nil, "", fmt.Errorf("(%s) pub key is nil", op)
}
pubKeyPkix, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return nil, "", fmt.Errorf("(%s) error marshaling public key: %w", op, err)
}
keyId, err := KeyIdFromPkix(pubKeyPkix)
if err != nil {
return nil, "", fmt.Errorf("(%s) error getting key id: %w", op, err)
}
return pubKeyPkix, keyId, nil
}
// KeyIdFromPkix derives the library-specific key ID from the PKIX-encoed public
// key
func KeyIdFromPkix(pkixKey []byte) (string, error) {
const op = "nodeenrollment.KeyIdFromPkix"
if IsNil(pkixKey) {
return "", fmt.Errorf("(%s) pkix key is nil", op)
}
// This never returns a non-nil error (nor is there reason for it to), so
// ignore
reader := hkdf.New(sha256.New, pkixKey, pkixKey, pkixKey)
gen, _ := diceware.NewGenerator(&diceware.GeneratorInput{RandReader: reader})
words, err := gen.Generate(KeyIdNumWords)
if err != nil {
return "", fmt.Errorf("(%s) error generating key id: %w", op, err)
}
return strings.Join(words, "-"), nil
}