Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shaneutt
Last active April 14, 2024 00:52
Show Gist options
  • Save shaneutt/5e1995295cff6721c89a71d13a71c251 to your computer and use it in GitHub Desktop.
Save shaneutt/5e1995295cff6721c89a71d13a71c251 to your computer and use it in GitHub Desktop.
Golang: Demonstrate creating a CA Certificate, and Creating and Signing Certs with the CA
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/http"
"net/http/httptest"
"strings"
"time"
)
func main() {
// get our ca and server certificate
serverTLSConf, clientTLSConf, err := certsetup()
if err != nil {
panic(err)
}
// set up the httptest.Server using our certificate signed by our CA
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "success!")
}))
server.TLS = serverTLSConf
server.StartTLS()
defer server.Close()
// communicate with the server using an http.Client configured to trust our CA
transport := &http.Transport{
TLSClientConfig: clientTLSConf,
}
http := http.Client{
Transport: transport,
}
resp, err := http.Get(server.URL)
if err != nil {
panic(err)
}
// verify the response
respBodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
body := strings.TrimSpace(string(respBodyBytes[:]))
if body == "success!" {
fmt.Println(body)
} else {
panic("not successful!")
}
}
func certsetup() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err error) {
// set up our CA certificate
ca := &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"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
// create our private and public key
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, nil, err
}
// create the CA
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
return nil, nil, err
}
// pem encode
caPEM := new(bytes.Buffer)
pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
caPrivKeyPEM := new(bytes.Buffer)
pem.Encode(caPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey),
})
// 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.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, nil, err
}
certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey)
if err != nil {
return nil, nil, err
}
certPEM := new(bytes.Buffer)
pem.Encode(certPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
})
certPrivKeyPEM := new(bytes.Buffer)
pem.Encode(certPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
})
serverCert, err := tls.X509KeyPair(certPEM.Bytes(), certPrivKeyPEM.Bytes())
if err != nil {
return nil, nil, err
}
serverTLSConf = &tls.Config{
Certificates: []tls.Certificate{serverCert},
}
certpool := x509.NewCertPool()
certpool.AppendCertsFromPEM(caPEM.Bytes())
clientTLSConf = &tls.Config{
RootCAs: certpool,
}
return
}
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
@shaneutt
Copy link
Author

Hey no problem! glad it was helpful

@shaneutt
Copy link
Author

awesome glad to hear it 😄

@nzhong
Copy link

nzhong commented Dec 2, 2019

Thank you so much, this is very helpful. I have one question though: in line 130,
certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey)
is there any way to achieve the same effect with caPEM & caPrivKeyPEM, instead of ca & caPrivKey? In my situation, we may want to re-use caPEM & caPrivKeyPEM as they are Strings. I'm not sure how to recover ca & caPrivKey between reboots.

@darwhs
Copy link

darwhs commented Oct 22, 2020

how can we use the caPEM & caPrivKeyPEM in the ListenAndServeTLS method as it takes the path of cert file and key file, right now I am trying to write this cert and key file but it doesn't work.

@whereistimbo
Copy link

What is the License of this code? Can I use this for my project?

@shaneutt
Copy link
Author

shaneutt commented Jan 1, 2021

What is the License of this code? Can I use this for my project?

I've added the LICENSE file to the gist 👍

@fanpei91
Copy link

Very very nice! Helped my a lot!

@superzoeyian
Copy link

Thank you so much, this is very helpful. I have one question though: in line 130,
certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivKey.PublicKey, caPrivKey)
is there any way to achieve the same effect with caPEM & caPrivKeyPEM, instead of ca & caPrivKey? In my situation, we may want to re-use caPEM & caPrivKeyPEM as they are Strings. I'm not sure how to recover ca & caPrivKey between reboots.

same situation here! did u solve the problem?

@phylake
Copy link

phylake commented Jul 20, 2021

Do these certificates pass openssl verify? I'm getting error 20 at 0 depth lookup: unable to get local issuer certificate with openssl verify -CAfile ca.pem cert.pem

@shaneutt
Copy link
Author

Do these certificates pass openssl verify? I'm getting error 20 at 0 depth lookup: unable to get local issuer certificate with openssl verify -CAfile ca.pem cert.pem

