2018-01-21 13:18:26 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"fmt"
|
2020-01-15 09:54:03 +00:00
|
|
|
"math"
|
2018-01-21 13:18:26 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-02-02 19:29:16 +00:00
|
|
|
func ClientCertificatePath(serial uint64) string {
|
2020-01-15 09:54:03 +00:00
|
|
|
return path.Join(PKIDir, fmt.Sprintf("%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2)), "cert.pem")
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:29:16 +00:00
|
|
|
func ClientPrivkeyPath(serial uint64) string {
|
2020-01-15 09:54:03 +00:00
|
|
|
return path.Join(PKIDir, fmt.Sprintf("%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2)), "privkey.pem")
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:29:16 +00:00
|
|
|
func ClientP12Path(serial uint64) string {
|
2020-01-15 09:54:03 +00:00
|
|
|
return path.Join(PKIDir, fmt.Sprintf("%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2)), "team.p12")
|
2018-01-21 13:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 19:29:16 +00:00
|
|
|
func GenerateClient(serial uint64, notBefore time.Time, notAfter time.Time, parent_cert *x509.Certificate, parent_priv *ecdsa.PrivateKey) error {
|
|
|
|
var certid big.Int
|
|
|
|
certid.SetUint64(serial)
|
2018-01-21 13:18:26 +00:00
|
|
|
client := &x509.Certificate{
|
2018-02-02 19:29:16 +00:00
|
|
|
SerialNumber: &certid,
|
2018-01-21 13:18:26 +00:00
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"EPITA"},
|
|
|
|
OrganizationalUnit: []string{"SRS laboratory"},
|
|
|
|
Country: []string{"FR"},
|
|
|
|
Locality: []string{"Paris"},
|
2020-01-15 09:54:03 +00:00
|
|
|
CommonName: fmt.Sprintf("TEAM-%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2)),
|
2018-01-21 13:18:26 +00:00
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
IsCA: false,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub, priv, err := GeneratePrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client_b, err := x509.CreateCertificate(rand.Reader, client, parent_cert, pub, parent_priv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create intermediate directory
|
2020-01-15 09:54:03 +00:00
|
|
|
os.MkdirAll(path.Join(PKIDir, fmt.Sprintf("%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2))), 0777)
|
2018-01-21 13:18:26 +00:00
|
|
|
|
|
|
|
// Save certificate to file
|
|
|
|
if err := saveCertificate(ClientCertificatePath(serial), client_b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save private key to file
|
|
|
|
if err := savePrivateKey(ClientPrivkeyPath(serial), priv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:29:16 +00:00
|
|
|
func WriteP12(serial uint64, password string) error {
|
2018-01-21 13:18:26 +00:00
|
|
|
cmd := exec.Command("/usr/bin/openssl", "pkcs12", "-export",
|
|
|
|
"-inkey", ClientPrivkeyPath(serial),
|
|
|
|
"-in", ClientCertificatePath(serial),
|
2020-01-15 09:54:03 +00:00
|
|
|
"-name", fmt.Sprintf("TEAM-%0[2]*[1]X", serial, int(math.Ceil(math.Log2(float64(serial))/8)*2)),
|
2018-01-21 13:18:26 +00:00
|
|
|
"-passout", "pass:" + password,
|
|
|
|
"-out", ClientP12Path(serial))
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|