mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-06 05:21:22 +03:00
some directory restructuring
git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@141 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
This commit is contained in:
parent
70ed44946e
commit
4a82037346
@ -99,6 +99,7 @@ ifndef CONFIG_PLATFORM_CYGWIN
|
||||
# Cygwin
|
||||
else
|
||||
CFLAGS += -DCONFIG_PLATFORM_CYGWIN
|
||||
LDFLAGS += -enable-auto-import
|
||||
endif
|
||||
endif
|
||||
|
||||
|
@ -35,10 +35,13 @@ AXTLS_HOME=..
|
||||
|
||||
OBJ=\
|
||||
aes.o \
|
||||
bigint.o \
|
||||
crypto_misc.o \
|
||||
hmac.o \
|
||||
md2.o \
|
||||
md5.o \
|
||||
rc4.o \
|
||||
rsa.o \
|
||||
sha1.o
|
||||
|
||||
include ../config/makefile.post
|
||||
|
@ -32,8 +32,6 @@
|
||||
#define BIGINT_HEADER
|
||||
|
||||
#include "crypto.h"
|
||||
#include "os_port.h"
|
||||
#include "bigint_impl.h"
|
||||
|
||||
BI_CTX *bi_initialize(void);
|
||||
void bi_terminate(BI_CTX *ctx);
|
@ -41,6 +41,8 @@ extern "C" {
|
||||
|
||||
#include "config.h"
|
||||
#include "os_port.h"
|
||||
#include "bigint_impl.h"
|
||||
#include "bigint.h"
|
||||
|
||||
/* enable features based on a 'super-set' capbaility. */
|
||||
#if defined(CONFIG_SSL_FULL_MODE)
|
||||
@ -157,7 +159,61 @@ void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
|
||||
void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest);
|
||||
|
||||
/**************************************************************************
|
||||
* RSA declarations
|
||||
**************************************************************************/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bigint *m; /* modulus */
|
||||
bigint *e; /* public exponent */
|
||||
bigint *d; /* private exponent */
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
bigint *p; /* p as in m = pq */
|
||||
bigint *q; /* q as in m = pq */
|
||||
bigint *dP; /* d mod (p-1) */
|
||||
bigint *dQ; /* d mod (q-1) */
|
||||
bigint *qInv; /* q^-1 mod p */
|
||||
#endif
|
||||
int num_octets;
|
||||
BI_CTX *bi_ctx;
|
||||
} RSA_CTX;
|
||||
|
||||
void RSA_priv_key_new(RSA_CTX **rsa_ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
const uint8_t *pub_exp, int pub_len,
|
||||
const uint8_t *priv_exp, int priv_len
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
, const uint8_t *p, int p_len,
|
||||
const uint8_t *q, int q_len,
|
||||
const uint8_t *dP, int dP_len,
|
||||
const uint8_t *dQ, int dQ_len,
|
||||
const uint8_t *qInv, int qInv_len
|
||||
#endif
|
||||
);
|
||||
void RSA_pub_key_new(RSA_CTX **rsa_ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
const uint8_t *pub_exp, int pub_len);
|
||||
void RSA_free(RSA_CTX *ctx);
|
||||
int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
|
||||
int is_decryption);
|
||||
bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
|
||||
bigint *modulus, bigint *pub_exp);
|
||||
bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
|
||||
int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
|
||||
uint8_t *out_data, int is_signing);
|
||||
void RSA_print(const RSA_CTX *ctx);
|
||||
#endif
|
||||
|
||||
/**************************************************************************
|
||||
* RNG declarations
|
||||
**************************************************************************/
|
||||
EXP_FUNC void STDCALL RNG_initialize(const uint8_t *seed_buf, int size);
|
||||
EXP_FUNC void STDCALL RNG_terminate(void);
|
||||
EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
|
||||
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -37,7 +37,7 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include "crypto_misc.h"
|
||||
#include "crypto.h"
|
||||
|
||||
void RSA_priv_key_new(RSA_CTX **ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
@ -252,7 +252,7 @@ int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
|
||||
/* now encrypt it */
|
||||
dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size);
|
||||
encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) :
|
||||
RSA_public(ctx, dat_bi);
|
||||
RSA_public(ctx, dat_bi);
|
||||
bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
|
||||
|
||||
/* save a few bytes of memory */
|
||||
@ -260,46 +260,4 @@ int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
|
||||
return byte_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a signature and decrypt it.
|
||||
*/
|
||||
bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
|
||||
bigint *modulus, bigint *pub_exp)
|
||||
{
|
||||
int i, size;
|
||||
bigint *decrypted_bi, *dat_bi;
|
||||
bigint *bir = NULL;
|
||||
uint8_t *block = (uint8_t *)alloca(sig_len);
|
||||
|
||||
/* decrypt */
|
||||
dat_bi = bi_import(ctx, sig, sig_len);
|
||||
ctx->mod_offset = BIGINT_M_OFFSET;
|
||||
|
||||
/* convert to a normal block */
|
||||
decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
|
||||
|
||||
bi_export(ctx, decrypted_bi, block, sig_len);
|
||||
ctx->mod_offset = BIGINT_M_OFFSET;
|
||||
|
||||
i = 10; /* start at the first possible non-padded byte */
|
||||
while (block[i++] && i < sig_len);
|
||||
size = sig_len - i;
|
||||
|
||||
/* get only the bit we want */
|
||||
if (size > 0)
|
||||
{
|
||||
int len;
|
||||
const uint8_t *sig_ptr = x509_get_signature(&block[i], &len);
|
||||
|
||||
if (sig_ptr)
|
||||
{
|
||||
bir = bi_import(ctx, sig_ptr, len);
|
||||
}
|
||||
}
|
||||
|
||||
/* save a few bytes of memory */
|
||||
bi_clear_cache(ctx);
|
||||
return bir;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SSL_CERT_VERIFICATION */
|
@ -251,6 +251,13 @@ config CONFIG_OPENSSL_COMPATIBLE
|
||||
Note: not all the API is implemented, so parts may still break. And
|
||||
it's definitely not 100% compatible.
|
||||
|
||||
config CONFIG_GEN_CERTIFICATES
|
||||
bool "Enable the generation of certificates"
|
||||
default n
|
||||
depends on CONFIG_SSL_CERT_VERIFICATION
|
||||
help
|
||||
A primitive self-signed certificate generator.
|
||||
|
||||
config CONFIG_PERFORMANCE_TESTING
|
||||
bool "Build the bigint performance test tool"
|
||||
default n
|
||||
|
13
ssl/Makefile
13
ssl/Makefile
@ -48,8 +48,10 @@ BASETARGET=libaxtls.so
|
||||
CRYPTO_PATH=$(AXTLS_HOME)/crypto/
|
||||
ifdef CONFIG_PLATFORM_CYGWIN
|
||||
TARGET2=$(AXTLS_HOME)/$(STAGE)/libaxtls.dll.a
|
||||
TARGET3=$(AXTLS_HOME)/$(STAGE)/gen_cert.exe
|
||||
else
|
||||
TARGET2=$(AXTLS_HOME)/$(STAGE)/$(LIBMINOR)
|
||||
TARGET3=$(AXTLS_HOME)/$(STAGE)/gen_cert
|
||||
endif
|
||||
|
||||
# shared library major/minor numbers
|
||||
@ -62,26 +64,26 @@ STATIC_LIB=$(AXTLS_HOME)/$(STAGE)/axtls.static.lib
|
||||
CRYPTO_PATH=$(AXTLS_HOME)\\crypto\\
|
||||
endif
|
||||
|
||||
libs: $(TARGET1) $(TARGET2)
|
||||
libs: $(TARGET1) $(TARGET2) $(TARGET3)
|
||||
|
||||
CRYPTO_OBJ=\
|
||||
$(CRYPTO_PATH)aes.o \
|
||||
$(CRYPTO_PATH)bigint.o \
|
||||
$(CRYPTO_PATH)crypto_misc.o \
|
||||
$(CRYPTO_PATH)hmac.o \
|
||||
$(CRYPTO_PATH)md2.o \
|
||||
$(CRYPTO_PATH)md5.o \
|
||||
$(CRYPTO_PATH)rc4.o \
|
||||
$(CRYPTO_PATH)rsa.o \
|
||||
$(CRYPTO_PATH)sha1.o
|
||||
|
||||
OBJ=\
|
||||
asn1.o \
|
||||
x509.o \
|
||||
bigint.o \
|
||||
crypto_misc.o \
|
||||
os_port.o \
|
||||
loader.o \
|
||||
openssl.o \
|
||||
p12.o \
|
||||
rsa.o \
|
||||
tls1.o \
|
||||
tls1_svr.o \
|
||||
tls1_clnt.o
|
||||
@ -104,6 +106,9 @@ else
|
||||
-Wl,--enable-auto-import $(CRYPTO_OBJ) $(OBJ)
|
||||
endif
|
||||
|
||||
$(TARGET3): gen_cert.o
|
||||
$(LD) $(LDFLAGS) -o $@ $< -L$(AXTLS_HOME)/$(STAGE) -laxtls
|
||||
|
||||
else # Win32
|
||||
CRYPTO_OBJ:=$(CRYPTO_OBJ:.o=.obj)
|
||||
|
||||
|
@ -29,8 +29,6 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file asn1.c
|
||||
*
|
||||
* Some primitive asn methods for extraction ASN.1 data.
|
||||
*/
|
||||
|
||||
@ -61,7 +59,7 @@ int get_asn1_length(const uint8_t *buf, int *offset)
|
||||
{
|
||||
len = buf[(*offset)++];
|
||||
}
|
||||
else /* long form */
|
||||
else /* long form */
|
||||
{
|
||||
int length_bytes = buf[(*offset)++]&0x7f;
|
||||
len = 0;
|
||||
|
@ -42,62 +42,6 @@ extern "C" {
|
||||
#include "crypto.h"
|
||||
#include "bigint.h"
|
||||
|
||||
/**************************************************************************
|
||||
* RSA declarations
|
||||
**************************************************************************/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bigint *m; /* modulus */
|
||||
bigint *e; /* public exponent */
|
||||
bigint *d; /* private exponent */
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
bigint *p; /* p as in m = pq */
|
||||
bigint *q; /* q as in m = pq */
|
||||
bigint *dP; /* d mod (p-1) */
|
||||
bigint *dQ; /* d mod (q-1) */
|
||||
bigint *qInv; /* q^-1 mod p */
|
||||
#endif
|
||||
int num_octets;
|
||||
BI_CTX *bi_ctx;
|
||||
} RSA_CTX;
|
||||
|
||||
void RSA_priv_key_new(RSA_CTX **rsa_ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
const uint8_t *pub_exp, int pub_len,
|
||||
const uint8_t *priv_exp, int priv_len
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
, const uint8_t *p, int p_len,
|
||||
const uint8_t *q, int q_len,
|
||||
const uint8_t *dP, int dP_len,
|
||||
const uint8_t *dQ, int dQ_len,
|
||||
const uint8_t *qInv, int qInv_len
|
||||
#endif
|
||||
);
|
||||
void RSA_pub_key_new(RSA_CTX **rsa_ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
const uint8_t *pub_exp, int pub_len);
|
||||
void RSA_free(RSA_CTX *ctx);
|
||||
int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
|
||||
int is_decryption);
|
||||
bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
|
||||
bigint *modulus, bigint *pub_exp);
|
||||
bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
|
||||
int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
|
||||
uint8_t *out_data, int is_signing);
|
||||
void RSA_print(const RSA_CTX *ctx);
|
||||
#endif
|
||||
|
||||
/**************************************************************************
|
||||
* RNG declarations
|
||||
**************************************************************************/
|
||||
EXP_FUNC void STDCALL RNG_initialize(const uint8_t *seed_buf, int size);
|
||||
EXP_FUNC void STDCALL RNG_terminate(void);
|
||||
EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
|
||||
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
|
||||
|
||||
/**************************************************************************
|
||||
* X509 declarations
|
||||
**************************************************************************/
|
||||
|
316
ssl/gen_cert.c
Normal file
316
ssl/gen_cert.c
Normal file
@ -0,0 +1,316 @@
|
||||
/*
|
||||
* Copyright (c) 2007, Cameron Rich
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* * Neither the name of the axTLS project nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#ifdef CONFIG_GEN_CERTIFICATES
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "crypto_misc.h"
|
||||
|
||||
/**
|
||||
* This file is not completed.
|
||||
*/
|
||||
|
||||
/* OBJECT IDENTIFIER sha1withRSAEncryption (1 2 840 113549 1 1 5) */
|
||||
static const uint8_t sig_oid[] =
|
||||
{
|
||||
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05
|
||||
};
|
||||
|
||||
/* OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1) */
|
||||
static const uint8_t rsa_enc_oid[] =
|
||||
{
|
||||
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01
|
||||
};
|
||||
|
||||
static const uint8_t pub_key_seq[] =
|
||||
{
|
||||
0x02, 0x03, 0x01, 0x00, 0x01
|
||||
};
|
||||
|
||||
static uint8_t set_gen_length(int len, uint8_t *buf, int *offset)
|
||||
{
|
||||
if (len < 0x80) /* short form */
|
||||
{
|
||||
buf[(*offset)++] = len;
|
||||
return 1;
|
||||
}
|
||||
else /* long form */
|
||||
{
|
||||
int i, length_bytes = 0;
|
||||
|
||||
if (len & 0x00FF0000)
|
||||
length_bytes = 3;
|
||||
else if (len & 0x0000FF00)
|
||||
length_bytes = 2;
|
||||
else if (len & 0x000000FF)
|
||||
length_bytes = 1;
|
||||
|
||||
buf[(*offset)++] = 0x80 + length_bytes;
|
||||
|
||||
for (i = length_bytes-1; i >= 0; i--)
|
||||
{
|
||||
buf[*offset+i] = len & 0xFF;
|
||||
len >>= 8;
|
||||
}
|
||||
|
||||
*offset += length_bytes;
|
||||
return length_bytes+1;
|
||||
}
|
||||
}
|
||||
|
||||
static int pre_adjust_with_size(uint8_t type,
|
||||
int *seq_offset, uint8_t *buf, int *offset)
|
||||
{
|
||||
buf[(*offset)++] = type;
|
||||
*seq_offset = *offset;
|
||||
*offset += 4; /* fill in later */
|
||||
return *offset;
|
||||
}
|
||||
|
||||
static void adjust_with_size(int seq_size, int seq_start,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
uint8_t seq_byte_size;
|
||||
int orig_seq_size = seq_size;
|
||||
int orig_seq_start = seq_start;
|
||||
|
||||
seq_size = *offset-seq_size;
|
||||
seq_byte_size = set_gen_length(seq_size, buf, &seq_start);
|
||||
|
||||
if (seq_byte_size != 4)
|
||||
{
|
||||
memmove(&buf[orig_seq_start+seq_byte_size],
|
||||
&buf[orig_seq_size], seq_size);
|
||||
*offset -= 4-seq_byte_size;
|
||||
}
|
||||
}
|
||||
|
||||
static void gen_serial_number(uint8_t *buf, int *offset)
|
||||
{
|
||||
buf[(*offset)++] = ASN1_INTEGER;
|
||||
buf[(*offset)++] = 1;
|
||||
buf[(*offset)++] = 0x7F;
|
||||
}
|
||||
|
||||
static void gen_signature_alg(uint8_t *buf, int *offset)
|
||||
{
|
||||
buf[(*offset)++] = ASN1_SEQUENCE;
|
||||
set_gen_length(13, buf, offset);
|
||||
buf[(*offset)++] = ASN1_OID;
|
||||
set_gen_length(sizeof(sig_oid), buf, offset);
|
||||
memcpy(&buf[*offset], sig_oid, sizeof(sig_oid));
|
||||
*offset += sizeof(sig_oid);
|
||||
buf[(*offset)++] = ASN1_NULL;
|
||||
buf[(*offset)++] = 0;
|
||||
}
|
||||
|
||||
static void gen_dn(const char *name, uint8_t dn_type,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
int name_size = strlen(name);
|
||||
|
||||
if (name_size > 0x70) /* just too big */
|
||||
{
|
||||
printf(unsupported_str);
|
||||
return;
|
||||
}
|
||||
|
||||
buf[(*offset)++] = ASN1_SET;
|
||||
set_gen_length(9+name_size, buf, offset);
|
||||
buf[(*offset)++] = ASN1_SEQUENCE;
|
||||
set_gen_length(7+name_size, buf, offset);
|
||||
buf[(*offset)++] = ASN1_OID;
|
||||
buf[(*offset)++] = 3;
|
||||
buf[(*offset)++] = 0x55;
|
||||
buf[(*offset)++] = 0x04;
|
||||
buf[(*offset)++] = dn_type;
|
||||
buf[(*offset)++] = ASN1_PRINTABLE_STR;
|
||||
buf[(*offset)++] = name_size;
|
||||
strcpy(&buf[*offset], name);
|
||||
*offset += name_size;
|
||||
}
|
||||
|
||||
static void gen_issuer(const char *cn, const char *o, const char *ou,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
int seq_offset;
|
||||
int seq_size = pre_adjust_with_size(
|
||||
ASN1_SEQUENCE, &seq_offset, buf, offset);
|
||||
|
||||
if (cn != NULL)
|
||||
gen_dn(cn, 3, buf, offset);
|
||||
|
||||
if (o != NULL)
|
||||
gen_dn(o, 10, buf, offset);
|
||||
|
||||
if (ou != NULL)
|
||||
gen_dn(o, 11, buf, offset);
|
||||
|
||||
adjust_with_size(seq_size, seq_offset, buf, offset);
|
||||
}
|
||||
|
||||
static void gen_utc_time(uint8_t *buf, int *offset)
|
||||
{
|
||||
time_t curr_time = time(NULL);
|
||||
struct tm *now_tm = gmtime(&curr_time);
|
||||
|
||||
buf[(*offset)++] = ASN1_SEQUENCE;
|
||||
set_gen_length(30, buf, offset);
|
||||
|
||||
now_tm->tm_year -= 100;
|
||||
now_tm->tm_mon++;
|
||||
buf[(*offset)++] = ASN1_UTC_TIME;
|
||||
buf[(*offset)++] = 13;
|
||||
buf[(*offset)++] = now_tm->tm_year/10 + '0';
|
||||
buf[(*offset)++] = now_tm->tm_year%10 + '0';
|
||||
buf[(*offset)++] = now_tm->tm_mon/10 + '0';
|
||||
buf[(*offset)++] = now_tm->tm_mon%10 + '0';
|
||||
buf[(*offset)++] = now_tm->tm_mday/10 + '0';
|
||||
buf[(*offset)++] = now_tm->tm_mday%10 + '0';
|
||||
memset(&buf[*offset], '0', 6);
|
||||
*offset += 6;
|
||||
buf[(*offset)++] = 'Z';
|
||||
now_tm->tm_year += 30; /* add 30 years */
|
||||
memcpy(&buf[*offset], &buf[*offset-15], 15);
|
||||
buf[*offset + 2] = now_tm->tm_year/10 + '0';
|
||||
buf[*offset + 3] = now_tm->tm_year%10 + '0';
|
||||
*offset += 15;
|
||||
}
|
||||
|
||||
static void gen_pub_key2(const uint8_t *key, int key_size,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
int seq_offset;
|
||||
int seq_size = pre_adjust_with_size(
|
||||
ASN1_SEQUENCE, &seq_offset, buf, offset);
|
||||
buf[(*offset)++] = ASN1_INTEGER;
|
||||
buf[(*offset)++] = key_size;
|
||||
memcpy(&buf[*offset], key, key_size);
|
||||
*offset += key_size;
|
||||
adjust_with_size(seq_size, seq_offset, buf, offset);
|
||||
}
|
||||
|
||||
static void gen_pub_key1(const uint8_t *key, int key_size,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
int seq_offset;
|
||||
int seq_size = pre_adjust_with_size(
|
||||
ASN1_BIT_STRING, &seq_offset, buf, offset);
|
||||
buf[(*offset)++] = 0; /* bit string is multiple of 8 */
|
||||
gen_pub_key2(key, key_size, buf, offset);
|
||||
adjust_with_size(seq_size, seq_offset, buf, offset);
|
||||
}
|
||||
|
||||
static void gen_pub_key(const uint8_t *key, int key_size,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
int seq_offset;
|
||||
int seq_size = pre_adjust_with_size(
|
||||
ASN1_SEQUENCE, &seq_offset, buf, offset);
|
||||
|
||||
buf[(*offset)++] = ASN1_SEQUENCE;
|
||||
set_gen_length(13, buf, offset);
|
||||
buf[(*offset)++] = ASN1_OID;
|
||||
set_gen_length(sizeof(rsa_enc_oid), buf, offset);
|
||||
memcpy(&buf[*offset], rsa_enc_oid, sizeof(rsa_enc_oid));
|
||||
*offset += sizeof(rsa_enc_oid);
|
||||
buf[(*offset)++] = ASN1_NULL;
|
||||
buf[(*offset)++] = 0;
|
||||
gen_pub_key1(key, key_size, buf, offset);
|
||||
memcpy(&buf[*offset], pub_key_seq, sizeof(pub_key_seq));
|
||||
*offset += sizeof(pub_key_seq);
|
||||
adjust_with_size(seq_size, seq_offset, buf, offset);
|
||||
}
|
||||
|
||||
static void gen_signature(const uint8_t *sig, int sig_size,
|
||||
uint8_t *buf, int *offset)
|
||||
{
|
||||
buf[(*offset)++] = ASN1_BIT_STRING;
|
||||
set_gen_length(sig_size+1, buf, offset);
|
||||
buf[(*offset)++] = 0; /* bit string is multiple of 8 */
|
||||
memcpy(&buf[*offset], sig, sig_size);
|
||||
*offset += sig_size;
|
||||
}
|
||||
|
||||
static void gen_tbs_cert(const char *cn, const char *o, const char *ou,
|
||||
const uint8_t *key, int key_size, uint8_t *buf, int *offset)
|
||||
{
|
||||
int seq_offset;
|
||||
int seq_size = pre_adjust_with_size(
|
||||
ASN1_SEQUENCE, &seq_offset, buf, offset);
|
||||
gen_serial_number(buf, offset);
|
||||
gen_signature_alg(buf, offset);
|
||||
gen_issuer(cn, o, ou, buf, offset);
|
||||
gen_utc_time(buf, offset);
|
||||
gen_issuer(cn, o, ou, buf, offset);
|
||||
gen_pub_key(key, key_size, buf, offset);
|
||||
adjust_with_size(seq_size, seq_offset, buf, offset);
|
||||
}
|
||||
|
||||
int gen_cert(const char *cn, const char *o, const char *ou,
|
||||
const uint8_t *key, int key_size, uint8_t *buf)
|
||||
{
|
||||
int offset = 0;
|
||||
int seq_offset;
|
||||
int seq_size = pre_adjust_with_size(
|
||||
ASN1_SEQUENCE, &seq_offset, buf, &offset);
|
||||
uint8_t sig[128];
|
||||
memset(sig, 0, sizeof(sig));
|
||||
|
||||
gen_tbs_cert(cn, o, ou, key, key_size, buf, &offset);
|
||||
gen_signature_alg(buf, &offset);
|
||||
gen_signature(sig, sizeof(sig), buf, &offset);
|
||||
|
||||
adjust_with_size(seq_size, seq_offset, buf, &offset);
|
||||
print_blob("GA", buf, offset);
|
||||
return offset; /* the size of the certificate */
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uint8_t key[16];
|
||||
uint8_t buf[2048];
|
||||
int offset = 0;
|
||||
memset(key, 0, sizeof(key));
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
//gen_tbs_cert("abc", "def", "ghi", key, sizeof(key), buf, &offset);
|
||||
offset = gen_cert("abc", "def", "ghi", "blah", 5, buf);
|
||||
FILE *f = fopen("blah.dat", "w");
|
||||
fwrite(buf, offset, 1, f);
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
44
ssl/x509.c
44
ssl/x509.c
@ -205,6 +205,48 @@ void x509_free(X509_CTX *x509_ctx)
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
/**
|
||||
* Take a signature and decrypt it.
|
||||
*/
|
||||
static bigint *sig_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
|
||||
bigint *modulus, bigint *pub_exp)
|
||||
{
|
||||
int i, size;
|
||||
bigint *decrypted_bi, *dat_bi;
|
||||
bigint *bir = NULL;
|
||||
uint8_t *block = (uint8_t *)alloca(sig_len);
|
||||
|
||||
/* decrypt */
|
||||
dat_bi = bi_import(ctx, sig, sig_len);
|
||||
ctx->mod_offset = BIGINT_M_OFFSET;
|
||||
|
||||
/* convert to a normal block */
|
||||
decrypted_bi = bi_mod_power2(ctx, dat_bi, modulus, pub_exp);
|
||||
|
||||
bi_export(ctx, decrypted_bi, block, sig_len);
|
||||
ctx->mod_offset = BIGINT_M_OFFSET;
|
||||
|
||||
i = 10; /* start at the first possible non-padded byte */
|
||||
while (block[i++] && i < sig_len);
|
||||
size = sig_len - i;
|
||||
|
||||
/* get only the bit we want */
|
||||
if (size > 0)
|
||||
{
|
||||
int len;
|
||||
const uint8_t *sig_ptr = x509_get_signature(&block[i], &len);
|
||||
|
||||
if (sig_ptr)
|
||||
{
|
||||
bir = bi_import(ctx, sig_ptr, len);
|
||||
}
|
||||
}
|
||||
|
||||
/* save a few bytes of memory */
|
||||
bi_clear_cache(ctx);
|
||||
return bir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do some basic checks on the certificate chain.
|
||||
*
|
||||
@ -296,7 +338,7 @@ int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert)
|
||||
ctx = cert->rsa_ctx->bi_ctx;
|
||||
mod = next_cert->rsa_ctx->m;
|
||||
expn = next_cert->rsa_ctx->e;
|
||||
cert_sig = RSA_sign_verify(ctx, cert->signature, cert->sig_len,
|
||||
cert_sig = sig_verify(ctx, cert->signature, cert->sig_len,
|
||||
bi_clone(ctx, mod), bi_clone(ctx, expn));
|
||||
|
||||
if (cert_sig)
|
||||
|
@ -7086,7 +7086,7 @@ if (useJavaSaver)
|
||||
<div id="contentStash"></div>
|
||||
<div id="storeArea">
|
||||
<div tiddler="(built-in shadow tiddler)" modifier="CameronRich" modified="200702240024" created="200702240024" tags="">changes, notes and errata</div>
|
||||
<div tiddler="Changelog" modifier="YourName" modified="200711050225" created="200702240022" tags="">!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.8@@\n\n!!__SSL Library__\n* Now using a BSD style license.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.7@@\n\n!!__SSL Library__\n\n* Variable sized session id's is now better handled for session caching. It has meant a new API call ssl_get_session_id_size() and a change to ssl_client_new() to define the session id size.\n* Muliple records with a single header are now better supported (thanks to Hervé Sibert).\n* ~MD2 added for Verisign root cert verification (thanks to Byron Rakitzis).\n* The ~MD5/~SHA1 digests are calculated incrementally to reduce memory (thanks to Byron Rakitzis).\n* The bigint cache is now cleared regularly to reduce memory.\n\n!!__axhttpd__\n\n* Improved the POST handling (thanks to Christian Melki).\n* CSS files now work properly.\n* Lua's CGI launcher location is configurable.\n* vfork() is now used for CGI for performance reasons.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.6@@\n\n!!__SSL Library__\n\n* ~RC4 speed improvements\n* Lua samples/bindings now work properly\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.5@@\n\n!!__SSL Library__\n\n* Session id's can now be variable lengths in server hello messages.\n* 0 length client certificates are now supported.\n* ssl_version() now returns just the version and not the date.\n* ssl_write() was not sending complete packets under load.\n\n!!__axhttpd__\n\n* Completely updated the CGI code.\n* Lua now integrated - Lua scripts and Lua Pages now run.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.4@@\n\n!!__SSL Library__\n\n* Fixed a Win32 crypto library issue with non-Administrator users\n* Removed compiler warnings that showed up in ~FC6.\n* GNU TLS certificates are now accepted.\n* Separated the send/receive headers for HMAC calculations.\n* Fixed a compilation problem with swig/perl/~FC6.\n* Fixed an issue with loading PEM CA certificates.\n\n!!__axhttpd__\n\n* Made //setuid()/setgid()// call an mconf option.\n* Made //chroot()// an mconf option. Default to //chdir()// instead.\n* Removed optional permissions checking.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.1@@\n\n!!__SSL Library__\n\n* AES should now work on 16bit processors (there was an alignment problem).\n* Various freed objects are cleared before freeing.\n* Header files now installed in ///usr/local/include/axTLS//.\n* -DCYGWIN replaced with -~DCONFIG_PLATFORM_CYGWIN (and the same for Solaris).\n* removed "-noextern" option in Swig. Fixed some other warnings in Win32.\n* SSLCTX changed to ~SSL_CTX (to be consistent with openssl). SSLCTX still exists for backwards compatibility.\n* malloc() and friends call abort() on failure.\n* Fixed a memory leak in directory listings.\n* Added openssl() compatibility functions.\n* Fixed Cygwin 'make install' issue.\n\n!!__axhttpd__\n\n* main.c now becomes axhttpd.c.\n* Header file issue fixed (in mime_types.c).\n* //chroot()// now used for better security.\n* Basic authentication implemented (via .htpasswd).\n* SSL access/denial protection implemented (via .htaccess).\n* Directory access protection implemented (via .htaccess).\n* Can now have more than one CGI file extension in mconf.\n* "~If-Modified-Since" request now handled properly.\n* Performance tweaks to remove //ssl_find()//.</div>
|
||||
<div tiddler="Changelog" modifier="CameronRich" modified="200711060908" created="200702240022" tags="">!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.8 (yet to be released)@@\n\n!!__SSL Library__\n* Now using a BSD style license.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.7@@\n\n!!__SSL Library__\n\n* Variable sized session id's is now better handled for session caching. It has meant a new API call ssl_get_session_id_size() and a change to ssl_client_new() to define the session id size.\n* Muliple records with a single header are now better supported (thanks to Hervé Sibert).\n* ~MD2 added for Verisign root cert verification (thanks to Byron Rakitzis).\n* The ~MD5/~SHA1 digests are calculated incrementally to reduce memory (thanks to Byron Rakitzis).\n* The bigint cache is now cleared regularly to reduce memory.\n\n!!__axhttpd__\n\n* Improved the POST handling (thanks to Christian Melki).\n* CSS files now work properly.\n* Lua's CGI launcher location is configurable.\n* vfork() is now used for CGI for performance reasons.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.6@@\n\n!!__SSL Library__\n\n* ~RC4 speed improvements\n* Lua samples/bindings now work properly\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.5@@\n\n!!__SSL Library__\n\n* Session id's can now be variable lengths in server hello messages.\n* 0 length client certificates are now supported.\n* ssl_version() now returns just the version and not the date.\n* ssl_write() was not sending complete packets under load.\n\n!!__axhttpd__\n\n* Completely updated the CGI code.\n* Lua now integrated - Lua scripts and Lua Pages now run.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.4@@\n\n!!__SSL Library__\n\n* Fixed a Win32 crypto library issue with non-Administrator users\n* Removed compiler warnings that showed up in ~FC6.\n* GNU TLS certificates are now accepted.\n* Separated the send/receive headers for HMAC calculations.\n* Fixed a compilation problem with swig/perl/~FC6.\n* Fixed an issue with loading PEM CA certificates.\n\n!!__axhttpd__\n\n* Made //setuid()/setgid()// call an mconf option.\n* Made //chroot()// an mconf option. Default to //chdir()// instead.\n* Removed optional permissions checking.\n\n!@@bgcolor(#ff0000):color(#ffffff):Changes for 1.1.1@@\n\n!!__SSL Library__\n\n* AES should now work on 16bit processors (there was an alignment problem).\n* Various freed objects are cleared before freeing.\n* Header files now installed in ///usr/local/include/axTLS//.\n* -DCYGWIN replaced with -~DCONFIG_PLATFORM_CYGWIN (and the same for Solaris).\n* removed "-noextern" option in Swig. Fixed some other warnings in Win32.\n* SSLCTX changed to ~SSL_CTX (to be consistent with openssl). SSLCTX still exists for backwards compatibility.\n* malloc() and friends call abort() on failure.\n* Fixed a memory leak in directory listings.\n* Added openssl() compatibility functions.\n* Fixed Cygwin 'make install' issue.\n\n!!__axhttpd__\n\n* main.c now becomes axhttpd.c.\n* Header file issue fixed (in mime_types.c).\n* //chroot()// now used for better security.\n* Basic authentication implemented (via .htpasswd).\n* SSL access/denial protection implemented (via .htaccess).\n* Directory access protection implemented (via .htaccess).\n* Can now have more than one CGI file extension in mconf.\n* "~If-Modified-Since" request now handled properly.\n* Performance tweaks to remove //ssl_find()//.</div>
|
||||
<div tiddler="DefaultTiddlers" modifier="CameronRich" modified="200702240019" created="200702240019" tags="">[[Read Me]]</div>
|
||||
<div tiddler="License" modifier="YourName" modified="200711050226" created="200702240022" tags="">axTLS uses a BSD style license:\n\nCopyright (c) 2007, Cameron Rich All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\nRedistributions of source code must retain the above copyright notice, this\nlist of conditions and the following disclaimer. Redistributions in binary\nform must reproduce the above copyright notice, this list of conditions and\nthe following disclaimer in the documentation and/or other materials\nprovided with the distribution. Neither the name of the axTLS Project nor\nthe names of its contributors may be used to endorse or promote products\nderived from this software without specific prior written permission. \n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\nARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\nLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\nOUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH\nDAMAGE.</div>
|
||||
<div tiddler="MainMenu" modifier="CameronRich" modified="200702250353" created="200702240021" tags="">[[Read Me]] \n[[Changelog]]\n[[axhttpd]]\n[[License]]</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user