More cleanup
This commit is contained in:
parent
68f2c436e3
commit
fc6d9b9797
12 changed files with 153 additions and 135 deletions
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@
|
||||||
DEBUG = true
|
DEBUG = true
|
||||||
export DEBUG
|
export DEBUG
|
||||||
|
|
||||||
OBJS = crypto/rsa.o crypto/encoding/asn1.o crypto/encoding/base58.o crypto/encoding/base64.o \
|
OBJS = crypto/rsa.o crypto/encoding/base58.o crypto/encoding/base64.o \
|
||||||
crypto/encoding/x509.o thirdparty/mbedtls/*.o
|
crypto/encoding/x509.o thirdparty/mbedtls/*.o
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -O0 -I../../include -g3
|
CFLAGS = -O0 -I../../include -g3
|
||||||
LFLAGS =
|
LFLAGS =
|
||||||
DEPS =
|
DEPS = ../../include/libp2p/crypto/encoding/base58.h ../../include/libp2p/crypto/encoding/base64.h \
|
||||||
OBJS = asn1.o base58.o base64.o x509.o
|
../../include/libp2p/crypto/encoding/x509.h
|
||||||
|
OBJS = base58.o base64.o x509.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
|
@ -18,12 +18,12 @@
|
||||||
* @param bytes_written the number of bytes written to output_data
|
* @param bytes_written the number of bytes written to output_data
|
||||||
* @returns true(1) on success, otherwise false
|
* @returns true(1) on success, otherwise false
|
||||||
*/
|
*/
|
||||||
int base64_encode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written) {
|
int libp2p_crypto_encoding_base64_encode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written) {
|
||||||
int retVal = mbedtls_base64_encode(output_data, max_output_length, bytes_written, input_data, input_length);
|
int retVal = mbedtls_base64_encode(output_data, max_output_length, bytes_written, input_data, input_length);
|
||||||
return retVal == 0;
|
return retVal == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t base64_encode_length(const unsigned char* input_data, size_t input_length) {
|
size_t libp2p_crypto_encoding_base64_encode_length(const unsigned char* input_data, size_t input_length) {
|
||||||
size_t req_bytes;
|
size_t req_bytes;
|
||||||
mbedtls_base64_encode(NULL, 0, &req_bytes, input_data, input_length);
|
mbedtls_base64_encode(NULL, 0, &req_bytes, input_data, input_length);
|
||||||
return req_bytes;
|
return req_bytes;
|
||||||
|
@ -38,12 +38,12 @@ size_t base64_encode_length(const unsigned char* input_data, size_t input_length
|
||||||
* @param bytes_written the number of bytes written to output_data
|
* @param bytes_written the number of bytes written to output_data
|
||||||
* @returns a pointer to the decoded data
|
* @returns a pointer to the decoded data
|
||||||
*/
|
*/
|
||||||
int base64_decode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written) {
|
int libp2p_crypto_encoding_base64_decode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written) {
|
||||||
int retVal = mbedtls_base64_decode(output_data, max_output_length, bytes_written, input_data, input_length);
|
int retVal = mbedtls_base64_decode(output_data, max_output_length, bytes_written, input_data, input_length);
|
||||||
return retVal == 0;
|
return retVal == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t base64_decode_length(const unsigned char* input_data, size_t input_length) {
|
size_t libp2p_crypto_encoding_base64_decode_length(const unsigned char* input_data, size_t input_length) {
|
||||||
size_t req_bytes;
|
size_t req_bytes;
|
||||||
mbedtls_base64_decode(NULL, 0, &req_bytes, input_data, input_length);
|
mbedtls_base64_decode(NULL, 0, &req_bytes, input_data, input_length);
|
||||||
return req_bytes;
|
return req_bytes;
|
||||||
|
|
|
@ -65,10 +65,33 @@ int libp2p_crypto_encoding_x509_private_key_to_der(struct RsaPrivateKey* private
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
int libp2p_crypto_encoding_x509_der_to_private_key(unsigned char* der, size_t der_length, struct RsaPrivateKey* private_key) {
|
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_context ctx;
|
||||||
mbedtls_pk_init(&ctx);
|
mbedtls_pk_init(&ctx);
|
||||||
|
|
||||||
mbedtls_pk_parse_key(&ctx, der, der_length, NULL, 0);
|
int retVal = mbedtls_pk_parse_key(&ctx, der, der_length, NULL, 0);
|
||||||
return 1;
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
mbedtls_pk_free(&ctx);
|
||||||
|
|
||||||
|
return retVal >= 0;
|
||||||
}
|
}
|
||||||
|
|
80
crypto/rsa.c
80
crypto/rsa.c
|
@ -20,6 +20,7 @@
|
||||||
#include "mbedtls/x509.h"
|
#include "mbedtls/x509.h"
|
||||||
#include "mbedtls/rsa.h"
|
#include "mbedtls/rsa.h"
|
||||||
#include "mbedtls/asn1write.h"
|
#include "mbedtls/asn1write.h"
|
||||||
|
#include "mbedtls/oid.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Take an rsa context and turn it into a der formatted byte stream.
|
* Take an rsa context and turn it into a der formatted byte stream.
|
||||||
|
@ -30,7 +31,7 @@
|
||||||
* @param size the max size of the buffer. The actual size used is returned in this value
|
* @param size the max size of the buffer. The actual size used is returned in this value
|
||||||
* @returns true(1) on success, else 0
|
* @returns true(1) on success, else 0
|
||||||
*/
|
*/
|
||||||
int libp2p_crypto_rsa_write_key_der( mbedtls_rsa_context *rsa, unsigned char *buf, size_t* size )
|
int libp2p_crypto_rsa_write_private_key_der( mbedtls_rsa_context *rsa, unsigned char *buf, size_t* size )
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
unsigned char *c = buf + *size;
|
unsigned char *c = buf + *size;
|
||||||
|
@ -54,13 +55,62 @@ int libp2p_crypto_rsa_write_key_der( mbedtls_rsa_context *rsa, unsigned char *bu
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Take a context and turn it into a der formatted byte stream.
|
||||||
|
* @param key the key
|
||||||
|
* @param buf the buffer to be filled
|
||||||
|
* @param size the max size of the buffer. The actual size used is returned in this value
|
||||||
|
* @returns true(1) on success, else false(0)
|
||||||
|
*/
|
||||||
|
int libp2p_crypto_rsa_write_public_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t* size )
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
unsigned char *c;
|
||||||
|
size_t len = 0, par_len = 0, oid_len;
|
||||||
|
const char *oid;
|
||||||
|
|
||||||
|
c = buf + *size;
|
||||||
|
|
||||||
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
|
||||||
|
|
||||||
|
if( c - buf < 1 ) // buffer is too small
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SubjectPublicKeyInfo ::= SEQUENCE {
|
||||||
|
* algorithm AlgorithmIdentifier,
|
||||||
|
* subjectPublicKey BIT STRING }
|
||||||
|
*/
|
||||||
|
*--c = 0;
|
||||||
|
len += 1;
|
||||||
|
|
||||||
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
||||||
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
|
||||||
|
|
||||||
|
if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
|
||||||
|
&oid, &oid_len ) ) != 0 )
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
|
||||||
|
par_len ) );
|
||||||
|
|
||||||
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
||||||
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
|
||||||
|
MBEDTLS_ASN1_SEQUENCE ) );
|
||||||
|
|
||||||
|
*size = len;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Generate an RSA keypair of a certain size, and place the results in the struct
|
* Generate an RSA keypair of a certain size, and place the results in the struct
|
||||||
* @param private_key where to put the results
|
* @param private_key where to put the results
|
||||||
* @param num_bits_for_keypair the number of bits for the key, 1024 is the minimum
|
* @param num_bits_for_keypair the number of bits for the key, 1024 is the minimum
|
||||||
* @returns true(1) on success
|
* @returns true(1) on success
|
||||||
*/
|
*/
|
||||||
int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair) {
|
int libp2p_crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair) {
|
||||||
|
|
||||||
mbedtls_rsa_context rsa;
|
mbedtls_rsa_context rsa;
|
||||||
mbedtls_entropy_context entropy;
|
mbedtls_entropy_context entropy;
|
||||||
|
@ -110,17 +160,28 @@ int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long
|
||||||
|
|
||||||
size_t buffer_size = 1600;
|
size_t buffer_size = 1600;
|
||||||
buffer = malloc(sizeof(char) * buffer_size);
|
buffer = malloc(sizeof(char) * buffer_size);
|
||||||
retVal = libp2p_crypto_rsa_write_key_der(&rsa, buffer, &buffer_size);
|
retVal = libp2p_crypto_rsa_write_private_key_der(&rsa, buffer, &buffer_size);
|
||||||
if (retVal == 0)
|
if (retVal == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// allocate memory for the der
|
// allocate memory for the private key der
|
||||||
private_key->der = malloc(sizeof(char) * buffer_size);
|
|
||||||
private_key->der_length = buffer_size;
|
private_key->der_length = buffer_size;
|
||||||
|
private_key->der = malloc(sizeof(char) * buffer_size);
|
||||||
// add in the der to the buffer
|
// add in the der to the buffer
|
||||||
memcpy(private_key->der, &buffer[1600-buffer_size], buffer_size);
|
memcpy(private_key->der, &buffer[1600-buffer_size], buffer_size);
|
||||||
|
|
||||||
//TODO: Add the peer id
|
// now do the public key. First we need a pk_context
|
||||||
|
mbedtls_pk_context ctx;
|
||||||
|
ctx.pk_ctx = (void*)&rsa;
|
||||||
|
ctx.pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
|
||||||
|
buffer_size = 1600;
|
||||||
|
memset(buffer, 0, buffer_size);
|
||||||
|
retVal = libp2p_crypto_rsa_write_public_key_der(&ctx, buffer, &buffer_size);
|
||||||
|
|
||||||
|
// allocate memory for the public key der
|
||||||
|
private_key->public_key_length = buffer_size;
|
||||||
|
private_key->public_key_der = malloc(sizeof(char) * buffer_size);
|
||||||
|
memcpy(private_key->public_key_der, &buffer[1600-buffer_size], buffer_size);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
|
@ -135,10 +196,13 @@ exit:
|
||||||
/***
|
/***
|
||||||
* Free resources used by RsaPrivateKey
|
* Free resources used by RsaPrivateKey
|
||||||
* @param private_key the resources
|
* @param private_key the resources
|
||||||
* @returns 0
|
* @returns true(1)
|
||||||
*/
|
*/
|
||||||
int crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key) {
|
int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key) {
|
||||||
if (private_key->der != NULL)
|
if (private_key->der != NULL)
|
||||||
free(private_key->der);
|
free(private_key->der);
|
||||||
|
if (private_key->public_key_der != NULL)
|
||||||
|
free(private_key->public_key_der);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
* @param bytes_written the number of bytes written to output_data
|
* @param bytes_written the number of bytes written to output_data
|
||||||
* @returns true(1) on success, otherwise false
|
* @returns true(1) on success, otherwise false
|
||||||
*/
|
*/
|
||||||
int base64_encode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
int libp2p_crypto_encoding_base64_encode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
||||||
|
|
||||||
size_t base64_encode_length(const unsigned char* input_data, size_t input_length);
|
size_t base64_encode_length(const unsigned char* input_data, size_t input_length);
|
||||||
|
|
||||||
|
@ -33,8 +33,8 @@ size_t base64_encode_length(const unsigned char* input_data, size_t input_length
|
||||||
* @param bytes_written the number of bytes written to output_data
|
* @param bytes_written the number of bytes written to output_data
|
||||||
* @returns a pointer to the decoded data
|
* @returns a pointer to the decoded data
|
||||||
*/
|
*/
|
||||||
int base64_decode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
int libp2p_crypto_encoding_base64_decode(const unsigned char* input_data, size_t input_length, unsigned char* output_data, size_t max_output_length, size_t* bytes_written);
|
||||||
|
|
||||||
size_t base64_decode_length(const unsigned char* input_data, size_t input_length);
|
size_t libp2p_crypto_encoding_base64_decode_length(const unsigned char* input_data, size_t input_length);
|
||||||
|
|
||||||
#endif /* base64_h */
|
#endif /* base64_h */
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct RsaPrivateKey {
|
struct RsaPrivateKey {
|
||||||
|
// the basics of a key pair
|
||||||
unsigned long long QP;
|
unsigned long long QP;
|
||||||
unsigned long long DQ;
|
unsigned long long DQ;
|
||||||
unsigned long long DP;
|
unsigned long long DP;
|
||||||
|
@ -20,8 +21,13 @@ struct RsaPrivateKey {
|
||||||
unsigned long long D;
|
unsigned long long D;
|
||||||
unsigned long long E;
|
unsigned long long E;
|
||||||
unsigned long long N;
|
unsigned long long N;
|
||||||
|
// the keys in DER format
|
||||||
|
// private
|
||||||
char* der;
|
char* der;
|
||||||
size_t der_length;
|
size_t der_length;
|
||||||
|
// public
|
||||||
|
char* public_key_der;
|
||||||
|
size_t public_key_length;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,13 +36,13 @@ struct RsaPrivateKey {
|
||||||
* @param num_bits_for_keypair the size of the key (1024 minimum)
|
* @param num_bits_for_keypair the size of the key (1024 minimum)
|
||||||
* @returns true(1) on success
|
* @returns true(1) on success
|
||||||
*/
|
*/
|
||||||
int crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair);
|
int libp2p_crypto_rsa_generate_keypair(struct RsaPrivateKey* private_key, unsigned long num_bits_for_keypair);
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Free resources used by RsaPrivateKey
|
* Free resources used by RsaPrivateKey
|
||||||
* @param private_key the resources
|
* @param private_key the resources
|
||||||
* @returns 0
|
* @returns 0
|
||||||
*/
|
*/
|
||||||
int crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key);
|
int libp2p_crypto_rsa_rsa_private_key_free(struct RsaPrivateKey* private_key);
|
||||||
|
|
||||||
#endif /* rsa_h */
|
#endif /* rsa_h */
|
||||||
|
|
|
@ -6,13 +6,13 @@ CFLAGS += -g3
|
||||||
endif
|
endif
|
||||||
|
|
||||||
LFLAGS = -L../
|
LFLAGS = -L../
|
||||||
DEPS = crypto/test_base58.h crypto/test_mbedtls.h crypto/test_rsa.h
|
DEPS = crypto/test_base58.h crypto/test_rsa.h
|
||||||
OBJS = testit.o
|
OBJS = testit.o
|
||||||
|
|
||||||
%.o: %.c $(DEPS)
|
%.o: %.c $(DEPS)
|
||||||
$(CC) -c -o $@ $< $(CFLAGS)
|
$(CC) -c -o $@ $< $(CFLAGS)
|
||||||
|
|
||||||
testit_libp2p: $(OBJS)
|
testit_libp2p: $(OBJS) $(DEPS)
|
||||||
$(CC) -o $@ $(OBJS) $(LFLAGS) -lp2p -lm
|
$(CC) -o $@ $(OBJS) $(LFLAGS) -lp2p -lm
|
||||||
|
|
||||||
all: testit_libp2p
|
all: testit_libp2p
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
/**
|
|
||||||
* These are mainly functions to reverse engineer some of the
|
|
||||||
* mbedtls stuff to make sure we're storing and retrieving
|
|
||||||
* something that mbedtls can use
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mbedtls/asn1write.h"
|
|
||||||
#include "mbedtls/rsa.h"
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
#include "mbedtls/entropy.h"
|
|
||||||
|
|
||||||
// taken from mbedtls/programs/pkwrite.c
|
|
||||||
int mbedtls_pk_write_key_der( mbedtls_rsa_context *rsa, unsigned char *buf, size_t size )
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
unsigned char *c = buf + size;
|
|
||||||
size_t len = 0;
|
|
||||||
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->QP ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DQ ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DP ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->Q ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->P ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->D ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->E ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->N ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
|
|
||||||
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
|
||||||
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
|
|
||||||
MBEDTLS_ASN1_SEQUENCE ) );
|
|
||||||
|
|
||||||
return( (int) len );
|
|
||||||
}
|
|
||||||
|
|
||||||
int mbedtls_generate_key(mbedtls_rsa_context* ctx) {
|
|
||||||
mbedtls_entropy_context entropy;
|
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
|
||||||
|
|
||||||
int exponent = 65537;
|
|
||||||
int retVal = 1;
|
|
||||||
|
|
||||||
const char *pers = "rsa_genkey";
|
|
||||||
|
|
||||||
// initialize mbedtls structs
|
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
|
||||||
mbedtls_entropy_init( &entropy );
|
|
||||||
|
|
||||||
// seed the routines
|
|
||||||
if( ( retVal = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
|
||||||
(const unsigned char *) pers,
|
|
||||||
strlen( pers ) ) ) != 0 )
|
|
||||||
{
|
|
||||||
retVal = 0;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// finally, generate the key
|
|
||||||
if( ( retVal = mbedtls_rsa_gen_key( ctx, mbedtls_ctr_drbg_random, &ctr_drbg, (unsigned int)2046,
|
|
||||||
exponent ) ) != 0 )
|
|
||||||
{
|
|
||||||
retVal = 0;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
retVal = 1;
|
|
||||||
|
|
||||||
exit:
|
|
||||||
|
|
||||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
|
||||||
mbedtls_entropy_free( &entropy );
|
|
||||||
|
|
||||||
return retVal;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int test_mbedtls_pk_write_key_der() {
|
|
||||||
// generate private key
|
|
||||||
mbedtls_rsa_context key;
|
|
||||||
mbedtls_rsa_init( &key, MBEDTLS_RSA_PKCS_V15, 0 );
|
|
||||||
|
|
||||||
mbedtls_generate_key(&key);
|
|
||||||
// write it out in a section of memory in der format
|
|
||||||
size_t size = 1600;
|
|
||||||
unsigned char buf[size];
|
|
||||||
memset(buf, 0, size);
|
|
||||||
int retVal = mbedtls_pk_write_key_der(&key, buf, size);
|
|
||||||
// examine it
|
|
||||||
printf("Size: %d\n", retVal);
|
|
||||||
for(int i = retVal-1; i < size; i++)
|
|
||||||
printf("%02x", buf[i]);
|
|
||||||
printf("\n");
|
|
||||||
// use it
|
|
||||||
// free it
|
|
||||||
mbedtls_rsa_free( &key );
|
|
||||||
return 1;
|
|
||||||
}
|
|
|
@ -23,7 +23,7 @@
|
||||||
int test_crypto_rsa_private_key_der() {
|
int test_crypto_rsa_private_key_der() {
|
||||||
|
|
||||||
struct RsaPrivateKey private_key;
|
struct RsaPrivateKey private_key;
|
||||||
crypto_rsa_generate_keypair(&private_key, 2048);
|
libp2p_crypto_rsa_generate_keypair(&private_key, 2048);
|
||||||
|
|
||||||
if (private_key.der_length == 0)
|
if (private_key.der_length == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -32,18 +32,41 @@ int test_crypto_rsa_private_key_der() {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int test_crypto_x509_der_to_private() {
|
int test_crypto_x509_der_to_private2() {
|
||||||
char* der = "CAASpwkwggSjAgEAAoIBAQDTDJBWjDzS/HxDNOHazvzH2bu9CPMVHUrrvKRdBUM5ansL6/CC3MVZ6HVm4O6QHRapN6EF2CbrTgI4KBOXIL125Xo8MlROnyfXYk3O5q2tgwL/MbW8kXjtkyCfBak7MUoLOdLU7Svg0gkl3l+uDAiDcCLnwJVcFfq9ch6z4wMOhYJqE5dtx0uXxn6IuKWl1B69FTvBXCc0thw8Rw54b941FDcsBH5ttV9mRNCJym3poZ5qalNgXlxoIIB+PUx5QD+aq7KMJdpAX8HkapBntCOahP/QUceRmma0grlZLeYkH6/oi/hIrM6se3KUZ+F6tBuDFys8UAZy/X2BCUbKjbxtAgMBAAECggEANWfQfpYuLhXGPBt9q6kFPm1SnJtPJ+CpvM2XqhJS2IyhZnrl+bd0GTRBwS7aL42s1lVFYf04nAK5fQxnKK8YQqX/MIxr2RldM5ukpN6qxGWKtJkXrAgD2dqJPrRoBpqKahzPxSHfIJ0Fw5dqDtjsrpYJvyt0oEDPmnDuZAbmFx4sJqnesPNhKxtRMBx1+yxGVuRVJjHcqAgqPqwNiuoMEaYMY+G9yzT6vza8ovCpbX7BBIgM5fAT9PD8TBG//Vu9THvj/ZomiVG2qv6RL0qQyVb+DUzPZz1amBsSvahtXCl72jA3JwAZ943RxSR66P934S0ashkVwLUi46z/EAbJ4QKBgQDojGIO07BEVL2+7VxlGL9XGZQp4Y3qlhh2zDDQGwkCq/KQ+BdNYWitPwqRl9GqFLgpmeQIhyHTOa/IThx+AXGKVQ24ROH+skUs4IbO6R3qY7BKtb5lkZE/Yln09x70BBngUYAzh/rtnsXO3cl1x2XDDqUbCwlGcDAs8Jh/6UnvQwKBgQDoVSQs7Uq9MJCGIUM2bixX89tHzSxq5mn9wMD3/XRVfT5Ua8YkYBuzcmlcT39N7L5BwuyFqX3Vi7lv/Ya/qaQP6XkrZ8W1OAaTlYewfE5ZgknJqSpXcNWhABKeNmqndvqyQ/8HNCv/j8AdraGB2DGO57Xso5J0CQ43W/U9+QIyjwKBgHLL2hw3o+wXaRO3WMUPUmVM2zdRgR0suybp5a7Vqb0H5NZrohUw4NulIzJ8H6Q2VjMzJL6Q9sGu2HepF6ecTtBa7ErqtiVlG4Dr1aCOs5XhYEWBMlwxX+JKSt4Cn+UVoTB7Cy5lEhn7JurX0Xuy0ylXMWoIKKv89cs5eg6quzTBAoGAaq9eEztLjKCWXOE9SetBdYnG8aunb9cqaJlwgu/h0bfXPVDYBbAUSEyLURY4MQI7Q1tM3Pu9iqfEmUZj7/LoIV5mg6X9RX/alT6etk3+dF+9nlqN1OU9U9cCtZ/rTcb2y5EptJcidRH/eCFY/pTV/PcttOJPx/S4kHcroC+N8MUCgYEA6DA5QHxHfNN6Nxv+pEzy2DIxFe9RrBxS+KPBsra1C8jgdeMf4EmfU0Nox92V0q0bRrD5ztqQwSONI0hSRb1iiMWR6MuFnAFajUJfASjjIlZ6nIQjQslI7vjlvYyyHS/p/Codxap+yJlTLWwVEOXp2D9pWwiMq1xEyf0TH1BosvM=";
|
// this is an example private key. The type is not added. Therefore, it is not compatible with the go version
|
||||||
size_t b64_length = base64_decode_length((unsigned char*)der, strlen(der));
|
char* der = "MIIEogIBAAKCAQEAmpDhRGecWN+MwfxW9UqsyEsZwnROHb2hCRBUATh7e0thgyi5Te6lT9x2++89x1sC7B9Egma1AJvY7BinIJYeFiOwzkKDe6/JxCle6TmLtCO1qmb8aGOIlu8NzO3L8EjcriYcgp0J5sZn5I3B4iU6q78u5jJQFpi2V7wsasanmfFwfF9ZUSxAnuwkcRkuhGnp5sBauHsQ4dn3IaiutiZDxPsAdxQyXX0SdrB4ew72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4WulyvLKDMhO97KVcJzt1RMwjqQy3fnURIOyJvGOML6+/CDisLzqlV9WwIGrHQeGGwwSqoSqJnxcDy1AoGAMVmdK7mTMBtayUXRpthNCUmRAubaUKAjhKYm0j7ciPKWmo+mBHITbUshti07HqYeqxZE3LAIw/j0nowPsf4HIOxc69E+ZQqvETQwGyCwyHKJNYSFJi3tkHzCGEoP0/k9l6Fu8Qqcs1xhNQu8AcjQ1QL17rs/r9N44GKS2iXI+PY=";
|
||||||
|
size_t b64_length = libp2p_crypto_encoding_base64_decode_length((unsigned char*)der, strlen(der));
|
||||||
unsigned char buffer[b64_length];
|
unsigned char buffer[b64_length];
|
||||||
unsigned char* b = buffer;
|
unsigned char* b = buffer;
|
||||||
size_t ultimate_length;
|
size_t ultimate_length;
|
||||||
int retVal = base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length);
|
int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length);
|
||||||
if (retVal == 0)
|
if (retVal == 0)
|
||||||
return 0;
|
return 0;
|
||||||
struct RsaPrivateKey private_key;
|
// we now have the bytes
|
||||||
libp2p_crypto_encoding_x509_der_to_private_key(b, ultimate_length, &private_key);
|
struct RsaPrivateKey private_key = {0};
|
||||||
return 1;
|
retVal = libp2p_crypto_encoding_x509_der_to_private_key(b, ultimate_length, &private_key);
|
||||||
|
if (retVal == 0)
|
||||||
|
return 0;
|
||||||
|
return private_key.D > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_crypto_x509_der_to_private() {
|
||||||
|
// this is a base64 encoded string from the go version of ipfs
|
||||||
|
char* der = "CAASpwkwggSjAgEAAoIBAQDTDJBWjDzS/HxDNOHazvzH2bu9CPMVHUrrvKRdBUM5ansL6/CC3MVZ6HVm4O6QHRapN6EF2CbrTgI4KBOXIL125Xo8MlROnyfXYk3O5q2tgwL/MbW8kXjtkyCfBak7MUoLOdLU7Svg0gkl3l+uDAiDcCLnwJVcFfq9ch6z4w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q9eEztLjKCWXOE9SetBdYnG8aunb9cqaJlwgu/h0bfXPVDYBbAUSEyLURY4MQI7Q1tM3Pu9iqfEmUZj7/LoIV5mg6X9RX/alT6etk3+dF+9nlqN1OU9U9cCtZ/rTcb2y5EptJcidRH/eCFY/pTV/PcttOJPx/S4kHcroC+N8MUCgYEA6DA5QHxHfNN6Nxv+pEzy2DIxFe9RrBxS+KPBsra1C8jgdeMf4EmfU0Nox92V0q0bRrD5ztqQwSONI0hSRb1iiMWR6MuFnAFajUJfASjjIlZ6nIQjQslI7vjlvYyyHS/p/Codxap+yJlTLWwVEOXp2D9pWwiMq1xEyf0TH1BosvM=";
|
||||||
|
size_t b64_length = libp2p_crypto_encoding_base64_decode_length((unsigned char*)der, strlen(der));
|
||||||
|
unsigned char buffer[b64_length];
|
||||||
|
unsigned char* b = buffer;
|
||||||
|
size_t ultimate_length;
|
||||||
|
int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length);
|
||||||
|
if (retVal == 0)
|
||||||
|
return 0;
|
||||||
|
// we now have the bytes, but we must strip off the type (5 bytes)
|
||||||
|
struct RsaPrivateKey private_key = {0};
|
||||||
|
int bytesToStrip = 5;
|
||||||
|
retVal = libp2p_crypto_encoding_x509_der_to_private_key(&b[bytesToStrip], ultimate_length-bytesToStrip, &private_key);
|
||||||
|
if (retVal == 0)
|
||||||
|
return 0;
|
||||||
|
return private_key.D > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include "crypto/test_rsa.h"
|
#include "crypto/test_rsa.h"
|
||||||
//#include "multihash/test_multihash.h"
|
//#include "multihash/test_multihash.h"
|
||||||
#include "crypto/test_base58.h"
|
#include "crypto/test_base58.h"
|
||||||
#include "crypto/test_mbedtls.h"
|
//#include "crypto/test_mbedtls.h"
|
||||||
|
|
||||||
int testit(const char* name, int (*func)(void)) {
|
int testit(const char* name, int (*func)(void)) {
|
||||||
printf("Testing %s...\n", name);
|
printf("Testing %s...\n", name);
|
||||||
|
@ -19,7 +19,8 @@ int testit(const char* name, int (*func)(void)) {
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
//testit("test_crypto_rsa_public_key_bytes", test_crypto_rsa_public_key_bytes);
|
//testit("test_crypto_rsa_public_key_bytes", test_crypto_rsa_public_key_bytes);
|
||||||
//testit("test_crypto_x509_private_to_der", test_crypto_x509_private_to_der);
|
//testit("test_crypto_x509_private_to_der", test_crypto_x509_private_to_der);
|
||||||
//testit("test_crypto_x509_der_to_private", test_crypto_x509_der_to_private);
|
testit("test_crypto_x509_der_to_private2", test_crypto_x509_der_to_private2);
|
||||||
|
testit("test_crypto_x509_der_to_private", test_crypto_x509_der_to_private);
|
||||||
//testit("test_multihash_encode", test_multihash_encode);
|
//testit("test_multihash_encode", test_multihash_encode);
|
||||||
//testit("test_multihash_decode", test_multihash_decode);
|
//testit("test_multihash_decode", test_multihash_decode);
|
||||||
//testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_decode);
|
//testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_decode);
|
||||||
|
@ -29,7 +30,7 @@ int main(int argc, char** argv) {
|
||||||
testit("test_base58_size", test_base58_size);
|
testit("test_base58_size", test_base58_size);
|
||||||
testit("test_base58_max_size", test_base58_max_size);
|
testit("test_base58_max_size", test_base58_max_size);
|
||||||
testit("test_base58_peer_address", test_base58_peer_address);
|
testit("test_base58_peer_address", test_base58_peer_address);
|
||||||
testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der);
|
//testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue