def testcase_12(headers, body_content, domain, key): headers = headers.copy() body_content = body_content.copy() body_content['access_token'] = "INVALID_ACCESS_TOKEN" concat_dict = body_content.copy() concat_dict['domain'] = domain concat_dict['key'] = key concat_text = common_data.get_concat_text(concat_dict) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.get(url + path + domain + "/" + key, params=body_content, headers=headers) #response = requests.get(url + path + domain +"/" + key, params=body_content, headers=headers, allow_redirects=False) if response.status_code == 401 and response.json()['code'] == "401.0": print "TEST CASE 12 OK" else: print "TEST CASE 12 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_0(headers, body_content, old_domain, new_domain): body_content['new_domain'] = new_domain headers = headers.copy() body_content = body_content.copy() concat_dict = body_content.copy() concat_dict['domain'] = old_domain concat_text = common_data.get_concat_text(concat_dict) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.put(url + path + old_domain, data=body_content, headers=headers) if response.status_code == 200: print "TEST CASE 0 OK" else: print "TEST CASE 0 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def parse_file(path): lines = [line.rstrip('\n') for line in open(path)] measurements = map(parse_line_to_measurements, lines) filename = get_filename_without_extension(path) signature = Signature(measurements, filename) return signature
def testcase_8(headers, body_content, domain): headers = headers.copy() body_content = body_content.copy() domain = "INVALID_DOMAIN@" concat_dict = body_content.copy() concat_dict['domain'] = quote(domain) concat_text = common_data.get_concat_text(concat_dict) print concat_text concat_text = body_content['access_token'] + body_content[ 'certificate_serial'] + "INVALID_DOMAIN@" signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) print signed_signature headers['X-Signature'] = signed_signature print quote(domain) response = requests.get(url + path + quote(domain), params=body_content, headers=headers) if response.status_code == 400: # and response.json()['code'] == "400.6": print "TEST CASE 8 OK" else: print "TEST CASE 8 FAILED" print response.status_code print "HTTP Path: " + url + path + domain print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_3(headers, body_content, domain): headers = headers.copy() body_content = body_content.copy() domain = "" concat_dict = body_content.copy() concat_dict['domain'] = domain concat_text = common_data.get_concat_text(concat_dict) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.get(url + path + domain, params=body_content, headers=headers) if response.status_code == 400: #and response.json()['code'] == "400.0": print "TEST CASE 3 OK" else: print "TEST CASE 3 FAILED" print response.status_code print "HTTP Path: " + url + path + domain print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_3(headers, body_content, domain, key): headers = headers.copy() body_content = body_content.copy() domain = "" concat_dict = body_content.copy() concat_dict['domain'] = domain concat_dict['key'] = key concat_text = common_data.get_concat_text(concat_dict) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.get(url + path + domain + "/" + key, params=body_content, headers=headers) #response = requests.get(url + path + domain +"/" + key, params=body_content, headers=headers, allow_redirects=False) # TODO check error code if response.status_code == 400 and response.json()['code'] == "400.0": print "TEST CASE 3 OK" else: print "TEST CASE 3 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def sign(self, z) -> Signature: k = randint(0, N) r = (k * G).x.num k_inv = pow(k, N - 2, N) s = (z + r * self.secret) * k_inv % N # ToDO:check why this if statement is needed if s > N / 2: s = N - 2 #r,s are neded to verify whteher given signature is valid return Signature(r, s)
def collectSig(self, sig=None): if not sig: sig = Signature() leftclauses = [ clause for clause in self.clauseDeleted if clause is not True ] for i in leftclauses: i.collectSig(sig) return sig
def collectSig(self, sig=None): """ Collect function- and predicate symbols into the signature. If none exists, create it. Return the signature """ if not sig: sig = Signature() for i in self.clauses: i.collectSig(sig) return sig
def parse_sig(self, macho): if not macho.has_lc('CODE_SIGNATURE'): return prev = self.f.tell() true_offset = (macho.offset + macho.get_lc('CODE_SIGNATURE').data['offset']) if true_offset >= self.file.size: data = { 'offset': true_offset, 'file_size': self.file.size } a = Abnormality(title='CODE_SIGNATURE OUT OF BOUNDS', data=data) self.add_abnormality(a) return self.f.seek(true_offset) magic = get_int(self.f) if magic != dictionary.signatures['EMBEDDED_SIGNATURE']: data = { 'offset': true_offset, 'magic': hex(magic), 'expected': hex(dictionary.signatures['EMBEDDED_SIGNATURE']) } a = Abnormality(title='BAD MAGIC - EMBEDDED_SIGNATURE', data=data) self.add_abnormality(a) self.f.seek(prev) return size = get_int(self.f) count = get_int(self.f) signature = Signature(offset=true_offset, size=size, count=count) while count > 0: index_type = get_int(self.f) try: index_type = dictionary.indeces[index_type] except: data = { 'offset': self.f.tell() - 4, 'index_type': index_type } a = Abnormality(title='INVALID CODE_SIGNATURE INDEX_TYPE', data=data) self.add_abnormality(a) offset = get_int(self.f) if index_type == 'SignatureSlot': self.parse_certs(signature, offset) elif index_type == 'CodeDirectorySlot': self.parse_codedirectory(signature, offset) elif index_type == 'EntitlementSlot': self.parse_entitlement(signature, offset) elif index_type == 'RequirementsSlot': self.parse_requirements(signature, offset) count -= 1 macho.signature = signature self.f.seek(prev)
def collectSig(self, sig=None): """ Collect function- and predicate symbols into the signature. If none exists, create it. Return the signature """ if not sig: sig = Signature() sig.addPred(termFunc(self.atom), len(self.atom) - 1) for s in termArgs(self.atom): termCollectSig(s, sig) return sig
def termCollectSig(t, sig=None): """ Insert all function symbols and their associated arities in t into the signature sig. For convenience, return it. If sig is not given, create it. """ if sig == None: sig = Signature() if termIsCompound(t): sig.addFun(termFunc(t), len(t) - 1) for s in termArgs(t): termCollectSig(s, sig) return sig
def testClauseSetChanges(self): """ Test that clause set initialization and parsing work. """ lexer = Lexer(self.spec) clauses = ClauseSet() clauses.parse(lexer) print(clauses) oldlen = len(clauses) c = clauses.clauses[0] clauses.extractClause(c) self.assertEqual(len(clauses), oldlen - 1) sig = Signature() clauses.collectSig(sig) print(sig)
def __init__(self, jsonstr=None, key=None): """Initialization of the object Args: jsonstr (str): a raw JSON string that is returned by a request. We store all the data in `self.json_data` and use `__getattr__` and `__setattr__` to make the data accessible like attributes of the object key (str): Optional key to use with jsonstr. If `key` exists, we'll load the data of `jsonstr[key]` instead of the whole `jsonstr` """ super(SignatureRequest, self).__init__(jsonstr, key) if 'signatures' in self.json_data: signature_list = [] for signature in self.signatures: signature_list.append(Signature(signature)) self.signatures = signature_list
def addEqAxioms(self): """ Add equality axioms (if necessary). Return True if equality is present, false otherwise. """ sig = Signature() for c in self.clauses: c.collectSig(sig) for f in self.formulas: f.collectSig(sig) if sig.isPred("="): res = generateEquivAxioms() res.extend(generateCompatAxioms(sig)) self.clauses.extend(res) return True return False
def collectSig(self, sig=None): """ Return the set of all function and predicate symbols used in the formula. """ if not sig: sig = Signature() if self.isLiteral(): self.child1.collectSig(sig) elif self.isUnary(): self.child1.collectSig(sig) elif self.isBinary(): self.child1.collectSig(sig) self.child2.collectSig(sig) else: assert self.isQuantified() self.child2.collectSig(sig) return sig
def testcase_1(headers, body_content): headers = headers.copy() body_content = body_content.copy() headers.pop('Content-Type') concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) if response.status_code == 400 and response.json()['code'] == "400.23": print "TEST CASE 1 OK!" else: print "TEST CASE 1 FAILED!" print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testCompatibility(self): """ Test that compatibility axioms are generated as expected. """ ax = generateFunCompatAx("f", 3) self.assertEqual(len(ax), 4) print(ax) ax = generatePredCompatAx("p", 5) self.assertEqual(len(ax), 7) print(ax) sig = Signature() sig.addFun("f", 2) sig.addPred("p", 3) sig.addFun("a", 0) tmp = generateCompatAxioms(sig) # Note: No axiom for a self.assertEqual(len(tmp), 2)
def get_device_authentication_token(): headers = dict() headers = init_headers(headers) body_content = dict() init_body_content(body_content) concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = response = requests.post(url + path, data=body_content, headers=headers) if response.status_code == 200: resp_data = response.json() if 'data' in resp_data and 'access_token' and 'refresh_token' in resp_data['data']: return resp_data['data'] else: return {"access_token": "", "refresh_token": ""} else: return {"access_token": "", "refresh_token": ""}
def testcase_9(headers, body_content): headers = headers.copy() body_content = body_content.copy() headers['X-Api-Key'] = "INVALID_X_API_KEY" concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) if response.status_code == 400: # and response.json()['code'] == "400.1": print "TEST CASE 9 OK!" else: print response.status_code print "TEST CASE 9 FAILED!" print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_18(headers, body_content): headers = headers.copy() body_content = body_content.copy() body_content['content'] = "INVALID_CONTENT" concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) if response.status_code == 400 and response.json()['code'] == "400.21": print "TEST CASE 18 OK" else: print "TEST CASE 18 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_11(headers, body_content): headers = headers.copy() body_content = body_content.copy() body_content['access_token'] = "INVALID_ACCESS_TOKEN" concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) if response.status_code == 400 and response.json()['code'] == "401.0": print "TEST CASE 11 OK!" else: print response.status_code print "TEST CASE 11 FAILED!" print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_2(headers, body_content): headers = headers.copy() body_content = body_content.copy() headers.pop('X-Api-Key') concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) # TODO check error code if response.status_code == 400: print "TEST CASE 2 OK" else: print "TEST CASE 2 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_16(headers, body_content): headers = headers.copy() body_content = body_content.copy() body_content['key'] = "QQQQ$%#%$$" concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) # TODO check error code if response.status_code == 400 and response.json()['code'] == "400.14": print "TEST CASE 16 OK" else: print "TEST CASE 16 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def collectSig(self, sig=None): """ Return the set of all function and predicate symbols used in the formula. """ if not sig: sig = Signature() todo = deque() todo.append(self) while todo: f = todo.popleft() if f.isLiteral(): f.child1.collectSig(sig) elif f.isUnary(): todo.append(f.child1) elif f.isBinary(): todo.append(f.child1) todo.append(f.child2) else: assert f.isQuantified() todo.append(f.child2) return sig
def testcase_0(headers, body_content): headers = headers.copy() body_content = body_content.copy() concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) if response.status_code == 200 : resp_data = response.json() if 'data' in resp_data and 'access_token' and 'refresh_token' in resp_data['data']: print "TEST CASE 0 OK" else: print "TEST CASE 0 FAILED" print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) else: print "TEST CASE 0 FAILED" print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
def testcase_10(headers, body_content): headers = headers.copy() body_content = body_content.copy() headers['Content-Type'] = "INVALID_CONTENT_TYPE" #text/plain concat_text = common_data.get_concat_text(body_content) signature = Signature() signature.load_key(common_data.certificate_serial) signed_signature = signature.sign(concat_text) headers['X-Signature'] = signed_signature response = requests.post(url + path, data=body_content, headers=headers) # TODO check error code if response.status_code == 400 and response.json()['code'] == "400.20": print "TEST CASE 10 OK" else: print "TEST CASE 10 FAILED" print response.status_code print "HTTP Header:" + str(headers) print "HTTP Body:" + str(body_content) print response.text
from tornado.escape import json_decode, json_encode from interface import InterfaceManager, Interface from config import Config from utils import ImageUtils, ParamUtils, Arithmetic from signature import Signature, ServerType from tornado.concurrent import run_on_executor from concurrent.futures import ThreadPoolExecutor from middleware import * from event_loop import event_loop tornado.options.define('request_count', default=dict(), type=dict) model_path = "model" system_config = Config(conf_path="config.yaml", model_path=model_path, graph_path="graph") sign = Signature(ServerType.TORNADO, system_config) arithmetic = Arithmetic() semaphore = asyncio.Semaphore(500) scheduler = BackgroundScheduler(timezone=utc) class BaseHandler(RequestHandler): def __init__(self, application, request, **kwargs): super().__init__(application, request, **kwargs) self.exception = Response(system_config.response_def_map) self.executor = ThreadPoolExecutor(workers) self.image_utils = ImageUtils(system_config) def data_received(self, chunk): pass
from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler from config import Config from utils import ImageUtils from constants import Response from interface import InterfaceManager from signature import Signature, ServerType from watchdog.observers import Observer from event_handler import FileEventHandler from middleware import * # The order cannot be changed, it must be before the flask. app = Flask(__name__) cache = Cache(app, config={'CACHE_TYPE': 'simple'}) sign = Signature(ServerType.FLASK) _except = Response() conf_path = 'config.yaml' model_path = 'model' graph_path = 'graph' system_config = Config(conf_path=conf_path, model_path=model_path, graph_path=graph_path) route_map = {i['Class']: i['Route'] for i in system_config.route_map} sign.set_auth([{ 'accessKey': system_config.access_key, 'secretKey': system_config.secret_key }]) logger = system_config.logger
import optparse import threading import tornado.ioloop import tornado.log from tornado.web import RequestHandler from constants import Response from json.decoder import JSONDecodeError from tornado.escape import json_decode, json_encode from interface import InterfaceManager from config import Config from utils import ImageUtils, ParamUtils from signature import Signature, ServerType from watchdog.observers import Observer from event_handler import FileEventHandler sign = Signature(ServerType.TORNADO) def rpc_request(image, model_name="", model_type=""): channel = grpc.insecure_channel('127.0.0.1:50054') stub = grpc_pb2_grpc.PredictStub(channel) response = stub.predict(grpc_pb2.PredictRequest( image=image, split_char=',', model_name=model_name, model_type=model_type )) return {"message": response.result, "code": response.code, "success": response.success} class BaseHandler(RequestHandler):