Skip to content

Commit a202e03

Browse files
committed
Added support for custom serial number for the forged certificate
1 parent 737917b commit a202e03

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

ForgeCert/CommandLineOptions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CommandLine;
2+
using Org.BouncyCastle.Math;
23

34
namespace ForgeCert
45
{
@@ -24,5 +25,8 @@ class CommandLineOptions
2425

2526
[Option("CRL", Required = false, HelpText = "ldap path to a CRL for the forged certificate")]
2627
public string CRLPath { get; set; }
28+
29+
[Option("Serial", Required = false, HelpText = "serial number for the forged certificate")]
30+
public BigInteger SerialNumber { get; set; }
2731
}
2832
}

ForgeCert/Program.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ private static void Start(CommandLineOptions options)
4343
options.SubjectAltName,
4444
caKeyPair,
4545
subjectKeyPair.Public,
46-
options.CRLPath
46+
options.CRLPath,
47+
options.SerialNumber
4748
);
4849

4950
PrintCertInfo("\nForged Certificate Information:", cert);
@@ -102,7 +103,8 @@ private static X509Certificate GenerateCertificate(
102103
X509Name issuer, string subject, string subjectAltName,
103104
KeyPair issuerKeyPair,
104105
AsymmetricKeyParameter subjectPublic,
105-
string CRL = "")
106+
string CRL = "",
107+
BigInteger SerialNumber = null)
106108
{
107109
ISignatureFactory signatureFactory;
108110
if (issuerKeyPair.Key is ECPrivateKeyParameters)
@@ -121,8 +123,15 @@ private static X509Certificate GenerateCertificate(
121123
var certGenerator = new X509V3CertificateGenerator();
122124
certGenerator.SetIssuerDN(issuer);
123125
certGenerator.SetSubjectDN(new X509Name(subject));
124-
certGenerator.SetSerialNumber(BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.Two.Pow(128), Random));
125-
126+
127+
if (SerialNumber == null)
128+
{
129+
certGenerator.SetSerialNumber(BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.Two.Pow(128), Random));
130+
} else
131+
{
132+
certGenerator.SetSerialNumber(SerialNumber);
133+
}
134+
126135
// Yes, the end lifetime can be changed easily, up to the lifetime of the CA certificate being used to forge
127136
certGenerator.SetNotAfter(DateTime.UtcNow.AddYears(1));
128137

0 commit comments

Comments
 (0)