This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
/
crypto.go
132 lines (111 loc) · 2.83 KB
/
crypto.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package acme
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/binary"
"errors"
"fmt"
"math/big"
)
func VerifySignature(sig *Signature, content []byte) (bool, error) {
if sig.Jwk.Kty != "RSA" {
// Unsupported key type
return false, errors.New("Unsupported key type")
}
// Compute signature input
nonceDec, err := Base64Decode(sig.Nonce)
if nil != err {
return false, err
}
if len(nonceDec) != 16 {
return false, fmt.Errorf("Invalid nonce length %d", len(nonceDec))
}
signatureInput := append(nonceDec, content...)
// Compute message digest
var hashType crypto.Hash
switch sig.Alg {
case "RS1":
hashType = crypto.SHA1
case "RS256":
hashType = crypto.SHA256
case "RS384":
hashType = crypto.SHA384
case "RS512":
hashType = crypto.SHA512
default:
return false, errors.New("Unsupported algorithm")
}
if !hashType.Available() {
return false, errors.New("Unsupported algorithm")
}
h := hashType.New()
h.Write(signatureInput)
publicKey, err := rsaPublicKeyFromJwk(sig.Jwk)
if nil != err {
return false, err
}
sigThing, err := Base64Decode(sig.Sig)
if nil != err {
return false, err
}
err = rsa.VerifyPKCS1v15(publicKey, hashType, h.Sum(nil), sigThing)
if nil != err {
return false, err
}
return true, nil
}
func VerifyCsr(csr *x509.CertificateRequest) error {
var hashType crypto.Hash
switch csr.SignatureAlgorithm {
case x509.SHA1WithRSA:
hashType = crypto.SHA1
case x509.SHA256WithRSA:
hashType = crypto.SHA256
case x509.SHA384WithRSA:
hashType = crypto.SHA384
case x509.SHA512WithRSA:
hashType = crypto.SHA512
default:
return x509.ErrUnsupportedAlgorithm
}
if !hashType.Available() {
return x509.ErrUnsupportedAlgorithm
}
h := hashType.New()
h.Write(csr.RawTBSCertificateRequest)
// only support RSA keys
switch pub := csr.PublicKey.(type) {
case *rsa.PublicKey:
return rsa.VerifyPKCS1v15(pub, hashType, h.Sum(nil), csr.Signature)
}
return x509.ErrUnsupportedAlgorithm
}
func rsaPublicKeyFromJwk(jwk Jwk) (*rsa.PublicKey, error) {
n, err := parseRSAModulusParam(jwk.N)
if err != nil {
return nil, fmt.Errorf("JWK RSA Public Key modulus: %s", err)
}
e, err := parseRSAPublicExponentParam(jwk.E)
if err != nil {
return nil, fmt.Errorf("JWK RSA Public Key exponent: %s", err)
}
return &rsa.PublicKey{N: n, E: e}, nil
}
func parseRSAModulusParam(nB64Url string) (*big.Int, error) {
nBytes, err := Base64Decode(nB64Url)
if err != nil {
return nil, fmt.Errorf("invalid base64 URL encoding: %s", err)
}
return new(big.Int).SetBytes(nBytes), nil
}
func parseRSAPublicExponentParam(eB64Url string) (int, error) {
eBytes, err := Base64Decode(eB64Url)
if err != nil {
return 0, fmt.Errorf("invalid base64 URL encoding: %s", err)
}
byteLen := len(eBytes)
buf := make([]byte, 4-byteLen, 4)
eBytes = append(buf, eBytes...)
return int(binary.BigEndian.Uint32(eBytes)), nil
}