-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reconcile 3.0.0 to main * Added additional store types, pfx discovery and inventory complete. * jks stores discovery and inventory complete * PKCS12 store discovery and inventory completed. * added add, remove, and create functionality to JKS store. Added store type classes. * Completed add/remove/create for PFX and PKCS12 * Added capability to PEM cert stores for creating an empty store location. * Update integration-manifest.json removing storepathtype and storepathvalue to prevent errors in KFUtil when creating store types.
- Loading branch information
1 parent
a82ff1d
commit 20a84ef
Showing
25 changed files
with
2,165 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2023 Keyfactor | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
// and limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Keyfactor.Logging; | ||
using Keyfactor.Orchestrators.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using Org.BouncyCastle.OpenSsl; | ||
using Org.BouncyCastle.Pkcs; | ||
|
||
namespace Keyfactor.Extensions.Orchestrator.HashicorpVault | ||
{ | ||
public static class CertUtility | ||
{ | ||
public static List<CurrentInventoryItem> CurrentInventoryFromPkcs12(Pkcs12Store store) | ||
{ | ||
var logger = LogHandler.GetClassLogger<HcvKeyValueClient>(); | ||
|
||
logger.MethodEntry(); | ||
var certs = new List<CurrentInventoryItem>(); | ||
|
||
try | ||
{ | ||
using (var memoryStream = new MemoryStream()) | ||
{ | ||
using (TextWriter streamWriter = new StreamWriter(memoryStream)) | ||
{ | ||
logger.LogTrace("Extracting Private Key..."); | ||
var pemWriter = new PemWriter(streamWriter); | ||
logger.LogTrace("Created pemWriter..."); | ||
var aliases = store.Aliases.Cast<string>().Where(a => store.IsKeyEntry(a)); | ||
//logger.LogTrace($"Alias = {alias}"); | ||
foreach (var alias in aliases) | ||
{ | ||
var certInventoryItem = new CurrentInventoryItem { Alias = alias }; | ||
|
||
var entryCerts = new List<string>(); | ||
logger.LogTrace("extracting public key"); | ||
var publicKey = store.GetCertificate(alias).Certificate.GetPublicKey(); | ||
var privateKeyEntry = store.GetKey(alias); | ||
if (privateKeyEntry != null) certInventoryItem.PrivateKeyEntry = true; | ||
pemWriter.WriteObject(publicKey); | ||
streamWriter.Flush(); | ||
var publicKeyString = Encoding.ASCII.GetString(memoryStream.GetBuffer()).Trim() | ||
.Replace("\r", "").Replace("\0", ""); | ||
entryCerts.Add(publicKeyString); | ||
|
||
var pemChain = new List<string>(); | ||
|
||
logger.LogTrace("getting chain certs"); | ||
|
||
var chain = store.GetCertificateChain(alias).ToList(); | ||
|
||
chain.ForEach(c => | ||
{ | ||
var cert = c.Certificate.GetEncoded(); | ||
var encoded = Pemify(Convert.ToBase64String(cert)); | ||
pemChain.Add(encoded); | ||
}); | ||
|
||
if (chain.Count() > 0) | ||
{ | ||
certInventoryItem.UseChainLevel = true; | ||
entryCerts.AddRange(pemChain); | ||
} | ||
certInventoryItem.Certificates = pemChain; | ||
certs.Add(certInventoryItem); | ||
} | ||
memoryStream.Close(); | ||
streamWriter.Close(); | ||
} | ||
logger.MethodExit(); | ||
return certs; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError("error extracting certs from pkcs12", ex); | ||
throw; | ||
} | ||
} | ||
|
||
public static Func<string, string> Pemify = base64Cert => | ||
{ | ||
string FormatBase64(string ss) => | ||
ss.Length <= 64 ? ss : ss.Substring(0, 64) + "\n" + FormatBase64(ss.Substring(64)); | ||
string header = "-----BEGIN CERTIFICATE-----\n"; | ||
string footer = "\n-----END CERTIFICATE-----"; | ||
return header + FormatBase64(base64Cert) + footer; | ||
}; | ||
|
||
public static string GenerateRandomString(int length) | ||
{ | ||
using (Aes crypto = Aes.Create()) | ||
{ | ||
crypto.GenerateKey(); | ||
return Convert.ToBase64String(crypto.Key).Substring(0, length); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2023 Keyfactor | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
// and limitations under the License. | ||
|
||
using System.Collections.Generic; | ||
using Keyfactor.Orchestrators.Extensions; | ||
|
||
namespace Keyfactor.Extensions.Orchestrator.HashicorpVault | ||
{ | ||
public interface IFileStore | ||
{ | ||
string AddCertificate(string alias, string pfxPassword, string entryContents, bool includeChain, string certContent, string passphrase); | ||
string RemoveCertificate(string alias, string passphrase, string storeFileContent); | ||
byte[] CreateFileStore(string passphrase); | ||
IEnumerable<CurrentInventoryItem> GetInventory(Dictionary<string, object> certFields); | ||
} | ||
} |
Oops, something went wrong.