-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert.go
56 lines (46 loc) · 1.14 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package unionpay
import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"math/big"
)
func parseCertificate(pemData []byte) (*x509.Certificate, error) {
// Extract the PEM-encoded data block
block, _ := pem.Decode(pemData)
if block == nil {
return nil, errors.New("cannot decode the pem file")
}
if got, want := block.Type, "CERTIFICATE"; got != want {
return nil, fmt.Errorf("unknown key type %q, want %q", got, want)
}
// Decode the certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("bad private key: %s", err)
}
return cert, nil
}
func certSerialNumber(pemData []byte) (*big.Int, error) {
cert, err := parseCertificate(pemData)
if err != nil {
return big.NewInt(0), err
}
return cert.SerialNumber, nil
}
func certSerialNumberFromFile(certpath string) (*big.Int, error) {
pemData, err := ioutil.ReadFile(certpath)
if err != nil {
return big.NewInt(0), err
}
return certSerialNumber(pemData)
}
func certPublickey(pemData []byte) (interface{}, error) {
cert, err := parseCertificate(pemData)
if err != nil {
return nil, err
}
return cert.PublicKey, nil
}