Skip to content

Commit babb024

Browse files
committed
Removed BouncyCastle dependency
1 parent 7057e0b commit babb024

3 files changed

Lines changed: 43 additions & 72 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Codecepticon Changelog
22

3+
## v1.2.2
4+
5+
* `[Update]` Removed `BouncyCastle` dependency, now certificates are generated using native .NET functionality.
6+
37
## v1.2.1
48

59
* `[New]` C#: Added support for renaming Structs.

Codecepticon/Codecepticon.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<LangVersion>9.0</LangVersion>
1010
<PackageId>Codecepticon</PackageId>
1111
<Title>Codecepticon</Title>
12-
<Version>1.2.1</Version>
12+
<Version>1.2.2</Version>
1313
<Authors>Pavel Tsakalidis</Authors>
1414
<Company>Accenture Security</Company>
1515
<Product>Codecepticon</Product>
@@ -57,7 +57,6 @@
5757

5858
<ItemGroup>
5959
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.9.2" />
60-
<PackageReference Include="BouncyCastle" Version="1.8.9" />
6160
<PackageReference Include="Microsoft.Build" Version="17.3.1" ExcludeAssets="runtime" />
6261
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.3" />
6362
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,69 @@
11
using Codecepticon.Utils;
2-
using Org.BouncyCastle.Asn1;
3-
using Org.BouncyCastle.Asn1.X509;
4-
using Org.BouncyCastle.Crypto;
5-
using Org.BouncyCastle.Crypto.Generators;
6-
using Org.BouncyCastle.Crypto.Operators;
7-
using Org.BouncyCastle.Crypto.Prng;
8-
using Org.BouncyCastle.Math;
9-
using Org.BouncyCastle.Pkcs;
10-
using Org.BouncyCastle.Security;
11-
using Org.BouncyCastle.Utilities;
12-
using Org.BouncyCastle.X509;
132
using System;
143
using System.Collections.Generic;
154
using System.IO;
5+
using System.Security.Cryptography;
6+
using System.Security.Cryptography.X509Certificates;
167

