-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dmitry S <[email protected]>
- Loading branch information
Showing
3 changed files
with
163 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2023 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. | ||
|
||
// code is based on the snippet https://gist.github.com/shaneutt/5e1995295cff6721c89a71d13a71c251 | ||
// with a permissive (Public Domain) License. | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"log" | ||
"math/big" | ||
"net" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// set up our CA certificate | ||
ca := &x509.Certificate{ | ||
SerialNumber: big.NewInt(2019), | ||
Subject: pkix.Name{ | ||
Organization: []string{"CA Company, INC."}, | ||
Country: []string{"US"}, | ||
Province: []string{""}, | ||
Locality: []string{"San Francisco"}, | ||
StreetAddress: []string{"Golden Gate Bridge"}, | ||
PostalCode: []string{"94016"}, | ||
}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().AddDate(10, 0, 0), | ||
IsCA: true, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning /*, x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth */}, | ||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, | ||
BasicConstraintsValid: true, | ||
EmailAddresses: []string{"[email protected]"}, | ||
} | ||
|
||
// create our private and public key | ||
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// create the CA | ||
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// pem encode | ||
caCertFile, err := os.OpenFile("cacert.pem", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | ||
if err != nil { | ||
log.Fatalf("unable to write to cacert.pem: %v", err) | ||
} | ||
err = pem.Encode(caCertFile, &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: caBytes, | ||
}) | ||
if err != nil { | ||
log.Fatalf("unable to write to cacert.pem: %v", err) | ||
} | ||
|
||
caPrivKeyFile, err := os.OpenFile("ca-key.pem", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0400) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer caPrivKeyFile.Close() | ||
err = pem.Encode(caPrivKeyFile, &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey), | ||
}) | ||
if err != nil { | ||
log.Fatalf("unable to create to ca-key.pem: %v", err) //nolint:gocritic | ||
} | ||
|
||
// set up our server certificate | ||
cert := &x509.Certificate{ | ||
SerialNumber: big.NewInt(2019), | ||
Subject: pkix.Name{ | ||
Organization: []string{"Company, INC."}, | ||
Country: []string{"US"}, | ||
Province: []string{""}, | ||
Locality: []string{"San Francisco"}, | ||
StreetAddress: []string{"Golden Gate Bridge"}, | ||
PostalCode: []string{"94016"}, | ||
}, | ||
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback}, | ||
NotBefore: time.Now(), | ||
NotAfter: time.Now().AddDate(10, 0, 0), | ||
SubjectKeyId: []byte{1, 2, 3, 4, 6}, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning /* x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth */}, | ||
KeyUsage: x509.KeyUsageDigitalSignature, | ||
EmailAddresses: []string{"[email protected]"}, | ||
DNSNames: []string{"next.hugeunicorn.xyz"}, | ||
} | ||
|
||
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
certFile, err := os.OpenFile("cert.pem", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer certFile.Close() | ||
err = pem.Encode(certFile, &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: certBytes, | ||
}) | ||
if err != nil { | ||
log.Fatalf("failed to encode cert: %v", err) | ||
} | ||
|
||
keyFile, err := os.OpenFile("key.pem", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0400) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer keyFile.Close() | ||
err = pem.Encode(keyFile, &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey), | ||
}) | ||
if err != nil { | ||
log.Fatalf("failed to encode private key: %v", err) | ||
} | ||
} |
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