-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathverify.go
More file actions
118 lines (100 loc) · 3.7 KB
/
Copy pathverify.go
File metadata and controls
118 lines (100 loc) · 3.7 KB
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
package tls
import (
"crypto/ed25519"
"crypto/x509"
"fmt"
"time"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
)
// VerifyOptions are the certificate verification options.
type VerifyOptions struct {
// CommonName is the expected certificate common name.
CommonName string
// Keys is the set of public keys that are allowed to sign the certificate.
Keys map[signature.PublicKey]bool
// AllowUnknownKeys specifies whether any key will be allowed iff Keys is nil.
AllowUnknownKeys bool
// AllowNoCertificate specifies whether connections presenting no certificates will be allowed.
AllowNoCertificate bool
}
// VerifyCertificates verifies a TLS certificates as required by Oasis Core. Instead of using CAs,
// public key pinning is used and certificates must follow the template.
func VerifyCertificates(certs []*x509.Certificate, opts VerifyOptions) error {
// Allowing no certificate is useful in case access control is performed by a higher layer.
if len(certs) == 0 && opts.AllowNoCertificate {
return nil
}
// Make sure there is only a single certificate.
if len(certs) != 1 {
return fmt.Errorf("tls: expecting a single certificate (got: %d)", len(certs))
}
cert := certs[0]
// Public key should match the pinned key.
if cert.PublicKeyAlgorithm != x509.Ed25519 || cert.SignatureAlgorithm != x509.PureEd25519 {
return fmt.Errorf("tls: bad public key algorithm (expected: Ed25519)")
}
pk, ok := cert.PublicKey.(ed25519.PublicKey)
if !ok {
// This should never happen due to the above check.
return fmt.Errorf("tls: bad public key type (expected: Ed25519 got: %T)", cert.PublicKey)
}
if !opts.AllowUnknownKeys || opts.Keys != nil {
var spk signature.PublicKey
if err := spk.UnmarshalBinary(pk[:]); err != nil {
// This should NEVER happen.
return fmt.Errorf("tls: bad public key: %w", err)
}
if !opts.Keys[spk] {
return fmt.Errorf("tls: bad public key (%s)", spk)
}
}
// Common name should match.
if cert.Subject.CommonName != opts.CommonName {
return fmt.Errorf("tls: bad common name (expected: %s got: %s)",
opts.CommonName,
cert.Subject.CommonName,
)
}
// Certificate serial number should match the template.
if cert.SerialNumber.Cmp(certTemplate.SerialNumber) != 0 {
return fmt.Errorf("tls: bad serial number (expected: %s got: %s)",
certTemplate.SerialNumber,
cert.SerialNumber,
)
}
// Certificate key usage should match the template.
if cert.KeyUsage != certTemplate.KeyUsage {
return fmt.Errorf("tls: bad key usage (expected: %d got: %d)",
certTemplate.KeyUsage,
cert.KeyUsage,
)
}
// Certificate extended key usage should match the template.
if len(cert.ExtKeyUsage) != len(certTemplate.ExtKeyUsage) || len(cert.UnknownExtKeyUsage) != 0 {
return fmt.Errorf("tls: bad extended key usage")
}
for i, eku := range certTemplate.ExtKeyUsage {
if eku != cert.ExtKeyUsage[i] {
return fmt.Errorf("tls: bad extended key usage (expected: %d got: %d)",
eku,
cert.ExtKeyUsage[i],
)
}
}
// There should be no extra extensions.
if len(cert.ExtraExtensions) != 0 || len(cert.UnhandledCriticalExtensions) != 0 {
return fmt.Errorf("tls: bad extensions")
}
// Certificate should not be expired.
now := time.Now()
if now.Before(cert.NotBefore) {
return fmt.Errorf("tls: current time %s is before %s", now.Format(time.RFC3339), cert.NotBefore.Format(time.RFC3339))
} else if now.After(cert.NotAfter) {
return fmt.Errorf("tls: current time %s is after %s", now.Format(time.RFC3339), cert.NotAfter.Format(time.RFC3339))
}
// Signature should be valid.
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err != nil {
return fmt.Errorf("tls: bad signature: %w", err)
}
return nil
}