-
Notifications
You must be signed in to change notification settings - Fork 51
/
verifier.go
181 lines (147 loc) · 4.57 KB
/
verifier.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package verifier
import (
"crypto/x509"
"encoding/asn1"
"encoding/json"
"fmt"
"sync"
"go.aporeto.io/enforcerd/trireme-lib/collector"
"go.aporeto.io/enforcerd/trireme-lib/policy"
)
// aporetoASNTagsExtension holds the value of the Aporeto Tags Extension
var aporetoASNTagsExtension asn1.ObjectIdentifier
// aporetoPingExtension holds the value of the Aporeto Ping Extension
var aporetoPingExtension asn1.ObjectIdentifier
// PolicyReporter is the interface to allow looking up policies and report stats
type PolicyReporter interface {
IDLookup(remoteContoller, remotePUID string, tags *policy.TagStore) bool
IPLookup() bool
Policy(tags *policy.TagStore) (*policy.FlowPolicy, *policy.FlowPolicy)
ReportStats(remoteType collector.EndPointType, remoteController string, remotePUID string, mode string, report *policy.FlowPolicy, packet *policy.FlowPolicy, accept bool)
}
// Verifier interface defines the methods a verifier must implement
type Verifier interface {
// TrustCA replaces the trusted CA list.
TrustCAs(caPool *x509.CertPool)
// VerifyPeerCertificate verifies if this TLS connection should be admitted.
VerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate, policy PolicyReporter, mustHaveClientIDCert bool) error
}
// verifier implements the Verifier interface
type verifier struct {
sync.RWMutex
// trustedCAs stores the list of certs to be trusted
trustedCAPool *x509.CertPool
}
func init() {
aporetoASNTagsExtension = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 50798, 1, 1}
aporetoPingExtension = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 50798, 1, 4}
}
// New returns a new instance of Verifier
func New(caPool *x509.CertPool) Verifier {
return &verifier{
trustedCAPool: caPool,
}
}
// certHasDNSOrIPSAN checks if a given name exists in a SAN for the certificate.
func certHasDNSOrIPSAN(san string, cert *x509.Certificate) bool {
// san found in SAN in certs
for _, name := range cert.DNSNames {
if san == name {
return true
}
}
for _, ip := range cert.IPAddresses {
if san == ip.String() {
return true
}
}
return false
}
// TrustCA replaces the trusted CA list.
func (v *verifier) TrustCAs(caPool *x509.CertPool) {
// Update verifier
v.Lock()
v.trustedCAPool = caPool
v.Unlock()
}
// VerifyPeerCertificate validates that policies allow mTLS between two enforcers based on
// aporeto-tags. If no aporeto tags are found, it applies IP based ACLs.
//
func (v *verifier) VerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate, pr PolicyReporter, mustHaveClientIDCert bool) error {
v.RLock()
opts := x509.VerifyOptions{
Roots: v.trustedCAPool,
KeyUsages: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
x509.ExtKeyUsageClientAuth,
},
}
v.RUnlock()
// Is this an Aporeto Cert we are trusting
if opts.Roots != nil {
for _, certChain := range verifiedChains {
tags := []string{}
ping := false
for _, cert := range certChain {
for _, e := range cert.Extensions {
if e.Id.Equal(aporetoPingExtension) {
ping = true
continue
}
// If there is an Aporeto extension, get the value
if e.Id.Equal(aporetoASNTagsExtension) {
if err := json.Unmarshal(e.Value, &tags); err == nil {
if ping {
break
}
}
}
}
// No Aporeto tags
if len(tags) == 0 {
continue
}
rtags := policy.NewTagStoreFromSlice(tags)
// check if we have remote controller
rcontroller, ok := rtags.Get(policy.TagKeyController)
if !ok {
continue
}
// check if $identity == processingunit
if pu, ok := rtags.Get(policy.TagKeyIdentity); !ok && pu != policy.TagValueProcessingUnit {
continue
}
// check if we have remote puid
rpuid, ok := rtags.Get(policy.TagKeyID)
if !ok {
continue
}
if _, err := cert.Verify(opts); err != nil {
continue
}
// TODO: Check controller against verified CA
// fmt.Println(strings.Join(tags, " "))
// if !certHasDNSOrIPSAN(controller, cert) {
// fmt.Println("No IP or DNS SAN", strings.Join(cert.DNSNames, " "))
// continue
// }
// If ping is enabled in the certificate, we defer the policy lookup and the server
// application will never receive any packets related to ping irrespective of policy.
if ping {
return nil
}
if !pr.IDLookup(rcontroller, rpuid, rtags) {
return fmt.Errorf("ID policy lookup rejection")
}
return nil
}
}
}
if mustHaveClientIDCert {
return fmt.Errorf("ID lookup not performed")
}
if !pr.IPLookup() {
return fmt.Errorf("IP policy lookup rejection")
}
return nil
}