def parseAndUpdateDB(strPEM): ''' check pem format ''' if not re.search(r'-----BEGIN CERTIFICATE-----',strPEM) or not re.search(r'-----END CERTIFICATE-----',strPEM): return False my_dict = x509_parse.parse_pem(strPEM) db = db_util.connectDB(hostname,port,user,passwd,db) try: cert_id = db_util.insertCert(db,my_dict['Version'],my_dict['Serial No'],my_dict['Sig Alg'],my_dict['Not Before'],my_dict['Not After']) issuer = my_dict['Issuer'] db_util.insertIssuer(db,issuer['C'],issuer['ST'],issuer['O'],issuer['OU'],issuer['CN'],issuer['L']) subject = my_dict['Subject'] db_util.insertSubject(db,subject['C'],subject['ST'],subject['O'],subject['OU'],subject['CN'],subject['L'],my_dict['pKeyAlg']) extension = my_dict['Extension'] for key in extension.keys(): value_str = (' ').join(extension[key]['value']) db_util.insertExtension(db,key,str(extension[key]['is_critical']),value_str,cert_id) db.commit() except Exception,e: print e db.rollback()
for key in extension.keys(): value_str = (' ').join(extension[key]['value']) db_util.insertExtension(db,key,str(extension[key]['is_critical']),value_str,cert_id) db.commit() except Exception,e: print e db.rollback() db.close() return True if __name__ == '__main__': ''' clean DB ''' db = db_util.connectDB(hostname,port,user,passwd,db) tables = ['certificate','cert_subject','cert_issuer','ext_entry'] db_util.cleanDB(db,tables) db.commit() db.close() ''' with open('top-1m.csv','r') as f: reader = csv.reader(f) for line in reader: strPEM = cert_util.downloadCert(str(line[1])) if strPEM: parseAndUpdateDB(strPEM) ''' '''