Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions DFe.Utils/Assinatura/CertificadoDigital.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
/// </summary>
/// <param name="openFlags"></param>
/// <returns></returns>
public static X509Store ObterX509Store(OpenFlags openFlags)
public static X509Store ObterX509Store(OpenFlags openFlags, StoreLocation storeLocation = StoreLocation.CurrentUser)
{
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
X509Store store = new X509Store(StoreName.My, storeLocation);
store.Open(openFlags);
return store;
}
Expand All @@ -41,7 +41,7 @@
throw new Exception(string.Format("Certificado digital {0} não encontrado!", arquivo));
}

var certificado = new X509Certificate2(arquivo, senha, keyStorageFlag);

Check warning on line 44 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.X509Certificate2(string, string?, X509KeyStorageFlags)' is obsolete: 'Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.' (https://aka.ms/dotnet-warnings/SYSLIB0057)
return certificado;
}

Expand All @@ -56,7 +56,7 @@
{
try
{
var certificado = new X509Certificate2(arrayBytes, senha, keyStorageFlag);
X509Certificate2 certificado = new X509Certificate2(arrayBytes, senha, keyStorageFlag);

Check warning on line 59 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.X509Certificate2(byte[], string?, X509KeyStorageFlags)' is obsolete: 'Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.' (https://aka.ms/dotnet-warnings/SYSLIB0057)
return certificado;
}
catch (Exception ex)
Expand All @@ -69,12 +69,12 @@
/// Obtém um objeto <see cref="X509Certificate2"/> pelo serial passado no parÂmetro
/// </summary>
/// <returns></returns>
private static X509Certificate2 ObterDoRepositorio(string serial, OpenFlags opcoesDeAbertura)
private static X509Certificate2 ObterDoRepositorio(string serial, OpenFlags opcoesDeAbertura, StoreLocation storeLocation = StoreLocation.CurrentUser)
{
if (string.IsNullOrEmpty(serial))
throw new ArgumentException("O número de série do certificado digital não foi informado!");
X509Certificate2 certificado = null;
var store = ObterX509Store(opcoesDeAbertura);
var store = ObterX509Store(opcoesDeAbertura, storeLocation);
try
{
foreach (var item in store.Certificates)
Expand All @@ -100,9 +100,9 @@
/// <param name="serial"></param>
/// <param name="senha"></param>
/// <returns></returns>
private static X509Certificate2 ObterDoRepositorioPassandoPin(string serial, string senha = null)
private static X509Certificate2 ObterDoRepositorioPassandoPin(string serial, string senha = null, StoreLocation storeLocation = StoreLocation.CurrentUser)
{
var certificado = ObterDoRepositorio(serial, OpenFlags.ReadOnly);
var certificado = ObterDoRepositorio(serial, OpenFlags.ReadOnly, storeLocation);
if (string.IsNullOrEmpty(senha)) return certificado;
certificado.DefinirPinParaChavePrivada(senha);
return certificado;
Expand All @@ -120,7 +120,7 @@
if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.Win32S)
{
if (certificado == null) throw new ArgumentNullException("certificado");
var key = (RSACryptoServiceProvider)certificado.PrivateKey;

Check warning on line 123 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.PrivateKey' is obsolete: 'X509Certificate2.PrivateKey is obsolete. Use the appropriate method to get the private key, such as GetRSAPrivateKey, or use the CopyWithPrivateKey method to create a new instance with a private key.' (https://aka.ms/dotnet-warnings/SYSLIB0028)

var providerHandle = IntPtr.Zero;
var pinBuffer = Encoding.ASCII.GetBytes(pin);
Expand Down Expand Up @@ -154,13 +154,13 @@
switch (configuracaoCertificado.TipoCertificado)
{
case TipoCertificado.A1Repositorio:
return ObterDoRepositorio(configuracaoCertificado.Serial, OpenFlags.MaxAllowed);
return ObterDoRepositorio(configuracaoCertificado.Serial, OpenFlags.MaxAllowed, configuracaoCertificado.StoreLocation);
case TipoCertificado.A1ByteArray:
return ObterDoArrayBytes(configuracaoCertificado.ArrayBytesArquivo, configuracaoCertificado.Senha, configuracaoCertificado.KeyStorageFlags);
case TipoCertificado.A1Arquivo:
return ObterDeArquivo(configuracaoCertificado.Arquivo, configuracaoCertificado.Senha, configuracaoCertificado.KeyStorageFlags);
case TipoCertificado.A3:
return ObterDoRepositorioPassandoPin(configuracaoCertificado.Serial, configuracaoCertificado.Senha);
return ObterDoRepositorioPassandoPin(configuracaoCertificado.Serial, configuracaoCertificado.Senha, configuracaoCertificado.StoreLocation);
default:
throw new ArgumentOutOfRangeException();
}
Expand Down Expand Up @@ -318,7 +318,7 @@

try
{
RSACryptoServiceProvider service = x509Certificate2.PrivateKey as RSACryptoServiceProvider;

Check warning on line 321 in DFe.Utils/Assinatura/CertificadoDigital.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.PrivateKey' is obsolete: 'X509Certificate2.PrivateKey is obsolete. Use the appropriate method to get the private key, such as GetRSAPrivateKey, or use the CopyWithPrivateKey method to create a new instance with a private key.' (https://aka.ms/dotnet-warnings/SYSLIB0028)

if (service != null)
{
Expand Down
4 changes: 2 additions & 2 deletions DFe.Utils/CertificadoDigitalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
/// Exibe a lista de certificados instalados no PC e devolve o certificado selecionado
/// </summary>
/// <returns></returns>
public static X509Certificate2 ListareObterDoRepositorio()
public static X509Certificate2 ListareObterDoRepositorio(StoreLocation storeLocation = StoreLocation.CurrentUser)
{
var store = CertificadoDigital.ObterX509Store(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var store = CertificadoDigital.ObterX509Store(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly, storeLocation);
var collection = store.Certificates;
var fcollection = collection.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
var scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificados válidos:", "Selecione o certificado que deseja usar",
Expand Down Expand Up @@ -80,7 +80,7 @@
throw new Exception("Certificado não se encontra no caminho especificado");
}

var cert = new X509Certificate2(caminho, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

Check warning on line 83 in DFe.Utils/CertificadoDigitalUtils.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.X509Certificate2(string, SecureString?, X509KeyStorageFlags)' is obsolete: 'Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.' (https://aka.ms/dotnet-warnings/SYSLIB0057)
return cert;
}

Expand Down Expand Up @@ -120,7 +120,7 @@
/// <returns></returns>
public static X509Certificate2 ObterDosBytes(byte[] bytes, SecureString password, X509KeyStorageFlags? keyStorageFlags)
{
var cert = new X509Certificate2(bytes, password, keyStorageFlags ?? (X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable));

Check warning on line 123 in DFe.Utils/CertificadoDigitalUtils.cs

View workflow job for this annotation

GitHub Actions / build (windows-2022)

'X509Certificate2.X509Certificate2(byte[], SecureString?, X509KeyStorageFlags)' is obsolete: 'Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates.' (https://aka.ms/dotnet-warnings/SYSLIB0057)
return cert;
}

Expand Down
16 changes: 16 additions & 0 deletions DFe.Utils/ConfiguracaoCertificado.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public class ConfiguracaoCertificado
private string _cacheId;
private byte[] _arrayBytesArquivo;
private X509KeyStorageFlags _keyStorageFlags;
private StoreLocation _storeLocation;

public ConfiguracaoCertificado()
{
StoreLocation = StoreLocation.CurrentUser;
KeyStorageFlags = X509KeyStorageFlags.MachineKeySet;
SignatureMethodSignedXml = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
DigestMethodReference = "http://www.w3.org/2000/09/xmldsig#sha1";
Expand Down Expand Up @@ -162,5 +164,19 @@ public X509KeyStorageFlags KeyStorageFlags
_keyStorageFlags = value;
}
}

/// <summary>
///
/// </summary>
public StoreLocation StoreLocation
{
get { return _storeLocation; }
set
{
if (value == _storeLocation)
return;
_storeLocation = value;
}
}
}
}
Loading