/// <summary> /// Get public and private keys /// </summary> /// <returns></returns> public Task <AsymmetricCipherKeyPair> GetKeys() { if (_keyPair == null) { if (_cacheData == null) { _keyPair = GenerateNewKeyPair(); _cacheData = _pemService.GetPem(_keyPair); } else { try { _keyPair = _pemService.ParsePem <AsymmetricCipherKeyPair>(_cacheData); if (_keyPair == null) { throw new InvalidDataException("key"); } } catch { _log.Error($"Unable to read cache data, creating new key..."); _cacheData = null; return(GetKeys()); } } } return(Task.FromResult(_keyPair)); }
/// <summary> /// Generate or return private key /// </summary> /// <returns></returns> public override AsymmetricKeyParameter GetPrivateKey() { if (_keyPair == null) { if (_cacheData == null) { _keyPair = GenerateNewKeyPair(); _cacheData = _pemService.GetPem(_keyPair.Private); } else { try { _keyPair = _pemService.ParsePem <AsymmetricCipherKeyPair>(_cacheData); if (_keyPair == null) { throw new InvalidDataException("key"); } } catch { throw new Exception($"Unable to read from cache file"); } } } return(_keyPair.Private); }
/// <summary> /// Generate or return private key /// </summary> /// <returns></returns> public override AsymmetricKeyParameter GetPrivateKey() { if (_keyPair == null) { if (_cacheData == null) { _keyPair = GenerateNewKeyPair(); _cacheData = _pemService.GetPem(_keyPair.Private); } else { try { _keyPair = _pemService.ParsePem <AsymmetricCipherKeyPair>(_cacheData); if (_keyPair == null) { throw new InvalidDataException("key"); } } catch { _log.Error($"Unable to read cache data, creating new key..."); _cacheData = null; return(GetPrivateKey()); } } } return(_keyPair.Private); }
public Target Generate() { // Read CSR string csrString; try { csrString = File.ReadAllText(_options.CsrFile); } catch (Exception ex) { _log.Error(ex, "Unable to read CSR from {CsrFile}", _options.CsrFile); return(null); } // Parse CSR List <string> alternativeNames; string commonName; byte[] csrBytes; try { var pem = _pem.ParsePem <Pkcs10CertificationRequest>(csrString); var info = pem.GetCertificationRequestInfo(); csrBytes = pem.GetEncoded(); commonName = ParseCn(info); alternativeNames = ParseSan(info).ToList(); if (!alternativeNames.Contains(commonName)) { alternativeNames.Add(commonName); } } catch (Exception ex) { _log.Error(ex, "Unable to parse CSR"); return(null); } AsymmetricKeyParameter pkBytes = null; if (!string.IsNullOrWhiteSpace(_options.PkFile)) { // Read PK string pkString; try { pkString = File.ReadAllText(_options.PkFile); } catch (Exception ex) { _log.Error(ex, "Unable to read private key from {PkFile}", _options.PkFile); return(null); } // Parse PK try { pkBytes = _pem.ParsePem <AsymmetricKeyParameter>(pkString); } catch (Exception ex) { _log.Error(ex, "Unable to parse private key"); return(null); } } return(new Target() { FriendlyName = $"[{nameof(Csr)}] {_options.CsrFile}", CommonName = commonName, Parts = new List <TargetPart> { new TargetPart { Identifiers = alternativeNames } }, CsrBytes = csrBytes, PrivateKey = pkBytes }); }
public async Task <Target> Generate() { // Read CSR string csrString; if (string.IsNullOrEmpty(_options.CsrFile)) { _log.Error("No CsrFile specified in options"); return(new NullTarget()); } try { csrString = File.ReadAllText(_options.CsrFile); } catch (Exception ex) { _log.Error(ex, "Unable to read CSR from {CsrFile}", _options.CsrFile); return(new NullTarget()); } // Parse CSR List <Identifier> alternativeNames; Identifier commonName; byte[] csrBytes; try { var pem = _pem.ParsePem <Pkcs10CertificationRequest>(csrString); if (pem == null) { throw new Exception("Unable decode PEM bytes to Pkcs10CertificationRequest"); } var info = pem.GetCertificationRequestInfo(); csrBytes = pem.GetEncoded(); commonName = ParseCn(info); alternativeNames = ParseSan(info).ToList(); if (!alternativeNames.Contains(commonName)) { alternativeNames.Add(commonName); } } catch (Exception ex) { _log.Error(ex, "Unable to parse CSR"); return(new NullTarget()); } AsymmetricKeyParameter?pkBytes = null; if (!string.IsNullOrWhiteSpace(_options.PkFile)) { // Read PK string pkString; try { pkString = File.ReadAllText(_options.PkFile); } catch (Exception ex) { _log.Error(ex, "Unable to read private key from {PkFile}", _options.PkFile); return(new NullTarget()); } // Parse PK try { var keyPair = _pem.ParsePem <AsymmetricCipherKeyPair>(pkString); pkBytes = keyPair != null ? keyPair.Private : _pem.ParsePem <AsymmetricKeyParameter>(pkString); if (pkBytes == null) { throw new Exception("No private key found"); } } catch (Exception ex) { _log.Error(ex, "Unable to parse private key"); return(new NullTarget()); } } var ret = new Target($"[{nameof(Csr)}] {_options.CsrFile}", commonName, new List <TargetPart> { new TargetPart(alternativeNames) }) { UserCsrBytes = csrBytes, PrivateKey = pkBytes }; return(ret); }