I am trying to create a CA certificate for local development via openSSL on Ubuntu from a script that I was provided.
Once the certificate is generated, I am trying to load it on C# via the X509Certificate2 and although the object is being created, it fails to be verified via the Verify function.
The script:
#!/usr/bin/env bash
name=server
rm -rf tmp certs
mkdir tmp
mkdir certs
echo "Generate certificate authority"
openssl genrsa -out "tmp/${name}CA.key" 2048
openssl req -x509 -config certificate-authority-options.conf -new -nodes -key "tmp/${name}CA.key" -sha256 -days 825 -out "certs/${name}CA.pem"
echo "Generate CA-signed Certificate"
openssl genrsa -out "certs/${name}.key" 2048
openssl req -new -config certificate-authority-options.conf -key "certs/${name}.key" -out "tmp/${name}.csr"
echo "Generate SSL Certificate"
openssl x509 -req -in "tmp/${name}.csr" -CA "certs/${name}CA.pem" -CAkey "tmp/${name}CA.key" -CAcreateserial -out "certs/${name}.crt" -days 825 -sha256 -extfile options.conf
# Cleanup stray file
rm certs/*.srl
The options:
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:TRUE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth, codeSigning, emailProtection
subjectAltName = @alt_names
[alt_names]
# Local hosts
DNS.1 = localhost
DNS.2 = 127.0.0.1
DNS.3 = ::1
DNS.4 = local.dev
The authority options:
[req]
prompt = no
distinguished_name = req_distinguished_name
[req_distinguished_name]
C = US
ST = Fake State
L = Fake Locality
O = Fake Company
OU = Org Unit Name
emailAddress = info@example.com
CN = local.dev
The testing code snippet:
#region
using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
#endregion
namespace Test;
public static class Program
{
public static async Task Main(string[] args)
{
var certificate = new X509Certificate2("my-certificate-path");
var chain = new X509Chain();
try
{
var chainBuilt = chain.Build(certificate);
if (chainBuilt == false)
foreach (var chainStatus in chain.ChainStatus)
{
var d = chainStatus.Status;
var b = chainStatus.StatusInformation;
}
}
catch (Exception ex)
{
// ignored
}
if (!certificate.Verify())
{
throw new Exception("Could not verify the current certificate");
}
}
}
I am using the x509Chain and put breakpoints in the debugger to try and see what the failing reasons are and I can see that the:
- chainStatus.Status is PartialChain
- chainStatus.StatusInformation is unable to get local issuer certificate
I am assuming that the dotnet API does not have an issue so it might have to be something with the way I am creating the certificate, I am really new to that and I would need some guidance/help.