diff options
Diffstat (limited to 'src/main.lib/Plugins/CsrPlugins/Ec/Ec.cs')
-rw-r--r-- | src/main.lib/Plugins/CsrPlugins/Ec/Ec.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/main.lib/Plugins/CsrPlugins/Ec/Ec.cs b/src/main.lib/Plugins/CsrPlugins/Ec/Ec.cs new file mode 100644 index 0000000..c09dba4 --- /dev/null +++ b/src/main.lib/Plugins/CsrPlugins/Ec/Ec.cs @@ -0,0 +1,70 @@ +using Org.BouncyCastle.Asn1; +using Org.BouncyCastle.Asn1.Sec; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Generators; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Security; +using PKISharp.WACS.Services; +using System; +using System.IO; + +namespace PKISharp.WACS.Plugins.CsrPlugins +{ + class Ec : CsrPlugin<Ec, EcOptions> + { + public Ec( + ILogService log, + ISettingsService settings, + PemService pemService, + EcOptions options) : base(log, settings, options, pemService) { } + + internal override AsymmetricCipherKeyPair GenerateNewKeyPair() + { + var generator = new ECKeyPairGenerator(); + var curve = GetEcCurve(); + var genParam = new ECKeyGenerationParameters( + SecNamedCurves.GetOid(curve), + new SecureRandom()); + generator.Init(genParam); + return generator.GenerateKeyPair(); + } + + /// <summary> + /// Parameters to generate the key for + /// </summary> + /// <returns></returns> + private string GetEcCurve() + { + var ret = "secp384r1"; // Default + try + { + var config = _settings.ECCurve; + DerObjectIdentifier curveOid = null; + try + { + curveOid = SecNamedCurves.GetOid(config); + } + catch {} + if (curveOid != null) + { + ret = config; + } + else + { + _log.Warning("Unknown curve {ECCurve}", config); + } + } + catch (Exception ex) + { + _log.Warning("Unable to get EC name, error: {@ex}", ex); + } + _log.Debug("ECCurve: {ECCurve}", ret); + return ret; + } + + public override string GetSignatureAlgorithm() + { + return "SHA512withECDSA"; + } + } +} |