-
Notifications
You must be signed in to change notification settings - Fork 278
/
cert.go
37 lines (33 loc) · 1.02 KB
/
cert.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
// Copyright The Athenz Authors
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package athenzutils
import (
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"strings"
)
// LoadX509Certificate reads and parses the x509.Certificate from the specified file.
func LoadX509Certificate(certFile string) (*x509.Certificate, error) {
pemBytes, err := os.ReadFile(certFile)
if err != nil {
return nil, err
}
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, fmt.Errorf("unable to parse x.509 certificate pem file: %s", certFile)
}
return x509.ParseCertificate(block.Bytes)
}
// LoadPublicKey returns public key object for the given PEM data
func LoadPublicKey(publicKeyPEM []byte) (interface{}, error) {
block, _ := pem.Decode(publicKeyPEM)
if block == nil {
return nil, fmt.Errorf("unable to load public key")
}
if !strings.HasSuffix(block.Type, "PUBLIC KEY") {
return nil, fmt.Errorf("invalid public key type: %s", block.Type)
}
return x509.ParsePKIXPublicKey(block.Bytes)
}