c-libp2p/crypto/encoding/x509.c

100 lines
2.4 KiB
C
Raw Permalink Normal View History

2016-11-07 20:11:58 +00:00
#include <stdio.h>
2016-12-01 18:05:14 +00:00
#include <stdlib.h>
2016-11-07 20:11:58 +00:00
#include <string.h>
#include "mbedtls/rsa.h"
#include "mbedtls/pk.h"
#include "mbedtls/ctr_drbg.h"
#include "libp2p/crypto/encoding/x509.h"
/**
* public methods
*/
/**
* convert a RSA Private Key to an array of bytes in DER format
* @param private_key the private key
* @param bytes where to put the resultant bytes
* @returns true(1) on success
*/
int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey* private_key, unsigned char* bytes[1600]) {
// get everything setup
mbedtls_pk_context ctx;
mbedtls_pk_init( &ctx);
mbedtls_pk_setup( &ctx, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
mbedtls_rsa_context* rsa = mbedtls_pk_rsa( ctx );
// set the values in the context
mbedtls_mpi N;
mbedtls_mpi_init(&N);
2016-11-09 16:22:25 +00:00
mbedtls_mpi_lset(&N, private_key->N);
2016-11-07 20:11:58 +00:00
mbedtls_mpi E;
mbedtls_mpi_init(&E);
2016-11-09 16:22:25 +00:00
mbedtls_mpi_lset(&E, private_key->E);
mbedtls_mpi Q;
mbedtls_mpi_init(&Q);
mbedtls_mpi_lset(&Q, private_key->Q);
2016-11-07 20:11:58 +00:00
rsa->N = N;
rsa->E = E;
2016-11-09 16:22:25 +00:00
rsa->Q = Q;
2016-11-07 20:11:58 +00:00
rsa->len = ( mbedtls_mpi_bitlen(&rsa->N ) + 7) >> 3;
// now write to DER
mbedtls_pk_write_key_der(&ctx, *bytes, 1600);
mbedtls_mpi_free(&N);
mbedtls_mpi_free(&E);
2016-11-09 16:22:25 +00:00
mbedtls_mpi_free(&Q);
2016-11-07 20:11:58 +00:00
mbedtls_rsa_free(rsa);
mbedtls_pk_free(&ctx);
return 1;
}
2016-11-10 17:04:48 +00:00
/***
* Parse a DER bytestring into a RsaPrivateKey struct
* @param der the incoming bytestring
* @param der_length the length of the bytestring
* @param private_key the RsaPrivateKey to fill
* @returns true(1) on success
*/
2016-11-07 20:11:58 +00:00
int libp2p_crypto_encoding_x509_der_to_private_key(unsigned char* der, size_t der_length, struct RsaPrivateKey* private_key) {
mbedtls_pk_context ctx;
mbedtls_pk_init(&ctx);
2016-11-10 17:04:48 +00:00
int retVal = mbedtls_pk_parse_key(&ctx, der, der_length, NULL, 0);
if (retVal >= 0) {
// parse the results into the structure
mbedtls_rsa_context* rsa = mbedtls_pk_rsa(ctx);
private_key->D = *(rsa->D.p);
private_key->DP = *(rsa->DP.p);
private_key->DQ = *(rsa->DQ.p);
private_key->E = *(rsa->E.p);
private_key->N = *(rsa->N.p);
private_key->P = *(rsa->P.p);
private_key->Q = *(rsa->Q.p);
private_key->QP = *(rsa->QP.p);
2017-02-22 16:48:14 +00:00
// now put the public DER format in.
private_key->der = malloc(sizeof(char) * der_length);
if (private_key->der == NULL)
return 0;
memcpy(private_key->der, der, der_length);
private_key->der_length = der_length;
2017-02-22 16:48:14 +00:00
//NOTE: the public DER stuff is done in rsa.c
}
2016-11-10 17:04:48 +00:00
mbedtls_pk_free(&ctx);
return retVal >= 0;
2016-11-07 20:11:58 +00:00
}