|
16 | 16 | using System.IO; |
17 | 17 | using System.Linq; |
18 | 18 | using System.Collections.Generic; |
19 | | -using System.Runtime.ConstrainedExecution; |
20 | 19 | using System.Security.Cryptography.X509Certificates; |
21 | 20 | using System.Text; |
22 | | -using System.Xml.Linq; |
23 | 21 | using com.citrix.netscaler.nitro.exception; |
24 | 22 | using com.citrix.netscaler.nitro.resource.Base; |
25 | 23 | using com.citrix.netscaler.nitro.resource.config.ssl; |
|
28 | 26 | using com.citrix.netscaler.nitro.util; |
29 | 27 | using Keyfactor.Logging; |
30 | 28 | using Keyfactor.Orchestrators.Extensions; |
| 29 | +using Keyfactor.PKI.CryptographicObjects.Formatters; |
| 30 | +using Keyfactor.PKI.PEM; |
| 31 | +using Keyfactor.PKI.PrivateKeys; |
31 | 32 | using Microsoft.Extensions.Logging; |
32 | 33 | using Newtonsoft.Json; |
33 | 34 | using Org.BouncyCastle.Crypto; |
@@ -328,8 +329,9 @@ public void UpdateKeyPair(string keyPairName, string certFileName, string keyFil |
328 | 329 | } |
329 | 330 | catch (nitro_exception ne) |
330 | 331 | { |
331 | | - Logger.LogError($"Exception occured while trying to add or update {keyPairName}. {LogHandler.FlattenException(ne)}"); |
332 | | - throw; |
| 332 | + string error = $"Exception occured while trying to add or update {keyPairName}."; |
| 333 | + Logger.LogError(error + LogHandler.FlattenException(ne)); |
| 334 | + throw new Exception(error, ne); |
333 | 335 | } |
334 | 336 |
|
335 | 337 | Logger.MethodExit(LogLevel.Debug); |
@@ -478,47 +480,33 @@ public void LinkToIssuer(string cert, string privateKeyPassword, string keyPairN |
478 | 480 | Logger.MethodExit(LogLevel.Debug); |
479 | 481 | } |
480 | 482 |
|
481 | | - private (string, string) GetPemFromPfx(byte[] pfxBytes, char[] pfxPassword) |
| 483 | + private (string, string) GetPemFromPfx(byte[] pfxBytes, char[] pfxPassword, string storePassword) |
482 | 484 | { |
483 | 485 | Logger.MethodEntry(LogLevel.Debug); |
484 | 486 |
|
485 | 487 | try |
486 | 488 | { |
487 | | - var p = new Pkcs12Store(new MemoryStream(pfxBytes), pfxPassword); |
488 | | - |
489 | | - // Extract private key |
490 | | - var memoryStream = new MemoryStream(); |
491 | | - TextWriter streamWriter = new StreamWriter(memoryStream); |
492 | | - var pemWriter = new PemWriter(streamWriter); |
| 489 | + Pkcs12StoreBuilder storeBuilder = new Pkcs12StoreBuilder(); |
| 490 | + Pkcs12Store store = storeBuilder.Build(); |
| 491 | + store.Load(new MemoryStream(pfxBytes), pfxPassword); |
493 | 492 |
|
494 | | - var alias = p.Aliases.Cast<string>().SingleOrDefault(a => p.IsKeyEntry(a)); |
495 | | - Logger.LogTrace($"alias: {alias}"); |
| 493 | + var alias = store.Aliases.Cast<string>().SingleOrDefault(p => store.IsKeyEntry(p)); |
496 | 494 |
|
497 | | - var publicKey = p.GetCertificate(alias).Certificate.GetPublicKey(); |
498 | | - if (p.GetKey(alias) == null) throw new Exception($"Unable to get the key for alias: {alias}"); |
499 | | - var privateKey = p.GetKey(alias).Key; |
500 | | - var keyPair = new AsymmetricCipherKeyPair(publicKey, privateKey); |
| 495 | + X509CertificateEntry[] chainEntries = store.GetCertificateChain(alias); |
| 496 | + Org.BouncyCastle.X509.X509Certificate endCertificate = chainEntries[0].Certificate; |
501 | 497 |
|
502 | | - pemWriter.WriteObject(keyPair.Private); |
503 | | - streamWriter.Flush(); |
504 | | - var privateKeyString = Encoding.ASCII.GetString(memoryStream.GetBuffer()).Trim().Replace("\r", "") |
505 | | - .Replace("\0", ""); |
506 | | - memoryStream.Close(); |
507 | | - streamWriter.Close(); |
| 498 | + AsymmetricKeyParameter privateKey = store.GetKey(alias).Key; |
| 499 | + PrivateKeyConverter keyConverter = PrivateKeyConverterFactory.FromBCPrivateKeyAndCert(privateKey, endCertificate); |
508 | 500 |
|
509 | | - // Extract server certificate |
510 | | - var certStart = "-----BEGIN CERTIFICATE-----\n"; |
511 | | - var certEnd = "\n-----END CERTIFICATE-----"; |
| 501 | + string pemString = CryptographicObjectFormatter.PEM.Format(endCertificate, false); |
| 502 | + string keyString = string.Empty; |
512 | 503 |
|
513 | | - string Pemify(string ss) |
514 | | - { |
515 | | - return ss.Length <= 64 ? ss : ss.Substring(0, 64) + "\n" + Pemify(ss.Substring(64)); |
516 | | - } |
| 504 | + if (string.IsNullOrEmpty(storePassword)) |
| 505 | + keyString = PemUtilities.DERToPEM(keyConverter.ToPkcs8BlobUnencrypted(), Keyfactor.PKI.PEM.PemUtilities.PemObjectType.PrivateKey); |
| 506 | + else |
| 507 | + keyString = CryptographicObjectFormatter.PEM.Format(keyConverter, storePassword); |
517 | 508 |
|
518 | | - var certPem = |
519 | | - certStart + Pemify(Convert.ToBase64String(p.GetCertificate(alias).Certificate.GetEncoded())) + |
520 | | - certEnd; |
521 | | - return (certPem, privateKeyString); |
| 509 | + return (pemString, keyString); |
522 | 510 | } |
523 | 511 | catch (Exception e) |
524 | 512 | { |
@@ -611,14 +599,14 @@ private systemfile GetSystemFile(string fileName) |
611 | 599 | } |
612 | 600 | } |
613 | 601 |
|
614 | | - public (systemfile pemFile, systemfile privateKeyFile) UploadCertificate(string contents, string pwd, |
| 602 | + public (systemfile pemFile, systemfile privateKeyFile) UploadCertificate(string contents, string certTempPassword, string storePassword, |
615 | 603 | string alias, bool overwrite) |
616 | 604 | { |
617 | 605 | Logger.MethodEntry(LogLevel.Debug); |
618 | 606 |
|
619 | 607 | try |
620 | 608 | { |
621 | | - var (certificate, privateKey) = GetPemFromPfx(Convert.FromBase64String(contents), pwd.ToCharArray()); |
| 609 | + var (certificate, privateKey) = GetPemFromPfx(Convert.FromBase64String(contents), certTempPassword.ToCharArray(), storePassword); |
622 | 610 |
|
623 | 611 | //upload certificate and key |
624 | 612 | systemfile certificateFile = UploadFile(alias, certificate, true, 0); |
|
0 commit comments