def __testSessionDecrypt(self, subdir): path = os.path.join(self.TEST_DATA, subdir) material = util.ReadFile(os.path.join(path, "2.session.material")) ciphertext = util.ReadFile(os.path.join(path, "2.session.ciphertext")) crypter = keyczar.Crypter.Read(path) session = keyczar.SessionDecrypter(crypter, material) decrypted = session.Decrypt(ciphertext) self.assertEqual(self.input, decrypted)
def __testDecrypt(self, subdir): path = os.path.join(self.TEST_DATA, subdir) crypter = keyczar.Crypter.Read(path) active_ciphertext = util.ReadFile(os.path.join(path, "1.out")) primary_ciphertext = util.ReadFile(os.path.join(path, "2.out")) active_decrypted = crypter.Decrypt(active_ciphertext) self.assertEqual(self.input, active_decrypted) primary_decrypted = crypter.Decrypt(primary_ciphertext) self.assertEqual(self.input, primary_decrypted)
def __readGoldenOutput(self, subdir, public=False): path = os.path.join(self.TEST_DATA, subdir) if not public: czar = keyczar.Verifier.Read(path) else: czar = keyczar.Verifier.Read(os.path.join(self.TEST_DATA, subdir+".public")) active_sig = util.ReadFile(os.path.join(path, "1.out")) primary_sig = util.ReadFile(os.path.join(path, "2.out")) return (czar, active_sig, primary_sig)
def __testDecrypt(self, subdir, reader=None): path = os.path.join(TEST_DATA, subdir) if reader: crypter = keyczar.Crypter(reader) else: crypter = keyczar.Crypter.Read(path) active_ciphertext = util.ReadFile(os.path.join(path, "1.out")) primary_ciphertext = util.ReadFile(os.path.join(path, "2.out")) active_decrypted = crypter.Decrypt(active_ciphertext) self.assertEqual(self.input_data, active_decrypted) primary_decrypted = crypter.Decrypt(primary_ciphertext) self.assertEqual(self.input_data, primary_decrypted)
def __testVerifyUnversioned(self, subdir, public=False): path = os.path.join(self.TEST_DATA, subdir) verifypath = path if public: verifypath = path + ".public" sig = util.ReadFile(os.path.join(path, "2.unversioned")) verifier = keyczar.UnversionedVerifier.Read(verifypath) self.assertTrue(verifier.Verify(self.input, sig))
def __testVerifyAttached(self, subdir, secret="", public =False): path = os.path.join(self.TEST_DATA, subdir) verifypath = path if public: verifypath = path +".public" ext = ".attached" if secret: ext = "." + secret + ext message = util.ReadFile(os.path.join(path, "2" + ext)) verifier = keyczar.Verifier.Read(verifypath) self.assertTrue(verifier.AttachedVerify(message, secret))
def __testDecryptStream(self, subdir, reader, input_data, stream_buffer_size, len_to_read, stream_source): """NOTE: input_data ignored here as we don't have a valid ".out" for random data""" path = os.path.join(TEST_DATA, subdir) if reader: crypter = keyczar.Crypter(reader) else: crypter = keyczar.Crypter.Read(path) active_ciphertext = util.ReadFile(os.path.join(path, "1.out")) if stream_source is None: decoder = None active_ciphertext = util.Base64WSDecode(active_ciphertext) else: decoder = util.IncrementalBase64WSStreamReader decryption_stream = crypter.CreateDecryptingStreamReader( StringIO.StringIO(active_ciphertext), decoder=decoder, buffer_size=stream_buffer_size) plaintext = self.__readFromStream(decryption_stream, len_to_read) self.assertEquals(self.input_data, plaintext, 'Active not equals for buffer:%d, read len:%d, src:%s' %( stream_buffer_size, len_to_read, stream_source )) primary_ciphertext = util.ReadFile(os.path.join(path, "2.out")) if stream_source is None: primary_ciphertext = util.Base64WSDecode(primary_ciphertext) decryption_stream = crypter.CreateDecryptingStreamReader( StringIO.StringIO(primary_ciphertext), decoder=decoder, buffer_size=stream_buffer_size) plaintext = self.__readFromStream(decryption_stream, len_to_read) self.assertEquals(self.input_data, plaintext, 'Primary not equals for buffer:%d, read len:%d, src:%s' %( stream_buffer_size, len_to_read, stream_source ))
def __testVerifyTimeout(self, subdir, expired =False, public =False): path = os.path.join(self.TEST_DATA, subdir) verifypath = path if public: verifypath = path +".public" date = lambda: datetime.datetime(2012, 12, 21, 11, 6) if expired: date = lambda:datetime.datetime(2012, 12, 21, 11, 16) sig = util.ReadFile(os.path.join(path, "2.timeout")) verifier = keyczar.TimeoutVerifier.Read(verifypath) verifier.SetCurrentTimeFunc(date) self.assertEqual(verifier.Verify(self.input, sig), not expired)
def GetKey(self, version_number): return util.ReadFile(os.path.join(self._location, str(version_number)))
def GetMetadata(self): return util.ReadFile(os.path.join(self._location, "meta"))
def __testDecryptSizes(self, subdir, size): path = os.path.join(self.TEST_DATA, subdir) + "-size" crypter = keyczar.Crypter.Read(path) active_ciphertext = util.ReadFile(os.path.join(path, size + ".out")) active_decrypted = crypter.Decrypt(active_ciphertext) self.assertEqual(self.input, active_decrypted)