def check_key_works(keys): verifying_key, signing_key = keys v = rsa.create_verifying_key_from_string(verifying_key) s = rsa.create_signing_key_from_string(signing_key) junk = os.urandom(42) sig = s.sign(junk) self.failUnless(v.verify(junk, sig))
def test_serialize_and_deserialize_signing_key_and_test(self): signer = rsa.generate(KEYSIZE) verifier = signer.get_verifying_key() serstr = signer.serialize() signer = None newsigner = rsa.create_signing_key_from_string(serstr) self._help_test_sign_and_check(newsigner, verifier, "a") self._help_test_sign_and_check_random(newsigner, verifier) self._help_test_sign_and_failcheck(newsigner, verifier, "a") self._help_test_sign_and_failcheck_random(newsigner, verifier)
def _try_to_validate_privkey(self, enc_privkey, reader, server): alleged_privkey_s = self._node._decrypt_privkey(enc_privkey) alleged_writekey = hashutil.ssk_writekey_hash(alleged_privkey_s) if alleged_writekey != self._node.get_writekey(): self.log("invalid privkey from %s shnum %d" % (reader, reader.shnum), level=log.WEIRD, umid="YIw4tA") if self._verify: self.servermap.mark_bad_share(server, reader.shnum, self.verinfo[-2]) e = CorruptShareError(server, reader.shnum, "invalid privkey") f = failure.Failure(e) self._bad_shares.add((server, reader.shnum, f)) return # it's good self.log("got valid privkey from shnum %d on reader %s" % (reader.shnum, reader)) privkey = rsa.create_signing_key_from_string(alleged_privkey_s) self._node._populate_encprivkey(enc_privkey) self._node._populate_privkey(privkey) self._need_privkey = False
def _try_to_validate_privkey(self, enc_privkey, peerid, shnum, lp): alleged_privkey_s = self._node._decrypt_privkey(enc_privkey) alleged_writekey = hashutil.ssk_writekey_hash(alleged_privkey_s) if alleged_writekey != self._node.get_writekey(): self.log("invalid privkey from %s shnum %d" % (idlib.nodeid_b2a(peerid)[:8], shnum), parent=lp, level=log.WEIRD, umid="YIw4tA") return # it's good self.log("got valid privkey from shnum %d on peerid %s" % (shnum, idlib.shortnodeid_b2a(peerid)), parent=lp) privkey = rsa.create_signing_key_from_string(alleged_privkey_s) self._node._populate_encprivkey(enc_privkey) self._node._populate_privkey(privkey) self._need_privkey = False
def __init__(self, name ): PublicKey.__init__(self) key_file = os.path.join('keys', name) if os.path.exists(key_file): log.info('loading %s\'s private key'%(name,)) self.private_key = rsa.create_signing_key_from_string( read_file(key_file) ) if self.private_key: log.info('private key %s loaded'%(name,)) else: log.error('invalid private key for %s'%(name,)) raise Exception('no private key for %s'%(name,)) else: log.info('genreating new private key for user %s'%(name,)) self.private_key = rsa.generate(get_default_key_size()) if not os.path.exists('keys'): os.mkdir('keys') write_file(key_file, self.private_key.serialize()) self.public_key = self.private_key.get_verifying_key()
def make_key_objs((verifying_key, signing_key)): v = rsa.create_verifying_key_from_string(verifying_key) s = rsa.create_signing_key_from_string(signing_key) return v, s
def test_create_from_string_invalid(self): try: rsa.create_signing_key_from_string("invalid string") except rsa.Error, le: self.failUnless("decode error" in str(le), le)