Esempio n. 1
0
void server(char server, struct item *key)
  /*@ requires [?f0]world(rpc_pub, rpc_key_clsfy) &*&
               principal(server, ?count) &*&
               item(key, symmetric_key_item(?creator, ?id), rpc_pub) &*&  
               shared_with(creator, id) == server;
  @*/
  /*@ ensures  [f0]world(rpc_pub, rpc_key_clsfy) &*&
               principal(server, count + 1) &*&
               item(key, symmetric_key_item(creator, id), rpc_pub);
  @*/
{
    struct network_status *net_stat = network_bind_and_accept(SERVER_PORT);
    
    struct item *request = 0;
    {
        struct item *r = network_receive(net_stat);
        check_is_pair(r);
        //@ assert item(r, pair_item(?h0, ?p0), rpc_pub);
        struct item *hmac1 = pair_get_first(r);
        //@ assert item(hmac1, ?h, rpc_pub);
        struct item *payload = pair_get_second(r);
        //@ assert item(payload, ?p, rpc_pub);
        
        /*@ if (!col)
            {
              assert h0 == h;
              assert p0 == p;
              open [_]rpc_pub(pair_item(h, p));
              open [_]rpc_pub(h);
              open [_]rpc_pub(p);
            }
        @*/
        struct item *hmac2 = create_hmac(key, payload);
        item_check_equal(hmac1, hmac2);
        item_free(hmac1);
        item_free(hmac2);
        item_free(r);
        //@ assert col || h == hmac_item(creator, id, some(p));
        struct item *tag = pair_get_first(payload);
        check_is_data(tag);
        int tagValue = item_get_data_as_int(tag);
        if (tagValue != 0) abort();
        //@ item d = data_item(chars_of_int(0));
        request = pair_get_second(payload);
        
        /*@ if (!col) 
            {  
              assert item(tag, d, rpc_pub);
              assert item(request, ?req, rpc_pub);
              assert p == pair_item(d, req); 
            }
        @*/
        item_free(tag);
        item_free(payload);
    }
    
    //@ assert item(request, ?req, rpc_pub);
    struct item *response = compute_response(server, request);
    //@ assert item(response, ?resp, rpc_pub);
    
    {
        struct item *reqresp = create_pair(request, response);
        //@ item p = pair_item(req, resp);
        //@ assert item(reqresp, p, rpc_pub);
        //@ close rpc_pub(p);
        //@ leak rpc_pub(p);
        item_free(response);
        item_free(request);
        struct item *tag = create_data_item_from_int(1);
        struct item *payload = create_pair(tag, reqresp);
        //@ item d = data_item(chars_of_int(1));
        //@ close rpc_pub(d);
        //@ leak rpc_pub(d);
        //@ assert item(payload, pair_item(d, p), rpc_pub);
        //@ close rpc_pub(pair_item(d, p));
        //@ leak rpc_pub(pair_item(d, p));
        item_free(tag);
        item_free(reqresp);
        struct item *hash = create_hmac(key, payload);
        //@ item h = hmac_item(creator, id, some(pair_item(d, p)));
        //@ if (!col) assert item(hash, h, rpc_pub);
        //@ close rpc_pub(h);
        //@ leak rpc_pub(h);
        struct item *m = create_pair(hash, payload);
        //@ assert item(m, ?msg, rpc_pub);
        //@ if (!col) msg == pair_item(h, pair_item(d, p));
        //@ if (!col) assert item(m, msg, rpc_pub);
        //@ close rpc_pub(msg);
        //@ leak rpc_pub(msg);
        item_free(hash);
        item_free(payload);
        network_send(net_stat, m);
        item_free(m);
    }
    
    network_disconnect(net_stat);
}
static void server_process_request_event(void *obj, const belle_sip_request_event_t *event){
	endpoint_t* endpoint = (endpoint_t*)obj;
	belle_sip_server_transaction_t* server_transaction =belle_sip_provider_create_server_transaction(endpoint->provider,belle_sip_request_event_get_request(event));
	belle_sip_request_t* req = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(server_transaction));
	belle_sip_response_t* resp;
	belle_sip_header_contact_t* contact;
	belle_sip_header_expires_t* expires;
	belle_sip_header_authorization_t* authorization;
	belle_sip_header_via_t* via;
	const char* raw_authenticate_digest = "WWW-Authenticate: Digest "
			"algorithm=MD5, realm=\"" SIPDOMAIN "\", opaque=\"1bc7f9097684320\"";

	belle_sip_header_www_authenticate_t* www_authenticate=NULL;
	const char* auth_uri;
	const char* qop;
	unsigned char auth_ok=0;
	char local_resp[33];

	belle_sip_message("caller_process_request_event received [%s] message",belle_sip_request_get_method(belle_sip_request_event_get_request(event)));

	switch (endpoint->auth) {
	case none: {
		auth_ok=1;
		break;
	}
	case digest_auth:
	case digest: {
		if ((authorization=belle_sip_message_get_header_by_type(req,belle_sip_header_authorization_t)) != NULL){
			qop=belle_sip_header_authorization_get_qop(authorization);

			if (qop && strcmp(qop,"auth")==0) {
				compute_response_auth_qop(	belle_sip_header_authorization_get_username(authorization)
											,belle_sip_header_authorization_get_realm(authorization)
											,PASSWD
											,endpoint->nonce
											,endpoint->nonce_count
											,belle_sip_header_authorization_get_cnonce(authorization)
											,belle_sip_header_authorization_get_qop(authorization)
											,belle_sip_request_get_method(req)
											,auth_uri=belle_sip_uri_to_string(belle_sip_header_authorization_get_uri(authorization))
											,local_resp);
			} else {
				/*digest*/
				compute_response(belle_sip_header_authorization_get_username(authorization)
						,belle_sip_header_authorization_get_realm(authorization)
						,PASSWD
						,endpoint->nonce
						,belle_sip_request_get_method(req)
						,auth_uri=belle_sip_uri_to_string(belle_sip_header_authorization_get_uri(authorization))
						,local_resp);

			}
			belle_sip_free((void*)auth_uri);
			auth_ok=strcmp(belle_sip_header_authorization_get_response(authorization),local_resp)==0;
		}
		if (auth_ok && endpoint->nonce_count<MAX_NC_COUNT ) {/*revoke nonce after MAX_NC_COUNT uses*/
			if (endpoint->auth == digest ) {
				sprintf(endpoint->nonce,"%p",authorization); //*change the nonce for next auth*/
			} else {
				endpoint->nonce_count++;
			}
		} else {
			auth_ok=0;
			www_authenticate=belle_sip_header_www_authenticate_parse(raw_authenticate_digest);
			sprintf(endpoint->nonce,"%p",authorization); //*change the nonce for next auth*/
			belle_sip_header_www_authenticate_set_nonce(www_authenticate,endpoint->nonce);
			if (endpoint->auth == digest_auth) {
				belle_sip_header_www_authenticate_add_qop(www_authenticate,"auth");
				if (endpoint->nonce_count>=MAX_NC_COUNT) {
					belle_sip_header_www_authenticate_set_stale(www_authenticate,1);
					endpoint->nonce_count=1;
				}
			}
		}
	}
	break;
	default:
		break;
	}
	if (auth_ok) {
		resp=belle_sip_response_create_from_request(belle_sip_request_event_get_request(event),200);
		if (!endpoint->expire_in_contact) {
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(expires=belle_sip_message_get_header_by_type(req,belle_sip_header_expires_t)));
		}
		if (strcmp(belle_sip_request_get_method(req),"REGISTER")==0) {
			contact=belle_sip_message_get_header_by_type(req,belle_sip_header_contact_t);
		} else {
			contact=belle_sip_header_contact_new();
		}
		if(endpoint->unreconizable_contact) {
			/*put an unexpected address*/
			belle_sip_uri_set_host(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact)),"nimportequoi.com");
		}
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(contact));
		if (strcmp(belle_sip_request_get_method(req),"PUBLISH")==0) {

			belle_sip_header_t* sip_if_match=belle_sip_message_get_header(BELLE_SIP_MESSAGE(resp),"SIP-If-Match");
			if (sip_if_match) {
				CU_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(sip_if_match)),"blablietag");
			}
			/*check for body*/
			CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_body(BELLE_SIP_MESSAGE(req)));
			if (belle_sip_message_get_body(BELLE_SIP_MESSAGE(req))) {
				CU_ASSERT_STRING_EQUAL(belle_sip_message_get_body(BELLE_SIP_MESSAGE(req)),publish_body);
			}
			CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header_by_type(req,belle_sip_header_content_type_t));
			CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header_by_type(req,belle_sip_header_content_length_t));
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),belle_sip_header_create("SIP-ETag","blablietag"));
		}
	} else {
		resp=belle_sip_response_create_from_request(belle_sip_request_event_get_request(event),401);
		if (www_authenticate)
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(www_authenticate));
	}
	if (endpoint->received) {
		via=belle_sip_message_get_header_by_type(req,belle_sip_header_via_t);
		belle_sip_header_via_set_received(via,endpoint->received);
	}
	belle_sip_server_transaction_send_response(server_transaction,resp);
}