|
| 1 | +package certificates |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rsa" |
| 5 | + "crypto/x509" |
| 6 | + "crypto/x509/pkix" |
| 7 | + "encoding/pem" |
| 8 | + "math/big" |
| 9 | + mathrand "math/rand" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func FuzzParseCertificateFromPEM(f *testing.F) { |
| 16 | + validCertPEM, validKeyPEM := generateFuzzPEMCertificate(f, 1) |
| 17 | + otherCertPEM, otherKeyPEM := generateFuzzPEMCertificate(f, 2) |
| 18 | + truncatedCertPEM := validCertPEM[:len(validCertPEM)/2] |
| 19 | + truncatedKeyPEM := validKeyPEM[:len(validKeyPEM)/2] |
| 20 | + corruptedCertPEM := corruptPEMBody(validCertPEM) |
| 21 | + corruptedKeyPEM := corruptPEMBody(validKeyPEM) |
| 22 | + |
| 23 | + seedInputs := []struct { |
| 24 | + certPEM string |
| 25 | + keyPEM string |
| 26 | + }{ |
| 27 | + {certPEM: validCertPEM, keyPEM: validKeyPEM}, |
| 28 | + {certPEM: otherCertPEM, keyPEM: otherKeyPEM}, |
| 29 | + {certPEM: validCertPEM, keyPEM: otherKeyPEM}, |
| 30 | + {certPEM: truncatedCertPEM, keyPEM: validKeyPEM}, |
| 31 | + {certPEM: validCertPEM, keyPEM: truncatedKeyPEM}, |
| 32 | + {certPEM: corruptedCertPEM, keyPEM: validKeyPEM}, |
| 33 | + {certPEM: validCertPEM, keyPEM: corruptedKeyPEM}, |
| 34 | + {certPEM: "", keyPEM: ""}, |
| 35 | + {certPEM: "invalid-pem", keyPEM: validKeyPEM}, |
| 36 | + {certPEM: invalidBase64PEM("CERTIFICATE"), keyPEM: validKeyPEM}, |
| 37 | + {certPEM: validCertPEM, keyPEM: "invalid-key"}, |
| 38 | + {certPEM: validCertPEM, keyPEM: invalidBase64PEM("RSA PRIVATE KEY")}, |
| 39 | + {certPEM: validKeyPEM, keyPEM: validCertPEM}, |
| 40 | + {certPEM: "前置\n" + validCertPEM + "後置", keyPEM: "🔐\n" + validKeyPEM}, |
| 41 | + {certPEM: strings.Repeat("A", 256), keyPEM: strings.Repeat("B", 256)}, |
| 42 | + } |
| 43 | + |
| 44 | + for _, input := range seedInputs { |
| 45 | + f.Add(input.certPEM, input.keyPEM) |
| 46 | + } |
| 47 | + |
| 48 | + f.Fuzz(func(t *testing.T, certPEM, keyPEM string) { |
| 49 | + firstCert, firstKey, firstErr := ParseCertificateFromPEM(certPEM, keyPEM) |
| 50 | + secondCert, secondKey, secondErr := ParseCertificateFromPEM(certPEM, keyPEM) |
| 51 | + |
| 52 | + if (firstErr == nil) != (secondErr == nil) { |
| 53 | + t.Fatalf("ParseCertificateFromPEM error mismatch for cert len=%d key len=%d: first=%v second=%v", len(certPEM), len(keyPEM), firstErr, secondErr) |
| 54 | + } |
| 55 | + |
| 56 | + if firstErr != nil { |
| 57 | + verifyParseCertError(t, certPEM, keyPEM, firstErr, secondErr, firstCert, secondCert, firstKey, secondKey) |
| 58 | + |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + verifyParseCertSuccess(t, certPEM, keyPEM, firstCert, secondCert, firstKey, secondKey) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func verifyParseCertError(t *testing.T, certPEM, keyPEM string, firstErr, secondErr error, firstCert, secondCert *x509.Certificate, firstKey, secondKey *rsa.PrivateKey) { |
| 67 | + t.Helper() |
| 68 | + |
| 69 | + if firstErr.Error() != secondErr.Error() { |
| 70 | + t.Fatalf("ParseCertificateFromPEM error text mismatch for cert len=%d key len=%d: first=%q second=%q", len(certPEM), len(keyPEM), firstErr.Error(), secondErr.Error()) |
| 71 | + } |
| 72 | + |
| 73 | + if firstCert != nil || secondCert != nil || firstKey != nil || secondKey != nil { |
| 74 | + t.Fatalf("ParseCertificateFromPEM returned parsed data alongside an error for cert len=%d key len=%d", len(certPEM), len(keyPEM)) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func verifyParseCertSuccess(t *testing.T, certPEM, keyPEM string, firstCert, secondCert *x509.Certificate, firstKey, secondKey *rsa.PrivateKey) { |
| 79 | + t.Helper() |
| 80 | + |
| 81 | + if firstCert == nil || secondCert == nil || firstKey == nil || secondKey == nil { |
| 82 | + t.Fatalf("ParseCertificateFromPEM returned nil data without an error for cert len=%d key len=%d", len(certPEM), len(keyPEM)) |
| 83 | + } |
| 84 | + |
| 85 | + if firstCert.SerialNumber.Cmp(secondCert.SerialNumber) != 0 { |
| 86 | + t.Fatalf("ParseCertificateFromPEM serial number mismatch for cert len=%d key len=%d", len(certPEM), len(keyPEM)) |
| 87 | + } |
| 88 | + |
| 89 | + if firstCert.Subject.CommonName != secondCert.Subject.CommonName { |
| 90 | + t.Fatalf("ParseCertificateFromPEM common name mismatch for cert len=%d key len=%d: first=%q second=%q", len(certPEM), len(keyPEM), firstCert.Subject.CommonName, secondCert.Subject.CommonName) |
| 91 | + } |
| 92 | + |
| 93 | + if firstKey.E != secondKey.E || firstKey.N.Cmp(secondKey.N) != 0 || firstKey.D.Cmp(secondKey.D) != 0 { |
| 94 | + t.Fatalf("ParseCertificateFromPEM returned different private keys for cert len=%d key len=%d", len(certPEM), len(keyPEM)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func invalidBase64PEM(blockType string) string { |
| 99 | + return "-----BEGIN " + blockType + "-----\n!!!!\n-----END " + blockType + "-----\n" |
| 100 | +} |
| 101 | + |
| 102 | +func corruptPEMBody(input string) string { |
| 103 | + block, _ := pem.Decode([]byte(input)) |
| 104 | + if block == nil || len(block.Bytes) == 0 { |
| 105 | + return input |
| 106 | + } |
| 107 | + |
| 108 | + corrupted := append([]byte(nil), block.Bytes...) |
| 109 | + index := len(corrupted) / 2 |
| 110 | + corrupted[index] ^= 0xff |
| 111 | + |
| 112 | + return string(pem.EncodeToMemory(&pem.Block{Type: block.Type, Bytes: corrupted})) |
| 113 | +} |
| 114 | + |
| 115 | +func generateFuzzPEMCertificate(tb testing.TB, seed int64) (certPEM, keyPEM string) { |
| 116 | + tb.Helper() |
| 117 | + |
| 118 | + rng := mathrand.New(mathrand.NewSource(seed)) |
| 119 | + |
| 120 | + privateKey, err := rsa.GenerateKey(rng, 1024) |
| 121 | + if err != nil { |
| 122 | + tb.Fatalf("failed to generate RSA key: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + serialNumber := new(big.Int).SetInt64(rng.Int63()) |
| 126 | + |
| 127 | + fixedTime := time.Date(2099, 1, 1, 0, 0, 0, 0, time.UTC) |
| 128 | + |
| 129 | + template := x509.Certificate{ |
| 130 | + SerialNumber: serialNumber, |
| 131 | + Subject: pkix.Name{ |
| 132 | + CommonName: "fuzz-cert", |
| 133 | + Organization: []string{"console"}, |
| 134 | + }, |
| 135 | + NotBefore: fixedTime.Add(-time.Hour), |
| 136 | + NotAfter: fixedTime.Add(24 * time.Hour), |
| 137 | + KeyUsage: x509.KeyUsageDigitalSignature, |
| 138 | + BasicConstraintsValid: true, |
| 139 | + } |
| 140 | + |
| 141 | + certBytes, err := x509.CreateCertificate(rng, &template, &template, &privateKey.PublicKey, privateKey) |
| 142 | + if err != nil { |
| 143 | + tb.Fatalf("failed to create certificate: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + certPEM = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes})) |
| 147 | + keyPEM = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})) |
| 148 | + |
| 149 | + return certPEM, keyPEM |
| 150 | +} |
0 commit comments