generate a self-signed certificate for the handshake fuzzer

This commit is contained in:
Marten Seemann
2020-09-03 10:52:52 +07:00
parent eaf5f47308
commit b3c28ef2ea
2 changed files with 59 additions and 5 deletions

View File

@@ -1,11 +1,18 @@
package helper
import (
"crypto"
"crypto/rand"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/hex"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"time"
)
// NthBit gets the n-th bit of a byte (counting starts at 0).
@@ -35,3 +42,32 @@ func WriteCorpusFile(path string, data []byte) error {
func WriteCorpusFileWithPrefix(path string, data []byte, n int) error {
return WriteCorpusFile(path, append(make([]byte, n), data...))
}
// GenerateCertificate generates a self-signed certificate.
// It returns the certificate and a x509.CertPool containing that certificate.
func GenerateCertificate(priv crypto.Signer) (*tls.Certificate, *x509.CertPool, error) {
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{Organization: []string{"quic-go fuzzer"}},
NotBefore: time.Now().Add(-24 * time.Hour),
NotAfter: time.Now().Add(30 * 24 * time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
DNSNames: []string{"localhost"},
BasicConstraintsValid: true,
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public(), priv)
if err != nil {
return nil, nil, err
}
cert, err := x509.ParseCertificate(derBytes)
if err != nil {
return nil, nil, err
}
certPool := x509.NewCertPool()
certPool.AddCert(cert)
return &tls.Certificate{
Certificate: [][]byte{derBytes},
PrivateKey: priv,
}, certPool, nil
}