Skip to content

Commit

Permalink
Added support for custom serial number for the forged certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
f-bader committed Aug 8, 2021
1 parent 737917b commit a202e03
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions ForgeCert/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommandLine;
using Org.BouncyCastle.Math;

namespace ForgeCert
{
Expand All @@ -24,5 +25,8 @@ class CommandLineOptions

[Option("CRL", Required = false, HelpText = "ldap path to a CRL for the forged certificate")]
public string CRLPath { get; set; }

[Option("Serial", Required = false, HelpText = "serial number for the forged certificate")]
public BigInteger SerialNumber { get; set; }
}
}
17 changes: 13 additions & 4 deletions ForgeCert/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private static void Start(CommandLineOptions options)
options.SubjectAltName,
caKeyPair,
subjectKeyPair.Public,
options.CRLPath
options.CRLPath,
options.SerialNumber
);

PrintCertInfo("\nForged Certificate Information:", cert);
Expand Down Expand Up @@ -102,7 +103,8 @@ private static X509Certificate GenerateCertificate(
X509Name issuer, string subject, string subjectAltName,
KeyPair issuerKeyPair,
AsymmetricKeyParameter subjectPublic,
string CRL = "")
string CRL = "",
BigInteger SerialNumber = null)
{
ISignatureFactory signatureFactory;
if (issuerKeyPair.Key is ECPrivateKeyParameters)
Expand All @@ -121,8 +123,15 @@ private static X509Certificate GenerateCertificate(
var certGenerator = new X509V3CertificateGenerator();
certGenerator.SetIssuerDN(issuer);
certGenerator.SetSubjectDN(new X509Name(subject));
certGenerator.SetSerialNumber(BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.Two.Pow(128), Random));


if (SerialNumber == null)
{
certGenerator.SetSerialNumber(BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.Two.Pow(128), Random));
} else
{
certGenerator.SetSerialNumber(SerialNumber);
}

// Yes, the end lifetime can be changed easily, up to the lifetime of the CA certificate being used to forge
certGenerator.SetNotAfter(DateTime.UtcNow.AddYears(1));

Expand Down

0 comments on commit a202e03

Please sign in to comment.