- Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathrsa_utils.go
107 lines (88 loc) · 2.87 KB
/
rsa_utils.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
98
99
100
101
102
103
104
105
106
107
package jwt
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)
var (
ErrKeyMustBePEMEncoded=errors.New("invalid key: Key must be a PEM encoded PKCS1 or PKCS8 key")
ErrNotRSAPrivateKey=errors.New("key is not a valid RSA private key")
ErrNotRSAPublicKey=errors.New("key is not a valid RSA public key")
)
// ParseRSAPrivateKeyFromPEM parses a PEM encoded PKCS1 or PKCS8 private key
funcParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
varerrerror
// Parse PEM block
varblock*pem.Block
ifblock, _=pem.Decode(key); block==nil {
returnnil, ErrKeyMustBePEMEncoded
}
varparsedKeyinterface{}
ifparsedKey, err=x509.ParsePKCS1PrivateKey(block.Bytes); err!=nil {
ifparsedKey, err=x509.ParsePKCS8PrivateKey(block.Bytes); err!=nil {
returnnil, err
}
}
varpkey*rsa.PrivateKey
varokbool
ifpkey, ok=parsedKey.(*rsa.PrivateKey); !ok {
returnnil, ErrNotRSAPrivateKey
}
returnpkey, nil
}
// ParseRSAPrivateKeyFromPEMWithPassword parses a PEM encoded PKCS1 or PKCS8 private key protected with password
//
// Deprecated: This function is deprecated and should not be used anymore. It uses the deprecated x509.DecryptPEMBlock
// function, which was deprecated since RFC 1423 is regarded insecure by design. Unfortunately, there is no alternative
// in the Go standard library for now. See https://github.com/golang/go/issues/8860.
funcParseRSAPrivateKeyFromPEMWithPassword(key []byte, passwordstring) (*rsa.PrivateKey, error) {
varerrerror
// Parse PEM block
varblock*pem.Block
ifblock, _=pem.Decode(key); block==nil {
returnnil, ErrKeyMustBePEMEncoded
}
varparsedKeyinterface{}
varblockDecrypted []byte
ifblockDecrypted, err=x509.DecryptPEMBlock(block, []byte(password)); err!=nil {
returnnil, err
}
ifparsedKey, err=x509.ParsePKCS1PrivateKey(blockDecrypted); err!=nil {
ifparsedKey, err=x509.ParsePKCS8PrivateKey(blockDecrypted); err!=nil {
returnnil, err
}
}
varpkey*rsa.PrivateKey
varokbool
ifpkey, ok=parsedKey.(*rsa.PrivateKey); !ok {
returnnil, ErrNotRSAPrivateKey
}
returnpkey, nil
}
// ParseRSAPublicKeyFromPEM parses a certificate or a PEM encoded PKCS1 or PKIX public key
funcParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
varerrerror
// Parse PEM block
varblock*pem.Block
ifblock, _=pem.Decode(key); block==nil {
returnnil, ErrKeyMustBePEMEncoded
}
// Parse the key
varparsedKeyinterface{}
ifparsedKey, err=x509.ParsePKIXPublicKey(block.Bytes); err!=nil {
ifcert, err:=x509.ParseCertificate(block.Bytes); err==nil {
parsedKey=cert.PublicKey
} else {
ifparsedKey, err=x509.ParsePKCS1PublicKey(block.Bytes); err!=nil {
returnnil, err
}
}
}
varpkey*rsa.PublicKey
varokbool
ifpkey, ok=parsedKey.(*rsa.PublicKey); !ok {
returnnil, ErrNotRSAPublicKey
}
returnpkey, nil
}