Debian Codegraph @

test/fuzz/pem/fuzzcert_test.go

raw
1 //
2 // Copyright 2022 The Sigstore Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package pem
17
18 import (
19 "bytes"
20 "crypto/x509"
21 "crypto/x509/pkix"
22 "encoding/pem"
23 "reflect"
24 "testing"
25
26 "github.com/sigstore/sigstore/pkg/cryptoutils"
27 )
28
29 func FuzzLoadCertificates(f *testing.F) {
30 f.Fuzz(func(t *testing.T, data []byte) {
31 b, _ := pem.Decode(data)
32 if b == nil {
33 t.Skip("invalid pem")
34 }
35
36 result, err := cryptoutils.LoadCertificatesFromPEM(bytes.NewReader(data))
37 if err != nil {
38 if result != nil {
39 t.Errorf("result %v should be nil when there is an error %v", result, err)
40 }
41 t.Skip("invalid pem")
42 }
43 for _, cert := range result {
44 if len(cert.Raw) == 0 {
45 t.Errorf("x509 cert raw is empty")
46 }
47 }
48 })
49 }
50
51 func FuzzUnmarshalCertificatesFromPEM(f *testing.F) {
52 f.Fuzz(func(t *testing.T, data []byte) {
53 b, _ := pem.Decode(data)
54 if b == nil {
55 t.Skip("invalid pem")
56 }
57 result, err := cryptoutils.UnmarshalCertificatesFromPEM(data)
58 if err != nil {
59 if result != nil {
60 t.Errorf("result %v should be nil when there is an error %v", result, err)
61 }
62 t.Skip("invalid pem")
63 }
64 for _, cert := range result {
65 if len(cert.Raw) == 0 {
66 t.Errorf("x509 cert raw is empty")
67 }
68 }
69 })
70 }
71
72 func FuzzUnmarshalPEMToPublicKey(f *testing.F) {
73 f.Fuzz(func(t *testing.T, data []byte) {
74 b, _ := pem.Decode(data)
75 if b == nil {
76 t.Skip("invalid pem")
77 }
78 result, err := cryptoutils.UnmarshalPEMToPublicKey(data)
79 if err != nil {
80 if result != nil && !reflect.ValueOf(result).IsNil() {
81 t.Errorf("result %v should be nil when there is an error %v", result, err)
82 }
83 t.Skip("invalid pem")
84 }
85 if result == nil {
86 t.Errorf("result %v should not be nil", result)
87 }
88 })
89 }
90
91 func FuzzCertificate(f *testing.F) {
92 f.Fuzz(func(t *testing.T, withLimited bool, pemBytes []byte, iterations int) {
93 var certs []*x509.Certificate
94 var err error
95 if withLimited {
96 certs, err = cryptoutils.UnmarshalCertificatesFromPEMLimited(pemBytes, iterations)
97 if err != nil {
98 return
99 }
100 cryptoutils.MarshalCertificatesToPEM(certs)
101 } else {
102 certs, err = cryptoutils.UnmarshalCertificatesFromPEM(pemBytes)
103 if err != nil {
104 return
105 }
106 cryptoutils.MarshalCertificatesToPEM(certs)
107 }
108 for _, cert := range certs {
109 cryptoutils.GetSubjectAlternateNames(cert)
110 }
111 })
112 }
113
114 func FuzzPrivateKey(f *testing.F) {
115 f.Fuzz(func(t *testing.T, pemBytes, password []byte) {
116 passFunc := cryptoutils.StaticPasswordFunc(password)
117 privateKey, err := cryptoutils.UnmarshalPEMToPrivateKey(pemBytes, passFunc)
118 if err != nil {
119 return
120 }
121
122 cryptoutils.MarshalPrivateKeyToPEM(privateKey)
123 cryptoutils.MarshalPrivateKeyToEncryptedDER(privateKey, passFunc)
124 })
125 }
126
127 func FuzzPublicKey(f *testing.F) {
128 f.Fuzz(func(t *testing.T, firstKeyBytes, secondKeyBytes []byte) {
129 first, err := cryptoutils.UnmarshalPEMToPublicKey(firstKeyBytes)
130 if err != nil {
131 return
132 }
133 second, err := cryptoutils.UnmarshalPEMToPublicKey(secondKeyBytes)
134 if err != nil {
135 return
136 }
137 cryptoutils.EqualKeys(first, second)
138 cryptoutils.MarshalPublicKeyToPEM(first)
139 cryptoutils.MarshalPublicKeyToPEM(second)
140 cryptoutils.SKID(first)
141 cryptoutils.SKID(second)
142 })
143 }
144
145 func FuzzUnmarshalOtherNameSAN(f *testing.F) {
146 f.Fuzz(func(t *testing.T, value []byte) {
147 exts := []pkix.Extension{
148 pkix.Extension{
149 Id: cryptoutils.SANOID,
150 Value: value,
151 },
152 }
153 cryptoutils.UnmarshalOtherNameSAN(exts)
154 })
155 }
156
157 func FuzzMarshalUnmarshalOtherNameSAN(f *testing.F) {
158 f.Fuzz(func(t *testing.T, name string, critical bool) {
159 ext, err := cryptoutils.MarshalOtherNameSAN(name, critical)
160 if err != nil {
161 return
162 }
163 exts := []pkix.Extension{*ext}
164 cryptoutils.UnmarshalOtherNameSAN(exts)
165 })
166 }