-
Notifications
You must be signed in to change notification settings - Fork 51
/
pkiverifier.go
147 lines (120 loc) · 3.55 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package pkiverifier
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"errors"
"math/big"
"time"
jwt "github.com/dgrijalva/jwt-go"
"go.aporeto.io/trireme-lib/utils/cache"
"go.uber.org/zap"
)
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, []string) ([]byte, error)
}
// PKITokenVerifier is the interface of an object that can verify a PKI token.
type PKITokenVerifier interface {
Verify([]byte) (*DatapathKey, error)
}
type verifierClaims struct {
X *big.Int
Y *big.Int
Tags []string `json:"tags,omitempty"`
jwt.StandardClaims
}
type tokenManager struct {
publicKeys []*ecdsa.PublicKey
privateKey *ecdsa.PrivateKey
signMethod jwt.SigningMethod
keycache cache.DataStore
validity time.Duration
}
// DatapathKey holds the data path key with the corresponding claims.
type DatapathKey struct {
PublicKey *ecdsa.PublicKey
Tags []string
Expiration time.Time
}
// 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) (*DatapathKey, error) {
tokenString := string(token)
if pk, err := p.keycache.Get(tokenString); err == nil {
return pk.(*DatapathKey), err
}
claims := &verifierClaims{}
var t *jwt.Token
var err error
for _, pk := range p.publicKeys {
if pk == nil {
continue
}
t, err = jwt.ParseWithClaims(tokenString, claims, func(_ *jwt.Token) (interface{}, error) { // nolint
return pk, nil
})
if err != nil || !t.Valid {
continue
}
expTime := time.Unix(claims.ExpiresAt, 0)
dp := &DatapathKey{
PublicKey: &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: claims.X,
Y: claims.Y,
},
Tags: claims.Tags,
Expiration: expTime,
}
p.keycache.AddOrUpdate(tokenString, dp)
// if the token expires before our default validity, update the timer
// so that we expire it no longer than its validity.
if time.Now().Add(p.validity).After(expTime) {
if err := p.keycache.SetTimeOut(tokenString, time.Until(expTime)); err != nil {
zap.L().Warn("Failed to update cache validity for token", zap.Error(err))
}
}
return dp, 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, tags []string) ([]byte, error) {
// Combine the application claims with the standard claims
claims := &verifierClaims{
X: cert.PublicKey.(*ecdsa.PublicKey).X,
Y: cert.PublicKey.(*ecdsa.PublicKey).Y,
Tags: tags,
}
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
}