Ejemplo n.º 1
0
 private string PathForIdentifier(Identifier identifier) => Path.Combine(_path, PfxFile.Filename(identifier.Value));
Ejemplo n.º 2
0
        public async Task Save(CertificateInfo input)
        {
            _log.Information("Exporting .pem files to {folder}", _path);
            try
            {
                // Determine name
                var name = PfxFile.Filename(input.CommonName.Value, "");

                // Base certificate
                var certificateExport = input.Certificate.Export(X509ContentType.Cert);
                var certString        = _pemService.GetPem("CERTIFICATE", certificateExport);
                var chainString       = "";
                await File.WriteAllTextAsync(Path.Combine(_path, $"{name}{CertFilenameSuffix}{FilenameExtension}"), certString);

                // Rest of the chain
                foreach (var chainCertificate in input.Chain)
                {
                    // Do not include self-signed certificates, root certificates
                    // are supposed to be known already by the client.
                    if (chainCertificate.Subject != chainCertificate.Issuer)
                    {
                        var chainCertificateExport = chainCertificate.Export(X509ContentType.Cert);
                        chainString += _pemService.GetPem("CERTIFICATE", chainCertificateExport);
                    }
                }

                // Save complete chain
                await File.WriteAllTextAsync(Path.Combine(_path, $"{name}{ChainFilenameSuffix}{FilenameExtension}"), certString + chainString);

                await File.WriteAllTextAsync(Path.Combine(_path, $"{name}{ChainOnlyFilenameSuffix}{FilenameExtension}"), chainString);

                input.StoreInfo.TryAdd(
                    GetType(),
                    new StoreInfo()
                {
                    Name = PemFilesOptions.PluginName,
                    Path = _path
                });

                // Private key
                if (input.CacheFile != null)
                {
                    var pkPem = "";
                    var store = new Pkcs12Store(input.CacheFile.OpenRead(), input.CacheFilePassword?.ToCharArray());
                    var alias = store.Aliases.OfType <string>().FirstOrDefault(p => store.IsKeyEntry(p));
                    if (alias == null)
                    {
                        _log.Warning("No key entries found");
                        return;
                    }
                    var entry = store.GetKey(alias);
                    var key   = entry.Key;
                    if (key.IsPrivate)
                    {
                        pkPem = _pemService.GetPem(entry.Key, _password);
                    }
                    if (!string.IsNullOrEmpty(pkPem))
                    {
                        await File.WriteAllTextAsync(Path.Combine(_path, $"{name}{KeyFilenameSuffix}{FilenameExtension}"), pkPem);
                    }
                    else
                    {
                        _log.Warning("No private key found in Pkcs12Store");
                    }
                }
                else
                {
                    _log.Warning("No private key found in cache");
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex, "Error exporting .pem files to folder");
            }
        }