Skip to content

Commit 63ed82f

Browse files
committed
refactor(crypto): remove System.Security.Cryptography refs, use BouncyCastle
Per the project's crypto policy: all certificate/key/hash handling must use BouncyCastle, never BCL System.Security.Cryptography. Two legacy violations remained: - ExtractSerialFromPem in CERTInextCAPlugin.cs used X509Certificate2 to parse a cert's serial number for audit logging. Replaced with Org.BouncyCastle.X509.X509CertificateParser; output is the same uppercase-hex serial format the BCL produced via .SerialNumber. Side benefit: silences the SYSLIB0057 warning that X509Certificate2's byte[] constructor produces on net10.0. - ComputeAuthKey in CERTInextClient.cs used SHA256.HashData to derive the CERTInext request authKey. Replaced with Org.BouncyCastle.Crypto.Digests.Sha256Digest. Bytes-out are identical to the BCL implementation by construction (both are stock SHA-256), so the wire-level authKey is unchanged. Verified live against the sandbox: Ping/GetProductDetails/ListOrders all PASS, meaning the new authKey continues to authenticate. Removed the now-unused `using System.Security.Cryptography;` from CERTInextClient.cs. 146/146 unit tests pass; live Ping/Sync verifies the wire-level behaviour is unchanged.
1 parent dd82989 commit 63ed82f

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

CERTInext/CERTInextCAPlugin.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,9 @@ private static string GetStringValue(
16601660
/// Extracts the X.509 serial number from a PEM-encoded certificate for inclusion
16611661
/// in audit log entries. Returns "(parse-error)" rather than throwing, so that a
16621662
/// logging failure never suppresses an audit record.
1663+
///
1664+
/// Implemented with BouncyCastle (per the project's crypto policy: all certificate
1665+
/// and key handling goes through BouncyCastle, never BCL System.Security.Cryptography).
16631666
/// </summary>
16641667
private static string ExtractSerialFromPem(string pem)
16651668
{
@@ -1677,8 +1680,13 @@ private static string ExtractSerialFromPem(string pem)
16771680
return "(empty-pem)";
16781681

16791682
byte[] der = Convert.FromBase64String(b64);
1680-
using var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(der);
1681-
return cert.SerialNumber;
1683+
var parser = new Org.BouncyCastle.X509.X509CertificateParser();
1684+
var cert = parser.ReadCertificate(der);
1685+
if (cert == null)
1686+
return "(parse-error)";
1687+
// Match the prior format produced by X509Certificate2.SerialNumber:
1688+
// uppercase hex, no separators, no leading zeros for normal serials.
1689+
return cert.SerialNumber.ToString(16).ToUpperInvariant();
16821690
}
16831691
catch
16841692
{

CERTInext/Client/CERTInextClient.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Collections.Generic;
1010
using System.Net;
1111
using System.Runtime.CompilerServices;
12-
using System.Security.Cryptography;
1312
using System.Text;
1413
using System.Text.Json;
1514
using System.Threading;
@@ -1031,11 +1030,18 @@ private Task<RequestMeta> BuildMetaAsync(CancellationToken ct)
10311030

10321031
/// <summary>
10331032
/// Computes the CERTInext authKey: SHA256(accessKey + ts + txn) as lowercase hex.
1033+
///
1034+
/// Implemented with BouncyCastle (per the project's crypto policy: all hashing and
1035+
/// key handling goes through BouncyCastle, never BCL System.Security.Cryptography).
10341036
/// </summary>
10351037
private static string ComputeAuthKey(string accessKey, string ts, string txn)
10361038
{
10371039
string input = accessKey + ts + txn;
1038-
byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(input));
1040+
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
1041+
var digest = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
1042+
digest.BlockUpdate(inputBytes, 0, inputBytes.Length);
1043+
byte[] hash = new byte[digest.GetDigestSize()];
1044+
digest.DoFinal(hash, 0);
10391045
return Convert.ToHexString(hash).ToLowerInvariant();
10401046
}
10411047

0 commit comments

Comments
 (0)