-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fasthttp.GenerateTestCertificate and use in tests
Remove ssl-cert-snakeoil so our tests don't fail in 2025.
- Loading branch information
1 parent
838d3ab
commit fbe6a2d
Showing
7 changed files
with
96 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package fasthttp | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"math/big" | ||
"time" | ||
) | ||
|
||
// GenerateTestCertificate generates a test certificate and private key based on the given host. | ||
func GenerateTestCertificate(host string) ([]byte, []byte, error) { | ||
priv, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
cert := &x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{"fasthttp test"}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().Add(365 * 24 * time.Hour), | ||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, | ||
SignatureAlgorithm: x509.SHA256WithRSA, | ||
DNSNames: []string{host}, | ||
BasicConstraintsValid: true, | ||
IsCA: true, | ||
} | ||
|
||
certBytes, err := x509.CreateCertificate( | ||
rand.Reader, cert, cert, &priv.PublicKey, priv, | ||
) | ||
|
||
p := pem.EncodeToMemory( | ||
&pem.Block{ | ||
Type: "PRIVATE KEY", | ||
Bytes: x509.MarshalPKCS1PrivateKey(priv), | ||
}, | ||
) | ||
|
||
b := pem.EncodeToMemory( | ||
&pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: certBytes, | ||
}, | ||
) | ||
|
||
return b, p, err | ||
} |