Last active
January 13, 2017 21:55
-
-
Save joemiller/c97a52d46cae0a4b38df841db8307fc4 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"crypto/rand" | |
"crypto/rsa" | |
"crypto/x509" | |
"crypto/x509/pkix" | |
"encoding/pem" | |
"fmt" | |
"log" | |
"math/big" | |
"os" | |
"time" | |
"github.com/davecgh/go-spew/spew" | |
) | |
func main() { | |
priv, err := rsa.GenerateKey(rand.Reader, 2048) | |
if err != nil { | |
log.Fatalf("failed to generate rsa key: %s", err) | |
} | |
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | |
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | |
if err != nil { | |
log.Fatalf("error generating serial number: %s", err) | |
} | |
template := x509.Certificate{ | |
SerialNumber: serialNumber, | |
Subject: pkix.Name{ | |
Country: []string{"c1", "c2"}, | |
Locality: []string{"l1", "l2"}, | |
StreetAddress: []string{"addr1", "addr2"}, | |
PostalCode: []string{"post1", "post2"}, | |
Province: []string{"prov1", "prov2"}, | |
Organization: []string{"Acme Co", "second org"}, | |
OrganizationalUnit: []string{"ou1", "ou2"}, | |
}, | |
NotBefore: time.Now(), | |
NotAfter: time.Now().Add(3 * time.Hour), | |
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | |
BasicConstraintsValid: true, | |
} | |
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) | |
if err != nil { | |
log.Fatalf("Failed to create certificate: %s", err) | |
} | |
certOut, err := os.Create("cert.pem") | |
if err != nil { | |
log.Fatalf("error opening cert.pem: %s", err) | |
} | |
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) | |
certOut.Close() | |
reparsed, err := x509.ParseCertificate(derBytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("re-parsed cert subject: %s\n", spew.Sdump(reparsed.Subject)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This demonstrates handling of x509 certs with multiple
OU
(OrganizationalUnit)s in golang.Summary:
x509.CreateCertificate()
is able to create certs whose subject contains multiple OUsx509.ParseCertificate()
parses certs containing multiple OUs but will drop all but the first value.This code will generate a new self-signed certificate with a subject containing two OU's:
ou1, ou2
using thex509.CreateCertificate()
func. The result of this func is written to a filecert.pem
.Next, it will call
x509.ParseCertificate()
using the DER-encoded []byte returned fromx509.CreateCertificate()
and then print the subject line decoded from the cert.Example:
The parsed certificates subject contains only the first specified values.
Next, parse the
cert.pem
using OpenSSL and see that it contains multiple values for each of the expected attributes.