Esempio n. 1
0
Context create_context(){
  Principal p1 = principal_pcpl(A);
  Formula f1 = formula_pred(OK, p1);

  Principal p2 = principal_pcpl(B);
  Formula f2 = formula_pred(ALRIGHT, p2);

  Principal p3 = principal_pcpl(C);
  Formula f3 = formula_pred(OK, p3);

  Principal p4 = principal_pcpl(D);
  Formula f4 = formula_pred(ALRIGHT, p4);

  Principal p5 = principal_pcpl(E);
  Formula f5 = formula_pred(OK, p5);

  Principal p6 = principal_pcpl(F);
  Formula f6 = formula_pred(ALRIGHT, p6);

  Principal p7 = principal_pcpl(G);
  Formula f7 = formula_pred(OK, p7);

  Context c = context_alloc(4);
  push(c, f1);
  push(c, f2);
  push(c, f3);
  push(c, f4);
  push(c, f5);
  push(c, f6);
  push(c, f7);
  push(c, f1);

  return c;
}
Esempio n. 2
0
int
main (int argc, char **argv)
{

  if (argc > 1 && !strcmp (argv[1], "--verbose"))
    verbose = 1;
  else if (argc > 1 && !strcmp (argv[1], "--debug"))
    verbose = debug = 1;

  if (!gcry_check_version (GCRYPT_VERSION))
    die ("version mismatch\n");

  xgcry_control (GCRYCTL_DISABLE_SECMEM, 0);
  xgcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  if (debug)
    xgcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
  xgcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);

  set_get_point ();
  context_alloc ();
  context_param ();
  basic_ec_math ();

  /* The tests are for P-192 and ed25519 which are not supported in
     FIPS mode.  */
  if (!gcry_fips_mode_active())
    {
      basic_ec_math_simplified ();
      twistededwards_math ();
    }

  info ("All tests completed. Errors: %d\n", error_count);
  return error_count ? 1 : 0;
}
Esempio n. 3
0
int
main (int argc, char **argv)
{

  if (argc > 1 && !strcmp (argv[1], "--verbose"))
    verbose = 1;
  else if (argc > 1 && !strcmp (argv[1], "--debug"))
    verbose = debug = 1;

  if (!gcry_check_version (GCRYPT_VERSION))
    die ("version mismatch\n");

  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  if (debug)
    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);

  set_get_point ();
  context_alloc ();
  context_param ();
  basic_ec_math ();
  basic_ec_math_simplified ();

  show ("All tests completed. Errors: %d\n", error_count);
  return error_count ? 1 : 0;
}
Esempio n. 4
0
struct resolv_context *
__resolv_context_get_override (struct __res_state *resp)
{
  /* NB: As explained asbove, context_alloc will put the context on
     the current list.  */
  struct resolv_context *ctx = context_alloc (resp);
  if (ctx == NULL)
    return NULL;

  ctx->__from_res = false;
  return ctx;
}
Esempio n. 5
0
void test_proof_check_assump_bad() {
  Context c = context_alloc(1);
  Principal prinA = principal_pcpl(A);
  Predicate pred = OK;
  Formula fpred = formula_pred(pred, prinA);
  Proof pf = proof_assump(fpred);
  bool b;
  cprintf("Expected error:\n");
  b = proof_check_lib(fpred, pf, c);
  cprintf(":End expected\n\n");
  if (b != 0)
    cprintf("ERROR: assump proof check (bad context): 0 == %u\n", b);
}
Esempio n. 6
0
// Deep copy of the context. Also copies all the formulas below the stack pointer.
Context context_cp(Context c) {
    if (c == NULL)
        return NULL;

    Context cret = context_alloc(c->size);
    int i = 0;

    while (i < c->topOfContext) {
        cret->contextData[i] = formula_cp(c->contextData[i]);
        i++;
    }
    cret->topOfContext = c->topOfContext;
    return cret;
}
Esempio n. 7
0
/*
 * implementation for context_init
 */