178
namespace Codecepticon.Modules.Sign
189
{
1910
class CertificateManager
2011
{
21-
private const string SignatureAlgorithm = "SHA256WithRSA";
22-
2312
private const int KeyLength = 2048;
2413

2514
public bool GenerateCertificate(string Subject, string Issuer, DateTime NotBefore, DateTime NotAfter, string Password, string PfxOutput)
2615
{
27-
// https://mcse.cloud/create-a-self-signed-certificate-with-bouncy-castle-and-c/
28-
Logger.Debug("Initialising random generators and certificate generators...");
29-
SecureRandom secureRandom = new SecureRandom(new CryptoApiRandomGenerator());
30-
31-
X509V3CertificateGenerator certificateGenerator = new X509V3CertificateGenerator();
32-
33-
// Create and set serial number.
34-
Logger.Verbose("Setting up certificate properties...");
35-
BigInteger serialNumber = BigIntegers.CreateRandomInRange(BigInteger.One, BigInteger.ValueOf(Int64.MaxValue), secureRandom);
36-
certificateGenerator.SetSerialNumber(serialNumber);
37-
38-
Logger.Debug("Creating Subject: " + Subject);
39-
X509Name subjectDN = new X509Name(true, Subject);
40-
certificateGenerator.SetSubjectDN(subjectDN);
41-
42-
Logger.Debug("Creating Issuer: " + Issuer);
43-
X509Name issuerDN = new X509Name(true, Issuer);
44-
certificateGenerator.SetIssuerDN(issuerDN);
45-
46-
Logger.Debug("Setting NotBefore: " + NotBefore);
47-
certificateGenerator.SetNotBefore(NotBefore);
48-
Logger.Debug("Setting NotAfter: " + NotAfter);
49-
certificateGenerator.SetNotAfter(NotAfter);
50-
51-
KeyGenerationParameters keyGeneration = new KeyGenerationParameters(secureRandom, KeyLength);
52-
53-
// Create RSA key.
54-
Logger.Verbose("Generating RSA keypair...");
55-
RsaKeyPairGenerator keyPairGenerator = new RsaKeyPairGenerator();
56-
keyPairGenerator.Init(keyGeneration);
57-
AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair();
58-
59-
// Add the public/private keys to the certificate generator.
60-
Logger.Debug("Setting public key...");
61-
certificateGenerator.SetPublicKey(keyPair.Public);
62-
ISignatureFactory signatureFactory = new Asn1SignatureFactory(SignatureAlgorithm, keyPair.Private, secureRandom);
63-
X509Certificate certificate = certificateGenerator.Generate(signatureFactory);
64-
65-
Logger.Debug("Creating keystore...");
66-
Pkcs12Store keyStore = new Pkcs12Store();
67-
X509CertificateEntry certificateEntry = new X509CertificateEntry(certificate);
68-
keyStore.SetCertificateEntry(certificate.SubjectDN.ToString(), certificateEntry);
69-
keyStore.SetKeyEntry(certificate.SubjectDN.ToString(), new AsymmetricKeyEntry(keyPair.Private), new[] { certificateEntry });
70-
71-
// Convert to .NET Certificate.
72-
Logger.Debug("Converting to a .NET certificate...");
73-
MemoryStream stream = new MemoryStream();
74-
keyStore.Save(stream, Password.ToCharArray(), secureRandom);
16+
// https://stackoverflow.com/a/48210587/2445959
17+
RSA keyPair = RSA.Create(KeyLength);
18+
19+
Logger.Verbose("Generating issuer certificate...");
20+
Logger.Verbose("Issuer is " + Issuer);
21+
X509Certificate2 certificateIssuer = GenerateIssuerCertificate(Issuer, NotBefore, NotAfter);
22+
23+
Logger.Info("Generating signing certificate...");
24+
Logger.Verbose("Subject is " + Subject);
25+
CertificateRequest certRequest = new(Subject, keyPair, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
26+
certRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
27+
//certRequest.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new Oid("1.3.6.1.5.5.7.3.8") }, true));
28+
certRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(certRequest.PublicKey, false));
29+
X509Certificate2 cert = certRequest.Create(certificateIssuer, NotBefore, NotAfter, new byte[] { 1, 2, 3, 4 });
30+
31+
// Add the private key back to the certificate.
32+
X509Certificate2 certificate = cert.CopyWithPrivateKey(keyPair);
33+
34+
Logger.Info("Exporting certificate to file...");
35+
File.WriteAllBytes(PfxOutput, certificate.Export(X509ContentType.Pfx, Password));
36+
37+
return true;
38+
}
7539

76-
System.Security.Cryptography.X509Certificates.X509Certificate2 netCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(stream.ToArray(), Password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.PersistKeySet | System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable);
40+
private X509Certificate2 GenerateIssuerCertificate(string Issuer, DateTime NotBefore, DateTime NotAfter)
41+
{
42+
RSA keyPair = RSA.Create(KeyLength);
7743

78-
Logger.Verbose("Writing certificate to " + PfxOutput);
79-
File.WriteAllBytes(PfxOutput, netCertificate.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pfx, Password));
80-
return true;
44+
CertificateRequest issuerRequest = new(Issuer, keyPair, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
45+
issuerRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, true));
46+
issuerRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(issuerRequest.PublicKey, false));
47+
return issuerRequest.CreateSelfSigned(NotBefore, NotAfter);
8148
}
8249

8350
public bool CheckPfxPassword(string pfxFile, string password)
8451
{
8552
try
8653
{
87-
Pkcs12Store keyStore = new Pkcs12Store(File.OpenRead(pfxFile), password.ToCharArray());
88-
} catch (Exception e)
54+
X509Certificate2 certificate = new(pfxFile, password);
55+
}
56+
catch (Exception e)
8957
{
9058
return false;
9159
}
92-
60+
9361
return true;
9462
}
9563

96-
public System.Security.Cryptography.X509Certificates.X509Certificate GetCertificateFromFile(string signedFile)
64+
public X509Certificate GetCertificateFromFile(string signedFile)
9765
{
98-
return System.Security.Cryptography.X509Certificates.X509Certificate2.CreateFromSignedFile(signedFile);
66+
return X509Certificate.CreateFromSignedFile(signedFile);
9967
}
10068
}
10169
}

0 commit comments

Comments
 (0)