#!/bin/bash

# Create the private key for the CA
certtool --generate-privkey --outfile ca-key.pem

# Generate the self-signed CA
certtool --generate-self-signed --load-privkey ca-key.pem \
    --outfile ca-cert.pem --template CA-certtool.template

# Generate a private key for server certificate
certtool --generate-privkey --outfile server-key.pem

# Generate the server certificate
certtool --generate-certificate \
    --load-privkey server-key.pem \
    --load-ca-certificate ca-cert.pem \
    --load-ca-privkey ca-key.pem \
    --outfile server-cert.pem \
    --template ServerCert-certtool.template

# Dump the human readable CA Cert in a file
certtool --certificate-info --infile ca-cert.pem \
    |& tee CACert-human-readable.txt

# Dump the human readable ServerCert in a file
certtool --certificate-info --infile server-cert.pem \
    |& tee ServerCert-human-readable.txt

# Test the CA 
#gnutls-serv --priority=NORMAL -p 9000 \
#    --x509cafile=./ca-cert.pem \
#    --x509certfile=./server-cert.pem \
#    --x509keyfile=./server-key.pem &
#
#gnutls-cli --priority=NORMAL -p 9000 \
#    --x509cafile=./ca-cert.pem localhost