void context_init() {
  PVOID pTopFiber = (top_context == NULL || top_context != current_context)? NULL: top_context->pFiber;
  context_deinit();

  /*
   * there is strange but fibers always exist
   * PVOID pCurrentFiber = GetCurrentFiber();
   * if (pCurrentFiber != NULL) DeleteFiber(pCurrentFiber);
   */

  current_context = top_context = context_alloc();
  if (top_context != NULL) top_context->pFiber = (pTopFiber == NULL)? ConvertThreadToFiber(NULL): pTopFiber;
  return;
}
Esempio n. 8
0
void test_proof_check_assump() {
  Context c = context_alloc(1);
  Principal prinA = principal_pcpl(A);
  Predicate pred = OK;
  Formula fpred = formula_pred(pred, prinA);
  Proof pf = proof_assump(fpred);
  bool b;
  push(c, fpred);

//  proof_print(pf);
//  cprintf("\\\\\\\\ \n");
  b = proof_check_lib(fpred, pf, c);
  if (b != 1)
    cprintf("ERROR: assump proof check: 1 == %u\n", b);
}
Esempio n. 9
0
/* Backing function for the __resolv_context_get family of
   functions.  */
static struct resolv_context *
context_get (bool preinit)
{
  if (current != NULL)
    return context_reuse ();

  struct resolv_context *ctx = context_alloc (&_res);
  if (ctx == NULL)
    return NULL;
  if (!maybe_init (ctx, preinit))
    {
      context_free (ctx);
      return NULL;
    }
  return ctx;
}
Esempio n. 10
0
void test_proof_check_tauto() {
  Principal prinA = principal_pcpl(A);
  Principal prinB = principal_pcpl(B);
  Predicate pred = OK;
  Formula fpred = formula_pred(pred, prinA);
  Formula f1 = formula_says(prinB, fpred);
  Proof p1 = proof_assump(fpred);
  Proof p2 = proof_tauto(f1, p1);
  Context c = context_alloc(1);
  push(c, fpred);

//  proof_print(p2);
//  cprintf("\\\\\\\\ \n");

  bool b = proof_check_lib(f1, p2, c);
  if (b != 1)
    cprintf("ERROR: tauto proof check: 1 == %u\n", b);
}
Esempio n. 11
0
// Push a new formula onto the Context stack.  Copies the formula.
void push(Context *c, Formula f) {
    // If the push will overflow the given context double the size
    if ((*c)->topOfContext == (*c)->size) {
        uint32_t newSize = (*c)->size * 2;
        Context cNew = context_alloc(newSize);
        if (!cNew) {
            printf("ERROR: Context is full and we are out of memory. "
                   "Formula was not added\n");
            return;
        }
        memcpy(cNew->contextData, (*c)->contextData, (*c)->size*sizeof(Formula));
        cNew->size = newSize;
        cNew->topOfContext = (*c)->topOfContext;
        *c = cNew;
    }
    Formula fCopy = formula_cp(f);
    (*c)->contextData[(*c)->topOfContext] = fCopy;
    (*c)->topOfContext++;
}
Esempio n. 12
0
void test_proof_check_delegation() {
  Proof dpf = delegate_from_signed(A, B, OK);
  Proof apf = approval_from_signed(B, OK, C);
  Proof delegation = use_delegation(A, B, C, OK, dpf, apf);
  Principal prinA = principal_pcpl(A);
  Principal prinB = principal_pcpl(B);
  Principal prinC = principal_pcpl(C);
  Formula OKC = formula_pred(OK, prinC);
  Formula AsaysOKC = formula_says(prinA, OKC);
  bool b;
  Context c = context_alloc(10);
  Formula BsaysOKC = formula_says(prinB, OKC);
  Formula impl = formula_impl(BsaysOKC, OKC);
  push(c, impl);
  b = proof_check_lib(AsaysOKC, delegation, c);

  if (b != 1)
    cprintf("ERROR: delegation proof check: 1 == %u\n", b);
}
Esempio n. 13
0
void test_proof_check_impl() {
  Principal prinA = principal_pcpl(A);
  Principal prinB = principal_pcpl(B);
  Predicate predOK = OK;
  Predicate predALRIGHT = ALRIGHT;
  Formula fpred1 = formula_pred(predOK, prinA);
  Formula fpred2 = formula_pred(predALRIGHT, prinB);
  Formula f1 = formula_impl(fpred1, fpred2);
  Proof p1 = proof_assump(fpred1);
  Proof p2 = proof_assump(f1);
  Proof p3 = proof_impl(fpred2, p1, p2);
  Context c = context_alloc(10);
  push(c, f1);
  push(c, fpred1);

//  proof_print(p3);
//  cprintf("\\\\\\\\ \n");

  bool b = proof_check_lib(fpred2, p3, c);
  if (b != 1)
    cprintf("ERROR: impl proof check: 1 == %u\n", b);
}
Esempio n. 14
0
void test_proof_check_says_says() {
  Principal prinA = principal_pcpl(A);
  Predicate predOK = OK;
  Formula f1 = formula_pred(predOK, prinA);
  Formula f2 = formula_cp(f1);

  Formula Asays1 = formula_says(prinA, f1);
  Formula Asays2 = formula_says(prinA, f2);
  Proof p1 = proof_assump(Asays1);
  Proof p_f1 = proof_assump(f1);
  Proof p2 = proof_tauto(Asays2, p_f1);
  Proof p3 = proof_says_says(Asays2, p1, p2);
  Context c = context_alloc(10);
  push(c,Asays1);

//  proof_print(p3);
//  cprintf("\\\\\\\\ \n");

  bool b = proof_check_lib(Asays2, p3, c);
  if (b != 1)
    cprintf("ERROR: says_says proof check: 1 == %u\n", b);
}
Esempio n. 15
0
void test_proof_check_tauto_bad() {
  Principal prinA = principal_pcpl(A);
  Principal prinB = principal_pcpl(B);
  Predicate pred = OK;
  Formula fpred = formula_pred(pred, prinA);
  Formula f1 = formula_says(prinB, fpred);
  Proof p1 = proof_assump(fpred);
  Proof p2 = proof_tauto(f1, p1);
  Context c = context_alloc(1);
  push(c, fpred);

//  proof_print(p2);
//  cprintf("\\\\\\\\ \n");

  Formula fpred2 = formula_pred(pred, prinB);
  f1 = formula_says(prinB, fpred2);
  p1 = proof_assump(fpred);
  p2 = proof_tauto(f1, p1);
  cprintf("Expected error:\n");
  bool b = proof_check_lib(fpred, p2, c);
  cprintf(":End expected\n\n");
  if (b != 0)
    cprintf("ERROR: tauto proof check (bad): 0 == %u\n", b);
}
Esempio n. 16
0
static inline int hep_handle_req(struct tcp_req *req,
							struct tcp_connection *con, int _max_msg_chunks)
{
	struct receive_info local_rcv;
	char *msg_buf;
	int msg_len;
	long size;

	int ret=0;

	struct hep_context *hep_ctx;
	context_p ctx=NULL;

	if (req->complete){
		/* update the timeout - we successfully read the request */
		tcp_conn_set_lifetime( con, tcp_con_lifetime);
		con->timeout=con->lifetime;

		/* just for debugging use sendipv4 as receiving socket  FIXME*/
		con->rcv.proto_reserved1=con->id; /* copy the id */

		/* prepare for next request */
		size=req->pos-req->parsed;

		msg_buf = req->start;
		msg_len = req->parsed-req->start;
		local_rcv = con->rcv;

		if (!size) {
			/* did not read any more things -  we can release
			 * the connection */
			LM_DBG("Nothing more to read on TCP conn %p, currently in state %d \n",
				con,con->state);
			if (req != &hep_current_req) {
				/* we have the buffer in the connection tied buff -
				 *	detach it , release the conn and free it afterwards */
				con->con_req = NULL;
			}
			/* TODO - we could indicate to the TCP net layer to release
			 * the connection -> other worker may read the next available
			 * message on the pipe */
		} else {
			LM_DBG("We still have things on the pipe - "
				"keeping connection \n");
		}

		if( msg_buf[0] == 'H' && msg_buf[1] == 'E' && msg_buf[2] == 'P' ) {
			if ((hep_ctx = shm_malloc(sizeof(struct hep_context))) == NULL) {
				LM_ERR("no more shared memory!\n");
				return -1;
			}
			memset(hep_ctx, 0, sizeof(struct hep_context));
			memcpy(&hep_ctx->ri, &local_rcv, sizeof(struct receive_info));

			/* HEP related */
			if (unpack_hepv3(msg_buf, msg_len, &hep_ctx->h)) {
				LM_ERR("failed to unpack hepV3\n");
				goto error_free_hep;
			}
			update_recv_info(&local_rcv, &hep_ctx->h);

			/* set context for receive_msg */
			if ((ctx=context_alloc(CONTEXT_GLOBAL)) == NULL) {
				LM_ERR("failed to allocate new context! skipping...\n");
				goto error_free_hep;
			}

			memset(ctx, 0, context_size(CONTEXT_GLOBAL));

			context_put_ptr(CONTEXT_GLOBAL, ctx, hep_ctx_idx, hep_ctx);
			/* run hep callbacks; set the current processing context
			 * to hep context; this way callbacks will have all the data
			 * needed */
			current_processing_ctx = ctx;
			ret=run_hep_cbs();
			if (ret < 0) {
				LM_ERR("failed to run hep callbacks\n");
				goto error_free_hep;
			}
			current_processing_ctx = NULL;

			msg_len = hep_ctx->h.u.hepv3.payload_chunk.chunk.length-
											sizeof(hep_chunk_t);
			/* remove the hep header; leave only the payload */
			msg_buf = hep_ctx->h.u.hepv3.payload_chunk.data;
		}

		/* skip receive msg if we were told so from at least one callback */
		if (ret != HEP_SCRIPT_SKIP &&
				receive_msg(msg_buf, msg_len, &local_rcv, ctx) <0)
				LM_ERR("receive_msg failed \n");

		if (!size && req != &hep_current_req) {
			/* if we no longer need this tcp_req
			 * we can free it now */
			pkg_free(req);
		}

		if (size) {
			memmove(req->buf, req->parsed, size);
			req->pos = req->buf + size;
			/* anything that was parsed was already processed */
			req->parsed = req->buf;
		}

		/* if we still have some unparsed bytes, try to  parse them too*/
		if (size) return 1;
	} else {
		/* request not complete - check the if the thresholds are exceeded */
		if (con->msg_attempts==0)
			/* if first iteration, set a short timeout for reading
			 * a whole SIP message */
			con->timeout = get_ticks() + tcp_max_msg_time;

		con->msg_attempts ++;
		if (con->msg_attempts == _max_msg_chunks) {
			LM_ERR("Made %u read attempts but message is not complete yet - "
				   "closing connection \n",con->msg_attempts);
			goto error;
		}

		if (req == &hep_current_req) {
			/* let's duplicate this - most likely another conn will come in */

			LM_DBG("We didn't manage to read a full request\n");
			con->con_req = pkg_malloc(sizeof(struct tcp_req));
			if (con->con_req == NULL) {
				LM_ERR("No more mem for dynamic con request buffer\n");
				goto error;
			}

			if (req->pos != req->buf) {
				/* we have read some bytes */
				memcpy(con->con_req->buf,req->buf,req->pos-req->buf);
				con->con_req->pos = con->con_req->buf + (req->pos-req->buf);
			} else {
				con->con_req->pos = con->con_req->buf;
			}

			if (req->parsed != req->buf)
				con->con_req->parsed =con->con_req->buf+(req->parsed-req->buf);
			else
				con->con_req->parsed = con->con_req->buf;


			con->con_req->complete=req->complete;
			con->con_req->content_len=req->content_len;
			con->con_req->error = req->error;
			/* req will be reset on the next usage */
		}
	}

