Skip to content

Commit

Permalink
#104 coverage test for X509Certificate2
Browse files Browse the repository at this point in the history
  • Loading branch information
maythamfahmi committed Nov 3, 2024
1 parent da22904 commit 319b725
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
48 changes: 34 additions & 14 deletions CryptoNet.Models/CryptoNetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace CryptoNet.Models;

Expand All @@ -19,16 +20,35 @@ public class CryptoNetInfo
public AesDetail? AesDetail { get; set; }
}

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public class RsaDetail
{
public RSA Rsa { get; set; }

public RSA? Rsa { get; set; }
public byte[] PublicKey { get; set; }
public byte[] PrivateKey { get; set; }
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

public RsaDetail(RSA rsa)
{
Rsa = rsa ?? throw new ArgumentNullException(nameof(rsa));
PublicKey = Array.Empty<byte>();
PrivateKey = Array.Empty<byte>();
}

public RsaDetail(byte[] publicKey, byte[] privateKey)
{
if (publicKey == null || publicKey.Length <= 0)
{
throw new ArgumentNullException(nameof(publicKey));
}

if (privateKey == null || privateKey.Length <= 0)
{
throw new ArgumentNullException(nameof(privateKey));
}

PublicKey = publicKey;
PrivateKey = privateKey;
}
}

public class AesDetail
{
Expand All @@ -44,24 +64,24 @@ public AesDetail(byte[] key, byte[] iv)
throw new ArgumentNullException(nameof(iv));
}

AesKeyValue = new AesKeyValue()
{
Key = key,
Iv = iv
};
AesKeyValue = new AesKeyValue(key, iv);
}

public Aes? Aes { get; set; }
public AesKeyValue AesKeyValue { get; set; }
}

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public class AesKeyValue
{
public byte[] Key { get; set; }
public byte[] Iv { get; set; }
public byte[] Key { get; }
public byte[] Iv { get; }

public AesKeyValue(byte[] key, byte[] iv)
{
Key = key ?? throw new ArgumentNullException(nameof(key));
Iv = iv ?? throw new ArgumentNullException(nameof(iv));
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

public enum KeyType
{
Expand Down
48 changes: 39 additions & 9 deletions CryptoNet.UnitTests/CryptoNetRsaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ public void Encrypt_With_PublicKey_Decrypt_With_PrivateKey_Of_Content_Test()
Common.ConfidentialDummyData.ShouldBe(decryptedData);
}

[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
[Test]
public void Encrypt_Decrypt_Using_X509_Certificate_Test()
{
// Arrange
X509Certificate2? certificate = CryptoNetExtensions.GetCertificateFromStore("CN=Maytham");
// You can change to test real system certificate by using CryptoNetExtensions.GetCertificateFromStore("CN=MaythamCertificateName")
X509Certificate2 ? certificate = CreateSelfSignedCertificate();
var rsaPublicKey = new CryptoNetRsa(certificate, KeyType.PublicKey);
var rsaPrivateKey = new CryptoNetRsa(certificate, KeyType.PrivateKey);

Expand All @@ -218,11 +219,12 @@ public void Encrypt_Decrypt_Using_X509_Certificate_Test()

}

[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
[Test]
public void Export_Public_Key_For_X509_Certificate_Test()
{
// Arrange
X509Certificate2? certificate = CryptoNetExtensions.GetCertificateFromStore("CN=Maytham");
// You can change to test real system certificate by using CryptoNetExtensions.GetCertificateFromStore("CN=MaythamCertificateName")
X509Certificate2? certificate = CreateSelfSignedCertificate();
var rsa = new CryptoNetRsa(certificate, KeyType.PublicKey);

// Act
Expand All @@ -233,16 +235,17 @@ public void Export_Public_Key_For_X509_Certificate_Test()
publicKey.ShouldNotBeEmpty();
}

[Ignore("Private: This test works only on local Windows enviroment for debugging and testing. You can also put your own certifciate when debugging and testing")]
[Test]
public void Customize_PEM_Key_Encryption_Decryption_Test()
{
// Arrange
X509Certificate2? cert = CryptoNetExtensions.GetCertificateFromStore("CN=Maytham");
// You can change to test real system certificate by using CryptoNetExtensions.GetCertificateFromStore("CN=MaythamCertificateName")
X509Certificate2? certificate = CreateSelfSignedCertificate();

var pubKeyPem = Common.ExportPemKey(cert!, false);
var priKeyPem = Common.ExportPemKey(cert!);
var pubKeyPem = Common.ExportPemKey(certificate!, false);
var priKeyPem = Common.ExportPemKey(certificate!);
var password = "password";
var encryptedPriKeyBytes = Common.ExportPemKeyWithPassword(cert!, password);
var encryptedPriKeyBytes = Common.ExportPemKeyWithPassword(certificate!, password);

// Act
ICryptoNetRsa cryptoNet1 = ImportPemKeyWithPassword(encryptedPriKeyBytes, password);
Expand Down Expand Up @@ -273,4 +276,31 @@ public static ICryptoNetRsa ImportPemKeyWithPassword(byte[] encryptedPrivateKey,
cryptoNet.Info.RsaDetail?.Rsa?.ImportEncryptedPkcs8PrivateKey(password, encryptedPrivateKey, out _);
return cryptoNet;
}

public static X509Certificate2 CreateSelfSignedCertificate()
{
using var rsa = RSA.Create(2048); // Generate a new RSA key pair for the certificate
var request = new CertificateRequest(
"CN=TestCertificate",
rsa,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1
);

// Add extensions (e.g., for key usage, if needed)
request.CertificateExtensions.Add(
new X509KeyUsageExtension(
X509KeyUsageFlags.DigitalSignature,
critical: true
)
);

// Create a self-signed certificate that is valid for one year
var certificate = request.CreateSelfSigned(
DateTimeOffset.Now.AddDays(-1),
DateTimeOffset.Now.AddYears(1)
);

return certificate;
}
}

0 comments on commit 319b725

Please sign in to comment.