forked from sigstore/rekor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx509.go
More file actions
206 lines (183 loc) · 5.11 KB
/
x509.go
File metadata and controls
206 lines (183 loc) · 5.11 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package x509
import (
"bytes"
"crypto"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
validator "github.com/go-playground/validator/v10"
"github.com/sigstore/sigstore/pkg/cryptoutils"
sigsig "github.com/sigstore/sigstore/pkg/signature"
)
// EmailAddressOID defined by https://oidref.com/1.2.840.113549.1.9.1
var EmailAddressOID asn1.ObjectIdentifier = []int{1, 2, 840, 113549, 1, 9, 1}
type Signature struct {
signature []byte
}
// NewSignature creates and validates an x509 signature object
func NewSignature(r io.Reader) (*Signature, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return &Signature{
signature: b,
}, nil
}
// CanonicalValue implements the pki.Signature interface
func (s Signature) CanonicalValue() ([]byte, error) {
return s.signature, nil
}
// Verify implements the pki.Signature interface
func (s Signature) Verify(r io.Reader, k interface{}, opts ...sigsig.VerifyOption) error {
if len(s.signature) == 0 {
//lint:ignore ST1005 X509 is proper use of term
return fmt.Errorf("X509 signature has not been initialized")
}
key, ok := k.(*PublicKey)
if !ok {
return fmt.Errorf("invalid public key type for: %v", k)
}
p := key.key
if p == nil {
p = key.cert.c.PublicKey
}
verifier, err := sigsig.LoadVerifier(p, crypto.SHA256)
if err != nil {
return err
}
return verifier.VerifySignature(bytes.NewReader(s.signature), r, opts...)
}
// PublicKey Public Key that follows the x509 standard
type PublicKey struct {
key interface{}
cert *cert
}
type cert struct {
c *x509.Certificate
b []byte
}
// NewPublicKey implements the pki.PublicKey interface
func NewPublicKey(r io.Reader) (*PublicKey, error) {
rawPub, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
block, _ := pem.Decode(rawPub)
if block == nil {
return nil, errors.New("invalid public key: failure decoding PEM")
}
switch block.Type {
case string(cryptoutils.PublicKeyPEMType):
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
return &PublicKey{key: key}, nil
case string(cryptoutils.CertificatePEMType):
c, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return &PublicKey{
cert: &cert{
c: c,
b: block.Bytes,
}}, nil
}
return nil, fmt.Errorf("invalid public key: cannot handle type %v", block.Type)
}
// CanonicalValue implements the pki.PublicKey interface
func (k PublicKey) CanonicalValue() (encoded []byte, err error) {
switch {
case k.key != nil:
encoded, err = cryptoutils.MarshalPublicKeyToPEM(k.key)
case k.cert != nil:
encoded, err = cryptoutils.MarshalCertificateToPEM(k.cert.c)
default:
err = fmt.Errorf("x509 public key has not been initialized")
}
return
}
func (k PublicKey) CryptoPubKey() crypto.PublicKey {
if k.cert != nil {
return k.cert.c.PublicKey
}
return k.key
}
// EmailAddresses implements the pki.PublicKey interface
func (k PublicKey) EmailAddresses() []string {
var names []string
if k.cert != nil {
for _, name := range k.cert.c.EmailAddresses {
validate := validator.New()
errs := validate.Var(name, "required,email")
if errs == nil {
names = append(names, strings.ToLower(name))
}
}
}
return names
}
func CertChainToPEM(certChain []*x509.Certificate) ([]byte, error) {
var pemBytes bytes.Buffer
for _, cert := range certChain {
if err := pem.Encode(&pemBytes, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
return nil, err
}
}
return pemBytes.Bytes(), nil
}
func ParseTimestampCertChain(pemBytes []byte) ([]*x509.Certificate, error) {
certChain := []*x509.Certificate{}
var block *pem.Block
block, pemBytes = pem.Decode(pemBytes)
for ; block != nil; block, pemBytes = pem.Decode(pemBytes) {
if block.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
certChain = append(certChain, cert)
} else {
return nil, errors.New("invalid block type")
}
}
if len(certChain) == 0 {
return nil, errors.New("no valid certificates in chain")
}
// Verify cert chain for timestamping
roots := x509.NewCertPool()
intermediates := x509.NewCertPool()
for _, cert := range certChain[1:(len(certChain) - 1)] {
intermediates.AddCert(cert)
}
roots.AddCert(certChain[len(certChain)-1])
if _, err := certChain[0].Verify(x509.VerifyOptions{
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageTimeStamping},
Intermediates: intermediates,
}); err != nil {
return nil, err
}
return certChain, nil
}