-
Notifications
You must be signed in to change notification settings - Fork 51
/
pkiverifier.go
127 lines (102 loc) · 3.02 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package pkiverifier
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"errors"
"math/big"
"time"
"github.com/dgrijalva/jwt-go"
"go.aporeto.io/trireme-lib/utils/cache"
)
const (
// defaultValidity is the default cache validity in seconds
defaultValidity = 1
)
// PKITokenIssuer is the interface of an object that can issue a PKI token.
type PKITokenIssuer interface {
CreateTokenFromCertificate(*x509.Certificate) ([]byte, error)
}
// PKITokenVerifier is the interface of an object that can verify a PKI token.
type PKITokenVerifier interface {
Verify([]byte) (*ecdsa.PublicKey, error)
}
type verifierClaims struct {
X *big.Int
Y *big.Int
jwt.StandardClaims
}
type tokenManager struct {
publicKeys []*ecdsa.PublicKey
privateKey *ecdsa.PrivateKey
signMethod jwt.SigningMethod
keycache cache.DataStore
validity time.Duration
}
// NewPKIIssuer initializes a new signer structure
func NewPKIIssuer(privateKey *ecdsa.PrivateKey) PKITokenIssuer {
return &tokenManager{
privateKey: privateKey,
signMethod: jwt.SigningMethodES256,
}
}
// NewPKIVerifier returns a new PKIConfiguration.
func NewPKIVerifier(publicKeys []*ecdsa.PublicKey, cacheValidity time.Duration) PKITokenVerifier {
validity := defaultValidity * time.Second
if cacheValidity > 0 {
validity = cacheValidity
}
return &tokenManager{
publicKeys: publicKeys,
signMethod: jwt.SigningMethodES256,
keycache: cache.NewCacheWithExpiration("PKIVerifierKey", validity),
validity: validity,
}
}
// Verify verifies a token and returns the public key
func (p *tokenManager) Verify(token []byte) (*ecdsa.PublicKey, error) {
tokenString := string(token)
if pk, err := p.keycache.Get(tokenString); err == nil {
return pk.(*ecdsa.PublicKey), nil
}
claims := &verifierClaims{}
var JWTToken *jwt.Token
var err error
for _, pk := range p.publicKeys {
JWTToken, err = jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return pk, nil
})
if err != nil || !JWTToken.Valid {
continue
}
pk := KeyFromClaims(claims)
if time.Now().Add(p.validity).Unix() <= claims.ExpiresAt {
p.keycache.AddOrUpdate(tokenString, pk)
}
return pk, nil
}
return nil, errors.New("unable to verify token against any available public key")
}
// CreateTokenFromCertificate creates and signs a token
func (p *tokenManager) 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,
}
}