################################### try: dns.resolver.query('_printer._sub._http._tcp.dns-sd.org.','PTR') except: dns.resolver.default_resolver = dns.resolver.Resolver(configure=False) dns.resolver.default_resolver.nameservers = [ '2001:4860:4860::8888', '2001:4860:4860::8844', '8.8.8.8', '8.8.4.4' ] grasp.tprint("Couldn't resolve test domain, switched to Google nameservers") ################################### # Set up pretty printing ################################### grasp.init_bubble_text("GetDNSSD2") grasp.tprint("GetDNSSD2 is listening") ################################### # Negotiate as listener for ever ################################### while True: # listen for negotiation request err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj1) if err: grasp.tprint("listen_negotiate error:",grasp.etext[err]) time.sleep(5) #to calm things if there's a looping error else: #got a new negotiation request; kick off a separate negotiator #so that multiple requests can be handled in parallel
#This objective is for the negotiating test obj3 = grasp.objective("411:DNSSD") obj3.neg = True obj3.loop_count = 4 err = grasp.register_obj(asa_nonce, obj3) if not err: grasp.tprint("Objective", obj3.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("AskDNSSD") grasp.tprint("Ready to negotiate", obj3.name, "as requester") #main loop while True: #insert test cases here get_dns_info('_sip._udp.sip.voice.google.com') #get_dns_info('_dmarc.gmail.com') #get_dns_info('_http._tcp.dns-sd.org') #time.sleep(5) get_dns_info('_printer._sub._http._tcp.dns-sd.org.') time.sleep(5) get_dns_info('_http._tcp.dns-sd.org.') time.sleep(5)
flood_obj = grasp.objective("411:quadskip") flood_obj.loop_count = 4 flood_obj.synch = True #The value of this objective is the quadski public key PEM string err = grasp.register_obj(asa_nonce, flood_obj) if not err: grasp.tprint("Objective", flood_obj.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("QUADSKI Server") grasp.tprint("QUADSKI is operational") ################################### # Make a key pair for quadski ################################### private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend()) public_key = private_key.public_key() my_pem = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo) ###################################
obj2.loop_count = 4 obj2.synch = True if master: _err = grasp.register_obj(asa_nonce, obj2) if not _err: grasp.tprint("Objective", obj2.name, "registered OK") else: grasp.tprint("Fatal error:", grasp.etext[_err]) exit() #################################### # Start pretty printing #################################### grasp.init_bubble_text(btext) #################################### # If we have an initial pool, start # acting as a source of parameters by # flooding and listening for synch requests #################################### if master: # Arbitrary parameter values for demo only obj2.value = [[["role", "RSG"], ["prefix_length", 34]], [["role", "ASG"], ["prefix_length", 44]], [["role", "CSG"], ["prefix_length", 56]]] ## Or they could be: ## Turning the alists into maps:
grasp.tprint("Flood cache contents:") for x in grasp._flood_cache: grasp.tprint(x.objective.name,"count:",x.objective.loop_count,"value:", x.objective.value,"source",x.source.locator) time.sleep(5) dump_some() ################################### # Negotiate EX3 as initiator for ever ################################### grasp.tprint("Ready to negotiate EX3 as requester") grasp.ttprint("(Note: Cyrillic test case fails in a Windows console window, OK in IDLE window.)") failct = 0 grasp.init_bubble_text("Gray") while True: #start of a negotiating session iwant = grasp._prng.randint(10, 500) # random requested value limit = int(0.7*iwant) obj3.value = ["NZD",iwant] obj3.loop_count = grasp._prng.randint(4, 20) # random loop count #if not iwant%5: if iwant & 4: obj3.dry = True # signal a dry run at random else: obj3.dry = False #if not iwant%6:
# but is not running any ASA # # Because it's demonstration code written in an # interpreted language, performance is slow. # # SECURITY WARNINGS: # - assumes ACP up on all interfaces (or none) # - assumes BUT DOES NOT CHECK that layer 2 is secured # - does not watch for interface up/down changes # (but does handle IPv6 address changes) # # LIMITATIONS: # - only coded for IPv6, any IPv4 is accidental # - survival of address changes and CPU sleep/wakeup is patchy # - workarounds for defects in Python socket module and # Windows socket peculiarities. Not tested on Android. # # See grasp.py for license, copyright, and disclaimer. # ######################################################## ########################################################""" import grasp import time print("Starting GRASP daemon") grasp._initialise_grasp() grasp.init_bubble_text("GRASP daemon") grasp.tprint("Daemon running") while True: time.sleep(60)
#################################### # Register the GRASP objective #################################### _err = grasp.register_obj(_asa_nonce, proxy_obj) if not _err: grasp.tprint("Objective", proxy_obj.name, "registered OK") else: grasp.tprint("Objective registration failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors #################################### # Start pretty printing #################################### grasp.init_bubble_text("BRSKI Join Proxy") grasp.tprint("Proxy starting now") ################################### # Now find the registrar and pick one or two methods ################################### while True: registrar1 = None registrar2 = None _err, _results = grasp.get_flood(_asa_nonce, reg_obj) if not _err: # _results contains the returned locators if any for x in _results: # use whatever logic you want to decide which results to use. # For the demo code, we just pick one or two at random:
exit() #################################### # Construct GRASP objectives #################################### # These are empty GRASP objectives to find the peer(s) # They are only used for get_flood so don't need to be filled in acp_obj = grasp.objective("AN_ACP") acp_obj.synch = True est_obj = grasp.objective("SRV.est") est_obj.synch = True grasp.init_bubble_text("ACP Newby") grasp.tprint("Newby starting now") ################################### # Now find the flood(s) ################################### while True: _err, _results = grasp.get_flood(_asa_nonce, acp_obj) if not _err: # _results contains all the unexpired tagged objectives grasp.tprint("Found",len(_results),"result(s) for",acp_obj.name) for x in _results: # Print the result grasp.tprint(x.objective.name, "value", x.objective.value, "locator", x.source.locator,
#################################### # Register the GRASP objective #################################### _err = grasp.register_obj(asa_nonce, reg_obj) if not _err: grasp.tprint("Objective", reg_obj.name, "registered OK") else: grasp.tprint("Objective registration failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors #################################### # Start pretty printing #################################### grasp.init_bubble_text("BRSKI Join Registrar (flooding method)") grasp.tprint("Registrar starting now") #################################### # Start flooding thread #################################### flooder().start() grasp.tprint("Flooding", reg_obj.name, "for ever") ################################### # Listen for requests ################################### # Here, launch a thread to do the real work of the registrar # via the various ports But for the demo, we just pretend...
supported_obj = grasp.objective("411:mvFile") supported_obj.loop_count = 4 supported_obj.neg = True err = grasp.register_obj(asa_nonce, supported_obj) if not err: grasp.tprint("Objective", supported_obj.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("pusher") grasp.tprint("pusher is listening") ################################### # Negotiate as listener for ever ################################### while True: # listen for negotiation request err, snonce, request = grasp.listen_negotiate(asa_nonce, supported_obj) if err: grasp.tprint("listen_negotiate error:", grasp.etext[err]) time.sleep(5) #to calm things if there's a looping error else: #got a new negotiation request; kick off a separate negotiator #so that multiple requests can be handled in parallel
exit() #This objective is for the negotiating test requested_obj = grasp.objective("411:mvFile") requested_obj.neg = True requested_obj.loop_count = 4 err = grasp.register_obj(asa_nonce, requested_obj) if not err: grasp.tprint("Objective", requested_obj.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("getter") grasp.tprint("Ready to negotiate", requested_obj.name, "as requester") if os.name == "nt": directory = "got\\" else: directory = "got/" ################################### # Main loop ################################### while True: get_file(input('File name:'))
grasp.tprint("ASA Pledji registered OK") else: grasp.tprint("ASA registration failure:", grasp.etext[_err]) exit() #################################### # Construct a GRASP objective #################################### # This is an empty GRASP objective to find the proxy # It's only used for get_flood so doesn't need to be filled in proxy_obj = grasp.objective("AN_proxy") proxy_obj.synch = True grasp.init_bubble_text("BRSKI Pledge (flooding method)") grasp.tprint("Pledge starting now") ################################### # Now find the proxy(s) ################################### while True: proxy = None _err, _results = grasp.get_flood(_asa_nonce, proxy_obj) if not _err: # _results contains all the unexpired tagged objectives grasp.tprint("Found", len(_results), "result(s)") for x in _results: # Extract the details try:
#file as the return value of this objective, as an array [key, iv] #encoded in CBOR and then encrypted with the pledge's public key err = grasp.register_obj(asa_nonce,keys_obj) if not err: grasp.tprint("Objective", keys_obj.name, "registered OK") else: exit() ################################### # Set up pretty printing ################################### grasp.init_bubble_text("QUADS Pledge") grasp.tprint("Ready to negotiate", keys_obj.name,"as requester") ################################### # Main loop ################################### get_file() while not os.path.isfile("quadsk.py"): get_file() grasp.tprint("Installed new key file") if grasp._relay_needed: grasp.tprint("This is a relay node - launching encrypted GRASP daemon")
if err: grasp.tprint("end_negotiate error:", grasp.etext[err]) #end of a negotiating session ################################### # Negotiate EX3 as listener for ever ################################### obj3.value = ["NZD", 0] grasp.tprint("Ready to negotiate EX3 as listener") grasp.ttprint( "(Note: Cyrillic test case fails in a Windows console window, OK in IDLE window.)" ) grasp.init_bubble_text("Briggs") while True: #start of a negotiating session #create a random amount of resource and a random waiting time reserves = grasp._prng.randint(100, 400) wt = grasp._prng.randint(9000, 20000) grasp.tprint("Reserves: $", reserves, "Wait:", wt, "ms") #attempt to listen for negotiation err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj3) if err: grasp.tprint("listen_negotiate error:", grasp.etext[err]) time.sleep(5) #to calm things if there's a looping error else:
grasp.dump_all(partial=True) time.sleep(5) dump_some() ################################### # Negotiate EX3 as initiator for ever ################################### grasp.tprint("Ready to negotiate EX3 as requester") grasp.ttprint( "(Note: Cyrillic test case fails in a Windows console window, OK in IDLE window.)" ) failct = 0 grasp.init_bubble_text("Gray (old API)") while True: #start of a negotiating session iwant = _prng.randint(10, 500) # random requested value limit = int(0.7 * iwant) obj3.value = ["NZD", iwant] obj3.loop_count = _prng.randint(4, 20) # random loop count #if not iwant%5: if iwant & 4: obj3.dry = True # signal a dry run at random else: obj3.dry = False #if not iwant%6:
name = "TestClient" err,asa_handle = grasp.register_asa(name) if err: exit() grasp.tprint("ASA", name, "registered OK") obj3 = grasp.objective("EX3") obj3.neg = True err = grasp.register_obj(asa_handle,obj3) if not err: grasp.tprint("Objective EX3 registered OK") else: exit() grasp.init_bubble_text(name) grasp.tprint("Ready to negotiate EX3 as client") failct = 0 while True: #start of a negotiating session obj3.value = "Start" #discover a peer if failct > 3: failct = 0 grasp.tprint("Flushing EX3 discovery") _, ll = grasp.discover(asa_handle, obj3, 1000, flush = True) else: _, ll = grasp.discover(asa_handle, obj3, 1000)
err = grasp.register_obj(asa_nonce, obj3) if not err: grasp.tprint("Objective", obj_name, "registered OK") else: grasp.tprint("Objective registration failure:", grasp.etext[err]) time.sleep(60) exit() if grasp.test_mode: dump_some() ################################### # Negotiate MUDURL as initiator to send URL ################################### grasp.init_bubble_text(asa_name) grasp.tprint("Ready to negotiate", obj_name, "as requester") failct = 0 while True: obj3.value = input("Enter sample MUD URL:") #########this tests an error case########## if obj3.value == "magic": obj3.value = 987654321 grasp.ttprint("Starting discovery for", obj_name) #discover a peer if failct > 3: failct = 0 grasp.tprint("Flushing discovery") _, ll = grasp.discover(asa_nonce, obj3, 1000, flush=True) else:
else: grasp.tprint("Objective registration failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors _err = grasp.register_obj(_asa_nonce, est_obj) if not _err: grasp.tprint("Objective", est_obj.name, "registered OK") else: grasp.tprint("Objective registration failure:", grasp.etext[_err]) exit() # demo code doesn't handle registration errors #################################### # Start pretty printing #################################### grasp.init_bubble_text("ACP container") grasp.tprint("ACPcontainer starting now") ################################### # Now flood the objectives out at # a suitable frequency ################################### while True: acp_locator.port = 500 + grasp._prng.randint(0, 5) #slightly random for demo est_locator.port = 80 + grasp._prng.randint(0, 5) grasp.tprint("Flooding", acp_obj.name, acp_locator.protocol, acp_locator.port, "and", est_obj.name, est_locator.protocol, est_locator.port) grasp.flood(_asa_nonce, acp_ttl,
if err: grasp.tprint("end_negotiate error:", grasp.etext[err]) #end of a negotiating session ################################### # Negotiate EX3 as listener for ever ################################### obj3.value = ["NZD", 0] grasp.tprint("Ready to negotiate EX3 as listener") grasp.ttprint( "(Note: Cyrillic test case fails in a Windows console window, OK in IDLE window.)" ) grasp.init_bubble_text("Briggs (old API)") while True: #start of a negotiating session #create a random amount of resource and a random waiting time reserves = _prng.randint(100, 400) wt = _prng.randint(9000, 20000) grasp.tprint("Reserves: $", reserves, "Wait:", wt, "ms") #attempt to listen for negotiation err, snonce, answer = grasp.listen_negotiate(asa_nonce, obj3) if err: grasp.tprint("listen_negotiate error:", grasp.etext[err]) time.sleep(5) #to calm things if there's a looping error else: