def report_body(pkt): """Return a list of reports on a packet body. :Parameters: - `pkt`: instance of a `OpenPGP.packet.Packet` subclass :Returns: string reporting something about the packet body """ report = [] body = pkt.body if PKT_PUBKEYSESKEY == pkt.tag.type: report.append(" version: %s" % body.version) report.append(" key id: %s" % body.keyid) report.append(" key algorithm: %s (%s)" % (body.alg_pubkey, TXT.alg_pubkey_msg(body.alg_pubkey))) if body.alg_pubkey in [ASYM_RSA_EOS, ASYM_RSA_E]: report.append(" RSA 'me mod n' bit count: %s" % body.RSA_me_modn.bit_length) elif body.alg_pubkey in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]: report.append(" ElGamal 'gk mod p' bit count: %s" % body.ELGAMAL_gk_modp.bit_length) report.append(" ElGamal 'myk mod p' bit count: %s" % body.ELGAMAL_myk_modp.bit_length) elif PKT_SIGNATURE == pkt.tag.type: report.append(" version: %s type: %s %s" % (body.version, body.type, TXT.sig_msg(body.type))) report.append(" signer: %s" % body.keyid) report.append(" created: %s" % body.created) report.append(" hash: %s (%s)" % (body.alg_hash, TXT.alg_hash_msg(body.alg_hash))) report.append(" public key algorithm: %s (%s)" % (body.alg_pubkey, TXT.alg_pubkey_msg(body.alg_pubkey))) if 4 == body.version: report.append(" unhashed data:") for s in body.unhashed_subpkts: report.append(" %s (%s/%s): %s" % (TXT.sigsub_msg(s.type), s.type, hex(s.type), s.value)) report.append(" hashed data:") for s in body.hashed_subpkts: report.append(" %s (%s/%s): %s" % (TXT.sigsub_msg(s.type), s.type, hex(s.type), s.value)) if body.alg_pubkey in [ASYM_RSA_EOS, ASYM_RSA_S]: report.append(" RSA value bit count: %s" % body.RSA.bit_length) elif ASYM_DSA == body.alg_pubkey: report.append(" DSA 'r' bit count: %s" % body.DSA_r.bit_length) report.append(" DSA 's' bit count: %s" % body.DSA_s.bit_length) report.append(" hash fragment: %s" % [hex(ord(x)) for x in body.hash_frag]) elif PKT_SYMKEYSESKEY == pkt.tag.type: report.append(" version: %s" % body.version) report.append(" algorithm: %s (%s)" % (body.alg, TXT.alg_symkey_msg(body.alg))) if 's2k' in body.__dict__: report.append(" S2K type: %s" % body.s2k.type) if 'alg_hash' in body.s2k.__dict__: report.append(" S2K hash: %s (%s)" % (body.s2k.alg_hash, TXT.alg_hash_msg(body.s2k.alg_hash))) if 'salt' in body.s2k.__dict__: report.append(" S2K salt: %s" % [hex(ord(x)) for x in body.s2k.salt]) if 'count_code' in body.s2k.__dict__: report.append(" S2K count code: %s" % body.s2k.count_code) if 'count' in body.s2k.__dict__: report.append(" S2K count: %s" % body.s2k.count) elif PKT_ONEPASS == pkt.tag.type: report.append(" version: %s" % body.version) report.append(" signature type: %s (%s)" % (body.type, TXT.sig_msg(body.type))) report.append(" key id: %s" % body.keyid) report.append(" hash: %s (%s)" % (body.alg_hash, TXT.alg_hash_msg(body.alg_hash))) report.append(" key algorithm: %s (%s)" % (body.alg_pubkey, TXT.alg_pubkey_msg(body.alg_pubkey))) report.append(" nested: %s" % body.nest) elif pkt.tag.type in [PKT_PUBLICKEY, PKT_PUBLICSUBKEY, PKT_PRIVATEKEY, PKT_PRIVATESUBKEY]: report.append(" version: %s created: %s" % (body.version, body.created)) report.append(" algorithm: %s (%s)" % (body.alg, TXT.alg_pubkey_msg(body.alg))) report.append(" fingerprint: %s ID: %s" % (body.fingerprint, body.id)) if body.alg in [ASYM_RSA_EOS, ASYM_RSA_S]: report.append(" RSA 'n' bit count: %s" % body.RSA_n.bit_length) report.append(" RSA 'n' value: %s" % body.RSA_n.value) report.append(" RSA 'e' bit count: %s" % body.RSA_e.bit_length) report.append(" RSA 'e' value: %s" % body.RSA_e.value) elif ASYM_DSA == body.alg: report.append(" DSA 'p' bit count: %s" % body.DSA_p.bit_length) report.append(" DSA 'p' value: %s" % body.DSA_p.value) report.append(" DSA 'q' bit count: %s" % body.DSA_q.bit_length) report.append(" DSA 'q' value: %s" % body.DSA_q.value) report.append(" DSA 'g' bit count: %s" % body.DSA_g.bit_length) report.append(" DSA 'g' value: %s" % body.DSA_g.value) report.append(" DSA 'y' bit count: %s" % body.DSA_y.bit_length) report.append(" DSA 'y' value: %s" % body.DSA_y.value) elif body.alg in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]: report.append(" ElGamal 'p' bit count: %s" % body.ELGAMAL_p.bit_length) report.append(" ElGamal 'p' value: %s" % body.ELGAMAL_p.value) report.append(" ElGamal 'g' bit count: %s" % body.ELGAMAL_g.bit_length) report.append(" ElGamal 'g' value: %s" % body.ELGAMAL_g.value) report.append(" ElGamal 'y' bit count: %s" % body.ELGAMAL_y.bit_length) report.append(" ElGamal 'y' value: %s" % body.ELGAMAL_y.value) if hasattr(body, 'expires'): report.append(" expires: %s" % body.expires) if hasattr(body, 'expires'): report.append(" expires: %s" % body.expires) if pkt.tag.type in [PKT_PRIVATEKEY, PKT_PRIVATESUBKEY]: if hasattr(body, 'alg_sym'): report.append(" encrypted with algorithm: %s %s" % (body.alg_sym, TXT.alg_symkey_msg(body.alg_sym))) if hasattr(body, 's2k_usg'): report.append(" S2K usage: %s" % body.s2k_usg) if hasattr(body, 'iv'): report.append(" IV: %s" % [hex(ord(x)) for x in body.iv]) if hasattr(body, 's2k'): report.append(" S2K type: %s" % body.s2k.type) if hasattr(body.s2k, 'alg_hash'): report.append(" S2K hash: %s (%s)" % (body.s2k.alg_hash, TXT.alg_hash_msg(body.s2k.alg_hash))) if hasattr(body.s2k, 'salt'): report.append(" S2K salt: %s" % ''.join(['\\x%s' % hex(ord(c))[2:].zfill(2) for c in body.s2k.salt])) if hasattr(body.s2k, 'count_code'): report.append(" S2K count code: %s" % body.s2k.count_code) if hasattr(body.s2k, 'count'): report.append(" S2K count: %s" % body.s2k.count) if hasattr(body, '_enc_d'): report.append(" encrypted data: %s octets" % len(body._enc_d)) #report.append(" encrypted data: %s" % [hex(ord(x)) for x in body._enc_d]) report.append(" encrypted data: %s" % ''.join(['\\x%s' % hex(ord(c))[2:].zfill(2) for c in body._enc_d])) else: if body.alg in [ASYM_RSA_EOS, ASYM_RSA_S]: report.append(" Secret RSA 'd' bit count: %s" % body.RSA_d.bit_length) report.append(" Secret RSA 'd' value: %s" % body.RSA_d.value) report.append(" Secret RSA 'p' bit count: %s" % body.RSA_p.bit_length) report.append(" Secret RSA 'p' value: %s" % body.RSA_p.value) report.append(" Secret RSA 'q' bit count: %s" % body.RSA_q.bit_length) report.append(" Secret RSA 'q' value: %s" % body.RSA_q.value) report.append(" Secret RSA 'u' bit count: %s" % body.RSA_u.bit_length) report.append(" Secret RSA 'u' value: %s" % body.RSA_u.value) elif ASYM_DSA == body.alg: report.append(" Secret DSA 'x' bit count: %s" % body.DSA_x.bit_length) report.append(" Secret DSA 'x' value: %s" % body.DSA_x.value) elif body.alg in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]: report.append(" Secret ElGamal 'x' bit count: %s" % body.ELGAMAL_x.bit_length) report.append(" Secret ElGamal 'x' value: %s" % body.ELGAMAL_x.value) if hasattr(body, 'chksum') and body.chksum: # for chksum == None?? report.append(" checksum: %s" % [hex(ord(x)) for x in body.chksum]) elif PKT_COMPRESSED == pkt.tag.type: report.append(" compression type %s (%s)" % (pkt.body.alg, TXT.alg_comp_msg(pkt.body.alg))) elif PKT_SYMENCDATA == pkt.tag.type: report.append(" encrypted data: %s octets" % len(body.data)) elif PKT_MARKER == pkt.tag.type: report.append(" left a mark: %s" % pkt.body.value) elif PKT_LITERAL == pkt.tag.type: report.append(" format: %s" % pkt.body.format) report.append(" modified: %s" % pkt.body.modified) report.append(" filename: %s" % pkt.body.filename) report.append(" data length: %s" % len(pkt.body.data)) elif PKT_TRUST == pkt.tag.type: pass elif PKT_USERID == pkt.tag.type: report.append(" ID: %s" % body.value) elif PKT_USERATTR == pkt.tag.type: pass elif PKT_SYMENCINTDATA == pkt.tag.type: report.append(" version: %s" % body.version) # suspect exception report.append(" encrypted data: %s octets" % len(body.data)) # suspect exception elif PKT_MODDETECT == pkt.tag.type: # will this ever be shown? report.append(" detection hash: %s" % [hex(ord(x)) for x in body.hash]) # suspect exception else: pass return linesep.join(report)
def report_body(pkt): """Return a list of reports on a packet body. :Parameters: - `pkt`: instance of a `OpenPGP.packet.Packet` subclass :Returns: string reporting something about the packet body """ report = [] body = pkt.body if PKT_PUBKEYSESKEY == pkt.tag.type: report.append(" version: %s" % body.version) report.append(" key id: %s" % body.keyid) report.append(" key algorithm: %s (%s)" % (body.alg_pubkey, TXT.alg_pubkey_msg(body.alg_pubkey))) if body.alg_pubkey in [ASYM_RSA_EOS, ASYM_RSA_E]: report.append(" RSA 'me mod n' bit count: %s" % body.RSA_me_modn.bit_length) elif body.alg_pubkey in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]: report.append(" ElGamal 'gk mod p' bit count: %s" % body.ELGAMAL_gk_modp.bit_length) report.append(" ElGamal 'myk mod p' bit count: %s" % body.ELGAMAL_myk_modp.bit_length) elif PKT_SIGNATURE == pkt.tag.type: report.append(" version: %s type: %s %s" % (body.version, body.type, TXT.sig_msg(body.type))) report.append(" signer: %s" % body.keyid) report.append(" created: %s" % body.created) report.append(" hash: %s (%s)" % (body.alg_hash, TXT.alg_hash_msg(body.alg_hash))) report.append(" public key algorithm: %s (%s)" % (body.alg_pubkey, TXT.alg_pubkey_msg(body.alg_pubkey))) if 4 == body.version: report.append(" unhashed data:") for s in body.unhashed_subpkts: report.append( " %s (%s/%s): %s" % (TXT.sigsub_msg(s.type), s.type, hex(s.type), s.value)) report.append(" hashed data:") for s in body.hashed_subpkts: report.append( " %s (%s/%s): %s" % (TXT.sigsub_msg(s.type), s.type, hex(s.type), s.value)) if body.alg_pubkey in [ASYM_RSA_EOS, ASYM_RSA_S]: report.append(" RSA value bit count: %s" % body.RSA.bit_length) elif ASYM_DSA == body.alg_pubkey: report.append(" DSA 'r' bit count: %s" % body.DSA_r.bit_length) report.append(" DSA 's' bit count: %s" % body.DSA_s.bit_length) report.append(" hash fragment: %s" % [hex(ord(x)) for x in body.hash_frag]) elif PKT_SYMKEYSESKEY == pkt.tag.type: report.append(" version: %s" % body.version) report.append(" algorithm: %s (%s)" % (body.alg, TXT.alg_symkey_msg(body.alg))) if 's2k' in body.__dict__: report.append(" S2K type: %s" % body.s2k.type) if 'alg_hash' in body.s2k.__dict__: report.append( " S2K hash: %s (%s)" % (body.s2k.alg_hash, TXT.alg_hash_msg(body.s2k.alg_hash))) if 'salt' in body.s2k.__dict__: report.append(" S2K salt: %s" % [hex(ord(x)) for x in body.s2k.salt]) if 'count_code' in body.s2k.__dict__: report.append(" S2K count code: %s" % body.s2k.count_code) if 'count' in body.s2k.__dict__: report.append(" S2K count: %s" % body.s2k.count) elif PKT_ONEPASS == pkt.tag.type: report.append(" version: %s" % body.version) report.append(" signature type: %s (%s)" % (body.type, TXT.sig_msg(body.type))) report.append(" key id: %s" % body.keyid) report.append(" hash: %s (%s)" % (body.alg_hash, TXT.alg_hash_msg(body.alg_hash))) report.append(" key algorithm: %s (%s)" % (body.alg_pubkey, TXT.alg_pubkey_msg(body.alg_pubkey))) report.append(" nested: %s" % body.nest) elif pkt.tag.type in [ PKT_PUBLICKEY, PKT_PUBLICSUBKEY, PKT_PRIVATEKEY, PKT_PRIVATESUBKEY ]: report.append(" version: %s created: %s" % (body.version, body.created)) report.append(" algorithm: %s (%s)" % (body.alg, TXT.alg_pubkey_msg(body.alg))) report.append(" fingerprint: %s ID: %s" % (body.fingerprint, body.id)) if body.alg in [ASYM_RSA_EOS, ASYM_RSA_S]: report.append(" RSA 'n' bit count: %s" % body.RSA_n.bit_length) report.append(" RSA 'n' value: %s" % body.RSA_n.value) report.append(" RSA 'e' bit count: %s" % body.RSA_e.bit_length) report.append(" RSA 'e' value: %s" % body.RSA_e.value) elif ASYM_DSA == body.alg: report.append(" DSA 'p' bit count: %s" % body.DSA_p.bit_length) report.append(" DSA 'p' value: %s" % body.DSA_p.value) report.append(" DSA 'q' bit count: %s" % body.DSA_q.bit_length) report.append(" DSA 'q' value: %s" % body.DSA_q.value) report.append(" DSA 'g' bit count: %s" % body.DSA_g.bit_length) report.append(" DSA 'g' value: %s" % body.DSA_g.value) report.append(" DSA 'y' bit count: %s" % body.DSA_y.bit_length) report.append(" DSA 'y' value: %s" % body.DSA_y.value) elif body.alg in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]: report.append(" ElGamal 'p' bit count: %s" % body.ELGAMAL_p.bit_length) report.append(" ElGamal 'p' value: %s" % body.ELGAMAL_p.value) report.append(" ElGamal 'g' bit count: %s" % body.ELGAMAL_g.bit_length) report.append(" ElGamal 'g' value: %s" % body.ELGAMAL_g.value) report.append(" ElGamal 'y' bit count: %s" % body.ELGAMAL_y.bit_length) report.append(" ElGamal 'y' value: %s" % body.ELGAMAL_y.value) if hasattr(body, 'expires'): report.append(" expires: %s" % body.expires) if hasattr(body, 'expires'): report.append(" expires: %s" % body.expires) if pkt.tag.type in [PKT_PRIVATEKEY, PKT_PRIVATESUBKEY]: if hasattr(body, 'alg_sym'): report.append(" encrypted with algorithm: %s %s" % (body.alg_sym, TXT.alg_symkey_msg(body.alg_sym))) if hasattr(body, 's2k_usg'): report.append(" S2K usage: %s" % body.s2k_usg) if hasattr(body, 'iv'): report.append(" IV: %s" % [hex(ord(x)) for x in body.iv]) if hasattr(body, 's2k'): report.append(" S2K type: %s" % body.s2k.type) if hasattr(body.s2k, 'alg_hash'): report.append(" S2K hash: %s (%s)" % (body.s2k.alg_hash, TXT.alg_hash_msg(body.s2k.alg_hash))) if hasattr(body.s2k, 'salt'): report.append(" S2K salt: %s" % ''.join([ '\\x%s' % hex(ord(c))[2:].zfill(2) for c in body.s2k.salt ])) if hasattr(body.s2k, 'count_code'): report.append(" S2K count code: %s" % body.s2k.count_code) if hasattr(body.s2k, 'count'): report.append(" S2K count: %s" % body.s2k.count) if hasattr(body, '_enc_d'): report.append(" encrypted data: %s octets" % len(body._enc_d)) #report.append(" encrypted data: %s" % [hex(ord(x)) for x in body._enc_d]) report.append(" encrypted data: %s" % ''.join( ['\\x%s' % hex(ord(c))[2:].zfill(2) for c in body._enc_d])) else: if body.alg in [ASYM_RSA_EOS, ASYM_RSA_S]: report.append(" Secret RSA 'd' bit count: %s" % body.RSA_d.bit_length) report.append(" Secret RSA 'd' value: %s" % body.RSA_d.value) report.append(" Secret RSA 'p' bit count: %s" % body.RSA_p.bit_length) report.append(" Secret RSA 'p' value: %s" % body.RSA_p.value) report.append(" Secret RSA 'q' bit count: %s" % body.RSA_q.bit_length) report.append(" Secret RSA 'q' value: %s" % body.RSA_q.value) report.append(" Secret RSA 'u' bit count: %s" % body.RSA_u.bit_length) report.append(" Secret RSA 'u' value: %s" % body.RSA_u.value) elif ASYM_DSA == body.alg: report.append(" Secret DSA 'x' bit count: %s" % body.DSA_x.bit_length) report.append(" Secret DSA 'x' value: %s" % body.DSA_x.value) elif body.alg in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]: report.append(" Secret ElGamal 'x' bit count: %s" % body.ELGAMAL_x.bit_length) report.append(" Secret ElGamal 'x' value: %s" % body.ELGAMAL_x.value) if hasattr(body, 'chksum') and body.chksum: # for chksum == None?? report.append(" checksum: %s" % [hex(ord(x)) for x in body.chksum]) elif PKT_COMPRESSED == pkt.tag.type: report.append(" compression type %s (%s)" % (pkt.body.alg, TXT.alg_comp_msg(pkt.body.alg))) elif PKT_SYMENCDATA == pkt.tag.type: report.append(" encrypted data: %s octets" % len(body.data)) elif PKT_MARKER == pkt.tag.type: report.append(" left a mark: %s" % pkt.body.value) elif PKT_LITERAL == pkt.tag.type: report.append(" format: %s" % pkt.body.format) report.append(" modified: %s" % pkt.body.modified) report.append(" filename: %s" % pkt.body.filename) report.append(" data length: %s" % len(pkt.body.data)) elif PKT_TRUST == pkt.tag.type: pass elif PKT_USERID == pkt.tag.type: report.append(" ID: %s" % body.value) elif PKT_USERATTR == pkt.tag.type: pass elif PKT_SYMENCINTDATA == pkt.tag.type: report.append(" version: %s" % body.version) # suspect exception report.append(" encrypted data: %s octets" % len(body.data)) # suspect exception elif PKT_MODDETECT == pkt.tag.type: # will this ever be shown? report.append(" detection hash: %s" % [hex(ord(x)) for x in body.hash]) # suspect exception else: pass return linesep.join(report)
def list_as_signed(pgp_d, **kw): """With respect to the API, list useful OpenPGP items. :Parameters: - `pgp_d`: str either ASCII-armored text or native OpenPGP data - armored text may contain multiple armored blocks, while native OpenPGP data should exist as a single unbroken string :Keywords: - `leftover`: *optional* list to which leftover (non-message) packets will be appended - this will include repeats of detached or standalone signature packets - `decompress`: set to True to auto-decompress compressed messages - `detached`: detached string, signed by signatures in `pgp_d` :Returns: list of message instances and special signature tuples This function takes OpenPGP data and returns a list of items that are "useful" for the API. In particular, this includes detached, standalone, and key signature tuples (hence, "as_signed") which are returned as:: ([det_sig1, det_sig2, ..], string) # detached signatures ([key_sig1, key_sig2, ..], key) # key_sigs within a key message ([standalone_sig], None) # standalone signature :note: This is basically a `list_msgs()` which works with armored strings. One important difference is that the focus is turned away from packets (leftover) toward 'usable things' with respect to the API. :note: I'm debating whether or not to scrap the whole ([sigs], target) tuple deal and automagically convert detached sigs and clearsigned sigs to signed messages, using filename='' and modified='' for both, format='b' for detached and format='t' for clearsigned. Higher up, this will have the consequence of spitting out the signed stuff (could be a lot) with some literal packet header junk prepended. Right now, only detached and clearsigned stuff are sent out as tuples. It's just ugly worrying about whether we're working with instances or tuples all over the place (or ignoring the possibilities and just letting an exception be raised). """ saplog = logging.getLogger("saplog") det_d = kw.get('detached', None) # stringify pgp_d and det_d if not pgp_d: return [] # det_d requires detached signatures, so it's safe to return #elif hasattr(pgp_d, 'read'): #elif isinstance(pgp_d, (StringIO, file)): # pgp_d = pgp_d.read() elif isinstance(pgp_d, str): pass else: raise TypeError("Invalid OpenPGP type. Please send string, file, or StringIO.") if det_d: if hasattr(det_d, 'read'): det_d = det_d.read() elif isinstance(det_d, str): pass else: raise Hell players = [] # bona fide messages and detached signature tuples # similar "looks_armored" deal in list_pkts(), perhaps can consolidate if looks_armored(pgp_d): arm_d = [] # native data for arm in list_armored(pgp_d): if 'signed' in arm.__dict__: # distinguished a clearsigned message # this is where we can just create a signed message, # literalname = '', modified = 0, format = 't' sigs = [p for p in list_pkts(arm.data) if PKT_SIGNATURE == p.tag.type] players.append((sigs, arm.signed)) else: arm_d.append(arm.data) pgp_d = ''.join(arm_d) leftover = [] msgs = list_msgs(list_pkts(pgp_d), leftover=leftover) detached_sigs = [] detached_types = [SIG_BINARY, SIG_TEXT] standalone_types = [SIG_STANDALONE, SIG_TIMESTAMP, SIG_THIRDPARTY] for pkt in leftover: # distinguish leftover.. if PKT_SIGNATURE == pkt.tag.type: if pkt.body.type in detached_types: # ..detached sigs from.. detached_sigs.append(pkt) elif pkt.body.type in standalone_types: #..standalone sigs standalone_sigs.append(pkt) players.append(([pkt],None)) if kw.get('decompress'): for i in range(len(msgs)): if MSG_COMPRESSED == msgs[i].type: saplog.info("Decompressing compressed message (%r)." % TXT.alg_comp_msg(msgs[i].compressed.body.alg)) compmsg = msgs.pop(i) compplayers = list_as_signed(compmsg.compressed.body.data) for j in range(len(compplayers)): # to preserve the order in.. msgs.insert(i+j, compplayers[j]) # ..which they appeared if detached_sigs: players.append((detached_sigs, det_d)) players += msgs if isinstance(kw.get('leftover'), list): kw['leftover'].extend(leftover) return players