0
I’m developing a system where we register companies by digital certificate, where the user uploads the file, puts the password and imports. I need to extract the CNPJ that is within the certificate to consult the Company’s data in the Internal Revenue Service and register.
The system aims to consult Tax Documents in Sefaz, so we need the Certificate and Company data.
On a Windows machine (in my development environment), was using this method to extract CNPJ and was working:
public static string ExtrairCNPJArquivo(X509Certificate2 arquivo)
{
const string oid = "2.16.76.1.3.3";
StringBuilder cnpj = new StringBuilder();
foreach (X509Extension extension in arquivo.Extensions)
{
string texto = extension.Format(true);
string[] linhas = texto.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < linhas.Length; i++)
{
if (linhas[i].Trim().StartsWith(oid))
{
string valores = linhas[i].Substring(linhas[i].IndexOf('=') + 1);
string[] elementos = valores.Split(' ');
byte[] cnpjBytes = new byte[14];
for (int j = 0; j < cnpjBytes.Length; j++)
cnpjBytes[j] = Convert.ToByte(elementos[j + 2], 16);
cnpj.Append(Encoding.UTF8.GetString(cnpjBytes));
break;
}
}
if (!string.IsNullOrEmpty(cnpj.ToString()))
break;
}
return cnpj.ToString();
}
But in my production environment, I use a Linux machine that runs on Docker, and this form used above doesn’t work for that platform.
The text of extension.Format(true);
was coming as othername:<unsupported>
and that’s where the CNPJ of the Certificate comes from. Then I saw that Linux doesn’t work that way.
How can I make it work on both platforms?