No, there was no intention to do so: this is meant as an example of the core workflow, but not something that should be pasted directly.

@phylake
Copy link

phylake commented Jul 21, 2021

I was creating multiple CAs and certs and had the wrong pair as input to openssl verify

@shaneutt
Copy link
Author

I was creating multiple CAs and certs and had the wrong pair as input to openssl verify

Ah, alright then no worries. If you want me to add some more examples (maybe from a shell perspective) to this example let me know I would be happy to.

@Ran-Xing
Copy link

Hello, I am using golang to create a root certificate, an intermediate certificate and a service certificate certificate, and export the public key and private key. Could you please give me another tutorial?

@reddec
Copy link

reddec commented Nov 24, 2021

@Ran-Xing
Copy link

@reddec Your project is very good

@yakuter
Copy link

yakuter commented Feb 16, 2022

Hey @shaneutt, thanks for the code. On line 130, isn't it certPrivKey not caPrivKey?

@shaneutt
Copy link
Author

Hi @yakuter,

The caPrivKey argument is given to the priv parameter in x509.CreateCertificate() which has the following documentation:

The certificate is signed by parent. If parent is equal to template then the
certificate is self-signed. The parameter pub is the public key of the
certificate to be generated and priv is the private key of the signer.

It is my understanding that if you use the certPrivKey in place of the caPrivKey when creating the x509 certificate on :130 you are instructing the certificate to sign itself, rather than being signed by the CA which will be added to the rootCA trust of the HTTP client. In either case if you make this change the program fails:

Get "https://127.0.0.1:40245": x509: certificate signed by unknown authority

To me this makes sense: we want the CA cert we created to be the signer, but perhaps I've made some mistake or missed something? Did you run into some problem when using the code or was there some other context that led you to believe there's a mistake here?

@yakuter
Copy link

yakuter commented Feb 16, 2022

Yeah I got it. Thanks for the explanation @shaneutt

@Ran-Xing
Copy link

Ran-Xing commented Feb 26, 2022

h := "127.0.0.1"
certificate := &x509.Certificate{
	Subject: pkix.Name{
		CommonName:   commonName,
	},
+	IPAddresses: []net.IP{net.ParseIP(h)},
}
if net.ParseIP(h) != nil {
-	certificate.IPAddresses = []net.IP{net.ParseIP(h)}
}

I found many cases in the gist and found that IPAddresses cannot be added @ certificate.IPAddresses = []net.IP{net.ParseIP(h)}
Do you know why this is? @shaneutt

@shaneutt
Copy link
Author

@XRSec not sure if I'm understanding the problem: using net.ParseIP("127.0.0.1") should be equivalent to the example I provided above that uses net.IPv4(127, 0, 0, 1), e.g.:

gore> :import net
gore> :import reflect
gore> a := net.ParseIP("127.0.0.1")
net.IP{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01,
}
gore> b := net.IPv4(127,0,0,1)
net.IP{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01,
}
gore> reflect.DeepEqual(a,b)
true

What are you seeing that suggests to you that it can't be added, are you receiving some kind of error?

For some simple testing you can manually verify whether the IP address you add to an x509 certificate in Go is included in the SAN using the openssl command line. For instance I ran the following by connecting to the httptest server using the exact code from above:

$ openssl s_client -connect localhost:39781 | openssl x509 -noout -text |grep -B 1 'IP Address'
            X509v3 Subject Alternative Name: 
                IP Address:127.0.0.1, IP Address:0:0:0:0:0:0:0:1

@dipankardas011
Copy link

dipankardas011 commented Dec 16, 2023

Thanks @shaneutt for this blog. it is helping me to automate certificate generation to be used for etcd cluster for my project ksctl 😄

@albertjin
Copy link

This code helps a lot! Note that Safari may reject a certificate filled NotBefore as time.Now() displaying that "xxx certificate is not standards compliant". Changing it to time.Now().Add(-1 * time.Hour) solves the problem.

@aceeric
Copy link

aceeric commented Mar 9, 2024

hey @shaneutt thank you for this - I was able to use it to build unit tests: https://github.com/aceeric/ociregistry/blob/main/impl/upstream/queue_test.go - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment