Ejemplo n.º 1
0
/*
 * Verify the signature on an assertion.
 */
int
kn_verify_assertion(char *buf, int len)
{
    struct assertion *as;
    int res;

    keynote_errno = 0;
    as = keynote_parse_assertion(buf, len, ASSERT_FLAG_SIGVER);
    if (as == NULL)
      return -1;

    res = keynote_sigverify_assertion(as);
    keynote_free_assertion(as);
    return res;
}
Ejemplo n.º 2
0
/*
 * Produce the signature for an assertion.
 */
char *
kn_sign_assertion(char *buf, int buflen, char *key, char *sigalg, int vflag)
{
    int i, alg, hashtype, encoding, internalenc;
    struct keynote_deckey dc;
    struct assertion *as;
    char *s, *sig;

    keynote_errno = 0;
    s = NULL;

    if (sigalg == NULL || buf == NULL || key == NULL)
    {
	keynote_errno = ERROR_NOTFOUND;
	return NULL;
    }

    if (sigalg[0] == '\0' || sigalg[strlen(sigalg) - 1] != ':')
    {
	keynote_errno = ERROR_SYNTAX;
	return NULL;
    }

    /* We're using a different format for X509 private keys, so... */
    alg = keynote_get_sig_algorithm(sigalg, &hashtype, &encoding,
				    &internalenc);
    if (alg != KEYNOTE_ALGORITHM_X509)
    {
	/* Parse the private key */
	s = keynote_get_private_key(key);
	if (s == NULL)
	  return NULL;

	/* Decode private key */
	i = kn_decode_key(&dc, s, KEYNOTE_PRIVATE_KEY);
	if (i == -1)
	{
	    free(s);
	    return NULL;
	}
    }
    else /* X509 private key */
    {
	dc.dec_key = key;
	dc.dec_algorithm = alg;
    }

    as = keynote_parse_assertion(buf, buflen, ASSERT_FLAG_SIGGEN);
    if (as == NULL)
    {
	if (alg != KEYNOTE_ALGORITHM_X509)
	{
	    keynote_free_key(dc.dec_key, dc.dec_algorithm);
	    free(s);
	}
	return NULL;
    }

    sig = keynote_sign_assertion(as, sigalg, dc.dec_key, dc.dec_algorithm,
				 vflag);
    if (alg != KEYNOTE_ALGORITHM_X509)
      keynote_free_key(dc.dec_key, dc.dec_algorithm);
    keynote_free_assertion(as);
    if (s != NULL)
      free(s);
    return sig;
}
Ejemplo n.º 3
0
/*
 * process_request_msg -- process the request that should nominally be
 *	received from the client over the socket.
 *	The request is a pseudo-credential, which is close enough
 *	to the real thing that keynote can parse it and verify it.
 *	The request has as the authoriser the key of the client
 *	(and is signed by that key).
 *	There is no licensees per se, we use the word 'REQUEST' as
 *	a placeholder and so that the server can confirm that this
 *	is a request, not a credential.
 *
 *	The conditions are *assignments*. This is were it stops 
 *	looking like a real credential. We read these in as state
 *	variables for the keynote session (actions).
 *
 *	Processing takes these steps:
 *	a) verify the request (i.e. that it signed by the authorizer)
 *	b) extract the authorizer key and feed it into keynote as
 *	   the action authorizer (the key that makes the request)
 *	c) extract the state variables from the request and feed
 *	   them into keynote.
 *
 *	If all goes OK, we return 0, otherwise a negative number.
 */
int
process_request_msg(int sessionid, char *rbuf, int rlen)
{
	int n;
	int rval;
	char **assertlist;
	struct assertion *assertp;
	char *vps;
	char *vpe;
	char c;

	if ((assertlist = kn_read_asserts(rbuf, rlen, &n)) == NULL) {
		fprintf(stderr, "Out of memory while allocating memory for "
			"request.\n");
		return(-1);
	}

	if (n != 1) {
		fprintf(stderr, "Request must haev only one assetion\n");
		return(-1);
	}

	if (kn_verify_assertion(assertlist[0],  strlen(assertlist[0])) < 0) {
            switch (keynote_errno)
            {
                case ERROR_MEMORY:
                    fprintf(stderr,
                            "Out of memory while parsing request.\n");
                    break;

                case ERROR_SYNTAX:
                    fprintf(stderr,
                            "Syntax error while parsing request.\n");
                    break;

                default:
                    fprintf(stderr,
                            "Unknown error while parsing request.\n");
            }
	    return(-1);
	}

	/*
	 * verified, so now we use an internal keynote routine to
	 * parse the request pseudo-credential and extract the
	 * authoriser key and the variables.
	 */

	if ((assertp = keynote_parse_assertion(rbuf, rlen, 0)) == NULL) {
		fprintf(stderr, "keynote_parse_assertion failed\n");
		return(-1);
	}

	/*
	 * we now extract the authoriser key from the request and add it to the
	 * keynote database. This is to ensure that the key which signed the
	 * request is the authorizer key for this session.
	 */
	if ((rval =  add_authorizer(sessionid, assertp->as_authorizer_string_s, 
	    assertp->as_authorizer_string_e)) < 0) {
		fprintf(stderr, "add_authorizer failed\n");
		return(-1);
	}
	
	/*
	 * we now extract the variables from the request and insert them into
	 * the Keynote database
	 *
	 * Why do we trust the variables given to us by the client?
	 * we have added variables to the root policy (e.g. app_domain,
	 * nonce, date, etc) from our offer, in order to ensure that
	 * the request refers to *our* offer.
	 */
	vps = assertp->as_conditions_s;
	vpe = assertp->as_conditions_e;
	c = *vpe;
	*vpe = '\0';

	if ((rval =  add_variables(sessionid, vps)) < 0) {
			fprintf(stderr, "add_variable, returned %d\n", rval);
			return(-1);
	}
	*vpe = c;
	return(0);
}