	/* everything ok */
	return 0;
error_free_hep:
	shm_free(hep_ctx);
error:
	/* report error */
	return -1;

}
Esempio n. 17
0
inline static void final_response_handler( struct timer_link *fr_tl )
{
#define CANCEL_REASON_SIP_480  \
	"Reason: SIP;cause=480;text=\"NO_ANSWER\"" CRLF

	static context_p my_ctx = NULL;
	context_p old_ctx;
	struct retr_buf* r_buf;
	struct cell *t;

	if (fr_tl==0){
		/* or BUG?, ignoring it for now */
		LM_CRIT("final_response_handler(0) called\n");
		return;
	}
	r_buf = get_fr_timer_payload(fr_tl);
	t=r_buf->my_T;

#	ifdef EXTRA_DEBUG
	if (t->damocles)
	{
		LM_ERR("transaction %p scheduled for deletion and"
			" called from FR timer\n",r_buf->my_T);
		abort();
	}
#	endif

	reset_timer(  &(r_buf->retr_timer) );

	/* the transaction is already removed from FR_LIST by the timer */

	/* FR for local cancels.... */
	if (r_buf->activ_type==TYPE_LOCAL_CANCEL)
	{
		LM_DBG("stop retr for Local Cancel\n");
		return;
	}

	/* FR for replies (negative INVITE replies) */
	if (r_buf->activ_type>0) {
#		ifdef EXTRA_DEBUG
		if (t->uas.request->REQ_METHOD!=METHOD_INVITE
			|| t->uas.status < 200 ) {
			LM_ERR("unknown type reply buffer\n");
			abort();
		}
#		endif
		put_on_wait( t );
		return;
	};

	/* as this processing is outside the scope of other messages (it is
	   trigger from timer), a processing context must be attached to it */
	old_ctx = current_processing_ctx;
	if (my_ctx==NULL) {
		my_ctx = context_alloc(CONTEXT_GLOBAL);
		if (my_ctx==NULL) {
			LM_ERR("failed to alloc new ctx in pkg\n");
		}
	}
	memset( my_ctx, 0, context_size(CONTEXT_GLOBAL) );
	current_processing_ctx = my_ctx;
	/* set the T context too */
	set_t( t );

	/* out-of-lock do the cancel I/O */
	if (is_invite(t) && should_cancel_branch(t, r_buf->branch) ) {
		set_cancel_extra_hdrs( CANCEL_REASON_SIP_480, sizeof(CANCEL_REASON_SIP_480)-1);
		cancel_branch(t, r_buf->branch );
		set_cancel_extra_hdrs( NULL, 0);
	}
	/* lock reply processing to determine how to proceed reliably */
	LOCK_REPLIES( t );
	LM_DBG("Cancel sent out, sending 408 (%p)\n", t);
	fake_reply(t, r_buf->branch, 408 );

	/* flush the context */
	if (current_processing_ctx==NULL)
		my_ctx=NULL;
	else
		context_destroy(CONTEXT_GLOBAL, my_ctx);
	/* switch back to the old context */
	current_processing_ctx = old_ctx;
	/* reset the T context */
	init_t();

	LM_DBG("done\n");
}
Esempio n. 18
0
static int hep_udp_read_req(struct socket_info *si, int* bytes_read)
{
	struct receive_info ri;
	int len;
#ifdef DYN_BUF
	char* buf;
#else
	static char buf [BUF_SIZE+1];
#endif
	char *tmp;
	unsigned int fromlen;
	str msg;

	struct hep_context *hep_ctx;

	int ret = 0;

	context_p ctx=NULL;

#ifdef DYN_BUF
	buf=pkg_malloc(BUF_SIZE+1);
	if (buf==0){
		LM_ERR("could not allocate receive buffer\n");
		goto error;
	}
#endif

	fromlen=sockaddru_len(si->su);
	len=recvfrom(bind_address->socket, buf, BUF_SIZE,0,&ri.src_su.s,&fromlen);
	if (len==-1){
		if (errno==EAGAIN)
			return 0;
		if ((errno==EINTR)||(errno==EWOULDBLOCK)|| (errno==ECONNREFUSED))
			return -1;
		LM_ERR("recvfrom:[%d] %s\n", errno, strerror(errno));
		return -2;
	}


	if (len<MIN_UDP_PACKET) {
		LM_DBG("probing packet received len = %d\n", len);
		return 0;
	}

	/* we must 0-term the messages, receive_msg expects it */
	buf[len]=0; /* no need to save the previous char */

	ri.bind_address = si;
	ri.dst_port = si->port_no;
	ri.dst_ip = si->address;
	ri.proto = si->proto;
	ri.proto_reserved1 = ri.proto_reserved2 = 0;

	su2ip_addr(&ri.src_ip, &ri.src_su);
	ri.src_port=su_getport(&ri.src_su);

	/* if udp we are sure that version 1 or 2 of the
	 * protocol is used */
	if ((hep_ctx = shm_malloc(sizeof(struct hep_context))) == NULL) {
		LM_ERR("no more shared memory!\n");
		return -1;
	}

	memset(hep_ctx, 0, sizeof(struct hep_context));
	memcpy(&hep_ctx->ri, &ri, sizeof(struct receive_info));


	if (len < 4) {
		LM_ERR("invalid message! too short!\n");
		return -1;
	}

	if (!memcmp(buf, HEP_HEADER_ID, HEP_HEADER_ID_LEN)) {
		/* HEPv3 */
		if (unpack_hepv3(buf, len, &hep_ctx->h)) {
			LM_ERR("hepv3 unpacking failed\n");
			return -1;
		}
	} else {
		/* HEPv2 */
		if (unpack_hepv12(buf, len, &hep_ctx->h)) {
			LM_ERR("hepv12 unpacking failed\n");
			return -1;
		}
	}

	/* set context for receive_msg */
	if ((ctx=context_alloc(CONTEXT_GLOBAL)) == NULL) {
		LM_ERR("failed to allocate new context! skipping...\n");
		goto error_free_hep;
	}

	memset(ctx, 0, context_size(CONTEXT_GLOBAL));

	context_put_ptr(CONTEXT_GLOBAL, ctx, hep_ctx_idx, hep_ctx);

	update_recv_info(&ri, &hep_ctx->h);

	/* run hep callbacks; set the current processing context
	 * to hep context; this way callbacks will have all the data
	 * needed */
	current_processing_ctx = ctx;
	ret=run_hep_cbs();
	if (ret < 0) {
		LM_ERR("failed to run hep callbacks\n");
		return -1;
	}
	current_processing_ctx = NULL;

	if (hep_ctx->h.version == 3) {
		/* HEPv3 */
		msg.len =
			hep_ctx->h.u.hepv3.payload_chunk.chunk.length- sizeof(hep_chunk_t);
		msg.s = hep_ctx->h.u.hepv3.payload_chunk.data;
	} else {
		/* HEPv12 */
		msg.len = len - hep_ctx->h.u.hepv12.hdr.hp_l;
		msg.s = buf + hep_ctx->h.u.hepv12.hdr.hp_l;

		if (hep_ctx->h.u.hepv12.hdr.hp_v == 2) {
			msg.s += sizeof(struct hep_timehdr);
			msg.len -= sizeof(struct hep_timehdr);
		}
	}

	if (ri.src_port==0){
		tmp=ip_addr2a(&ri.src_ip);
		LM_INFO("dropping 0 port packet for %s\n", tmp);
		return 0;
	}

	if (ret != HEP_SCRIPT_SKIP) {
		/* receive_msg must free buf too!*/
		receive_msg( msg.s, msg.len, &ri, ctx);
	}

	return 0;

error_free_hep:
	shm_free(hep_ctx);
	return -1;

}