def fill(self, d): self._d = d self.version = STN.str2int(d[0:1]) self.type = STN.str2int(d[1:2]) self.alg_hash = STN.str2int(d[2:3]) self.alg_pubkey = STN.str2int(d[3:4]) self.keyid = STN.str2hex(d[4:12]) self.nest = STN.str2int(d[12:13])
def get_id(self): """Get a key's public ID as an uppercase hex string. """ if 3 == self.version: # only use first "RSA n" MPI return str2hex(mpi2str([self.mpi[0]], None)[-8:]) elif 4 == self.version: return self.get_fprint()[-16:]
def fill(self, d): self._d = d idx = 0 __version_d = d[idx:idx+1] self.version, idx = STN.strcalc(STN.str2int, __version_d, idx) if self.version in [2, 3, 4]: __created_d = d[idx:idx+4] self.created, idx = STN.strcalc(STN.str2int, __created_d, idx) if self.version in [2, 3]: self.__expires_d = d[idx:idx+2] self.expires, idx = STN.strcalc(STN.str2int, self.__expires_d, idx) __alg_d = d[idx:idx+1] self.alg, idx = STN.strcalc(STN.str2int, __alg_d, idx) # resolve MPIs if self.alg in [ASYM_RSA_EOS, ASYM_RSA_E, ASYM_RSA_S]: self.RSA_n, idx = MPI.strcalc_mpi(d[idx:], idx) self.RSA_e, idx = MPI.strcalc_mpi(d[idx:], idx) self._mpi_d = self.RSA_n._d + self.RSA_e._d elif ASYM_DSA == self.alg: self.DSA_p, idx = MPI.strcalc_mpi(d[idx:], idx) self.DSA_q, idx = MPI.strcalc_mpi(d[idx:], idx) self.DSA_g, idx = MPI.strcalc_mpi(d[idx:], idx) self.DSA_y, idx = MPI.strcalc_mpi(d[idx:], idx) self._mpi_d = self.DSA_p._d + self.DSA_q._d + self.DSA_g._d + self.DSA_y._d elif self.alg in [ASYM_ELGAMAL_E, ASYM_ELGAMAL_EOS]: self.ELGAMAL_p, idx = MPI.strcalc_mpi(d[idx:], idx) self.ELGAMAL_g, idx = MPI.strcalc_mpi(d[idx:], idx) self.ELGAMAL_y, idx = MPI.strcalc_mpi(d[idx:], idx) self._mpi_d = self.ELGAMAL_p._d + self.ELGAMAL_g._d + self.ELGAMAL_y._d else: raise NotImplementedError("Unsupported key algorithm. Received alg->(%s)" % self.alg) # set fingerprint if self.version in [2, 3]: integer_data = self.RSA_n._int_d + self.RSA_e._int_d self.fingerprint = md5.new(integer_data).hexdigest().upper() # see fingerprint/id notes in doc/NOTES.txt self.id = STN.str2hex(self.RSA_n._int_d[-8:]) elif 4 == self.version: f = ['\x99'] f_data = ''.join([__version_d, __created_d, __alg_d, self._mpi_d]) length = len(f_data) hi = (chr((0xffff & length) >> 8)) # high order packet length lo = (chr(0xff & length)) # low order packet length f.append(hi + lo) f.append(f_data) self.fingerprint = sha.new(''.join(f)).hexdigest().upper() # see fingerprint/id notes in doc/NOTES.txt self.id = self.fingerprint[-16:] else: # unsupported packet version raise NotImplementedError("Unsupported key version: %s" % self.version) return idx
def fill(self, d): self._d = d idx = 0 __version_d = d[idx:idx + 1] self.version, idx = STN.strcalc(STN.str2int, __version_d, idx) if self.version in [2, 3, 4]: __created_d = d[idx:idx + 4] self.created, idx = STN.strcalc(STN.str2int, __created_d, idx) if self.version in [2, 3]: self.__expires_d = d[idx:idx + 2] self.expires, idx = STN.strcalc(STN.str2int, self.__expires_d, idx) __alg_d = d[idx:idx + 1] self.alg, idx = STN.strcalc(STN.str2int, __alg_d, idx) # resolve MPIs if self.alg in [ASYM_RSA_EOS, ASYM_RSA_E, ASYM_RSA_S]: self.RSA_n, idx = MPI.strcalc_mpi(d[idx:], idx) self.RSA_e, idx = MPI.strcalc_mpi(d[idx:], idx) self._mpi_d = self.RSA_n._d + self.RSA_e._d elif ASYM_DSA == self.alg: self.DSA_p, idx = MPI.strcalc_mpi(d[idx:], idx) self.DSA_q, idx = MPI.strcalc_mpi(d[idx:], idx) self.DSA_g, idx = MPI.strcalc_mpi(d[idx:], idx) self.DSA_y, idx = MPI.strcalc_mpi(d[idx:], idx) self._mpi_d = self.DSA_p._d + self.DSA_q._d + self.DSA_g._d + self.DSA_y._d elif self.alg in [ASYM_ELGAMAL_E, ASYM_ELGAMAL_EOS]: self.ELGAMAL_p, idx = MPI.strcalc_mpi(d[idx:], idx) self.ELGAMAL_g, idx = MPI.strcalc_mpi(d[idx:], idx) self.ELGAMAL_y, idx = MPI.strcalc_mpi(d[idx:], idx) self._mpi_d = self.ELGAMAL_p._d + self.ELGAMAL_g._d + self.ELGAMAL_y._d else: raise NotImplementedError( "Unsupported key algorithm. Received alg->(%s)" % self.alg) # set fingerprint if self.version in [2, 3]: integer_data = self.RSA_n._int_d + self.RSA_e._int_d self.fingerprint = md5.new(integer_data).hexdigest().upper() # see fingerprint/id notes in doc/NOTES.txt self.id = STN.str2hex(self.RSA_n._int_d[-8:]) elif 4 == self.version: f = ['\x99'] f_data = ''.join( [__version_d, __created_d, __alg_d, self._mpi_d]) length = len(f_data) hi = (chr((0xffff & length) >> 8)) # high order packet length lo = (chr(0xff & length)) # low order packet length f.append(hi + lo) f.append(f_data) self.fingerprint = sha.new(''.join(f)).hexdigest().upper() # see fingerprint/id notes in doc/NOTES.txt self.id = self.fingerprint[-16:] else: # unsupported packet version raise NotImplementedError("Unsupported key version: %s" % self.version) return idx
def test02_str2hex_hex2str(self): "strnum: string/hex inversion (str2hex/hex2str)" fprint = self.key.body.fingerprint xprint = str2hex(hex2str(fprint)) self.assertEqual(fprint, xprint)
def fill(self, d): ord_d = ord(d[0]) if ord_d < 192: slice = d[0] size = ord_d elif 192 <= ord_d < 255: slice = d[:2] #size = size + STN.doubleoct2int(slice) size = STN.doubleoct2int(slice) elif 255 == ord_d: slice = d[:5] #size = size + STN.pentoct2int(slice) size = STN.pentoct2int(slice) len_slice = len(slice) self._d = d[:len_slice + size] type_d = d[len_slice:len_slice + 1] self.type = 127 & ord(type_d) self.critical = 128 & ord(type_d) value_d = d[len_slice + 1:len_slice + size] if SIGSUB_SIGNERID == self.type: self.value = STN.str2hex(value_d[:8]) elif self.type in [SIGSUB_CREATED, SIGSUB_EXPIRES, SIGSUB_KEYEXPIRES]: self.value = STN.str2int(value_d[:4]) self.value = STN.str2int(value_d) elif self.type in [ SIGSUB_EXPORTABLE, SIGSUB_REVOCABLE, SIGSUB_PRIMARYUID ]: self.value = STN.str2int(value_d[:1]) if self.value not in [0, 1]: raise SignatureSubpacketValueError, "Subpacket (# %s) value must be 0 or 1." % ( str(subtype)) elif SIGSUB_TRUST == self.type: # level, amount self.value = (STN.str2int(value_d[0]), STN.str2int(value_d[1])) elif self.type in [ SIGSUB_SYMCODE, SIGSUB_HASHCODE, SIGSUB_COMPCODE, SIGSUB_KEYSERVPREFS, SIGSUB_KEYFLAGS, SIGSUB_FEATURES ]: self.value = [ord(x) for x in value_d] elif SIGSUB_REVOKER == self.type: cls = STN.str2int(value_d[0]) alg = STN.str2int(value_d[1]) fprint = STN.str2hex(value_d[2:22]) self.value = (cls, alg, fprint) elif SIGSUB_NOTE == self.type: flags = [STN.str2int(x) for x in value_d[:4]] # first four flag octs name_len = STN.str2int(value_d[4:6]) val_len = STN.str2int(value_d[6:8]) nam = value_d[8:8 + name_len] val = value_d[8 + name_len:8 + name_len + val_len] self.value = (flags, nam, val) elif self.type in [ SIGSUB_KEYSERV, SIGSUB_POLICYURL, SIGSUB_SIGNERUID, SIGSUB_REGEX ]: self.value = value_d elif SIGSUB_REVOCREASON == self.type: # code, reason self.value = (STN.str2int(value_d[0]), value_d[1:]) elif SIGSUB_SIGTARGET == self.type: raise NotImplementedError, "SIGTARGET not supported" else: # the subpacket has an unknown type, so just pack the data in self.value = value_d
def fill(self, d): ord_d = ord(d[0]) if ord_d < 192: slice = d[0] size = ord_d elif 192 <= ord_d < 255: slice = d[:2] #size = size + STN.doubleoct2int(slice) size = STN.doubleoct2int(slice) elif 255 == ord_d: slice = d[:5] #size = size + STN.pentoct2int(slice) size = STN.pentoct2int(slice) len_slice = len(slice) self._d = d[:len_slice+size] type_d = d[len_slice:len_slice+1] self.type = 127 & ord(type_d) self.critical = 128 & ord(type_d) value_d = d[len_slice+1:len_slice+size] if SIGSUB_SIGNERID == self.type: self.value = STN.str2hex(value_d[:8]) elif self.type in [SIGSUB_CREATED, SIGSUB_EXPIRES, SIGSUB_KEYEXPIRES]: self.value = STN.str2int(value_d[:4]) self.value = STN.str2int(value_d) elif self.type in [SIGSUB_EXPORTABLE, SIGSUB_REVOCABLE, SIGSUB_PRIMARYUID]: self.value = STN.str2int(value_d[:1]) if self.value not in [0, 1]: raise SignatureSubpacketValueError, "Subpacket (# %s) value must be 0 or 1." % (str(subtype)) elif SIGSUB_TRUST == self.type: # level, amount self.value = (STN.str2int(value_d[0]), STN.str2int(value_d[1])) elif self.type in [SIGSUB_SYMCODE, SIGSUB_HASHCODE, SIGSUB_COMPCODE, SIGSUB_KEYSERVPREFS, SIGSUB_KEYFLAGS, SIGSUB_FEATURES]: self.value = [ord(x) for x in value_d] elif SIGSUB_REVOKER == self.type: cls = STN.str2int(value_d[0]) alg = STN.str2int(value_d[1]) fprint = STN.str2hex(value_d[2:22]) self.value = (cls, alg, fprint) elif SIGSUB_NOTE == self.type: flags = [STN.str2int(x) for x in value_d[:4]] # first four flag octs name_len = STN.str2int(value_d[4:6]) val_len = STN.str2int(value_d[6:8]) nam = value_d[8:8+name_len] val = value_d[8+name_len:8+name_len+val_len] self.value = (flags, nam, val) elif self.type in [SIGSUB_KEYSERV, SIGSUB_POLICYURL, SIGSUB_SIGNERUID, SIGSUB_REGEX]: self.value = value_d elif SIGSUB_REVOCREASON == self.type: # code, reason self.value = (STN.str2int(value_d[0]), value_d[1:]) elif SIGSUB_SIGTARGET == self.type: raise NotImplementedError, "SIGTARGET not supported" else: # the subpacket has an unknown type, so just pack the data in self.value = value_d