Skip to content

Commit 171e03b

Browse files
Merge pull request #10 from Keyfactor/invfixes
Invfixes
2 parents e0d1c9e + e711dd8 commit 171e03b

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

AnyAgent/CertManager.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ public AnyErrors AddPubCert(AnyJobConfigInfo addPubConfig, CertStoreInfo ci, Nam
478478

479479
private AnyErrors RemoveCertFromDomain(AnyJobConfigInfo removeConfig, CertStoreInfo ci, NamePrefix np)
480480
{
481-
var error = new AnyErrors {HasError = false};
481+
var error = new AnyErrors { HasError = false };
482482
Logger.Trace($"Entering RemoveCertStore for {removeConfig.Job.Alias} ");
483483
Logger.Trace(
484484
$"Entering RemoveCertStore for Domain: {ci.Domain} and Certificate Store: {ci.CertificateStore}");
@@ -535,7 +535,7 @@ private AnyErrors RemoveCertFromDomain(AnyJobConfigInfo removeConfig, CertStoreI
535535

536536
private AnyErrors RemoveFile(AnyJobConfigInfo removeConfig, CertStoreInfo ci, string filename)
537537
{
538-
var error = new AnyErrors {HasError = false};
538+
var error = new AnyErrors { HasError = false };
539539
Logger.Trace($"Entering RemoveFile for {removeConfig.Job.Alias} ");
540540
Logger.Trace($"Entering RemoveFile for Domain: {ci.Domain} and Certificate Store: {ci.CertificateStore}");
541541
var apiClient = new ApiClient(removeConfig.Server.Username, removeConfig.Server.Password,
@@ -728,14 +728,14 @@ private void ReplaceCryptoObject(CertStoreInfo ci, string cryptoCertObjectName,
728728
public InventoryResult GetPublicCerts(ApiClient apiClient)
729729
{
730730
var result = new InventoryResult();
731-
var error = new AnyErrors {HasError = false};
731+
var error = new AnyErrors { HasError = false };
732732

733733
Logger.Trace("GetPublicCerts");
734734
var viewCert = new ViewPublicCertificatesRequest();
735735
var viewCertificateCollection = apiClient.ViewPublicCertificates(viewCert);
736736

737737
var intCount = 0;
738-
char[] s = {','};
738+
char[] s = { ',' };
739739

740740

741741
var intMax = Convert.ToInt32(_appConfig.AppSettings.Settings["MaxInventoryCapacity"].Value);
@@ -756,23 +756,22 @@ public InventoryResult GetPublicCerts(ApiClient apiClient)
756756

757757
Logger.Trace($"Add to List: {pc.Name}");
758758
var pem = Convert.FromBase64String(viewCertResponse.File);
759-
var pemString = Encoding.UTF8.GetString(pem);
759+
760+
var pemString = Utility.GetPemFromResponse(pem);
761+
760762
Logger.Trace($"Pem File: {pemString}");
761763

762764
if (pemString.Contains("BEGIN CERTIFICATE"))
763765
{
764766
Logger.Trace("Valid Pem File Adding to KF");
765-
var cert = new X509Certificate2(pem);
766-
var b64 = Convert.ToBase64String(cert.Export(X509ContentType.Cert));
767-
Logger.Trace($"Created X509Certificate2: {cert.SerialNumber} : {cert.Subject}");
768767

769768
if (intCount < intMax)
770769
{
771-
if (!blackList.Contains(pc.Name) && cert.Thumbprint != null)
770+
if (!blackList.Contains(pc.Name))
772771
inventoryItems.Add(
773772
new AgentCertStoreInventoryItem
774773
{
775-
Certificates = new[] {b64},
774+
Certificates = new[] { pemString },
776775
Alias = pc.Name,
777776
PrivateKeyEntry = false,
778777
ItemStatus = AgentInventoryItemStatus.Unknown,
@@ -807,7 +806,7 @@ public InventoryResult GetPublicCerts(ApiClient apiClient)
807806
public InventoryResult GetCerts(ApiClient apiClient)
808807
{
809808
var result = new InventoryResult();
810-
var error = new AnyErrors {HasError = false};
809+
var error = new AnyErrors { HasError = false };
811810

812811
Logger.Trace("GetCerts");
813812
var viewCert = new ViewCryptoCertificatesRequest(apiClient.Domain);

AnyAgent/utility.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Security.Cryptography.X509Certificates;
17+
using System.Text;
18+
using CSS.PKI.PEM;
1619
using DataPower.API.api;
1720
using Keyfactor.Platform.Extensions.Agents;
1821
using Newtonsoft.Json;
@@ -37,8 +40,8 @@ public static NamePrefix ParseStoreProperties(AnyJobConfigInfo config)
3740

3841
public static string Base64Encode(string plainText)
3942
{
40-
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
41-
return System.Convert.ToBase64String(plainTextBytes);
43+
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
44+
return Convert.ToBase64String(plainTextBytes);
4245
}
4346

4447
public static CertStoreInfo ParseCertificateConfig(AnyJobConfigInfo config)
@@ -83,6 +86,37 @@ public static string ReplaceFirstOccurrence(string source, string find, string r
8386
return result;
8487
}
8588

89+
public static string GetPemFromResponse(byte[] pem)
90+
{
91+
92+
string pemString;
93+
try
94+
{
95+
pemString = PemUtilities.DERToPEM(pem, PemUtilities.PemObjectType.Certificate);
96+
var ba = Encoding.ASCII.GetBytes(pemString);
97+
var cert = new X509Certificate2(ba);
98+
}
99+
catch (Exception e)
100+
{
101+
pemString = String.Empty;
102+
}
103+
104+
if (pemString.Length == 0)
105+
{
106+
try
107+
{
108+
pemString = Encoding.UTF8.GetString(pem);
109+
var ba = Encoding.ASCII.GetBytes(pemString);
110+
var cert = new X509Certificate2(ba);
111+
}
112+
catch (Exception)
113+
{
114+
pemString = String.Empty;
115+
}
116+
}
117+
118+
return pemString;
119+
}
86120

87121
}
88122
}

0 commit comments

Comments
 (0)