Blame lib/privkey_raw.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2010-2014 Free Software Foundation, Inc.
Packit 549fdc
 * 
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include <gnutls/pkcs11.h>
Packit 549fdc
#include <stdio.h>
Packit 549fdc
#include <string.h>
Packit 549fdc
#include "errors.h"
Packit 549fdc
#include <datum.h>
Packit 549fdc
#include <pkcs11_int.h>
Packit 549fdc
#include <gnutls/abstract.h>
Packit 549fdc
#include <pk.h>
Packit 549fdc
#include <x509_int.h>
Packit 549fdc
#include <tls-sig.h>
Packit 549fdc
#include <algorithms.h>
Packit 549fdc
#include <fips.h>
Packit 549fdc
#include <abstract_int.h>
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_export_rsa_raw:
Packit 549fdc
 * @key: Holds the certificate
Packit 549fdc
 * @m: will hold the modulus
Packit 549fdc
 * @e: will hold the public exponent
Packit 549fdc
 * @d: will hold the private exponent
Packit 549fdc
 * @p: will hold the first prime (p)
Packit 549fdc
 * @q: will hold the second prime (q)
Packit 549fdc
 * @u: will hold the coefficient
Packit 549fdc
 * @e1: will hold e1 = d mod (p-1)
Packit 549fdc
 * @e2: will hold e2 = d mod (q-1)
Packit 549fdc
 *
Packit 549fdc
 * This function will export the RSA private key's parameters found
Packit 549fdc
 * in the given structure. The new parameters will be allocated using
Packit 549fdc
 * gnutls_malloc() and will be stored in the appropriate datum. For
Packit 549fdc
 * EdDSA keys, the @y value should be %NULL.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.3.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_export_rsa_raw(gnutls_privkey_t key,
Packit 549fdc
				    gnutls_datum_t * m, gnutls_datum_t * e,
Packit 549fdc
				    gnutls_datum_t * d, gnutls_datum_t * p,
Packit 549fdc
				    gnutls_datum_t * q, gnutls_datum_t * u,
Packit 549fdc
				    gnutls_datum_t * e1,
Packit 549fdc
				    gnutls_datum_t * e2)
