예제 #1
0
파일: key_utils.c 프로젝트: cawka/PyNDN
int
ndn_keypair(int public_only, struct ndn_pkey *private_key,
            PyObject **py_private_key_ndn, PyObject **py_public_key_ndn)
{
	struct ndn_pkey *public_key = NULL;
	struct ndn_pkey *private_key_copy = NULL;
	PyObject *py_private_key = NULL, *py_public_key = NULL;
	unsigned int err;
	int r;

	if (!public_only && py_private_key_ndn) {
                private_key_copy = (struct ndn_pkey *) EVP_PKEY_new();
                JUMP_IF_NULL(private_key_copy, openssl_error);

                py_private_key = NDNObject_New(PKEY_PRIV, private_key_copy);
		JUMP_IF_NULL(py_private_key, error);
                
                RSA *private_key_rsa = EVP_PKEY_get1_RSA ((EVP_PKEY *)private_key);
                JUMP_IF_NULL(private_key_rsa, openssl_error);
                
                RSA* private_key_rsa_copy = RSAPrivateKey_dup (private_key_rsa);
                JUMP_IF_NULL(private_key_rsa_copy, openssl_error);
                
		r = EVP_PKEY_set1_RSA((EVP_PKEY *) private_key_copy, private_key_rsa_copy);
		RSA_free(private_key_rsa_copy);
		JUMP_IF_NULL(r, error);
	}

	if (py_public_key_ndn) {
		public_key = (struct ndn_pkey *) EVP_PKEY_new();
		JUMP_IF_NULL(public_key, openssl_error);

		py_public_key = NDNObject_New(PKEY_PUB, public_key);
		JUMP_IF_NULL(py_public_key, error);

                RSA *private_key_rsa = EVP_PKEY_get1_RSA ((EVP_PKEY *)private_key);
                JUMP_IF_NULL(private_key_rsa, openssl_error);
                
                RSA* public_key_rsa = RSAPublicKey_dup (private_key_rsa);
                JUMP_IF_NULL(public_key_rsa, openssl_error);

		r = EVP_PKEY_set1_RSA((EVP_PKEY *) public_key, public_key_rsa);
		RSA_free(public_key_rsa);
		JUMP_IF_NULL(r, error);
	}

	if (py_private_key_ndn) {
		*py_private_key_ndn = public_only ? (Py_INCREF(Py_None), Py_None) :
				py_private_key;
	}

	if (py_public_key_ndn)
		*py_public_key_ndn = py_public_key;

	return 0;

openssl_error:
	err = ERR_get_error();
	PyErr_Format(g_PyExc_NDNKeyError, "Unable to generate keypair from the key:"
			" %s", ERR_reason_error_string(err));
error:
	if (!py_public_key && public_key)
		ndn_pubkey_free(public_key);
	Py_XDECREF(py_public_key);
	Py_XDECREF(py_private_key);
	return -1;
}
예제 #2
0
PyObject *
KeyLocator_obj_from_ndn(PyObject *py_keylocator)
{
    struct ndn_buf_decoder decoder, *d;
    struct ndn_charbuf *keylocator;
    struct ndn_charbuf *name;
    struct ndn_pkey *pubkey;
    size_t start, stop;
    int r;
    PyObject *py_o;
    PyObject *py_KeyLocator_obj = NULL;

    keylocator = NDNObject_Get(KEY_LOCATOR, py_keylocator);

    debug("KeyLocator_from_ndn start\n");

    d = ndn_buf_decoder_start(&decoder, keylocator->buf, keylocator->length);
    assert(d); //should always succeed

    if (!ndn_buf_match_dtag(d, NDN_DTAG_KeyLocator)) {
        PyErr_SetString(g_PyExc_NDNKeyLocatorError, "The input isn't a valid"
                        " KeyLocator");
        return NULL;
    }
    ndn_buf_advance(d);

    if (ndn_buf_match_dtag(d, NDN_DTAG_KeyName)) {
        const unsigned char *bname;
        size_t bname_size;
        PyObject *py_name_obj;

        ndn_buf_advance(d);

        start = d->decoder.token_index;
        r = ndn_parse_Name(d, NULL);
        stop = d->decoder.token_index;
        if (r < 0)
            return PyErr_Format(g_PyExc_NDNKeyLocatorError, "Error finding"
                                " NDN_DTAG_Name for KeyName (decoder state: %d)",
                                d->decoder.state);

        assert(stop > start);
        bname_size = stop - start;
        bname = d->buf + start;
        /*
        		r = ndn_ref_tagged_BLOB(NDN_DTAG_Name, d->buf, start, stop, &bname,
        				&bname_size);
        		if (r < 0)
        			return PyErr_Format(g_PyExc_NDNKeyLocatorError, "Error getting"
        				" NDN_DTAG_Name BLOB for KeyName (decoder state: %d)",
        				d->decoder.state);
         */

        debug("Parse NDN_DTAG_Name inside KeyName, len=%zd\n", bname_size);

        py_o = NDNObject_New_charbuf(NAME, &name);
        if (!py_o)
            return NULL;

        r = ndn_charbuf_append(name, bname, bname_size);
        if (r < 0) {
            Py_DECREF(py_o);
            return PyErr_NoMemory();
        }

        py_name_obj = Name_obj_from_ndn(py_o);
        Py_DECREF(py_o);
        if (!py_name_obj)
            return NULL;

        py_KeyLocator_obj = PyObject_CallObject(g_type_KeyLocator, NULL);
        if (!py_KeyLocator_obj) {
            Py_DECREF(py_name_obj);
            goto error;
        }

        r = PyObject_SetAttrString(py_KeyLocator_obj, "keyName", py_name_obj);
        Py_DECREF(py_name_obj);
        JUMP_IF_NEG(r, error);

#pragma message "Parse and add digest to the keylocator"
    } else if (ndn_buf_match_dtag(d, NDN_DTAG_Key)) {
        const unsigned char *dkey;
        size_t dkey_size;
        PyObject *py_key_obj, *py_ndn_key;

        start = d->decoder.token_index;
        r = ndn_parse_required_tagged_BLOB(d, NDN_DTAG_Key, 1, -1);
        stop = d->decoder.token_index;
        if (r < 0)
            return PyErr_Format(g_PyExc_NDNKeyLocatorError, "Error finding"
                                " NDN_DTAG_Key for Key (decoder state: %d)", d->decoder.state);

        r = ndn_ref_tagged_BLOB(NDN_DTAG_Key, d->buf, start, stop, &dkey,
                                &dkey_size);
        if (r < 0)
            return PyErr_Format(g_PyExc_NDNKeyLocatorError, "Error getting"
                                " NDN_DTAG_Key BLOB for Key (decoder state: %d)",
                                d->decoder.state);

        debug("Parse NDN_DTAG_Key, len=%zd\n", dkey_size);

        pubkey = ndn_d2i_pubkey(dkey, dkey_size); // free with ndn_pubkey_free()
        if (!pubkey) {
            PyErr_SetString(g_PyExc_NDNKeyLocatorError, "Unable to parse key to"
                            " internal representation");
            return NULL;
        }
        py_ndn_key = NDNObject_New(PKEY_PUB, pubkey);
        if (!py_ndn_key) {
            ndn_pubkey_free(pubkey);
            return NULL;
        }

        py_key_obj = Key_obj_from_ndn(py_ndn_key);
        Py_DECREF(py_ndn_key);
        if (!py_key_obj)
            return NULL;

        py_KeyLocator_obj = PyObject_CallObject(g_type_KeyLocator, NULL);
        if (!py_KeyLocator_obj) {
            Py_DECREF(py_key_obj);
            goto error;
        }

        r = PyObject_SetAttrString(py_KeyLocator_obj, "key", py_key_obj);
        Py_DECREF(py_key_obj);
        JUMP_IF_NEG(r, error);
    } else if (ndn_buf_match_dtag(d, NDN_DTAG_Certificate)) {
        PyErr_SetString(PyExc_NotImplementedError, "Found certificate DTAG,"
                        " which currently is unsupported");
        return NULL;
    } else {
        PyErr_SetString(g_PyExc_NDNKeyLocatorError, "Unknown KeyLocator Type");
        return NULL;
    }

    ndn_buf_check_close(d); // we don't really check the parser, though-

    return py_KeyLocator_obj;

error:
    Py_XDECREF(py_KeyLocator_obj);
    return NULL;
}