Created
April 22, 2013 21:19
-
-
Save scardine/5438641 to your computer and use it in GitHub Desktop.
Download dos certificados Raiz da autoridade certificadora brasileira.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sh | |
import os | |
import re | |
from path import path | |
home = os.environ['HOME'] | |
p = path.joinpath(home, 'Documents', 'pki', 'nfe', 'certs') | |
if not p.isdir(): | |
p.makedirs() | |
os.chdir(p) | |
for i in range(1, 4): | |
print("Fazendo download de v{}_ff_.der".format(i)) | |
sh.wget('--no-check-certificate', "https://acraiz.icpbrasil.gov.br/repositorio/v{}_ff.der".format(i), '-O', 'v{}_ff.der'.format(i)) | |
print("Fazendo download de v{}_ff_sha512.txt".format(i)) | |
sh.wget('--no-check-certificate', "https://acraiz.icpbrasil.gov.br/repositorio/v{}_ff_sha512.txt".format(i), '-O', 'v{}_ff_sha512.txt'.format(i)) | |
print("Verificando arquivo:") | |
print(sh.sha512sum('-c', "v{}_ff_sha512.txt".format(i))) | |
print("Gerando arquivo no formato PEM...") | |
sh.openssl('pkcs7', '-inform', 'DER', '-in', 'v{}_ff.der'.format(i), '-out', 'cadeia_v{}.pem'.format(i), '-print_certs') | |
print("Separando certificados...") | |
with open('cadeia_v{}.pem'.format(i)) as input_file: | |
conteudo = input_file.read() | |
for cert in filter(lambda x: bool(x.strip()), conteudo.split('\n\n')): | |
try: | |
entidade = re.match(r'subject=/C=BR/O=ICP-Brasil(/OU=.+?|)/CN=(.+)', cert).group(2) | |
except AttributeError: | |
print("ERRO: {}".format(cert.split("\n")[0])) | |
continue | |
else: | |
print("\tGerando cert-v{}-{}.pem".format(i, '-'.join(entidade.split()))) | |
with open('cert-v{}-{}.pem'.format(i, '-'.join(entidade.split())), 'w') as output_file: | |
output_file.write(cert) | |
print('-' * 80) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment