Beispiel #1
0
    def run_veririca_pdf_tem_assinatura(self):
        global sss
        sss = 1

        def tree_print(field_name, fields):
            global sss
            ss = " "
            print(ss * sss, field_name, '.............')
            if not isinstance(fields, dict):
                ByteStringObject
                if field_name == '/Contents':
                    try:
                        signed_data = cms.ContentInfo.load(fields)['content']

                        for cert in signed_data['certificates']:
                            print('cert.issuer:',
                                  cert.native['tbs_certificate']['issuer'])
                            print('cert.subject:',
                                  cert.native['tbs_certificate']['subject'])

                    except Exception as e:
                        pass

                    with open(
                            '/home/leandro/Downloads/content{}.ext'.format(
                                sss), 'wb') as f:
                        f.write(fields)
                        f.close()
                    return
                else:
                    print(' ' * sss, fields)
                    return
            for field_name, value in fields.items():
                sss += 2
                tree_print(field_name, value)
                sss -= 2

        ifile = '/home/leandro/Downloads/016 - Projeto da LDO 2021_Assinado.pdf'
        ifile = '/home/leandro/Downloads/plol_violencia_nas_escolas.pdf'

        (hashok, signatureok, certok) = pdf.verify(open(ifile, 'rb').read())
        print('signature ok?', signatureok)
        print('hash ok?', hashok)
        print('cert ok?', certok)

        r = PdfFileReader(open(ifile, "rb"))

        fields = r.getFields()

        tree_print('file', fields)
Beispiel #2
0
def run_signed_name_and_date_extract(file):
    signs = {}
    fields = {}
    pdfdata = file.read()

    # se não tem byterange então não é assinado
    byterange = []
    n = -1
    while True:
        n = pdfdata.find(b"/ByteRange", n + 1)
        if n == -1:
            break
        byterange.append(n)

    if not byterange:
        return signs

    # tenta extrair via /Fields
    try:
        pdf = PdfFileReader(file)
        fields = pdf.getFields()
    except Exception as e:
        try:
            pdf = PdfFileReader(file, strict=False)
            fields = pdf.getFields()
        except Exception as ee:
            fields = ee

    try:
        # se a extração via /Fields ocorrer sem erros e forem capturadas
        # tantas assinaturas quanto byteranges
        if isinstance(fields, dict):
            signs = run_signed_name_and_date_via_fields(fields)
            if len(signs) == len(byterange):
                return signs
    except Exception as e:
        pass

    for n in byterange:

        start = pdfdata.find(b"[", n)
        stop = pdfdata.find(b"]", start)
        assert n != -1 and start != -1 and stop != -1
        n += 1

        br = [int(i, 10) for i in pdfdata[start + 1:stop].split()]
        contents = pdfdata[br[0] + br[1] + 1:br[2] - 1]
        bcontents = bytes.fromhex(contents.decode("utf8"))
        data1 = pdfdata[br[0]:br[0] + br[1]]
        data2 = pdfdata[br[2]:br[2] + br[3]]
        #signedData = data1 + data2

        nome = 'Nome do assinante não localizado.'
        try:
            signed_data = cms.ContentInfo.load(bcontents)['content']
            oun_old = []
            for cert in signed_data['certificates']:
                subject = cert.native['tbs_certificate']['subject']
                oun = subject['organizational_unit_name']

                if isinstance(oun, str):
                    continue

                if len(oun) > len(oun_old):
                    oun_old = oun
                    nome = subject['common_name'].split(':')[0]

                if nome not in signs:
                    signs[nome] = timezone.localtime()
        except:
            pass

    return signs