-
Notifications
You must be signed in to change notification settings - Fork 6
/
signature.go
48 lines (38 loc) · 1.33 KB
/
signature.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
package util
import (
"crypto"
"crypto/x509"
"encoding/pem"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Strings reflect the x509 standard and are distinct from golang's x509 library string
var signatureAlgorithmDetails = []struct {
algo x509.SignatureAlgorithm
name string
pubKeyAlgo x509.PublicKeyAlgorithm
hash crypto.Hash
}{
{x509.DSAWithSHA256, "dsaWithSha256", x509.DSA, crypto.SHA256},
{x509.ECDSAWithSHA256, "ecdsaWithSha256", x509.ECDSA, crypto.SHA256},
{x509.SHA256WithRSA, "sha256WithRsaEncryption", x509.RSA, crypto.SHA256},
}
func GetSignatureAlgorithmFromString(name string) (x509.SignatureAlgorithm, error) {
for _, details := range signatureAlgorithmDetails {
if details.name == name {
return details.algo, nil
}
}
return -1, sdkerrors.Wrap(sdkerrors.ErrNotSupported, "signature algorithm not supported")
}
func GetUserCertificateFromString(inputCert []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(inputCert)
if block == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrNotSupported, "failed to find certificate - PEM formatted block")
}
// try to extract a single cert from ASN.1 DER data
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrNotSupported, "failed to parse certificate")
}
return cert, nil
}