-
Notifications
You must be signed in to change notification settings - Fork 51
/
pkiverifier.go
111 lines (88 loc) · 2.71 KB
/
pkiverifier.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
package pkiverifier
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"fmt"
"math/big"
"time"
"github.com/dgrijalva/jwt-go"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme/cache"
)
const (
// defaultValidity is the default cache validity in seconds
defaultValidity = 1
)
// VerifierClaims captures all the clains of the verifier that is basically the public key
type VerifierClaims struct {
X *big.Int
Y *big.Int
jwt.StandardClaims
}
// PKIConfiguration is the configuration of the verifier
type PKIConfiguration struct {
publicKey *ecdsa.PublicKey
privateKey *ecdsa.PrivateKey
signMethod jwt.SigningMethod
keycache cache.DataStore
validity time.Duration
}
// NewConfig initializes a new signer structure
func NewConfig(publicKey *ecdsa.PublicKey, privateKey *ecdsa.PrivateKey, cacheValiditiy time.Duration) *PKIConfiguration {
validity := defaultValidity * time.Second
if cacheValiditiy > 0 {
validity = cacheValiditiy
}
return &PKIConfiguration{
publicKey: publicKey,
privateKey: privateKey,
signMethod: jwt.SigningMethodES256,
keycache: cache.NewCacheWithExpiration(validity),
validity: validity,
}
}
// Verify verifies a token and returns the public key
func (p *PKIConfiguration) Verify(token []byte) (*ecdsa.PublicKey, error) {
tokenString := string(token)
claims := &VerifierClaims{}
if pk, err := p.keycache.Get(tokenString); err == nil {
return pk.(*ecdsa.PublicKey), nil
}
// Parse the JWT token with the public key recovered
jwttoken, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return p.publicKey, nil
})
if err != nil || !jwttoken.Valid {
zap.L().Error("Failed to parse public key structure", zap.Error(err))
return nil, fmt.Errorf("error in token %v ", err.Error())
}
pk := KeyFromClaims(claims)
if time.Now().Add(p.validity).Unix() <= claims.ExpiresAt {
p.keycache.AddOrUpdate(tokenString, pk)
}
return pk, nil
}
// CreateTokenFromCertificate creates and signs a token
func (p *PKIConfiguration) CreateTokenFromCertificate(cert *x509.Certificate) ([]byte, error) {
// Combine the application claims with the standard claims
claims := &VerifierClaims{
X: cert.PublicKey.(*ecdsa.PublicKey).X,
Y: cert.PublicKey.(*ecdsa.PublicKey).Y,
}
claims.ExpiresAt = cert.NotAfter.Unix()
// Create the token and sign with our key
strtoken, err := jwt.NewWithClaims(p.signMethod, claims).SignedString(p.privateKey)
if err != nil {
return []byte{}, err
}
return []byte(strtoken), nil
}
// KeyFromClaims creates the public key structure from the claims
func KeyFromClaims(claims *VerifierClaims) *ecdsa.PublicKey {
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: claims.X,
Y: claims.Y,
}
}