Packit 549fdc
{
Packit 549fdc
	return gnutls_privkey_export_rsa_raw2(key, m, e, d, p, q, u, e1, e2, 0);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_export_rsa_raw2:
Packit 549fdc
 * @key: Holds the certificate
Packit 549fdc
 * @m: will hold the modulus
Packit 549fdc
 * @e: will hold the public exponent
Packit 549fdc
 * @d: will hold the private exponent
Packit 549fdc
 * @p: will hold the first prime (p)
Packit 549fdc
 * @q: will hold the second prime (q)
Packit 549fdc
 * @u: will hold the coefficient
Packit 549fdc
 * @e1: will hold e1 = d mod (p-1)
Packit 549fdc
 * @e2: will hold e2 = d mod (q-1)
Packit 549fdc
 * @flags: flags from %gnutls_abstract_export_flags_t
Packit 549fdc
 *
Packit 549fdc
 * This function will export the RSA private key's parameters found
Packit 549fdc
 * in the given structure. The new parameters will be allocated using
Packit 549fdc
 * gnutls_malloc() and will be stored in the appropriate datum.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.6.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_export_rsa_raw2(gnutls_privkey_t key,
Packit 549fdc
				    gnutls_datum_t * m, gnutls_datum_t * e,
Packit 549fdc
				    gnutls_datum_t * d, gnutls_datum_t * p,
Packit 549fdc
				    gnutls_datum_t * q, gnutls_datum_t * u,
Packit 549fdc
				    gnutls_datum_t * e1,
Packit 549fdc
				    gnutls_datum_t * e2,
Packit 549fdc
				    unsigned int flags)
Packit 549fdc
{
Packit 549fdc
gnutls_pk_params_st params;
Packit 549fdc
int ret;
Packit 549fdc
Packit 549fdc
	if (key == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	gnutls_pk_params_init(&params);
Packit 549fdc
Packit 549fdc
	ret = _gnutls_privkey_get_mpis(key, &params);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	ret = _gnutls_params_get_rsa_raw(&params, m, e, d, p, q, u, e1, e2, flags);
Packit 549fdc
Packit 549fdc
	gnutls_pk_params_release(&params);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_export_dsa_raw:
Packit 549fdc
 * @key: Holds the public key
Packit 549fdc
 * @p: will hold the p
Packit 549fdc
 * @q: will hold the q
Packit 549fdc
 * @g: will hold the g
Packit 549fdc
 * @y: will hold the y
Packit 549fdc
 * @x: will hold the x
Packit 549fdc
 *
Packit 549fdc
 * This function will export the DSA private key's parameters found
Packit 549fdc
 * in the given structure. The new parameters will be allocated using
Packit 549fdc
 * gnutls_malloc() and will be stored in the appropriate datum.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.3.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_export_dsa_raw(gnutls_privkey_t key,
Packit 549fdc
			     gnutls_datum_t * p, gnutls_datum_t * q,
Packit 549fdc
			     gnutls_datum_t * g, gnutls_datum_t * y,
Packit 549fdc
			     gnutls_datum_t * x)
Packit 549fdc
{
Packit 549fdc
	return gnutls_privkey_export_dsa_raw2(key, p, q, g, y, x, 0);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_export_dsa_raw2:
Packit 549fdc
 * @key: Holds the public key
Packit 549fdc
 * @p: will hold the p
Packit 549fdc
 * @q: will hold the q
Packit 549fdc
 * @g: will hold the g
Packit 549fdc
 * @y: will hold the y
Packit 549fdc
 * @x: will hold the x
Packit 549fdc
 * @flags: flags from %gnutls_abstract_export_flags_t
Packit 549fdc
 *
Packit 549fdc
 * This function will export the DSA private key's parameters found
Packit 549fdc
 * in the given structure. The new parameters will be allocated using
Packit 549fdc
 * gnutls_malloc() and will be stored in the appropriate datum.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.6.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_export_dsa_raw2(gnutls_privkey_t key,
Packit 549fdc
			     gnutls_datum_t * p, gnutls_datum_t * q,
Packit 549fdc
			     gnutls_datum_t * g, gnutls_datum_t * y,
Packit 549fdc
			     gnutls_datum_t * x, unsigned int flags)
Packit 549fdc
{
Packit 549fdc
gnutls_pk_params_st params;
Packit 549fdc
int ret;
Packit 549fdc
Packit 549fdc
	if (key == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	gnutls_pk_params_init(&params);
Packit 549fdc
Packit 549fdc
	ret = _gnutls_privkey_get_mpis(key, &params);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	ret = _gnutls_params_get_dsa_raw(&params, p, q, g, y, x, flags);
Packit 549fdc
Packit 549fdc
	gnutls_pk_params_release(&params);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_export_ecc_raw:
Packit 549fdc
 * @key: Holds the public key
Packit 549fdc
 * @curve: will hold the curve
Packit 549fdc
 * @x: will hold the x coordinate
Packit 549fdc
 * @y: will hold the y coordinate
Packit 549fdc
 * @k: will hold the private key
Packit 549fdc
 *
Packit 549fdc
 * This function will export the ECC private key's parameters found
Packit 549fdc
 * in the given structure. The new parameters will be allocated using
Packit 549fdc
 * gnutls_malloc() and will be stored in the appropriate datum.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.3.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_export_ecc_raw(gnutls_privkey_t key,
Packit 549fdc
				       gnutls_ecc_curve_t * curve,
Packit 549fdc
				       gnutls_datum_t * x,
Packit 549fdc
				       gnutls_datum_t * y,
Packit 549fdc
				       gnutls_datum_t * k)
Packit 549fdc
{
Packit 549fdc
	return gnutls_privkey_export_ecc_raw2(key, curve, x, y, k, 0);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_export_ecc_raw2:
Packit 549fdc
 * @key: Holds the public key
Packit 549fdc
 * @curve: will hold the curve
Packit 549fdc
 * @x: will hold the x coordinate
Packit 549fdc
 * @y: will hold the y coordinate
Packit 549fdc
 * @k: will hold the private key
Packit 549fdc
 * @flags: flags from %gnutls_abstract_export_flags_t
Packit 549fdc
 *
Packit 549fdc
 * This function will export the ECC private key's parameters found
Packit 549fdc
 * in the given structure. The new parameters will be allocated using
Packit 549fdc
 * gnutls_malloc() and will be stored in the appropriate datum.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.6.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_export_ecc_raw2(gnutls_privkey_t key,
Packit 549fdc
				       gnutls_ecc_curve_t * curve,
Packit 549fdc
				       gnutls_datum_t * x,
Packit 549fdc
				       gnutls_datum_t * y,
Packit 549fdc
				       gnutls_datum_t * k,
Packit 549fdc
				       unsigned int flags)
Packit 549fdc
{
Packit 549fdc
gnutls_pk_params_st params;
Packit 549fdc
int ret;
Packit 549fdc
Packit 549fdc
	if (key == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	gnutls_pk_params_init(&params);
Packit 549fdc
Packit 549fdc
	ret = _gnutls_privkey_get_mpis(key, &params);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	ret = _gnutls_params_get_ecc_raw(&params, curve, x, y, k, flags);
Packit 549fdc
Packit 549fdc
	gnutls_pk_params_release(&params);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_import_rsa_raw:
Packit 549fdc
 * @key: The structure to store the parsed key
Packit 549fdc
 * @m: holds the modulus
Packit 549fdc
 * @e: holds the public exponent
Packit 549fdc
 * @d: holds the private exponent
Packit 549fdc
 * @p: holds the first prime (p)
Packit 549fdc
 * @q: holds the second prime (q)
Packit 549fdc
 * @u: holds the coefficient (optional)
Packit 549fdc
 * @e1: holds e1 = d mod (p-1) (optional)
Packit 549fdc
 * @e2: holds e2 = d mod (q-1) (optional)
Packit 549fdc
 *
Packit 549fdc
 * This function will convert the given RSA raw parameters to the
Packit 549fdc
 * native #gnutls_privkey_t format.  The output will be stored in
Packit 549fdc
 * @key.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_import_rsa_raw(gnutls_privkey_t key,
Packit 549fdc
				    const gnutls_datum_t * m,
Packit 549fdc
				    const gnutls_datum_t * e,
Packit 549fdc
				    const gnutls_datum_t * d,
Packit 549fdc
				    const gnutls_datum_t * p,
Packit 549fdc
				    const gnutls_datum_t * q,
Packit 549fdc
				    const gnutls_datum_t * u,
Packit 549fdc
				    const gnutls_datum_t * e1,
Packit 549fdc
				    const gnutls_datum_t * e2)
Packit 549fdc
{
Packit 549fdc
int ret;
Packit 549fdc
gnutls_x509_privkey_t xkey;
Packit 549fdc
Packit 549fdc
	ret = gnutls_x509_privkey_init(&xkey);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	ret = gnutls_x509_privkey_import_rsa_raw2(xkey, m, e, d, p, q, u, e1, e1);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto error;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto error;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	return 0;
Packit 549fdc
Packit 549fdc
error:
Packit 549fdc
	gnutls_x509_privkey_deinit(xkey);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_import_dsa_raw:
Packit 549fdc
 * @key: The structure to store the parsed key
Packit 549fdc
 * @p: holds the p
Packit 549fdc
 * @q: holds the q
Packit 549fdc
 * @g: holds the g
Packit 549fdc
 * @y: holds the y
Packit 549fdc
 * @x: holds the x
Packit 549fdc
 *
Packit 549fdc
 * This function will convert the given DSA raw parameters to the
Packit 549fdc
 * native #gnutls_privkey_t format.  The output will be stored
Packit 549fdc
 * in @key.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_import_dsa_raw(gnutls_privkey_t key,
Packit 549fdc
				   const gnutls_datum_t * p,
Packit 549fdc
				   const gnutls_datum_t * q,
Packit 549fdc
				   const gnutls_datum_t * g,
Packit 549fdc
				   const gnutls_datum_t * y,
Packit 549fdc
				   const gnutls_datum_t * x)
Packit 549fdc
{
Packit 549fdc
int ret;
Packit 549fdc
gnutls_x509_privkey_t xkey;
Packit 549fdc
Packit 549fdc
	ret = gnutls_x509_privkey_init(&xkey);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	ret = gnutls_x509_privkey_import_dsa_raw(xkey, p, q, g, y, x);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto error;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto error;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	return 0;
Packit 549fdc
Packit 549fdc
error:
Packit 549fdc
	gnutls_x509_privkey_deinit(xkey);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_privkey_import_ecc_raw:
Packit 549fdc
 * @key: The key
Packit 549fdc
 * @curve: holds the curve
Packit 549fdc
 * @x: holds the x
Packit 549fdc
 * @y: holds the y
Packit 549fdc
 * @k: holds the k
Packit 549fdc
 *
Packit 549fdc
 * This function will convert the given elliptic curve parameters to the
Packit 549fdc
 * native #gnutls_privkey_t format.  The output will be stored
Packit 549fdc
 * in @key.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_privkey_import_ecc_raw(gnutls_privkey_t key,
Packit 549fdc
				   gnutls_ecc_curve_t curve,
Packit 549fdc
				   const gnutls_datum_t * x,
Packit 549fdc
				   const gnutls_datum_t * y,
Packit 549fdc
				   const gnutls_datum_t * k)
Packit 549fdc
{
Packit 549fdc
int ret;
Packit 549fdc
gnutls_x509_privkey_t xkey;
Packit 549fdc
Packit 549fdc
	ret = gnutls_x509_privkey_init(&xkey);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	ret = gnutls_x509_privkey_import_ecc_raw(xkey, curve, x, y, k);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto error;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	ret = gnutls_privkey_import_x509(key, xkey, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto error;
Packit 549fdc
	}
Packit 549fdc
	
Packit 549fdc
	return 0;
Packit 549fdc
Packit 549fdc
error:
Packit 549fdc
	gnutls_x509_privkey_deinit(xkey);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc