class ECVerifyerTestCase(unittest.TestCase): testdata1 = 'test data to sign' testdata2 = 'other data' test_mai_id = 'testmai' ec_name = EC.NID_secp112r1 def flush_mongodb(self): mongoco = Connection() mongoco.drop_database(self.app.config['MONGODB_DB']) def setUp(self): self.app = create_app('testing') self.flush_mongodb() self.ec1 = EC.gen_params(self.ec_name) self.ec1.gen_key() bio1 = BIO.MemoryBuffer() self.ec1.save_pub_key_bio(bio1) self.mai = MetaAppInstance(mai_id=self.test_mai_id, pubkey_ec=bio1.getvalue()) self.mai.save() self.ec2 = EC.gen_params(self.ec_name) self.ec2.gen_key() self.verifyer = crypto.ECVerifier(self.mai) self.sha = sha512() self.temp = tempfile.TemporaryFile() def tearDown(self): self.flush_mongodb() self.temp.close() def test_signature(self): self.sha.update(self.testdata1) sig1 = self.ec1.sign_dsa_asn1(self.sha.digest()) self.temp.write(sig1) assert self.verifyer.verify(self.testdata1, self.temp) == True assert self.verifyer.verify(self.testdata2, self.temp) == False def test_other_signature(self): self.sha.update(self.testdata1) sig2 = self.ec2.sign_dsa_asn1(self.sha.digest()) self.temp.write(sig2) assert self.verifyer.verify(self.testdata1, self.temp) == False
def mai_pubkey(mai_id): """Process a public key uploaded for a MetaAppInstance.""" # pversion = request.args.get('pversion') pubkeyfile = request.files["pubkeyfile"] if not pubkeyfile: return "No pubkeyfile uploaded -> key not uploaded.\n" if not file_allowed(us_maipubkeys, pubkeyfile): return "Filetype not allowed -> key not uploaded.\n" if MetaAppInstance.objects(mai_id=mai_id).count() >= 1: return ("This Meta App Instance (id=%s) already exists and has a key " "-> key not uploaded.\n") % mai_id mai = MetaAppInstance(mai_id=mai_id, pubkey_ec=pubkeyfile.read()) mai.save() return "Key saved.\n"
def request_mai_id(): """Generate an unused mai_id.""" max_tries = 20 found = False for i in range(max_tries): mai_id = crypto.sha512_hash_hex(str(request.headers) + str(time.time())) if MetaAppInstance.objects(mai_id=mai_id).count() == 0: found = True break if not found: abort(500) return mai_id
def setUp(self): self.app = create_app('testing') self.flush_mongodb() self.ec1 = EC.gen_params(self.ec_name) self.ec1.gen_key() bio1 = BIO.MemoryBuffer() self.ec1.save_pub_key_bio(bio1) self.mai = MetaAppInstance(mai_id=self.test_mai_id, pubkey_ec=bio1.getvalue()) self.mai.save() self.ec2 = EC.gen_params(self.ec_name) self.ec2.gen_key() self.verifyer = crypto.ECVerifier(self.mai) self.sha = sha512() self.temp = tempfile.TemporaryFile()
def ea_data(mai_id, name): """Process data uploaded by a MetaAppInstance for an ExpApp.""" # pversion = request.args.get('pversion') datafile = request.files["datafile"] sigfile = request.files["sigfile"] if not datafile: return "No datafile uploaded -> no data uploaded.\n" if not file_allowed(us_eadata, datafile): return "Filetype not allowed -> no data uploaded.\n" if not sigfile: return "No sigfile uploaded -> no data uploaded.\n" if not file_allowed(us_maisignatures, sigfile): return "Filetype not allowed -> no data uploaded.\n" mai = MetaAppInstance.objects(mai_id=mai_id).first() if not mai: return ("Unknown MetaAppInstance (id=%s) -> no data " "uploaded.\n") % mai_id ecv = crypto.ECVerifier(mai) datastring = datafile.read() if not ecv.verify(datastring, sigfile): return "Signature invalid -> no data uploaded.\n" ea = ExpApp.objects(name=name).first() if not ea: return ("Unknown ExpApp (name=%s) -> no data " "uploaded.\n") % name r = Result(data=json.loads(datastring)) r.metaappinstance = mai ea.results.append(r) ea.save() return "Data uploaded.\n"