Initial commit: ArgyllCMS v3.5.0 and multichannel profiling research
This commit is contained in:
40
Argyll_V3.5.0/ccast/axTLS/Jamfile
Executable file
40
Argyll_V3.5.0/ccast/axTLS/Jamfile
Executable file
@@ -0,0 +1,40 @@
|
||||
|
||||
#PREF_CCFLAGS = $(CCOPTFLAG) ; # Turn optimisation on
|
||||
PREF_CCFLAGS = $(CCDEBUGFLAG) ; # Debugging flags
|
||||
PREF_LINKFLAGS = $(LINKDEBUGFLAG) ;
|
||||
|
||||
if $(NT) {
|
||||
DEFINES += WIN32 ;
|
||||
} else {
|
||||
if $(OS) = MACOSX {
|
||||
} else if $(OS) = FREEBSD {
|
||||
} else {
|
||||
DEFINES += CONFIG_PLATFORM_LINUX ;
|
||||
}
|
||||
}
|
||||
|
||||
# axTLS library
|
||||
LIBSRCS =
|
||||
aes.c
|
||||
asn1.c
|
||||
bigint.c
|
||||
crypto_misc.c
|
||||
hmac.c
|
||||
loader.c
|
||||
md5.c
|
||||
os_port.c
|
||||
p12.c
|
||||
rc4.c
|
||||
rsa.c
|
||||
sha1.c
|
||||
sha256.c
|
||||
sha384.c
|
||||
sha512.c
|
||||
tls1.c
|
||||
tls1_clnt.c
|
||||
tls1_svr.c
|
||||
x509.c
|
||||
;
|
||||
|
||||
Library libaxtls.lib : $(LIBSRCS) ;
|
||||
|
||||
7
Argyll_V3.5.0/ccast/axTLS/Readme.txt
Normal file
7
Argyll_V3.5.0/ccast/axTLS/Readme.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
This is a cut down version of axTLS, just for client operation.
|
||||
It has been modified to return timeout status rather than
|
||||
erroring, and to be able to call ssl_read asynchronously
|
||||
once the handshaking is complete.
|
||||
|
||||
It is not used for any security sensitive purpose, but is used purely to
|
||||
communicate with the ChromeCast.
|
||||
452
Argyll_V3.5.0/ccast/axTLS/aes.c
Normal file
452
Argyll_V3.5.0/ccast/axTLS/aes.c
Normal file
@@ -0,0 +1,452 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* AES implementation - this is a small code version. There are much faster
|
||||
* versions around but they are much larger in size (i.e. they use large
|
||||
* submix tables).
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
#define rot1(x) (((x) << 24) | ((x) >> 8))
|
||||
#define rot2(x) (((x) << 16) | ((x) >> 16))
|
||||
#define rot3(x) (((x) << 8) | ((x) >> 24))
|
||||
|
||||
/*
|
||||
* This cute trick does 4 'mul by two' at once. Stolen from
|
||||
* Dr B. R. Gladman <brg@gladman.uk.net> but I'm sure the u-(u>>7) is
|
||||
* a standard graphics trick
|
||||
* The key to this is that we need to xor with 0x1b if the top bit is set.
|
||||
* a 1xxx xxxx 0xxx 0xxx First we mask the 7bit,
|
||||
* b 1000 0000 0000 0000 then we shift right by 7 putting the 7bit in 0bit,
|
||||
* c 0000 0001 0000 0000 we then subtract (c) from (b)
|
||||
* d 0111 1111 0000 0000 and now we and with our mask
|
||||
* e 0001 1011 0000 0000
|
||||
*/
|
||||
#define mt 0x80808080
|
||||
#define ml 0x7f7f7f7f
|
||||
#define mh 0xfefefefe
|
||||
#define mm 0x1b1b1b1b
|
||||
#define mul2(x,t) ((t)=((x)&mt), \
|
||||
((((x)+(x))&mh)^(((t)-((t)>>7))&mm)))
|
||||
|
||||
#define inv_mix_col(x,f2,f4,f8,f9) (\
|
||||
(f2)=mul2(x,f2), \
|
||||
(f4)=mul2(f2,f4), \
|
||||
(f8)=mul2(f4,f8), \
|
||||
(f9)=(x)^(f8), \
|
||||
(f8)=((f2)^(f4)^(f8)), \
|
||||
(f2)^=(f9), \
|
||||
(f4)^=(f9), \
|
||||
(f8)^=rot3(f2), \
|
||||
(f8)^=rot2(f4), \
|
||||
(f8)^rot1(f9))
|
||||
|
||||
/*
|
||||
* AES S-box
|
||||
*/
|
||||
static const uint8_t aes_sbox[256] =
|
||||
{
|
||||
0x63,0x7C,0x77,0x7B,0xF2,0x6B,0x6F,0xC5,
|
||||
0x30,0x01,0x67,0x2B,0xFE,0xD7,0xAB,0x76,
|
||||
0xCA,0x82,0xC9,0x7D,0xFA,0x59,0x47,0xF0,
|
||||
0xAD,0xD4,0xA2,0xAF,0x9C,0xA4,0x72,0xC0,
|
||||
0xB7,0xFD,0x93,0x26,0x36,0x3F,0xF7,0xCC,
|
||||
0x34,0xA5,0xE5,0xF1,0x71,0xD8,0x31,0x15,
|
||||
0x04,0xC7,0x23,0xC3,0x18,0x96,0x05,0x9A,
|
||||
0x07,0x12,0x80,0xE2,0xEB,0x27,0xB2,0x75,
|
||||
0x09,0x83,0x2C,0x1A,0x1B,0x6E,0x5A,0xA0,
|
||||
0x52,0x3B,0xD6,0xB3,0x29,0xE3,0x2F,0x84,
|
||||
0x53,0xD1,0x00,0xED,0x20,0xFC,0xB1,0x5B,
|
||||
0x6A,0xCB,0xBE,0x39,0x4A,0x4C,0x58,0xCF,
|
||||
0xD0,0xEF,0xAA,0xFB,0x43,0x4D,0x33,0x85,
|
||||
0x45,0xF9,0x02,0x7F,0x50,0x3C,0x9F,0xA8,
|
||||
0x51,0xA3,0x40,0x8F,0x92,0x9D,0x38,0xF5,
|
||||
0xBC,0xB6,0xDA,0x21,0x10,0xFF,0xF3,0xD2,
|
||||
0xCD,0x0C,0x13,0xEC,0x5F,0x97,0x44,0x17,
|
||||
0xC4,0xA7,0x7E,0x3D,0x64,0x5D,0x19,0x73,
|
||||
0x60,0x81,0x4F,0xDC,0x22,0x2A,0x90,0x88,
|
||||
0x46,0xEE,0xB8,0x14,0xDE,0x5E,0x0B,0xDB,
|
||||
0xE0,0x32,0x3A,0x0A,0x49,0x06,0x24,0x5C,
|
||||
0xC2,0xD3,0xAC,0x62,0x91,0x95,0xE4,0x79,
|
||||
0xE7,0xC8,0x37,0x6D,0x8D,0xD5,0x4E,0xA9,
|
||||
0x6C,0x56,0xF4,0xEA,0x65,0x7A,0xAE,0x08,
|
||||
0xBA,0x78,0x25,0x2E,0x1C,0xA6,0xB4,0xC6,
|
||||
0xE8,0xDD,0x74,0x1F,0x4B,0xBD,0x8B,0x8A,
|
||||
0x70,0x3E,0xB5,0x66,0x48,0x03,0xF6,0x0E,
|
||||
0x61,0x35,0x57,0xB9,0x86,0xC1,0x1D,0x9E,
|
||||
0xE1,0xF8,0x98,0x11,0x69,0xD9,0x8E,0x94,
|
||||
0x9B,0x1E,0x87,0xE9,0xCE,0x55,0x28,0xDF,
|
||||
0x8C,0xA1,0x89,0x0D,0xBF,0xE6,0x42,0x68,
|
||||
0x41,0x99,0x2D,0x0F,0xB0,0x54,0xBB,0x16,
|
||||
};
|
||||
|
||||
/*
|
||||
* AES is-box
|
||||
*/
|
||||
static const uint8_t aes_isbox[256] =
|
||||
{
|
||||
0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,
|
||||
0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
|
||||
0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,
|
||||
0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
|
||||
0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,
|
||||
0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
|
||||
0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,
|
||||
0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
|
||||
0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,
|
||||
0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
|
||||
0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,
|
||||
0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
|
||||
0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,
|
||||
0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
|
||||
0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,
|
||||
0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
|
||||
0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,
|
||||
0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
|
||||
0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,
|
||||
0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
|
||||
0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,
|
||||
0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
|
||||
0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,
|
||||
0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
|
||||
0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,
|
||||
0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
|
||||
0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,
|
||||
0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
|
||||
0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,
|
||||
0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
|
||||
0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,
|
||||
0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d
|
||||
};
|
||||
|
||||
static const unsigned char Rcon[30]=
|
||||
{
|
||||
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,
|
||||
0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f,
|
||||
0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4,
|
||||
0xb3,0x7d,0xfa,0xef,0xc5,0x91,
|
||||
};
|
||||
|
||||
/* ----- static functions ----- */
|
||||
static void AES_encrypt(const AES_CTX *ctx, uint32_t *data);
|
||||
static void AES_decrypt(const AES_CTX *ctx, uint32_t *data);
|
||||
|
||||
/* Perform doubling in Galois Field GF(2^8) using the irreducible polynomial
|
||||
x^8+x^4+x^3+x+1 */
|
||||
static unsigned char AES_xtime(uint32_t x)
|
||||
{
|
||||
return (x&0x80) ? (x<<1)^0x1b : x<<1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up AES with the key/iv and cipher size.
|
||||
*/
|
||||
void AES_set_key(AES_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, AES_MODE mode)
|
||||
{
|
||||
int i, ii;
|
||||
uint32_t *W, tmp, tmp2;
|
||||
const unsigned char *ip;
|
||||
int words;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case AES_MODE_128:
|
||||
i = 10;
|
||||
words = 4;
|
||||
break;
|
||||
|
||||
case AES_MODE_256:
|
||||
i = 14;
|
||||
words = 8;
|
||||
break;
|
||||
|
||||
default: /* fail silently */
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->rounds = i;
|
||||
ctx->key_size = words;
|
||||
W = ctx->ks;
|
||||
for (i = 0; i < words; i+=2)
|
||||
{
|
||||
W[i+0]= ((uint32_t)key[ 0]<<24)|
|
||||
((uint32_t)key[ 1]<<16)|
|
||||
((uint32_t)key[ 2]<< 8)|
|
||||
((uint32_t)key[ 3] );
|
||||
W[i+1]= ((uint32_t)key[ 4]<<24)|
|
||||
((uint32_t)key[ 5]<<16)|
|
||||
((uint32_t)key[ 6]<< 8)|
|
||||
((uint32_t)key[ 7] );
|
||||
key += 8;
|
||||
}
|
||||
|
||||
ip = Rcon;
|
||||
ii = 4 * (ctx->rounds+1);
|
||||
for (i = words; i<ii; i++)
|
||||
{
|
||||
tmp = W[i-1];
|
||||
|
||||
if ((i % words) == 0)
|
||||
{
|
||||
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff]<< 8;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ];
|
||||
tmp=tmp2^(((unsigned int)*ip)<<24);
|
||||
ip++;
|
||||
}
|
||||
|
||||
if ((words == 8) && ((i % words) == 4))
|
||||
{
|
||||
tmp2 =(uint32_t)aes_sbox[(tmp )&0xff] ;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16;
|
||||
tmp2|=(uint32_t)aes_sbox[(tmp>>24) ]<<24;
|
||||
tmp=tmp2;
|
||||
}
|
||||
|
||||
W[i]=W[i-words]^tmp;
|
||||
}
|
||||
|
||||
/* copy the iv across */
|
||||
memcpy(ctx->iv, iv, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a key for decryption.
|
||||
*/
|
||||
void AES_convert_key(AES_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
uint32_t *k,w,t1,t2,t3,t4;
|
||||
|
||||
k = ctx->ks;
|
||||
k += 4;
|
||||
|
||||
for (i= ctx->rounds*4; i > 4; i--)
|
||||
{
|
||||
w= *k;
|
||||
w = inv_mix_col(w,t1,t2,t3,t4);
|
||||
*k++ =w;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a byte sequence (with a block size 16) using the AES cipher.
|
||||
*/
|
||||
void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
|
||||
{
|
||||
int i;
|
||||
uint32_t tin[4], tout[4], iv[4];
|
||||
|
||||
memcpy(iv, ctx->iv, AES_IV_SIZE);
|
||||
for (i = 0; i < 4; i++)
|
||||
tout[i] = ntohl(iv[i]);
|
||||
|
||||
for (length -= AES_BLOCKSIZE; length >= 0; length -= AES_BLOCKSIZE)
|
||||
{
|
||||
uint32_t msg_32[4];
|
||||
uint32_t out_32[4];
|
||||
memcpy(msg_32, msg, AES_BLOCKSIZE);
|
||||
msg += AES_BLOCKSIZE;
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
tin[i] = ntohl(msg_32[i])^tout[i];
|
||||
|
||||
AES_encrypt(ctx, tin);
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
tout[i] = tin[i];
|
||||
out_32[i] = htonl(tout[i]);
|
||||
}
|
||||
|
||||
memcpy(out, out_32, AES_BLOCKSIZE);
|
||||
out += AES_BLOCKSIZE;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
iv[i] = htonl(tout[i]);
|
||||
memcpy(ctx->iv, iv, AES_IV_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt a byte sequence (with a block size 16) using the AES cipher.
|
||||
*/
|
||||
void AES_cbc_decrypt(AES_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
|
||||
{
|
||||
int i;
|
||||
uint32_t tin[4], xor[4], tout[4], data[4], iv[4];
|
||||
|
||||
memcpy(iv, ctx->iv, AES_IV_SIZE);
|
||||
for (i = 0; i < 4; i++)
|
||||
xor[i] = ntohl(iv[i]);
|
||||
|
||||
for (length -= 16; length >= 0; length -= 16)
|
||||
{
|
||||
uint32_t msg_32[4];
|
||||
uint32_t out_32[4];
|
||||
memcpy(msg_32, msg, AES_BLOCKSIZE);
|
||||
msg += AES_BLOCKSIZE;
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
tin[i] = ntohl(msg_32[i]);
|
||||
data[i] = tin[i];
|
||||
}
|
||||
|
||||
AES_decrypt(ctx, data);
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
tout[i] = data[i]^xor[i];
|
||||
xor[i] = tin[i];
|
||||
out_32[i] = htonl(tout[i]);
|
||||
}
|
||||
|
||||
memcpy(out, out_32, AES_BLOCKSIZE);
|
||||
out += AES_BLOCKSIZE;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
iv[i] = htonl(xor[i]);
|
||||
memcpy(ctx->iv, iv, AES_IV_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a single block (16 bytes) of data
|
||||
*/
|
||||
static void AES_encrypt(const AES_CTX *ctx, uint32_t *data)
|
||||
{
|
||||
/* To make this code smaller, generate the sbox entries on the fly.
|
||||
* This will have a really heavy effect upon performance.
|
||||
*/
|
||||
uint32_t tmp[4];
|
||||
uint32_t tmp1, old_a0, a0, a1, a2, a3, row;
|
||||
int curr_rnd;
|
||||
int rounds = ctx->rounds;
|
||||
const uint32_t *k = ctx->ks;
|
||||
|
||||
/* Pre-round key addition */
|
||||
for (row = 0; row < 4; row++)
|
||||
data[row] ^= *(k++);
|
||||
|
||||
/* Encrypt one block. */
|
||||
for (curr_rnd = 0; curr_rnd < rounds; curr_rnd++)
|
||||
{
|
||||
/* Perform ByteSub and ShiftRow operations together */
|
||||
for (row = 0; row < 4; row++)
|
||||
{
|
||||
a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF];
|
||||
a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF];
|
||||
a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF];
|
||||
a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF];
|
||||
|
||||
/* Perform MixColumn iff not last round */
|
||||
if (curr_rnd < (rounds - 1))
|
||||
{
|
||||
tmp1 = a0 ^ a1 ^ a2 ^ a3;
|
||||
old_a0 = a0;
|
||||
a0 ^= tmp1 ^ AES_xtime(a0 ^ a1);
|
||||
a1 ^= tmp1 ^ AES_xtime(a1 ^ a2);
|
||||
a2 ^= tmp1 ^ AES_xtime(a2 ^ a3);
|
||||
a3 ^= tmp1 ^ AES_xtime(a3 ^ old_a0);
|
||||
}
|
||||
|
||||
tmp[row] = ((a0 << 24) | (a1 << 16) | (a2 << 8) | a3);
|
||||
}
|
||||
|
||||
/* KeyAddition - note that it is vital that this loop is separate from
|
||||
the MixColumn operation, which must be atomic...*/
|
||||
for (row = 0; row < 4; row++)
|
||||
data[row] = tmp[row] ^ *(k++);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt a single block (16 bytes) of data
|
||||
*/
|
||||
static void AES_decrypt(const AES_CTX *ctx, uint32_t *data)
|
||||
{
|
||||
uint32_t tmp[4];
|
||||
uint32_t xt0,xt1,xt2,xt3,xt4,xt5,xt6;
|
||||
uint32_t a0, a1, a2, a3, row;
|
||||
int curr_rnd;
|
||||
int rounds = ctx->rounds;
|
||||
const uint32_t *k = ctx->ks + ((rounds+1)*4);
|
||||
|
||||
/* pre-round key addition */
|
||||
for (row=4; row > 0;row--)
|
||||
data[row-1] ^= *(--k);
|
||||
|
||||
/* Decrypt one block */
|
||||
for (curr_rnd = 0; curr_rnd < rounds; curr_rnd++)
|
||||
{
|
||||
/* Perform ByteSub and ShiftRow operations together */
|
||||
for (row = 4; row > 0; row--)
|
||||
{
|
||||
a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF];
|
||||
a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF];
|
||||
a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF];
|
||||
a3 = aes_isbox[(data[row%4])&0xFF];
|
||||
|
||||
/* Perform MixColumn iff not last round */
|
||||
if (curr_rnd<(rounds-1))
|
||||
{
|
||||
/* The MDS cofefficients (0x09, 0x0B, 0x0D, 0x0E)
|
||||
are quite large compared to encryption; this
|
||||
operation slows decryption down noticeably. */
|
||||
xt0 = AES_xtime(a0^a1);
|
||||
xt1 = AES_xtime(a1^a2);
|
||||
xt2 = AES_xtime(a2^a3);
|
||||
xt3 = AES_xtime(a3^a0);
|
||||
xt4 = AES_xtime(xt0^xt1);
|
||||
xt5 = AES_xtime(xt1^xt2);
|
||||
xt6 = AES_xtime(xt4^xt5);
|
||||
|
||||
xt0 ^= a1^a2^a3^xt4^xt6;
|
||||
xt1 ^= a0^a2^a3^xt5^xt6;
|
||||
xt2 ^= a0^a1^a3^xt4^xt6;
|
||||
xt3 ^= a0^a1^a2^xt5^xt6;
|
||||
tmp[row-1] = ((xt0<<24)|(xt1<<16)|(xt2<<8)|xt3);
|
||||
}
|
||||
else
|
||||
tmp[row-1] = ((a0<<24)|(a1<<16)|(a2<<8)|a3);
|
||||
}
|
||||
|
||||
for (row = 4; row > 0; row--)
|
||||
data[row-1] = tmp[row-1] ^ *(--k);
|
||||
}
|
||||
}
|
||||
814
Argyll_V3.5.0/ccast/axTLS/asn1.c
Normal file
814
Argyll_V3.5.0/ccast/axTLS/asn1.c
Normal file
@@ -0,0 +1,814 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2019, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Some primitive asn methods for extraction ASN.1 data.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
#include "crypto_misc.h"
|
||||
|
||||
/* 1.2.840.113549.1.1 OID prefix - handle the following */
|
||||
/* md5WithRSAEncryption(4) */
|
||||
/* sha1WithRSAEncryption(5) */
|
||||
/* sha256WithRSAEncryption (11) */
|
||||
/* sha384WithRSAEncryption (12) */
|
||||
/* sha512WithRSAEncryption (13) */
|
||||
static const uint8_t sig_oid_prefix[] =
|
||||
{
|
||||
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01
|
||||
};
|
||||
|
||||
/* 1.3.14.3.2.29 SHA1 with RSA signature */
|
||||
static const uint8_t sig_sha1WithRSAEncrypt[] =
|
||||
{
|
||||
0x2b, 0x0e, 0x03, 0x02, 0x1d
|
||||
};
|
||||
|
||||
/* 2.16.840.1.101.3.4.2.1 SHA-256 */
|
||||
static const uint8_t sig_sha256[] =
|
||||
{
|
||||
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
|
||||
};
|
||||
|
||||
/* 2.16.840.1.101.3.4.2.2 SHA-384 */
|
||||
static const uint8_t sig_sha384[] =
|
||||
{
|
||||
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
|
||||
};
|
||||
|
||||
/* 2.16.840.1.101.3.4.2.3 SHA-512 */
|
||||
static const uint8_t sig_sha512[] =
|
||||
{
|
||||
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
|
||||
};
|
||||
|
||||
static const uint8_t sig_subject_alt_name[] =
|
||||
{
|
||||
0x55, 0x1d, 0x11
|
||||
};
|
||||
|
||||
static const uint8_t sig_basic_constraints[] =
|
||||
{
|
||||
0x55, 0x1d, 0x13
|
||||
};
|
||||
|
||||
static const uint8_t sig_key_usage[] =
|
||||
{
|
||||
0x55, 0x1d, 0x0f
|
||||
};
|
||||
|
||||
/* CN, O, OU, L, C, ST */
|
||||
static const uint8_t g_dn_types[] = { 3, 10, 11, 7, 6, 8 };
|
||||
|
||||
uint32_t get_asn1_length(const uint8_t *buf, int *offset)
|
||||
{
|
||||
int i;
|
||||
uint32_t len;
|
||||
|
||||
if (!(buf[*offset] & 0x80)) /* short form */
|
||||
{
|
||||
len = buf[(*offset)++];
|
||||
}
|
||||
else /* long form */
|
||||
{
|
||||
int length_bytes = buf[(*offset)++]&0x7f;
|
||||
if (length_bytes > 4) /* limit number of bytes */
|
||||
return 0;
|
||||
|
||||
len = 0;
|
||||
for (i = 0; i < length_bytes; i++)
|
||||
{
|
||||
len <<= 8;
|
||||
len += buf[(*offset)++];
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip the ASN1.1 object type and its length. Get ready to read the object's
|
||||
* data.
|
||||
*/
|
||||
int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type)
|
||||
{
|
||||
if (buf[*offset] != obj_type)
|
||||
return X509_NOT_OK;
|
||||
|
||||
(*offset)++;
|
||||
return get_asn1_length(buf, offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip over an ASN.1 object type completely. Get ready to read the next
|
||||
* object.
|
||||
*/
|
||||
int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type)
|
||||
{
|
||||
int len;
|
||||
|
||||
if (buf[*offset] != obj_type)
|
||||
return X509_NOT_OK;
|
||||
(*offset)++;
|
||||
len = get_asn1_length(buf, offset);
|
||||
*offset += len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an integer value for ASN.1 data
|
||||
* Note: This function allocates memory which must be freed by the user.
|
||||
*/
|
||||
int asn1_get_big_int(const uint8_t *buf, int *offset, uint8_t **object)
|
||||
{
|
||||
int len;
|
||||
|
||||
if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0)
|
||||
goto end_big_int;
|
||||
|
||||
if (len > 1 && buf[*offset] == 0x00) /* ignore the negative byte */
|
||||
{
|
||||
len--;
|
||||
(*offset)++;
|
||||
}
|
||||
|
||||
*object = (uint8_t *)malloc(len);
|
||||
memcpy(*object, &buf[*offset], len);
|
||||
*offset += len;
|
||||
|
||||
end_big_int:
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an integer value for ASN.1 data
|
||||
*/
|
||||
int asn1_get_int(const uint8_t *buf, int *offset, int32_t *val)
|
||||
{
|
||||
int res = X509_OK;
|
||||
int len;
|
||||
int i;
|
||||
|
||||
if ((len = asn1_next_obj(buf, offset, ASN1_INTEGER)) < 0 ||
|
||||
len > sizeof(int32_t))
|
||||
{
|
||||
res = X509_NOT_OK;
|
||||
goto end_int;
|
||||
}
|
||||
|
||||
*val = 0;
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
*val <<= 8;
|
||||
*val |= buf[(*offset)++];
|
||||
}
|
||||
|
||||
end_int:
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read an boolean value for ASN.1 data
|
||||
*/
|
||||
int asn1_get_bool(const uint8_t *buf, int *offset, bool *val)
|
||||
{
|
||||
int res = X509_OK;
|
||||
|
||||
if (asn1_next_obj(buf, offset, ASN1_BOOLEAN) != 1)
|
||||
{
|
||||
res = X509_NOT_OK;
|
||||
goto end_bool;
|
||||
}
|
||||
|
||||
/* DER demands that "If the encoding represents the boolean value TRUE,
|
||||
its single contents octet shall have all eight bits set to one."
|
||||
Thus only 0 and 255 are valid encoded values. */
|
||||
*val = buf[(*offset)++] == 0xFF;
|
||||
|
||||
end_bool:
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an ASN.1 bit string into a 32 bit integer. Used for key usage
|
||||
*/
|
||||
int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val)
|
||||
{
|
||||
int res = X509_OK;
|
||||
int len, i;
|
||||
|
||||
if ((len = asn1_next_obj(buf, offset, ASN1_BIT_STRING)) < 0 || len > 5)
|
||||
{
|
||||
res = X509_NOT_OK;
|
||||
goto end_bit_string_as_int;
|
||||
}
|
||||
|
||||
/* number of bits left unused in the final byte of content */
|
||||
(*offset)++;
|
||||
len--;
|
||||
*val = 0;
|
||||
|
||||
/* not sure why key usage doesn't used proper DER spec version */
|
||||
for (i = len-1; i >= 0; --i)
|
||||
{
|
||||
*val <<= 8;
|
||||
*val |= buf[(*offset) + i];
|
||||
}
|
||||
|
||||
*offset += len;
|
||||
|
||||
end_bit_string_as_int:
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the RSA private key specifics from an ASN.1 encoded file
|
||||
*/
|
||||
int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx)
|
||||
{
|
||||
int offset = 7;
|
||||
uint8_t *modulus = NULL, *priv_exp = NULL, *pub_exp = NULL;
|
||||
int mod_len, priv_len, pub_len;
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
uint8_t *p = NULL, *q = NULL, *dP = NULL, *dQ = NULL, *qInv = NULL;
|
||||
int p_len, q_len, dP_len, dQ_len, qInv_len;
|
||||
#endif
|
||||
|
||||
/* not in der format */
|
||||
if (buf[0] != ASN1_SEQUENCE) /* basic sanity check */
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: This is not a valid ASN.1 file\n");
|
||||
#endif
|
||||
return X509_INVALID_PRIV_KEY;
|
||||
}
|
||||
|
||||
/* Use the private key to mix up the RNG if possible. */
|
||||
RNG_custom_init(buf, len);
|
||||
|
||||
mod_len = asn1_get_big_int(buf, &offset, &modulus);
|
||||
pub_len = asn1_get_big_int(buf, &offset, &pub_exp);
|
||||
priv_len = asn1_get_big_int(buf, &offset, &priv_exp);
|
||||
|
||||
if (mod_len <= 0 || pub_len <= 0 || priv_len <= 0)
|
||||
return X509_INVALID_PRIV_KEY;
|
||||
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
p_len = asn1_get_big_int(buf, &offset, &p);
|
||||
q_len = asn1_get_big_int(buf, &offset, &q);
|
||||
dP_len = asn1_get_big_int(buf, &offset, &dP);
|
||||
dQ_len = asn1_get_big_int(buf, &offset, &dQ);
|
||||
qInv_len = asn1_get_big_int(buf, &offset, &qInv);
|
||||
|
||||
if (p_len <= 0 || q_len <= 0 || dP_len <= 0 || dQ_len <= 0 || qInv_len <= 0)
|
||||
return X509_INVALID_PRIV_KEY;
|
||||
|
||||
RSA_priv_key_new(rsa_ctx,
|
||||
modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len,
|
||||
p, p_len, q, p_len, dP, dP_len, dQ, dQ_len, qInv, qInv_len);
|
||||
|
||||
free(p);
|
||||
free(q);
|
||||
free(dP);
|
||||
free(dQ);
|
||||
free(qInv);
|
||||
#else
|
||||
RSA_priv_key_new(rsa_ctx,
|
||||
modulus, mod_len, pub_exp, pub_len, priv_exp, priv_len);
|
||||
#endif
|
||||
|
||||
free(modulus);
|
||||
free(priv_exp);
|
||||
free(pub_exp);
|
||||
return X509_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* GWG
|
||||
* Read the modulus and public exponent of an RSA certificate.
|
||||
*/
|
||||
int asn1_get_public_key(const uint8_t *cert, int *offset, RSA_CTX **rsa_ctx)
|
||||
{
|
||||
int ret = X509_NOT_OK, mod_len, pub_len;
|
||||
uint8_t *modulus = NULL, *pub_exp = NULL;
|
||||
int len;
|
||||
|
||||
/* Hmm. Not checking that the two ints are within the squence ... */
|
||||
if ((len = asn1_next_obj(cert, offset, ASN1_SEQUENCE)) < 0)
|
||||
goto end_pub_key;
|
||||
|
||||
if ((mod_len = asn1_get_big_int(cert, offset, &modulus)) <= 0) {
|
||||
ret = X509_INVALID_PUB_KEY;
|
||||
goto end_pub_key;
|
||||
}
|
||||
if ((pub_len = asn1_get_big_int(cert, offset, &pub_exp)) <= 0) {
|
||||
free(modulus);
|
||||
ret = X509_INVALID_PUB_KEY;
|
||||
goto end_pub_key;
|
||||
}
|
||||
|
||||
RSA_pub_key_new(rsa_ctx, modulus, mod_len, pub_exp, pub_len);
|
||||
|
||||
free(modulus);
|
||||
free(pub_exp);
|
||||
|
||||
ret = X509_OK;
|
||||
|
||||
end_pub_key:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the time of a certificate.
|
||||
*/
|
||||
static int asn1_get_utc_time(const uint8_t *buf, int *offset, time_t *t)
|
||||
{
|
||||
int ret = X509_NOT_OK, len, t_offset, abs_year;
|
||||
struct tm tm;
|
||||
|
||||
/* see http://tools.ietf.org/html/rfc5280#section-4.1.2.5.1 */
|
||||
if (buf[*offset] == ASN1_UTC_TIME)
|
||||
{
|
||||
(*offset)++;
|
||||
|
||||
len = get_asn1_length(buf, offset);
|
||||
t_offset = *offset;
|
||||
|
||||
memset(&tm, 0, sizeof(struct tm));
|
||||
tm.tm_year = (buf[t_offset] - '0')*10 + (buf[t_offset+1] - '0');
|
||||
|
||||
/*if (tm.tm_year < 50) */ /* ignore 1951-2050 part of spec */
|
||||
{
|
||||
tm.tm_year += 100;
|
||||
}
|
||||
|
||||
tm.tm_mon = (buf[t_offset+2] - '0')*10 + (buf[t_offset+3] - '0') - 1;
|
||||
tm.tm_mday = (buf[t_offset+4] - '0')*10 + (buf[t_offset+5] - '0');
|
||||
tm.tm_hour = (buf[t_offset+6] - '0')*10 + (buf[t_offset+7] - '0');
|
||||
tm.tm_min = (buf[t_offset+8] - '0')*10 + (buf[t_offset+9] - '0');
|
||||
tm.tm_sec = (buf[t_offset+10] - '0')*10 + (buf[t_offset+11] - '0');
|
||||
*t = mktime(&tm);
|
||||
*offset += len;
|
||||
ret = X509_OK;
|
||||
}
|
||||
else if (buf[*offset] == ASN1_GENERALIZED_TIME)
|
||||
{
|
||||
(*offset)++;
|
||||
|
||||
len = get_asn1_length(buf, offset);
|
||||
t_offset = *offset;
|
||||
|
||||
memset(&tm, 0, sizeof(struct tm));
|
||||
abs_year = ((buf[t_offset] - '0')*1000 +
|
||||
(buf[t_offset+1] - '0')*100 + (buf[t_offset+2] - '0')*10 +
|
||||
(buf[t_offset+3] - '0'));
|
||||
|
||||
if (abs_year <= 1901)
|
||||
{
|
||||
tm.tm_year = 1;
|
||||
tm.tm_mon = 0;
|
||||
tm.tm_mday = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
tm.tm_year = abs_year - 1900;
|
||||
tm.tm_mon = (buf[t_offset+4] - '0')*10 +
|
||||
(buf[t_offset+5] - '0') - 1;
|
||||
tm.tm_mday = (buf[t_offset+6] - '0')*10 + (buf[t_offset+7] - '0');
|
||||
tm.tm_hour = (buf[t_offset+8] - '0')*10 + (buf[t_offset+9] - '0');
|
||||
tm.tm_min = (buf[t_offset+10] - '0')*10 + (buf[t_offset+11] - '0');
|
||||
tm.tm_sec = (buf[t_offset+12] - '0')*10 + (buf[t_offset+13] - '0');
|
||||
*t = mktime(&tm);
|
||||
}
|
||||
|
||||
*offset += len;
|
||||
ret = X509_OK;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the version type of a certificate
|
||||
*/
|
||||
int asn1_version(const uint8_t *cert, int *offset, int *val)
|
||||
{
|
||||
(*offset) += 2; /* get past explicit tag */
|
||||
return asn1_get_int(cert, offset, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the notbefore and notafter certificate times.
|
||||
*/
|
||||
int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
|
||||
{
|
||||
return (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_get_utc_time(cert, offset, &x509_ctx->not_before) ||
|
||||
asn1_get_utc_time(cert, offset, &x509_ctx->not_after));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the components of a distinguished name
|
||||
*/
|
||||
static int asn1_get_oid_x520(const uint8_t *buf, int *offset)
|
||||
{
|
||||
int dn_type = 0;
|
||||
int len;
|
||||
|
||||
if ((len = asn1_next_obj(buf, offset, ASN1_OID)) < 0)
|
||||
goto end_oid;
|
||||
|
||||
/* expect a sequence of 2.5.4.[x] where x is a one of distinguished name
|
||||
components we are interested in. */
|
||||
if (len == 3 && buf[(*offset)++] == 0x55 && buf[(*offset)++] == 0x04)
|
||||
dn_type = buf[(*offset)++];
|
||||
else
|
||||
{
|
||||
*offset += len; /* skip over it */
|
||||
}
|
||||
|
||||
end_oid:
|
||||
return dn_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain an ASN.1 printable string type.
|
||||
*/
|
||||
static int asn1_get_printable_str(const uint8_t *buf, int *offset, char **str)
|
||||
{
|
||||
int len = X509_NOT_OK;
|
||||
int asn1_type = buf[*offset];
|
||||
|
||||
/* some certs have this awful crud in them for some reason */
|
||||
if (asn1_type != ASN1_PRINTABLE_STR &&
|
||||
asn1_type != ASN1_PRINTABLE_STR2 &&
|
||||
asn1_type != ASN1_TELETEX_STR &&
|
||||
asn1_type != ASN1_IA5_STR &&
|
||||
asn1_type != ASN1_UNICODE_STR)
|
||||
goto end_pnt_str;
|
||||
|
||||
(*offset)++;
|
||||
len = get_asn1_length(buf, offset);
|
||||
|
||||
if (asn1_type == ASN1_UNICODE_STR)
|
||||
{
|
||||
int i;
|
||||
*str = (char *)malloc(len/2+1); /* allow for null */
|
||||
|
||||
for (i = 0; i < len; i += 2)
|
||||
(*str)[i/2] = buf[*offset + i + 1];
|
||||
|
||||
(*str)[len/2] = 0; /* null terminate */
|
||||
}
|
||||
else
|
||||
{
|
||||
*str = (char *)malloc(len+1); /* allow for null */
|
||||
memcpy(*str, &buf[*offset], len);
|
||||
(*str)[len] = 0; /* null terminate */
|
||||
}
|
||||
|
||||
*offset += len;
|
||||
|
||||
end_pnt_str:
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subject name (or the issuer) of a certificate.
|
||||
*/
|
||||
int asn1_name(const uint8_t *cert, int *offset, char *dn[])
|
||||
{
|
||||
int ret = X509_NOT_OK;
|
||||
int dn_type;
|
||||
char *tmp;
|
||||
|
||||
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
|
||||
goto end_name;
|
||||
|
||||
while (asn1_next_obj(cert, offset, ASN1_SET) >= 0)
|
||||
{
|
||||
int i, found = 0;
|
||||
|
||||
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
|
||||
(dn_type = asn1_get_oid_x520(cert, offset)) < 0)
|
||||
goto end_name;
|
||||
|
||||
tmp = NULL;
|
||||
|
||||
if (asn1_get_printable_str(cert, offset, &tmp) < 0)
|
||||
{
|
||||
free(tmp);
|
||||
goto end_name;
|
||||
}
|
||||
|
||||
/* find the distinguished named type */
|
||||
for (i = 0; i < X509_NUM_DN_TYPES; i++)
|
||||
{
|
||||
if (dn_type == g_dn_types[i])
|
||||
{
|
||||
if (dn[i] == NULL)
|
||||
{
|
||||
dn[i] = tmp;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found == 0) /* not found so get rid of it */
|
||||
{
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
ret = X509_OK;
|
||||
end_name:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the modulus and public exponent of a certificate.
|
||||
*/
|
||||
int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
|
||||
{
|
||||
int ret = X509_NOT_OK, mod_len, pub_len;
|
||||
uint8_t *modulus = NULL, *pub_exp = NULL;
|
||||
|
||||
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_skip_obj(cert, offset, ASN1_SEQUENCE) ||
|
||||
asn1_next_obj(cert, offset, ASN1_BIT_STRING) < 0)
|
||||
goto end_pub_key;
|
||||
|
||||
(*offset)++; /* ignore the padding bit field */
|
||||
|
||||
if (asn1_next_obj(cert, offset, ASN1_SEQUENCE) < 0)
|
||||
goto end_pub_key;
|
||||
|
||||
mod_len = asn1_get_big_int(cert, offset, &modulus);
|
||||
pub_len = asn1_get_big_int(cert, offset, &pub_exp);
|
||||
|
||||
RSA_pub_key_new(&x509_ctx->rsa_ctx, modulus, mod_len, pub_exp, pub_len);
|
||||
|
||||
free(modulus);
|
||||
free(pub_exp);
|
||||
ret = X509_OK;
|
||||
|
||||
end_pub_key:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG)
|
||||
/**
|
||||
* Read the signature of the certificate.
|
||||
*/
|
||||
int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx)
|
||||
{
|
||||
int ret = X509_NOT_OK;
|
||||
|
||||
if (cert[(*offset)++] != ASN1_BIT_STRING)
|
||||
goto end_sig;
|
||||
|
||||
x509_ctx->sig_len = get_asn1_length(cert, offset)-1;
|
||||
(*offset)++; /* ignore bit string padding bits */
|
||||
x509_ctx->signature = (uint8_t *)malloc(x509_ctx->sig_len);
|
||||
memcpy(x509_ctx->signature, &cert[*offset], x509_ctx->sig_len);
|
||||
*offset += x509_ctx->sig_len;
|
||||
ret = X509_OK;
|
||||
|
||||
end_sig:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up all of the CA certificates.
|
||||
*/
|
||||
void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if (ca_cert_ctx == NULL)
|
||||
return;
|
||||
|
||||
while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
|
||||
{
|
||||
x509_free(ca_cert_ctx->cert[i]);
|
||||
ca_cert_ctx->cert[i++] = NULL;
|
||||
}
|
||||
|
||||
free(ca_cert_ctx);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Compare 2 distinguished name components for equality
|
||||
* @return 0 if a match
|
||||
*/
|
||||
static int asn1_compare_dn_comp(const char *dn1, const char *dn2)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (dn1 == NULL && dn2 == NULL)
|
||||
ret = 0;
|
||||
else
|
||||
ret = (dn1 && dn2) ? strcmp(dn1, dn2) : 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare 2 distinguished names for equality
|
||||
* @return 0 if a match
|
||||
*/
|
||||
int asn1_compare_dn(char * const dn1[], char * const dn2[])
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < X509_NUM_DN_TYPES; i++)
|
||||
{
|
||||
if (asn1_compare_dn_comp(dn1[i], dn2[i]))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0; /* all good */
|
||||
}
|
||||
|
||||
int asn1_find_oid(const uint8_t* cert, int* offset,
|
||||
const uint8_t* oid, int oid_length)
|
||||
{
|
||||
int seqlen;
|
||||
if ((seqlen = asn1_next_obj(cert, offset, ASN1_SEQUENCE))> 0)
|
||||
{
|
||||
int end = *offset + seqlen;
|
||||
|
||||
while (*offset < end)
|
||||
{
|
||||
int type = cert[(*offset)++];
|
||||
int length = get_asn1_length(cert, offset);
|
||||
int noffset = *offset + length;
|
||||
|
||||
if (type == ASN1_SEQUENCE)
|
||||
{
|
||||
type = cert[(*offset)++];
|
||||
length = get_asn1_length(cert, offset);
|
||||
|
||||
if (type == ASN1_OID && length == oid_length &&
|
||||
memcmp(cert + *offset, oid, oid_length) == 0)
|
||||
{
|
||||
*offset += oid_length;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
*offset = noffset;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG)
|
||||
int asn1_is_subject_alt_name(const uint8_t *cert, int offset)
|
||||
{
|
||||
if (asn1_find_oid(cert, &offset, sig_subject_alt_name,
|
||||
sizeof(sig_subject_alt_name)))
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int asn1_is_basic_constraints(const uint8_t *cert, int offset)
|
||||
{
|
||||
if (asn1_find_oid(cert, &offset, sig_basic_constraints,
|
||||
sizeof(sig_basic_constraints)))
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int asn1_is_key_usage(const uint8_t *cert, int offset)
|
||||
{
|
||||
if (asn1_find_oid(cert, &offset, sig_key_usage,
|
||||
sizeof(sig_key_usage)))
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool asn1_is_critical_ext(const uint8_t *buf, int *offset)
|
||||
{
|
||||
/* critical is optional */
|
||||
bool res = false;
|
||||
|
||||
if (asn1_next_obj(buf, offset, ASN1_BOOLEAN) == 1)
|
||||
res = buf[(*offset)++] == 0xFF;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SSL_CERT_VERIFICATION */
|
||||
|
||||
/**
|
||||
* Read the signature type of the certificate. We only support RSA-MD5 and
|
||||
* RSA-SHA1 signature types.
|
||||
*/
|
||||
int asn1_signature_type(const uint8_t *cert,
|
||||
int *offset, X509_CTX *x509_ctx)
|
||||
{
|
||||
int ret = X509_NOT_OK, len;
|
||||
|
||||
if (cert[(*offset)++] != ASN1_OID)
|
||||
goto end_check_sig;
|
||||
|
||||
len = get_asn1_length(cert, offset);
|
||||
|
||||
if (len == sizeof(sig_sha1WithRSAEncrypt) &&
|
||||
memcmp(sig_sha1WithRSAEncrypt, &cert[*offset],
|
||||
sizeof(sig_sha1WithRSAEncrypt)) == 0)
|
||||
{
|
||||
x509_ctx->sig_type = SIG_TYPE_SHA1;
|
||||
}
|
||||
else if (len == sizeof(sig_sha256) &&
|
||||
memcmp(sig_sha256, &cert[*offset],
|
||||
sizeof(sig_sha256)) == 0)
|
||||
{
|
||||
x509_ctx->sig_type = SIG_TYPE_SHA256;
|
||||
}
|
||||
else if (len == sizeof(sig_sha384) &&
|
||||
memcmp(sig_sha384, &cert[*offset],
|
||||
sizeof(sig_sha384)) == 0)
|
||||
{
|
||||
x509_ctx->sig_type = SIG_TYPE_SHA384;
|
||||
}
|
||||
else if (len == sizeof(sig_sha512) &&
|
||||
memcmp(sig_sha512, &cert[*offset],
|
||||
sizeof(sig_sha512)) == 0)
|
||||
{
|
||||
x509_ctx->sig_type = SIG_TYPE_SHA512;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (memcmp(sig_oid_prefix, &cert[*offset], sizeof(sig_oid_prefix)))
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
int i;
|
||||
printf("invalid digest: ");
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%02x ", cert[*offset + i]);
|
||||
|
||||
printf("\n");
|
||||
#endif
|
||||
goto end_check_sig; /* unrecognised cert type */
|
||||
}
|
||||
|
||||
x509_ctx->sig_type = cert[*offset + sizeof(sig_oid_prefix)];
|
||||
}
|
||||
|
||||
*offset += len;
|
||||
asn1_skip_obj(cert, offset, ASN1_NULL); /* if it's there */
|
||||
ret = X509_OK;
|
||||
|
||||
end_check_sig:
|
||||
return ret;
|
||||
}
|
||||
|
||||
82
Argyll_V3.5.0/ccast/axTLS/axTLS_config.h
Normal file
82
Argyll_V3.5.0/ccast/axTLS/axTLS_config.h
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Automatically generated header file: don't edit
|
||||
*/
|
||||
|
||||
//#define HAVE_DOT_CONFIG 1
|
||||
//#define CONFIG_PLATFORM_LINUX 1
|
||||
//#undef CONFIG_PLATFORM_CYGWIN
|
||||
//#undef CONFIG_PLATFORM_WIN32
|
||||
|
||||
/*
|
||||
* General Configuration
|
||||
*/
|
||||
#undef CONFIG_DEBUG
|
||||
|
||||
/*
|
||||
* SSL Library
|
||||
*/
|
||||
#undef CONFIG_SSL_FULL_MODE
|
||||
#undef CONFIG_SSL_SERVER_ONLY
|
||||
#undef CONFIG_SSL_CERT_VERIFICATION
|
||||
#define CONFIG_SSL_ENABLE_CLIENT
|
||||
#undef CONFIG_SSL_SKELETON_MODE
|
||||
#undef CONFIG_SSL_PROT_LOW
|
||||
#define CONFIG_SSL_PROT_MEDIUM 1
|
||||
#undef CONFIG_SSL_PROT_HIGH
|
||||
#define CONFIG_SSL_USE_DEFAULT_KEY 1
|
||||
#define CONFIG_SSL_PRIVATE_KEY_LOCATION ""
|
||||
#define CONFIG_SSL_PRIVATE_KEY_PASSWORD ""
|
||||
#define CONFIG_SSL_X509_CERT_LOCATION ""
|
||||
#undef CONFIG_SSL_GENERATE_X509_CERT
|
||||
#define CONFIG_SSL_X509_COMMON_NAME ""
|
||||
#define CONFIG_SSL_X509_ORGANIZATION_NAME ""
|
||||
#define CONFIG_SSL_X509_ORGANIZATION_UNIT_NAME ""
|
||||
#define CONFIG_SSL_HAS_PEM 1
|
||||
#define CONFIG_SSL_USE_PKCS12 1
|
||||
#define CONFIG_SSL_EXPIRY_TIME 24
|
||||
#define CONFIG_X509_MAX_CA_CERTS 150
|
||||
#define CONFIG_SSL_MAX_CERTS 3
|
||||
#define CONFIG_SSL_CTX_MUTEXING 1
|
||||
#undef CONFIG_USE_DEV_URANDOM
|
||||
#undef CONFIG_WIN32_USE_CRYPTO_LIB
|
||||
#undef CONFIG_OPENSSL_COMPATIBLE
|
||||
#undef CONFIG_PERFORMANCE_TESTING
|
||||
#undef CONFIG_SSL_TEST
|
||||
#undef CONFIG_AXTLSWRAP
|
||||
#undef CONFIG_AXHTTPD
|
||||
#undef CONFIG_HTTP_STATIC_BUILD
|
||||
#define CONFIG_HTTP_PORT
|
||||
#define CONFIG_HTTP_HTTPS_PORT
|
||||
#define CONFIG_HTTP_SESSION_CACHE_SIZE
|
||||
#define CONFIG_HTTP_WEBROOT ""
|
||||
#define CONFIG_HTTP_TIMEOUT
|
||||
#undef CONFIG_HTTP_HAS_CGI
|
||||
#define CONFIG_HTTP_CGI_EXTENSIONS ""
|
||||
#undef CONFIG_HTTP_ENABLE_LUA
|
||||
#define CONFIG_HTTP_LUA_PREFIX ""
|
||||
#undef CONFIG_HTTP_BUILD_LUA
|
||||
#define CONFIG_HTTP_CGI_LAUNCHER ""
|
||||
#undef CONFIG_HTTP_DIRECTORIES
|
||||
#undef CONFIG_HTTP_HAS_AUTHORIZATION
|
||||
#undef CONFIG_HTTP_HAS_IPV6
|
||||
#undef CONFIG_HTTP_ENABLE_DIFFERENT_USER
|
||||
#define CONFIG_HTTP_USER ""
|
||||
#undef CONFIG_HTTP_VERBOSE
|
||||
#undef CONFIG_HTTP_IS_DAEMON
|
||||
|
||||
/*
|
||||
* BigInt Options
|
||||
*/
|
||||
#undef CONFIG_BIGINT_CLASSICAL
|
||||
#undef CONFIG_BIGINT_MONTGOMERY
|
||||
#define CONFIG_BIGINT_BARRETT 1
|
||||
#define CONFIG_BIGINT_CRT 1
|
||||
#undef CONFIG_BIGINT_KARATSUBA
|
||||
#define MUL_KARATSUBA_THRESH
|
||||
#define SQU_KARATSUBA_THRESH
|
||||
#define CONFIG_BIGINT_SLIDING_WINDOW 1
|
||||
#define CONFIG_BIGINT_SQUARE 1
|
||||
#undef CONFIG_BIGINT_CHECK_ON
|
||||
#define CONFIG_INTEGER_32BIT 1
|
||||
#undef CONFIG_INTEGER_16BIT
|
||||
#undef CONFIG_INTEGER_8BIT
|
||||
1
Argyll_V3.5.0/ccast/axTLS/axTLS_version.h
Normal file
1
Argyll_V3.5.0/ccast/axTLS/axTLS_version.h
Normal file
@@ -0,0 +1 @@
|
||||
#define AXTLS_VERSION "2.1.5"
|
||||
1512
Argyll_V3.5.0/ccast/axTLS/bigint.c
Normal file
1512
Argyll_V3.5.0/ccast/axTLS/bigint.c
Normal file
File diff suppressed because it is too large
Load Diff
99
Argyll_V3.5.0/ccast/axTLS/bigint.h
Normal file
99
Argyll_V3.5.0/ccast/axTLS/bigint.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef BIGINT_HEADER
|
||||
#define BIGINT_HEADER
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
BI_CTX *bi_initialize(void);
|
||||
void bi_terminate(BI_CTX *ctx);
|
||||
void bi_permanent(bigint *bi);
|
||||
void bi_depermanent(bigint *bi);
|
||||
void bi_clear_cache(BI_CTX *ctx);
|
||||
void bi_free(BI_CTX *ctx, bigint *bi);
|
||||
bigint *bi_copy(bigint *bi);
|
||||
bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
|
||||
void bi_export(BI_CTX *ctx, bigint *bi, uint8_t *data, int size);
|
||||
bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int len);
|
||||
bigint *int_to_bi(BI_CTX *ctx, comp i);
|
||||
|
||||
/* the functions that actually do something interesting */
|
||||
bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib);
|
||||
bigint *bi_subtract(BI_CTX *ctx, bigint *bia,
|
||||
bigint *bib, int *is_negative);
|
||||
bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim, int is_mod);
|
||||
bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib);
|
||||
bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp);
|
||||
bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp);
|
||||
int bi_compare(bigint *bia, bigint *bib);
|
||||
void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
|
||||
void bi_free_mod(BI_CTX *ctx, int mod_offset);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
void bi_print(const char *label, bigint *bi);
|
||||
bigint *bi_str_import(BI_CTX *ctx, const char *data);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @def bi_mod
|
||||
* Find the residue of B. bi_set_mod() must be called before hand.
|
||||
*/
|
||||
#define bi_mod(A, B) bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1)
|
||||
|
||||
/**
|
||||
* bi_residue() is technically the same as bi_mod(), but it uses the
|
||||
* appropriate reduction technique (which is bi_mod() when doing classical
|
||||
* reduction).
|
||||
*/
|
||||
#if defined(CONFIG_BIGINT_MONTGOMERY)
|
||||
#define bi_residue(A, B) bi_mont(A, B)
|
||||
bigint *bi_mont(BI_CTX *ctx, bigint *bixy);
|
||||
#elif defined(CONFIG_BIGINT_BARRETT)
|
||||
#define bi_residue(A, B) bi_barrett(A, B)
|
||||
bigint *bi_barrett(BI_CTX *ctx, bigint *bi);
|
||||
#else /* if defined(CONFIG_BIGINT_CLASSICAL) */
|
||||
#define bi_residue(A, B) bi_mod(A, B)
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_BIGINT_SQUARE
|
||||
bigint *bi_square(BI_CTX *ctx, bigint *bi);
|
||||
#else
|
||||
#define bi_square(A, B) bi_multiply(A, bi_copy(B), B)
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
bigint *bi_crt(BI_CTX *ctx, bigint *bi,
|
||||
bigint *dP, bigint *dQ,
|
||||
bigint *p, bigint *q,
|
||||
bigint *qInv);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
131
Argyll_V3.5.0/ccast/axTLS/bigint_impl.h
Normal file
131
Argyll_V3.5.0/ccast/axTLS/bigint_impl.h
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef BIGINT_IMPL_HEADER
|
||||
#define BIGINT_IMPL_HEADER
|
||||
|
||||
/* Maintain a number of precomputed variables when doing reduction */
|
||||
#define BIGINT_M_OFFSET 0 /**< Normal modulo offset. */
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
#define BIGINT_P_OFFSET 1 /**< p modulo offset. */
|
||||
#define BIGINT_Q_OFFSET 2 /**< q module offset. */
|
||||
#define BIGINT_NUM_MODS 3 /**< The number of modulus constants used. */
|
||||
#else
|
||||
#define BIGINT_NUM_MODS 1
|
||||
#endif
|
||||
|
||||
/* Architecture specific functions for big ints */
|
||||
#if defined(CONFIG_INTEGER_8BIT)
|
||||
#define COMP_RADIX 256U /**< Max component + 1 */
|
||||
#define COMP_MAX 0xFFFFU/**< (Max dbl comp -1) */
|
||||
#define COMP_BIT_SIZE 8 /**< Number of bits in a component. */
|
||||
#define COMP_BYTE_SIZE 1 /**< Number of bytes in a component. */
|
||||
#define COMP_NUM_NIBBLES 2 /**< Used For diagnostics only. */
|
||||
typedef uint8_t comp; /**< A single precision component. */
|
||||
typedef uint16_t long_comp; /**< A double precision component. */
|
||||
typedef int16_t slong_comp; /**< A signed double precision component. */
|
||||
#elif defined(CONFIG_INTEGER_16BIT)
|
||||
#define COMP_RADIX 65536U /**< Max component + 1 */
|
||||
#define COMP_MAX 0xFFFFFFFFU/**< (Max dbl comp -1) */
|
||||
#define COMP_BIT_SIZE 16 /**< Number of bits in a component. */
|
||||
#define COMP_BYTE_SIZE 2 /**< Number of bytes in a component. */
|
||||
#define COMP_NUM_NIBBLES 4 /**< Used For diagnostics only. */
|
||||
typedef uint16_t comp; /**< A single precision component. */
|
||||
typedef uint32_t long_comp; /**< A double precision component. */
|
||||
typedef int32_t slong_comp; /**< A signed double precision component. */
|
||||
#else /* regular 32 bit */
|
||||
#if defined(WIN32) && !defined(__GNUC__)
|
||||
#define COMP_RADIX 4294967296i64
|
||||
#define COMP_MAX 0xFFFFFFFFFFFFFFFFui64
|
||||
#else
|
||||
#define COMP_RADIX 4294967296ULL /**< Max component + 1 */
|
||||
#define COMP_MAX 0xFFFFFFFFFFFFFFFFULL/**< (Max dbl comp -1) */
|
||||
#endif
|
||||
#define COMP_BIT_SIZE 32 /**< Number of bits in a component. */
|
||||
#define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */
|
||||
#define COMP_NUM_NIBBLES 8 /**< Used For diagnostics only. */
|
||||
typedef uint32_t comp; /**< A single precision component. */
|
||||
typedef uint64_t long_comp; /**< A double precision component. */
|
||||
typedef int64_t slong_comp; /**< A signed double precision component. */
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @struct _bigint
|
||||
* @brief A big integer basic object
|
||||
*/
|
||||
struct _bigint
|
||||
{
|
||||
struct _bigint* next; /**< The next bigint in the cache. */
|
||||
short size; /**< The number of components in this bigint. */
|
||||
short max_comps; /**< The heapsize allocated for this bigint */
|
||||
int refs; /**< An internal reference count. */
|
||||
comp* comps; /**< A ptr to the actual component data */
|
||||
};
|
||||
|
||||
typedef struct _bigint bigint; /**< An alias for _bigint */
|
||||
|
||||
/**
|
||||
* Maintains the state of the cache, and a number of variables used in
|
||||
* reduction.
|
||||
*/
|
||||
typedef struct /**< A big integer "session" context. */
|
||||
{
|
||||
bigint *active_list; /**< Bigints currently used. */
|
||||
bigint *free_list; /**< Bigints not used. */
|
||||
bigint *bi_radix; /**< The radix used. */
|
||||
bigint *bi_mod[BIGINT_NUM_MODS]; /**< modulus */
|
||||
|
||||
#if defined(CONFIG_BIGINT_MONTGOMERY)
|
||||
bigint *bi_RR_mod_m[BIGINT_NUM_MODS]; /**< R^2 mod m */
|
||||
bigint *bi_R_mod_m[BIGINT_NUM_MODS]; /**< R mod m */
|
||||
comp N0_dash[BIGINT_NUM_MODS];
|
||||
#elif defined(CONFIG_BIGINT_BARRETT)
|
||||
bigint *bi_mu[BIGINT_NUM_MODS]; /**< Storage for mu */
|
||||
#endif
|
||||
bigint *bi_normalised_mod[BIGINT_NUM_MODS]; /**< Normalised mod storage. */
|
||||
bigint **g; /**< Used by sliding-window. */
|
||||
int window; /**< The size of the sliding window */
|
||||
int active_count; /**< Number of active bigints. */
|
||||
int free_count; /**< Number of free bigints. */
|
||||
|
||||
#ifdef CONFIG_BIGINT_MONTGOMERY
|
||||
uint8_t use_classical; /**< Use classical reduction. */
|
||||
#endif
|
||||
uint8_t mod_offset; /**< The mod offset we are using */
|
||||
} BI_CTX;
|
||||
|
||||
#ifndef WIN32
|
||||
#define max(a,b) ((a)>(b)?(a):(b)) /**< Find the maximum of 2 numbers. */
|
||||
#define min(a,b) ((a)<(b)?(a):(b)) /**< Find the minimum of 2 numbers. */
|
||||
#endif
|
||||
|
||||
#define PERMANENT 0x7FFF55AA /**< A magic number for permanents. */
|
||||
|
||||
#endif
|
||||
54
Argyll_V3.5.0/ccast/axTLS/cert.h
Normal file
54
Argyll_V3.5.0/ccast/axTLS/cert.h
Normal file
@@ -0,0 +1,54 @@
|
||||
unsigned char default_certificate[] = {
|
||||
0x30, 0x82, 0x02, 0x58, 0x30, 0x82, 0x01, 0x40, 0x02, 0x09, 0x00, 0xa5,
|
||||
0x2a, 0xc8, 0x78, 0x87, 0xf2, 0xe7, 0xc5, 0x30, 0x0d, 0x06, 0x09, 0x2a,
|
||||
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x34,
|
||||
0x31, 0x32, 0x30, 0x30, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x29, 0x61,
|
||||
0x78, 0x54, 0x4c, 0x53, 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65, 0x63, 0x74,
|
||||
0x20, 0x44, 0x6f, 0x64, 0x67, 0x79, 0x20, 0x43, 0x65, 0x72, 0x74, 0x69,
|
||||
0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f,
|
||||
0x72, 0x69, 0x74, 0x79, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x31, 0x32,
|
||||
0x33, 0x30, 0x32, 0x31, 0x30, 0x34, 0x32, 0x37, 0x5a, 0x17, 0x0d, 0x33,
|
||||
0x30, 0x30, 0x39, 0x30, 0x38, 0x32, 0x31, 0x30, 0x34, 0x32, 0x37, 0x5a,
|
||||
0x30, 0x2c, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
|
||||
0x0d, 0x61, 0x78, 0x54, 0x4c, 0x53, 0x20, 0x50, 0x72, 0x6f, 0x6a, 0x65,
|
||||
0x63, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13,
|
||||
0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x81,
|
||||
0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
|
||||
0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02,
|
||||
0x81, 0x81, 0x00, 0xbd, 0x0f, 0xd4, 0x42, 0xa8, 0x74, 0x87, 0x54, 0xaa,
|
||||
0xb9, 0x3a, 0x1f, 0x8b, 0xce, 0xbd, 0xb7, 0x65, 0xfb, 0x40, 0x3d, 0xd0,
|
||||
0x11, 0x9a, 0x9c, 0xdc, 0x82, 0x7c, 0xea, 0xa8, 0x17, 0xe1, 0x74, 0xf3,
|
||||
0x05, 0x0e, 0x61, 0xc1, 0xc1, 0x78, 0x8a, 0xb2, 0xba, 0x15, 0x22, 0x5a,
|
||||
0xff, 0x9b, 0xb8, 0x7a, 0x2e, 0x0f, 0x88, 0xb7, 0x74, 0xde, 0x04, 0x99,
|
||||
0xa5, 0xa2, 0x99, 0x53, 0x8b, 0xad, 0x78, 0x5a, 0x31, 0xed, 0xbc, 0x01,
|
||||
0xe7, 0xdf, 0xe9, 0xec, 0x2f, 0xa0, 0x5d, 0x53, 0xf6, 0xe6, 0x8a, 0xa0,
|
||||
0xc8, 0x6d, 0x41, 0x45, 0x63, 0x23, 0xb3, 0xcf, 0x4e, 0x50, 0x1f, 0x28,
|
||||
0xdf, 0x36, 0xe2, 0x73, 0xdf, 0xd6, 0xa1, 0xb3, 0x46, 0x4f, 0x6e, 0xbb,
|
||||
0x0d, 0x9b, 0xef, 0xa8, 0xf9, 0x4c, 0xa5, 0x71, 0xa1, 0x88, 0xdd, 0x07,
|
||||
0xa9, 0x86, 0x0d, 0x3f, 0xcd, 0x99, 0x23, 0xa2, 0x84, 0x77, 0x0f, 0x02,
|
||||
0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
|
||||
0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
|
||||
0x32, 0xe0, 0x3c, 0x6e, 0x21, 0xe6, 0xa6, 0xf4, 0xb8, 0x10, 0x9f, 0x8a,
|
||||
0xe6, 0x0b, 0x84, 0x4e, 0x2c, 0xe5, 0x14, 0xca, 0x56, 0x81, 0x3f, 0xc0,
|
||||
0x2c, 0xa3, 0x39, 0x89, 0x24, 0xce, 0xaf, 0x47, 0x2e, 0x19, 0x62, 0xb2,
|
||||
0xe4, 0x76, 0x91, 0x25, 0xbc, 0xe1, 0xa8, 0xee, 0x6a, 0x68, 0x3a, 0x77,
|
||||
0xb9, 0xb2, 0x62, 0x97, 0x0c, 0x25, 0x3c, 0x5e, 0x13, 0x48, 0x87, 0x80,
|
||||
0xa3, 0x91, 0xd9, 0x2e, 0xe6, 0x92, 0x2b, 0x1c, 0x52, 0x24, 0xb1, 0x77,
|
||||
0xc6, 0xf6, 0xde, 0xd8, 0x9b, 0xd9, 0x57, 0x37, 0x56, 0x68, 0x17, 0x32,
|
||||
0x66, 0x01, 0x08, 0x38, 0x08, 0x9a, 0xc1, 0x8c, 0x5e, 0x3f, 0xe7, 0xc9,
|
||||
0x44, 0xcb, 0x62, 0xb9, 0x48, 0xc7, 0x89, 0xa6, 0xff, 0x8e, 0x7d, 0x3d,
|
||||
0xe1, 0x46, 0x32, 0x9c, 0x13, 0x06, 0x9a, 0xd1, 0x17, 0xab, 0x3f, 0xa9,
|
||||
0x90, 0x04, 0x33, 0x2d, 0x3f, 0x81, 0x0a, 0xa5, 0x55, 0xce, 0xb6, 0x95,
|
||||
0x54, 0xad, 0xf1, 0x4f, 0xa2, 0xca, 0xc3, 0xf6, 0x25, 0x7b, 0x71, 0xd2,
|
||||
0x68, 0x85, 0xe9, 0x72, 0xb6, 0x99, 0x34, 0x6d, 0xe5, 0x5f, 0xf6, 0x74,
|
||||
0x1c, 0xb9, 0xa2, 0xda, 0x2b, 0x04, 0xff, 0x82, 0xc5, 0x09, 0x04, 0xc4,
|
||||
0xba, 0xbc, 0x82, 0x3e, 0xb4, 0x72, 0x18, 0x8e, 0x30, 0x68, 0x48, 0x4a,
|
||||
0x0d, 0xa7, 0x3d, 0xb5, 0xf4, 0x42, 0x3a, 0x97, 0x60, 0x7d, 0xa8, 0x61,
|
||||
0x8a, 0x9e, 0x98, 0xc4, 0x7e, 0x65, 0x99, 0xea, 0x7e, 0xca, 0x75, 0xe7,
|
||||
0xdb, 0x21, 0x5d, 0xce, 0x7c, 0x66, 0x3d, 0x7e, 0xdc, 0x14, 0xfe, 0x55,
|
||||
0x04, 0x97, 0xa8, 0x64, 0x12, 0xb4, 0xb5, 0x30, 0x48, 0x72, 0xbc, 0xdb,
|
||||
0xeb, 0x5b, 0x4f, 0xa6, 0xfb, 0x87, 0x01, 0x41, 0x91, 0xec, 0x98, 0x98,
|
||||
0xf1, 0x4b, 0x38, 0xa2, 0x40, 0xf1, 0x05, 0x90, 0xbb, 0x9b, 0x5d, 0x96,
|
||||
0xb1, 0x22, 0x6b, 0x50
|
||||
};
|
||||
unsigned int default_certificate_len = 604;
|
||||
270
Argyll_V3.5.0/ccast/axTLS/crypto.h
Normal file
270
Argyll_V3.5.0/ccast/axTLS/crypto.h
Normal file
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file crypto.h
|
||||
*/
|
||||
|
||||
#ifndef HEADER_CRYPTO_H
|
||||
#define HEADER_CRYPTO_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "bigint_impl.h"
|
||||
#include "bigint.h"
|
||||
|
||||
#ifndef STDCALL
|
||||
#define STDCALL
|
||||
#endif
|
||||
#ifndef EXP_FUNC
|
||||
#define EXP_FUNC
|
||||
#endif
|
||||
|
||||
|
||||
/* enable features based on a 'super-set' capbaility. */
|
||||
#if defined(CONFIG_SSL_FULL_MODE)
|
||||
#define CONFIG_SSL_ENABLE_CLIENT
|
||||
//#define CONFIG_SSL_CERT_VERIFICATION
|
||||
#elif defined(CONFIG_SSL_ENABLE_CLIENT)
|
||||
//#define CONFIG_SSL_CERT_VERIFICATION // GWG
|
||||
#endif
|
||||
|
||||
/**************************************************************************
|
||||
* AES declarations
|
||||
**************************************************************************/
|
||||
|
||||
#define AES_MAXROUNDS 14
|
||||
#define AES_BLOCKSIZE 16
|
||||
#define AES_IV_SIZE 16
|
||||
|
||||
typedef struct aes_key_st
|
||||
{
|
||||
uint16_t rounds;
|
||||
uint16_t key_size;
|
||||
uint32_t ks[(AES_MAXROUNDS+1)*8];
|
||||
uint8_t iv[AES_IV_SIZE];
|
||||
} AES_CTX;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
AES_MODE_128,
|
||||
AES_MODE_256
|
||||
} AES_MODE;
|
||||
|
||||
void AES_set_key(AES_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, AES_MODE mode);
|
||||
void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
|
||||
uint8_t *out, int length);
|
||||
void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
|
||||
void AES_convert_key(AES_CTX *ctx);
|
||||
|
||||
/**************************************************************************
|
||||
* RC4 declarations
|
||||
**************************************************************************/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t x, y, m[256];
|
||||
} RC4_CTX;
|
||||
|
||||
void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
|
||||
void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
|
||||
|
||||
/**************************************************************************
|
||||
* SHA1 declarations
|
||||
**************************************************************************/
|
||||
|
||||
#define SHA1_SIZE 20
|
||||
|
||||
/*
|
||||
* This structure will hold context information for the SHA-1
|
||||
* hashing operation
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
|
||||
uint32_t Length_Low; /* Message length in bits */
|
||||
uint32_t Length_High; /* Message length in bits */
|
||||
uint16_t Message_Block_Index; /* Index into message block array */
|
||||
uint8_t Message_Block[64]; /* 512-bit message blocks */
|
||||
} SHA1_CTX;
|
||||
|
||||
void SHA1_Init(SHA1_CTX *);
|
||||
void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
|
||||
void SHA1_Final(uint8_t *digest, SHA1_CTX *);
|
||||
|
||||
/**************************************************************************
|
||||
* SHA256 declarations
|
||||
**************************************************************************/
|
||||
|
||||
#define SHA256_SIZE 32
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t total[2];
|
||||
uint32_t state[8];
|
||||
uint8_t buffer[64];
|
||||
} SHA256_CTX;
|
||||
|
||||
void SHA256_Init(SHA256_CTX *c);
|
||||
void SHA256_Update(SHA256_CTX *, const uint8_t *input, int len);
|
||||
void SHA256_Final(uint8_t *digest, SHA256_CTX *);
|
||||
|
||||
/**************************************************************************
|
||||
* SHA512 declarations
|
||||
**************************************************************************/
|
||||
|
||||
#define SHA512_SIZE 64
|
||||
|
||||
typedef struct
|
||||
{
|
||||
union
|
||||
{
|
||||
uint64_t h[8];
|
||||
uint8_t digest[64];
|
||||
} h_dig;
|
||||
union
|
||||
{
|
||||
uint64_t w[80];
|
||||
uint8_t buffer[128];
|
||||
} w_buf;
|
||||
size_t size;
|
||||
uint64_t totalSize;
|
||||
} SHA512_CTX;
|
||||
|
||||
void SHA512_Init(SHA512_CTX *c);
|
||||
void SHA512_Update(SHA512_CTX *, const uint8_t *input, int len);
|
||||
void SHA512_Final(uint8_t *digest, SHA512_CTX *);
|
||||
|
||||
/**************************************************************************
|
||||
* SHA384 declarations
|
||||
**************************************************************************/
|
||||
|
||||
#define SHA384_SIZE 48
|
||||
|
||||
typedef SHA512_CTX SHA384_CTX;
|
||||
void SHA384_Init(SHA384_CTX *c);
|
||||
void SHA384_Update(SHA384_CTX *, const uint8_t *input, int len);
|
||||
void SHA384_Final(uint8_t *digest, SHA384_CTX *);
|
||||
|
||||
/**************************************************************************
|
||||
* MD5 declarations
|
||||
**************************************************************************/
|
||||
|
||||
#define MD5_SIZE 16
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t state[4]; /* state (ABCD) */
|
||||
uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
||||
uint8_t buffer[64]; /* input buffer */
|
||||
} MD5_CTX;
|
||||
|
||||
EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
|
||||
EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
|
||||
EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
|
||||
|
||||
/**************************************************************************
|
||||
* HMAC declarations
|
||||
**************************************************************************/
|
||||
void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest);
|
||||
void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest);
|
||||
void hmac_sha256(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 out_len, int is_decryption);
|
||||
int RSA_decrypt2(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data); // GWG
|
||||
bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
|
||||
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
|
||||
bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
|
||||
bigint *modulus, bigint *pub_exp);
|
||||
#endif
|
||||
#ifdef CONFIG_DEBUG
|
||||
void RSA_print(const RSA_CTX *ctx);
|
||||
#endif
|
||||
int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
|
||||
uint8_t *out_data, int is_signing);
|
||||
bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
|
||||
|
||||
/**************************************************************************
|
||||
* RNG declarations
|
||||
**************************************************************************/
|
||||
EXP_FUNC void STDCALL RNG_initialize(void);
|
||||
EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
|
||||
EXP_FUNC void STDCALL RNG_terminate(void);
|
||||
EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
|
||||
int get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
375
Argyll_V3.5.0/ccast/axTLS/crypto_misc.c
Normal file
375
Argyll_V3.5.0/ccast/axTLS/crypto_misc.c
Normal file
@@ -0,0 +1,375 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2019, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Some misc. routines to help things out
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto_misc.h"
|
||||
#ifdef CONFIG_WIN32_USE_CRYPTO_LIB
|
||||
#include "wincrypt.h"
|
||||
#endif
|
||||
|
||||
#ifndef WIN32
|
||||
static int rng_fd = -1;
|
||||
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
|
||||
static HCRYPTPROV gCryptProv;
|
||||
#endif
|
||||
|
||||
#if (!defined(CONFIG_USE_DEV_URANDOM) && !defined(CONFIG_WIN32_USE_CRYPTO_LIB))
|
||||
/* change to processor registers as appropriate */
|
||||
#define ENTROPY_POOL_SIZE 32
|
||||
#define ENTROPY_COUNTER1 ((((uint64_t)tv.tv_sec)<<32) | tv.tv_usec)
|
||||
#define ENTROPY_COUNTER2 rand()
|
||||
static uint8_t entropy_pool[ENTROPY_POOL_SIZE];
|
||||
#endif
|
||||
|
||||
const char * const unsupported_str = "Error: Feature not supported\n";
|
||||
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
/**
|
||||
* Retrieve a file and put it into memory
|
||||
* @return The size of the file, or -1 on failure.
|
||||
*/
|
||||
int get_file(const char *filename, uint8_t **buf)
|
||||
{
|
||||
int total_bytes = 0;
|
||||
int bytes_read = 0;
|
||||
int filesize;
|
||||
FILE *stream = fopen(filename, "rb");
|
||||
|
||||
if (stream == NULL)
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("file '%s' does not exist\n", filename); TTY_FLUSH();
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Win CE doesn't support stat() */
|
||||
fseek(stream, 0, SEEK_END);
|
||||
filesize = ftell(stream);
|
||||
*buf = (uint8_t *)malloc(filesize);
|
||||
fseek(stream, 0, SEEK_SET);
|
||||
|
||||
do
|
||||
{
|
||||
bytes_read = fread(*buf+total_bytes, 1, filesize-total_bytes, stream);
|
||||
total_bytes += bytes_read;
|
||||
} while (total_bytes < filesize && bytes_read > 0);
|
||||
|
||||
fclose(stream);
|
||||
return filesize;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Initialise the Random Number Generator engine.
|
||||
* - On Win32 use the platform SDK's crypto engine.
|
||||
* - On Linux use /dev/urandom
|
||||
* - If none of these work then use a custom RNG.
|
||||
*/
|
||||
EXP_FUNC void STDCALL RNG_initialize()
|
||||
{
|
||||
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
|
||||
rng_fd = open("/dev/urandom", O_RDONLY);
|
||||
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
|
||||
if (!CryptAcquireContext(&gCryptProv,
|
||||
NULL, NULL, PROV_RSA_FULL, 0))
|
||||
{
|
||||
if (GetLastError() == NTE_BAD_KEYSET &&
|
||||
!CryptAcquireContext(&gCryptProv,
|
||||
NULL,
|
||||
NULL,
|
||||
PROV_RSA_FULL,
|
||||
CRYPT_NEWKEYSET))
|
||||
{
|
||||
printf("CryptoLib: %x\n", unsupported_str, GetLastError());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
#else
|
||||
/* start of with a stack to copy across */
|
||||
// rand_r((unsigned int *)entropy_pool); // GWG
|
||||
int i = rand();
|
||||
memset(entropy_pool, 0, ENTROPY_POOL_SIZE);
|
||||
memcpy(entropy_pool, &i, sizeof(int));
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* If no /dev/urandom, then initialise the RNG with something interesting.
|
||||
*/
|
||||
EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size)
|
||||
{
|
||||
#if defined(WIN32) || defined(CONFIG_WIN32_USE_CRYPTO_LIB)
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ENTROPY_POOL_SIZE && i < size; i++)
|
||||
entropy_pool[i] ^= seed_buf[i];
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate the RNG engine.
|
||||
*/
|
||||
EXP_FUNC void STDCALL RNG_terminate(void)
|
||||
{
|
||||
#ifndef WIN32
|
||||
close(rng_fd);
|
||||
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
|
||||
CryptReleaseContext(gCryptProv, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a series of bytes with a random number. Individual bytes can be 0
|
||||
*/
|
||||
EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
|
||||
{
|
||||
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
|
||||
/* use the Linux default - read from /dev/urandom */
|
||||
if (read(rng_fd, rand_data, num_rand_bytes) < 0)
|
||||
return -1;
|
||||
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
|
||||
/* use Microsoft Crypto Libraries */
|
||||
CryptGenRandom(gCryptProv, num_rand_bytes, rand_data);
|
||||
#else /* nothing else to use, so use a custom RNG */
|
||||
/* The method we use when we've got nothing better. Use RC4, time
|
||||
and a couple of random seeds to generate a random sequence */
|
||||
AES_CTX rng_ctx;
|
||||
struct timeval tv;
|
||||
MD5_CTX rng_digest_ctx;
|
||||
uint8_t digest[MD5_SIZE];
|
||||
uint64_t *ep;
|
||||
int i;
|
||||
|
||||
/* A proper implementation would use counters etc for entropy */
|
||||
gettimeofday(&tv, NULL);
|
||||
ep = (uint64_t *)entropy_pool;
|
||||
ep[0] ^= ENTROPY_COUNTER1;
|
||||
ep[1] ^= ENTROPY_COUNTER2;
|
||||
|
||||
/* use a digested version of the entropy pool as a key */
|
||||
MD5_Init(&rng_digest_ctx);
|
||||
MD5_Update(&rng_digest_ctx, entropy_pool, ENTROPY_POOL_SIZE);
|
||||
MD5_Final(digest, &rng_digest_ctx);
|
||||
|
||||
/* come up with the random sequence */
|
||||
AES_set_key(&rng_ctx, digest, (const uint8_t *)ep, AES_MODE_128); /* use as a key */
|
||||
memcpy(rand_data, entropy_pool, num_rand_bytes < ENTROPY_POOL_SIZE ?
|
||||
num_rand_bytes : ENTROPY_POOL_SIZE);
|
||||
AES_cbc_encrypt(&rng_ctx, rand_data, rand_data, num_rand_bytes);
|
||||
|
||||
/* move things along */
|
||||
for (i = ENTROPY_POOL_SIZE-1; i >= MD5_SIZE ; i--)
|
||||
entropy_pool[i] = entropy_pool[i-MD5_SIZE];
|
||||
|
||||
/* insert the digest at the start of the entropy pool */
|
||||
memcpy(entropy_pool, digest, MD5_SIZE);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a series of bytes with a random number. Individual bytes are not zero.
|
||||
*/
|
||||
int get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
|
||||
{
|
||||
int i;
|
||||
if (get_random(num_rand_bytes, rand_data))
|
||||
return -1;
|
||||
|
||||
for (i = 0; i < num_rand_bytes; i++)
|
||||
{
|
||||
while (rand_data[i] == 0) /* can't be 0 */
|
||||
rand_data[i] = (uint8_t)(rand());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some useful diagnostic routines
|
||||
*/
|
||||
#if defined(CONFIG_DEBUG)
|
||||
int hex_finish;
|
||||
int hex_index;
|
||||
|
||||
static void print_hex_init(int finish)
|
||||
{
|
||||
hex_finish = finish;
|
||||
hex_index = 0;
|
||||
}
|
||||
|
||||
static void print_hex(uint8_t hex)
|
||||
{
|
||||
static int column;
|
||||
|
||||
if (hex_index == 0)
|
||||
{
|
||||
column = 0;
|
||||
}
|
||||
|
||||
printf("%02x ", hex);
|
||||
if (++column == 8)
|
||||
{
|
||||
printf(": ");
|
||||
}
|
||||
else if (column >= 16)
|
||||
{
|
||||
printf("\n");
|
||||
column = 0;
|
||||
}
|
||||
|
||||
if (++hex_index >= hex_finish && column > 0)
|
||||
{
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spit out a blob of data for diagnostics. The data is is a nice column format
|
||||
* for easy reading.
|
||||
*
|
||||
* @param format [in] The string (with possible embedded format characters)
|
||||
* @param size [in] The number of numbers to print
|
||||
* @param data [in] The start of data to use
|
||||
* @param ... [in] Any additional arguments
|
||||
*/
|
||||
EXP_FUNC void STDCALL print_blob(const char *format,
|
||||
const uint8_t *data, int size, ...)
|
||||
{
|
||||
int i;
|
||||
char tmp[80];
|
||||
va_list(ap);
|
||||
|
||||
va_start(ap, size);
|
||||
sprintf(tmp, "%s\n", format);
|
||||
vprintf(tmp, ap);
|
||||
print_hex_init(size);
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
print_hex(data[i]);
|
||||
}
|
||||
|
||||
va_end(ap);
|
||||
TTY_FLUSH();
|
||||
}
|
||||
#else
|
||||
#if defined(WIN32)
|
||||
/* VC6.0 doesn't handle variadic macros */
|
||||
EXP_FUNC void STDCALL print_blob(const char *format, const uint8_t *data,
|
||||
int size, ...) {}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_SSL_HAS_PEM) || defined(CONFIG_HTTP_HAS_AUTHORIZATION)
|
||||
/* base64 to binary lookup table */
|
||||
static const uint8_t map[128] =
|
||||
{
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
|
||||
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
|
||||
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
|
||||
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
|
||||
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
|
||||
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
|
||||
49, 50, 51, 255, 255, 255, 255, 255
|
||||
};
|
||||
|
||||
EXP_FUNC int STDCALL base64_decode(const char *in, int len,
|
||||
uint8_t *out, int *outlen)
|
||||
{
|
||||
int g, t, x, y, z;
|
||||
uint8_t c;
|
||||
int ret = -1;
|
||||
|
||||
g = 3;
|
||||
for (x = y = z = t = 0; x < len; x++)
|
||||
{
|
||||
if ((c = map[in[x]&0x7F]) == 0xff)
|
||||
continue;
|
||||
|
||||
if (c == 254) /* this is the end... */
|
||||
{
|
||||
c = 0;
|
||||
|
||||
if (--g < 0)
|
||||
goto error;
|
||||
}
|
||||
else if (g != 3) /* only allow = at end */
|
||||
goto error;
|
||||
|
||||
t = (t<<6) | c;
|
||||
|
||||
if (++y == 4)
|
||||
{
|
||||
out[z++] = (uint8_t)((t>>16)&255);
|
||||
|
||||
if (g > 1)
|
||||
out[z++] = (uint8_t)((t>>8)&255);
|
||||
|
||||
if (g > 2)
|
||||
out[z++] = (uint8_t)(t&255);
|
||||
|
||||
y = t = 0;
|
||||
}
|
||||
|
||||
/* check that we don't go past the output buffer */
|
||||
if (z > *outlen)
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (y != 0)
|
||||
goto error;
|
||||
|
||||
*outlen = z;
|
||||
ret = 0;
|
||||
|
||||
error:
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (ret < 0)
|
||||
printf("Error: Invalid base64\n"); TTY_FLUSH();
|
||||
#endif
|
||||
TTY_FLUSH();
|
||||
return ret;
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
218
Argyll_V3.5.0/ccast/axTLS/crypto_misc.h
Normal file
218
Argyll_V3.5.0/ccast/axTLS/crypto_misc.h
Normal file
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2017, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file crypto_misc.h
|
||||
*/
|
||||
|
||||
#ifndef HEADER_CRYPTO_MISC_H
|
||||
#define HEADER_CRYPTO_MISC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//#include <stdbool.h>
|
||||
|
||||
#define bool unsigned int
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
#include "crypto.h"
|
||||
#include "bigint.h"
|
||||
|
||||
/**************************************************************************
|
||||
* X509 declarations
|
||||
**************************************************************************/
|
||||
#define X509_OK 0
|
||||
#define X509_NOT_OK -1
|
||||
#define X509_VFY_ERROR_NO_TRUSTED_CERT -2
|
||||
#define X509_VFY_ERROR_BAD_SIGNATURE -3
|
||||
#define X509_VFY_ERROR_NOT_YET_VALID -4
|
||||
#define X509_VFY_ERROR_EXPIRED -5
|
||||
#define X509_VFY_ERROR_SELF_SIGNED -6
|
||||
#define X509_VFY_ERROR_INVALID_CHAIN -7
|
||||
#define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
|
||||
#define X509_INVALID_PRIV_KEY -9
|
||||
#define X509_INVALID_PUB_KEY -10
|
||||
#define X509_MAX_CERTS -11
|
||||
#define X509_VFY_ERROR_BASIC_CONSTRAINT -12
|
||||
|
||||
/*
|
||||
* The Distinguished Name
|
||||
*/
|
||||
#define X509_NUM_DN_TYPES 6
|
||||
#define X509_COMMON_NAME 0
|
||||
#define X509_ORGANIZATION 1
|
||||
#define X509_ORGANIZATIONAL_UNIT 2
|
||||
#define X509_LOCATION 3
|
||||
#define X509_COUNTRY 4
|
||||
#define X509_STATE 5
|
||||
|
||||
/*
|
||||
* Key Usage bits
|
||||
*/
|
||||
#define IS_SET_KEY_USAGE_FLAG(A, B) (A->key_usage & B)
|
||||
|
||||
#define KEY_USAGE_DIGITAL_SIGNATURE 0x0080
|
||||
#define KEY_USAGE_NON_REPUDIATION 0x0040
|
||||
#define KEY_USAGE_KEY_ENCIPHERMENT 0x0020
|
||||
#define KEY_USAGE_DATA_ENCIPHERMENT 0x0010
|
||||
#define KEY_USAGE_KEY_AGREEMENT 0x0008
|
||||
#define KEY_USAGE_KEY_CERT_SIGN 0x0004
|
||||
#define KEY_USAGE_CRL_SIGN 0x0002
|
||||
#define KEY_USAGE_ENCIPHER_ONLY 0x0001
|
||||
#define KEY_USAGE_DECIPHER_ONLY 0x8000
|
||||
|
||||
struct _x509_ctx
|
||||
{
|
||||
char *ca_cert_dn[X509_NUM_DN_TYPES];
|
||||
char *cert_dn[X509_NUM_DN_TYPES];
|
||||
char **subject_alt_dnsnames;
|
||||
time_t not_before;
|
||||
time_t not_after;
|
||||
uint8_t *signature;
|
||||
RSA_CTX *rsa_ctx;
|
||||
bigint *digest;
|
||||
uint16_t sig_len;
|
||||
uint8_t sig_type;
|
||||
bool basic_constraint_present;
|
||||
bool basic_constraint_is_critical;
|
||||
bool key_usage_present;
|
||||
bool key_usage_is_critical;
|
||||
bool subject_alt_name_present;
|
||||
bool subject_alt_name_is_critical;
|
||||
bool basic_constraint_cA;
|
||||
int basic_constraint_pathLenConstraint;
|
||||
uint32_t key_usage;
|
||||
struct _x509_ctx *next;
|
||||
};
|
||||
|
||||
typedef struct _x509_ctx X509_CTX;
|
||||
|
||||
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG)
|
||||
typedef struct
|
||||
{
|
||||
X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS];
|
||||
} CA_CERT_CTX;
|
||||
#endif
|
||||
|
||||
int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx);
|
||||
void x509_free(X509_CTX *x509_ctx);
|
||||
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG)
|
||||
int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert,
|
||||
int *pathLenConstraint);
|
||||
void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx);
|
||||
#endif
|
||||
const char * x509_display_error(int error);
|
||||
|
||||
/**************************************************************************
|
||||
* ASN1 declarations
|
||||
**************************************************************************/
|
||||
#define ASN1_BOOLEAN 0x01
|
||||
#define ASN1_INTEGER 0x02
|
||||
#define ASN1_BIT_STRING 0x03
|
||||
#define ASN1_OCTET_STRING 0x04
|
||||
#define ASN1_NULL 0x05
|
||||
#define ASN1_PRINTABLE_STR2 0x0C
|
||||
#define ASN1_OID 0x06
|
||||
#define ASN1_PRINTABLE_STR2 0x0C
|
||||
#define ASN1_PRINTABLE_STR 0x13
|
||||
#define ASN1_TELETEX_STR 0x14
|
||||
#define ASN1_IA5_STR 0x16
|
||||
#define ASN1_UTC_TIME 0x17
|
||||
#define ASN1_GENERALIZED_TIME 0x18
|
||||
#define ASN1_UNICODE_STR 0x1e
|
||||
#define ASN1_SEQUENCE 0x30
|
||||
#define ASN1_CONTEXT_DNSNAME 0x82
|
||||
#define ASN1_SET 0x31
|
||||
#define ASN1_V3_DATA 0xa3
|
||||
#define ASN1_IMPLICIT_TAG 0x80
|
||||
#define ASN1_CONTEXT_DNSNAME 0x82
|
||||
#define ASN1_EXPLICIT_TAG 0xa0
|
||||
#define ASN1_V3_DATA 0xa3
|
||||
|
||||
#define SIG_TYPE_MD5 0x04
|
||||
#define SIG_TYPE_SHA1 0x05
|
||||
#define SIG_TYPE_SHA256 0x0b
|
||||
#define SIG_TYPE_SHA384 0x0c
|
||||
#define SIG_TYPE_SHA512 0x0d
|
||||
|
||||
uint32_t get_asn1_length(const uint8_t *buf, int *offset);
|
||||
int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx);
|
||||
int asn1_get_public_key(const uint8_t *cert, int *offset, RSA_CTX **rsa_ctx); // GWG
|
||||
int asn1_next_obj(const uint8_t *buf, int *offset, int obj_type);
|
||||
int asn1_skip_obj(const uint8_t *buf, int *offset, int obj_type);
|
||||
int asn1_get_big_int(const uint8_t *buf, int *offset, uint8_t **object);
|
||||
int asn1_get_int(const uint8_t *buf, int *offset, int32_t *val);
|
||||
int asn1_get_bool(const uint8_t *buf, int *offset, bool *val);
|
||||
int asn1_get_bit_string_as_int(const uint8_t *buf, int *offset, uint32_t *val);
|
||||
int asn1_version(const uint8_t *cert, int *offset, int *val);
|
||||
int asn1_validity(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
|
||||
int asn1_name(const uint8_t *cert, int *offset, char *dn[]);
|
||||
int asn1_public_key(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
int asn1_signature(const uint8_t *cert, int *offset, X509_CTX *x509_ctx);
|
||||
int asn1_is_subject_alt_name(const uint8_t *cert, int offset);
|
||||
int asn1_is_basic_constraints(const uint8_t *cert, int offset);
|
||||
int asn1_is_key_usage(const uint8_t *cert, int offset);
|
||||
bool asn1_is_critical_ext(const uint8_t *buf, int *offset);
|
||||
#endif /* CONFIG_SSL_CERT_VERIFICATION */
|
||||
int asn1_compare_dn(char * const dn1[], char * const dn2[]);
|
||||
int asn1_signature_type(const uint8_t *cert,
|
||||
int *offset, X509_CTX *x509_ctx);
|
||||
|
||||
/**************************************************************************
|
||||
* MISC declarations
|
||||
**************************************************************************/
|
||||
#define SALT_SIZE 8
|
||||
|
||||
extern const char * const unsupported_str;
|
||||
|
||||
typedef void (*crypt_func)(void *, const uint8_t *, uint8_t *, int);
|
||||
typedef void (*hmac_func)(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest);
|
||||
|
||||
int get_file(const char *filename, uint8_t **buf);
|
||||
|
||||
#if defined(CONFIG_SSL_FULL_MODE) || defined(WIN32) || defined(CONFIG_DEBUG)
|
||||
EXP_FUNC void STDCALL print_blob(const char *format, const uint8_t *data, int size, ...);
|
||||
#else
|
||||
#define print_blob(...)
|
||||
#endif
|
||||
|
||||
EXP_FUNC int STDCALL base64_decode(const char *in, int len,
|
||||
uint8_t *out, int *outlen);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
139
Argyll_V3.5.0/ccast/axTLS/hmac.c
Normal file
139
Argyll_V3.5.0/ccast/axTLS/hmac.c
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* HMAC implementation - This code was originally taken from RFC2104
|
||||
* See http://www.ietf.org/rfc/rfc2104.txt and
|
||||
* http://www.faqs.org/rfcs/rfc2202.html
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
/**
|
||||
* Perform HMAC-MD5
|
||||
* NOTE: does not handle keys larger than the block size.
|
||||
*/
|
||||
void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest)
|
||||
{
|
||||
MD5_CTX context;
|
||||
uint8_t k_ipad[64];
|
||||
uint8_t k_opad[64];
|
||||
int i;
|
||||
|
||||
memset(k_ipad, 0, sizeof k_ipad);
|
||||
memset(k_opad, 0, sizeof k_opad);
|
||||
memcpy(k_ipad, key, key_len);
|
||||
memcpy(k_opad, key, key_len);
|
||||
|
||||
for (i = 0; i < 64; i++)
|
||||
{
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
MD5_Init(&context);
|
||||
MD5_Update(&context, k_ipad, 64);
|
||||
MD5_Update(&context, msg, length);
|
||||
MD5_Final(digest, &context);
|
||||
MD5_Init(&context);
|
||||
MD5_Update(&context, k_opad, 64);
|
||||
MD5_Update(&context, digest, MD5_SIZE);
|
||||
MD5_Final(digest, &context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform HMAC-SHA1
|
||||
* NOTE: does not handle keys larger than the block size.
|
||||
*/
|
||||
void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest)
|
||||
{
|
||||
SHA1_CTX context;
|
||||
uint8_t k_ipad[64];
|
||||
uint8_t k_opad[64];
|
||||
int i;
|
||||
|
||||
memset(k_ipad, 0, sizeof k_ipad);
|
||||
memset(k_opad, 0, sizeof k_opad);
|
||||
memcpy(k_ipad, key, key_len);
|
||||
memcpy(k_opad, key, key_len);
|
||||
|
||||
for (i = 0; i < 64; i++)
|
||||
{
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
SHA1_Init(&context);
|
||||
SHA1_Update(&context, k_ipad, 64);
|
||||
SHA1_Update(&context, msg, length);
|
||||
SHA1_Final(digest, &context);
|
||||
SHA1_Init(&context);
|
||||
SHA1_Update(&context, k_opad, 64);
|
||||
SHA1_Update(&context, digest, SHA1_SIZE);
|
||||
SHA1_Final(digest, &context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform HMAC-SHA256
|
||||
* NOTE: does not handle keys larger than the block size.
|
||||
*/
|
||||
void hmac_sha256(const uint8_t *msg, int length, const uint8_t *key,
|
||||
int key_len, uint8_t *digest)
|
||||
{
|
||||
SHA256_CTX context;
|
||||
uint8_t k_ipad[64];
|
||||
uint8_t k_opad[64];
|
||||
int i;
|
||||
|
||||
memset(k_ipad, 0, sizeof k_ipad);
|
||||
memset(k_opad, 0, sizeof k_opad);
|
||||
memcpy(k_ipad, key, key_len);
|
||||
memcpy(k_opad, key, key_len);
|
||||
|
||||
for (i = 0; i < 64; i++)
|
||||
{
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
SHA256_Init(&context);
|
||||
SHA256_Update(&context, k_ipad, 64);
|
||||
SHA256_Update(&context, msg, length);
|
||||
SHA256_Final(digest, &context);
|
||||
SHA256_Init(&context);
|
||||
SHA256_Update(&context, k_opad, 64);
|
||||
SHA256_Update(&context, digest, SHA256_SIZE);
|
||||
SHA256_Final(digest, &context);
|
||||
}
|
||||
|
||||
488
Argyll_V3.5.0/ccast/axTLS/loader.c
Normal file
488
Argyll_V3.5.0/ccast/axTLS/loader.c
Normal file
@@ -0,0 +1,488 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2014, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Load certificates/keys into memory. These can be in many different formats.
|
||||
* PEM support and other formats can be processed here.
|
||||
*
|
||||
* The PEM private keys may be optionally encrypted with AES128 or AES256.
|
||||
* The encrypted PEM keys were generated with something like:
|
||||
*
|
||||
* openssl genrsa -aes128 -passout pass:abcd -out axTLS.key_aes128.pem 512
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "os_port.h"
|
||||
#include "ssl.h"
|
||||
|
||||
static int do_obj(SSL_CTX *ssl_ctx, int obj_type,
|
||||
SSLObjLoader *ssl_obj, const char *password);
|
||||
#ifdef CONFIG_SSL_HAS_PEM
|
||||
static int ssl_obj_PEM_load(SSL_CTX *ssl_ctx, int obj_type,
|
||||
SSLObjLoader *ssl_obj, const char *password);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Load a file into memory that is in binary DER (or ascii PEM) format.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type,
|
||||
const char *filename, const char *password)
|
||||
{
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
static const char * const begin = "-----BEGIN";
|
||||
int ret = SSL_OK;
|
||||
SSLObjLoader *ssl_obj = NULL;
|
||||
|
||||
if (filename == NULL)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
goto error;
|
||||
}
|
||||
|
||||
ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader));
|
||||
ssl_obj->len = get_file(filename, &ssl_obj->buf);
|
||||
if (ssl_obj->len <= 0)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* is the file a PEM file? */
|
||||
if (strstr((char *)ssl_obj->buf, begin) != NULL)
|
||||
{
|
||||
#ifdef CONFIG_SSL_HAS_PEM
|
||||
ret = ssl_obj_PEM_load(ssl_ctx, obj_type, ssl_obj, password);
|
||||
#else
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("%s", unsupported_str);
|
||||
#endif
|
||||
ret = SSL_ERROR_NOT_SUPPORTED;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
ret = do_obj(ssl_ctx, obj_type, ssl_obj, password);
|
||||
|
||||
error:
|
||||
ssl_obj_free(ssl_obj);
|
||||
return ret;
|
||||
#else
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("%s", unsupported_str);
|
||||
#endif
|
||||
return SSL_ERROR_NOT_SUPPORTED;
|
||||
#endif /* CONFIG_SSL_SKELETON_MODE */
|
||||
}
|
||||
|
||||
/*
|
||||
* Transfer binary data into the object loader.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_obj_memory_load(SSL_CTX *ssl_ctx, int mem_type,
|
||||
const uint8_t *data, int len, const char *password)
|
||||
{
|
||||
int ret;
|
||||
SSLObjLoader *ssl_obj;
|
||||
|
||||
ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader));
|
||||
ssl_obj->buf = (uint8_t *)malloc(len);
|
||||
memcpy(ssl_obj->buf, data, len);
|
||||
ssl_obj->len = len;
|
||||
ret = do_obj(ssl_ctx, mem_type, ssl_obj, password);
|
||||
ssl_obj_free(ssl_obj);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Actually work out what we are doing
|
||||
*/
|
||||
static int do_obj(SSL_CTX *ssl_ctx, int obj_type,
|
||||
SSLObjLoader *ssl_obj, const char *password)
|
||||
{
|
||||
int ret = SSL_OK;
|
||||
|
||||
switch (obj_type)
|
||||
{
|
||||
case SSL_OBJ_RSA_KEY:
|
||||
ret = add_private_key(ssl_ctx, ssl_obj);
|
||||
break;
|
||||
|
||||
case SSL_OBJ_X509_CERT:
|
||||
ret = add_cert(ssl_ctx, ssl_obj->buf, ssl_obj->len);
|
||||
break;
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
case SSL_OBJ_X509_CACERT:
|
||||
add_cert_auth(ssl_ctx, ssl_obj->buf, ssl_obj->len);
|
||||
break;
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_SSL_USE_PKCS12
|
||||
case SSL_OBJ_PKCS8:
|
||||
ret = pkcs8_decode(ssl_ctx, ssl_obj, password);
|
||||
break;
|
||||
|
||||
case SSL_OBJ_PKCS12:
|
||||
ret = pkcs12_decode(ssl_ctx, ssl_obj, password);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("%s", unsupported_str);
|
||||
#endif
|
||||
ret = SSL_ERROR_NOT_SUPPORTED;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up our mess.
|
||||
*/
|
||||
void ssl_obj_free(SSLObjLoader *ssl_obj)
|
||||
{
|
||||
if (ssl_obj)
|
||||
{
|
||||
free(ssl_obj->buf);
|
||||
free(ssl_obj);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Support for PEM encoded keys/certificates.
|
||||
*/
|
||||
#ifdef CONFIG_SSL_HAS_PEM
|
||||
|
||||
#define NUM_PEM_TYPES 4
|
||||
#define IV_SIZE 16
|
||||
#define IS_RSA_PRIVATE_KEY 0
|
||||
#define IS_ENCRYPTED_PRIVATE_KEY 1
|
||||
#define IS_PRIVATE_KEY 2
|
||||
#define IS_CERTIFICATE 3
|
||||
|
||||
static const char * const begins[NUM_PEM_TYPES] =
|
||||
{
|
||||
"-----BEGIN RSA PRIVATE KEY-----",
|
||||
"-----BEGIN ENCRYPTED PRIVATE KEY-----",
|
||||
"-----BEGIN PRIVATE KEY-----",
|
||||
"-----BEGIN CERTIFICATE-----",
|
||||
};
|
||||
|
||||
static const char * const ends[NUM_PEM_TYPES] =
|
||||
{
|
||||
"-----END RSA PRIVATE KEY-----",
|
||||
"-----END ENCRYPTED PRIVATE KEY-----",
|
||||
"-----END PRIVATE KEY-----",
|
||||
"-----END CERTIFICATE-----",
|
||||
};
|
||||
|
||||
static const char * const aes_str[2] =
|
||||
{
|
||||
"DEK-Info: AES-128-CBC,",
|
||||
"DEK-Info: AES-256-CBC,"
|
||||
};
|
||||
|
||||
/**
|
||||
* Take a base64 blob of data and decrypt it (using AES) into its
|
||||
* proper ASN.1 form.
|
||||
*/
|
||||
static int pem_decrypt(const char *where, const char *end,
|
||||
const char *password, SSLObjLoader *ssl_obj)
|
||||
{
|
||||
int ret = -1;
|
||||
int is_aes_256 = 0;
|
||||
char *start = NULL;
|
||||
uint8_t iv[IV_SIZE];
|
||||
int i, pem_size;
|
||||
MD5_CTX md5_ctx;
|
||||
AES_CTX aes_ctx;
|
||||
uint8_t key[32]; /* AES256 size */
|
||||
|
||||
if (password == NULL || strlen(password) == 0)
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: Need a password for this PEM file\n");
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
|
||||
if ((start = strstr((const char *)where, aes_str[0]))) /* AES128? */
|
||||
{
|
||||
start += strlen(aes_str[0]);
|
||||
}
|
||||
else if ((start = strstr((const char *)where, aes_str[1]))) /* AES256? */
|
||||
{
|
||||
is_aes_256 = 1;
|
||||
start += strlen(aes_str[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: Unsupported password cipher\n");
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* convert from hex to binary - assumes uppercase hex */
|
||||
for (i = 0; i < IV_SIZE; i++)
|
||||
{
|
||||
char c = *start++ - '0';
|
||||
iv[i] = (c > 9 ? c + '0' - 'A' + 10 : c) << 4;
|
||||
c = *start++ - '0';
|
||||
iv[i] += (c > 9 ? c + '0' - 'A' + 10 : c);
|
||||
}
|
||||
|
||||
while (*start == '\r' || *start == '\n')
|
||||
start++;
|
||||
|
||||
/* turn base64 into binary */
|
||||
pem_size = (int)(end-start);
|
||||
if (base64_decode(start, pem_size, ssl_obj->buf, &ssl_obj->len) != 0)
|
||||
goto error;
|
||||
|
||||
/* work out the key */
|
||||
MD5_Init(&md5_ctx);
|
||||
MD5_Update(&md5_ctx, (const uint8_t *)password, strlen(password));
|
||||
MD5_Update(&md5_ctx, iv, SALT_SIZE);
|
||||
MD5_Final(key, &md5_ctx);
|
||||
|
||||
if (is_aes_256)
|
||||
{
|
||||
MD5_Init(&md5_ctx);
|
||||
MD5_Update(&md5_ctx, key, MD5_SIZE);
|
||||
MD5_Update(&md5_ctx, (const uint8_t *)password, strlen(password));
|
||||
MD5_Update(&md5_ctx, iv, SALT_SIZE);
|
||||
MD5_Final(&key[MD5_SIZE], &md5_ctx);
|
||||
}
|
||||
|
||||
/* decrypt using the key/iv */
|
||||
AES_set_key(&aes_ctx, key, iv, is_aes_256 ? AES_MODE_256 : AES_MODE_128);
|
||||
AES_convert_key(&aes_ctx);
|
||||
AES_cbc_decrypt(&aes_ctx, ssl_obj->buf, ssl_obj->buf, ssl_obj->len);
|
||||
ret = 0;
|
||||
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a base64 blob of data and turn it into its proper ASN.1 form.
|
||||
*/
|
||||
static int new_pem_obj(SSL_CTX *ssl_ctx, int is_cacert, char *where,
|
||||
int remain, const char *password)
|
||||
{
|
||||
int ret = SSL_ERROR_BAD_CERTIFICATE;
|
||||
SSLObjLoader *ssl_obj = NULL;
|
||||
|
||||
while (remain > 0)
|
||||
{
|
||||
int i, pem_size, obj_type;
|
||||
char *start = NULL, *end = NULL;
|
||||
|
||||
for (i = 0; i < NUM_PEM_TYPES; i++)
|
||||
{
|
||||
if ((start = strstr(where, begins[i])) &&
|
||||
(end = strstr(where, ends[i])))
|
||||
{
|
||||
remain -= (int)(end-where);
|
||||
start += strlen(begins[i]);
|
||||
pem_size = (int)(end-start);
|
||||
|
||||
ssl_obj = (SSLObjLoader *)calloc(1, sizeof(SSLObjLoader));
|
||||
|
||||
/* 4/3 bigger than what we need but so what */
|
||||
ssl_obj->buf = (uint8_t *)calloc(1, pem_size);
|
||||
ssl_obj->len = pem_size;
|
||||
|
||||
if (i == IS_RSA_PRIVATE_KEY &&
|
||||
strstr(start, "Proc-Type:") &&
|
||||
strstr(start, "4,ENCRYPTED"))
|
||||
{
|
||||
/* check for encrypted PEM file */
|
||||
if (pem_decrypt(start, end, password, ssl_obj) < 0)
|
||||
{
|
||||
ret = SSL_ERROR_BAD_CERTIFICATE;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ssl_obj->len = pem_size;
|
||||
if (base64_decode(start, pem_size,
|
||||
ssl_obj->buf, &ssl_obj->len) != 0)
|
||||
{
|
||||
ret = SSL_ERROR_BAD_CERTIFICATE;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case IS_RSA_PRIVATE_KEY:
|
||||
obj_type = SSL_OBJ_RSA_KEY;
|
||||
break;
|
||||
|
||||
case IS_ENCRYPTED_PRIVATE_KEY:
|
||||
case IS_PRIVATE_KEY:
|
||||
obj_type = SSL_OBJ_PKCS8;
|
||||
break;
|
||||
|
||||
case IS_CERTIFICATE:
|
||||
obj_type = is_cacert ?
|
||||
SSL_OBJ_X509_CACERT : SSL_OBJ_X509_CERT;
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = SSL_ERROR_BAD_CERTIFICATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* In a format we can now understand - so process it */
|
||||
if ((ret = do_obj(ssl_ctx, obj_type, ssl_obj, password)))
|
||||
goto error;
|
||||
|
||||
end += strlen(ends[i]);
|
||||
remain -= strlen(ends[i]);
|
||||
while (remain > 0 && (*end == '\r' || *end == '\n'))
|
||||
{
|
||||
end++;
|
||||
remain--;
|
||||
}
|
||||
|
||||
where = end;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ssl_obj_free(ssl_obj);
|
||||
ssl_obj = NULL;
|
||||
if (start == NULL)
|
||||
break;
|
||||
}
|
||||
error:
|
||||
ssl_obj_free(ssl_obj);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Load a file into memory that is in ASCII PEM format.
|
||||
*/
|
||||
static int ssl_obj_PEM_load(SSL_CTX *ssl_ctx, int obj_type,
|
||||
SSLObjLoader *ssl_obj, const char *password)
|
||||
{
|
||||
char *start;
|
||||
|
||||
/* add a null terminator */
|
||||
ssl_obj->len++;
|
||||
ssl_obj->buf = (uint8_t *)realloc(ssl_obj->buf, ssl_obj->len);
|
||||
ssl_obj->buf[ssl_obj->len-1] = 0;
|
||||
start = (char *)ssl_obj->buf;
|
||||
return new_pem_obj(ssl_ctx, obj_type == SSL_OBJ_X509_CACERT,
|
||||
start, ssl_obj->len, password);
|
||||
}
|
||||
#endif /* CONFIG_SSL_HAS_PEM */
|
||||
|
||||
/**
|
||||
* Load the key/certificates in memory depending on compile-time and user
|
||||
* options.
|
||||
*/
|
||||
int load_key_certs(SSL_CTX *ssl_ctx)
|
||||
{
|
||||
int ret = SSL_OK;
|
||||
uint32_t options = ssl_ctx->options;
|
||||
#ifdef CONFIG_SSL_GENERATE_X509_CERT
|
||||
uint8_t *cert_data = NULL;
|
||||
int cert_size;
|
||||
static const char *dn[] =
|
||||
{
|
||||
CONFIG_SSL_X509_COMMON_NAME,
|
||||
CONFIG_SSL_X509_ORGANIZATION_NAME,
|
||||
CONFIG_SSL_X509_ORGANIZATION_UNIT_NAME
|
||||
};
|
||||
#endif
|
||||
|
||||
/* do the private key first */
|
||||
if (strlen(CONFIG_SSL_PRIVATE_KEY_LOCATION) > 0)
|
||||
{
|
||||
if ((ret = ssl_obj_load(ssl_ctx, SSL_OBJ_RSA_KEY,
|
||||
CONFIG_SSL_PRIVATE_KEY_LOCATION,
|
||||
CONFIG_SSL_PRIVATE_KEY_PASSWORD)) < 0)
|
||||
goto error;
|
||||
}
|
||||
else if (!(options & SSL_NO_DEFAULT_KEY))
|
||||
{
|
||||
#if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE)
|
||||
static const /* saves a few more bytes */
|
||||
#include "private_key.h"
|
||||
|
||||
ssl_obj_memory_load(ssl_ctx, SSL_OBJ_RSA_KEY, default_private_key,
|
||||
default_private_key_len, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* now load the certificate */
|
||||
#ifdef CONFIG_SSL_GENERATE_X509_CERT
|
||||
if ((cert_size = ssl_x509_create(ssl_ctx, 0, dn, &cert_data)) < 0)
|
||||
{
|
||||
ret = cert_size;
|
||||
goto error;
|
||||
}
|
||||
|
||||
ssl_obj_memory_load(ssl_ctx, SSL_OBJ_X509_CERT, cert_data, cert_size, NULL);
|
||||
free(cert_data);
|
||||
#else
|
||||
if (strlen(CONFIG_SSL_X509_CERT_LOCATION))
|
||||
{
|
||||
if ((ret = ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT,
|
||||
CONFIG_SSL_X509_CERT_LOCATION, NULL)) < 0)
|
||||
goto error;
|
||||
}
|
||||
else if (!(options & SSL_NO_DEFAULT_KEY))
|
||||
{
|
||||
#if defined(CONFIG_SSL_USE_DEFAULT_KEY) || defined(CONFIG_SSL_SKELETON_MODE)
|
||||
#include "cert.h"
|
||||
ssl_obj_memory_load(ssl_ctx, SSL_OBJ_X509_CERT,
|
||||
default_certificate, default_certificate_len, NULL);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
error:
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (ret)
|
||||
{
|
||||
printf("Error: Certificate or key not loaded\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
162
Argyll_V3.5.0/ccast/axTLS/md2.c
Normal file
162
Argyll_V3.5.0/ccast/axTLS/md2.c
Normal file
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* RFC 1115/1319 compliant MD2 implementation
|
||||
* The MD2 algorithm was designed by Ron Rivest in 1989.
|
||||
*
|
||||
* http://www.ietf.org/rfc/rfc1115.txt
|
||||
* http://www.ietf.org/rfc/rfc1319.txt
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
/**
|
||||
* This code is only here to enable the verification of Verisign root
|
||||
* certificates. So only enable it for verification mode.
|
||||
*/
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
|
||||
static const uint8_t PI_SUBST[256] =
|
||||
{
|
||||
0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36,
|
||||
0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3,
|
||||
0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C,
|
||||
0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16,
|
||||
0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 0xBE, 0x4E,
|
||||
0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E,
|
||||
0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2,
|
||||
0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21,
|
||||
0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E,
|
||||
0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 0xFF, 0x19, 0x30, 0xB3,
|
||||
0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56,
|
||||
0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6,
|
||||
0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D,
|
||||
0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65,
|
||||
0xE6, 0x2D, 0xA8, 0x02, 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0,
|
||||
0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F,
|
||||
0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C,
|
||||
0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E,
|
||||
0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81,
|
||||
0x4D, 0x52, 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA,
|
||||
0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88,
|
||||
0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE,
|
||||
0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58,
|
||||
0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A,
|
||||
0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99,
|
||||
0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14
|
||||
};
|
||||
|
||||
/*
|
||||
* MD2 context setup
|
||||
*/
|
||||
EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof *ctx);
|
||||
}
|
||||
|
||||
static void md2_process(MD2_CTX *ctx)
|
||||
{
|
||||
int i, j;
|
||||
uint8_t t = 0;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
ctx->state[i + 16] = ctx->buffer[i];
|
||||
ctx->state[i + 32] = ctx->buffer[i] ^ ctx->state[i];
|
||||
}
|
||||
|
||||
for (i = 0; i < 18; i++)
|
||||
{
|
||||
for (j = 0; j < 48; j++)
|
||||
t = (ctx->state[j] ^= PI_SUBST[t]);
|
||||
|
||||
t = (t + i) & 0xFF;
|
||||
}
|
||||
|
||||
t = ctx->cksum[15];
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
t = (ctx->cksum[i] ^= PI_SUBST[ctx->buffer[i] ^ t]);
|
||||
}
|
||||
|
||||
/*
|
||||
* MD2 process buffer
|
||||
*/
|
||||
EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen)
|
||||
{
|
||||
int fill;
|
||||
|
||||
while (ilen > 0)
|
||||
{
|
||||
if (ctx->left + ilen > 16)
|
||||
fill = 16 - ctx->left;
|
||||
else
|
||||
fill = ilen;
|
||||
|
||||
memcpy(ctx->buffer + ctx->left, input, fill);
|
||||
|
||||
ctx->left += fill;
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
|
||||
if (ctx->left == 16)
|
||||
{
|
||||
ctx->left = 0;
|
||||
md2_process(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* MD2 final digest
|
||||
*/
|
||||
EXP_FUNC void STDCALL MD2_Final(uint8_t *output, MD2_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
uint8_t x;
|
||||
|
||||
x = (uint8_t)(16 - ctx->left);
|
||||
|
||||
for (i = ctx->left; i < 16; i++)
|
||||
ctx->buffer[i] = x;
|
||||
|
||||
md2_process(ctx);
|
||||
|
||||
memcpy(ctx->buffer, ctx->cksum, 16);
|
||||
md2_process(ctx);
|
||||
|
||||
memcpy(output, ctx->state, 16);
|
||||
}
|
||||
|
||||
#endif
|
||||
294
Argyll_V3.5.0/ccast/axTLS/md5.c
Normal file
294
Argyll_V3.5.0/ccast/axTLS/md5.c
Normal file
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This file implements the MD5 algorithm as defined in RFC1321
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
/* Constants for MD5Transform routine.
|
||||
*/
|
||||
#define S11 7
|
||||
#define S12 12
|
||||
#define S13 17
|
||||
#define S14 22
|
||||
#define S21 5
|
||||
#define S22 9
|
||||
#define S23 14
|
||||
#define S24 20
|
||||
#define S31 4
|
||||
#define S32 11
|
||||
#define S33 16
|
||||
#define S34 23
|
||||
#define S41 6
|
||||
#define S42 10
|
||||
#define S43 15
|
||||
#define S44 21
|
||||
|
||||
/* ----- static functions ----- */
|
||||
static void MD5Transform(uint32_t state[4], const uint8_t block[64]);
|
||||
static void Encode(uint8_t *output, uint32_t *input, uint32_t len);
|
||||
static void Decode(uint32_t *output, const uint8_t *input, uint32_t len);
|
||||
|
||||
static const uint8_t PADDING[64] =
|
||||
{
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/* F, G, H and I are basic MD5 functions.
|
||||
*/
|
||||
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
||||
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
||||
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
||||
#define I(x, y, z) ((y) ^ ((x) | (~z)))
|
||||
|
||||
/* ROTATE_LEFT rotates x left n bits. */
|
||||
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
||||
|
||||
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
||||
Rotation is separate from addition to prevent recomputation. */
|
||||
#define FF(a, b, c, d, x, s, ac) { \
|
||||
(a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
#define GG(a, b, c, d, x, s, ac) { \
|
||||
(a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
#define HH(a, b, c, d, x, s, ac) { \
|
||||
(a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
#define II(a, b, c, d, x, s, ac) { \
|
||||
(a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
|
||||
(a) = ROTATE_LEFT ((a), (s)); \
|
||||
(a) += (b); \
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5 initialization - begins an MD5 operation, writing a new ctx.
|
||||
*/
|
||||
EXP_FUNC void STDCALL MD5_Init(MD5_CTX *ctx)
|
||||
{
|
||||
ctx->count[0] = ctx->count[1] = 0;
|
||||
|
||||
/* Load magic initialization constants.
|
||||
*/
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xefcdab89;
|
||||
ctx->state[2] = 0x98badcfe;
|
||||
ctx->state[3] = 0x10325476;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an array of octets as the next portion of the message.
|
||||
*/
|
||||
EXP_FUNC void STDCALL MD5_Update(MD5_CTX *ctx, const uint8_t * msg, int len)
|
||||
{
|
||||
uint32_t x;
|
||||
int i, partLen;
|
||||
|
||||
/* Compute number of bytes mod 64 */
|
||||
x = (uint32_t)((ctx->count[0] >> 3) & 0x3F);
|
||||
|
||||
/* Update number of bits */
|
||||
if ((ctx->count[0] += ((uint32_t)len << 3)) < ((uint32_t)len << 3))
|
||||
ctx->count[1]++;
|
||||
ctx->count[1] += ((uint32_t)len >> 29);
|
||||
|
||||
partLen = 64 - x;
|
||||
|
||||
/* Transform as many times as possible. */
|
||||
if (len >= partLen)
|
||||
{
|
||||
memcpy(&ctx->buffer[x], msg, partLen);
|
||||
MD5Transform(ctx->state, ctx->buffer);
|
||||
|
||||
for (i = partLen; i + 63 < len; i += 64)
|
||||
MD5Transform(ctx->state, &msg[i]);
|
||||
|
||||
x = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
|
||||
/* Buffer remaining input */
|
||||
memcpy(&ctx->buffer[x], &msg[i], len-i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 128-bit message digest into the user's array
|
||||
*/
|
||||
EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *ctx)
|
||||
{
|
||||
uint8_t bits[8];
|
||||
uint32_t x, padLen;
|
||||
|
||||
/* Save number of bits */
|
||||
Encode(bits, ctx->count, 8);
|
||||
|
||||
/* Pad out to 56 mod 64.
|
||||
*/
|
||||
x = (uint32_t)((ctx->count[0] >> 3) & 0x3f);
|
||||
padLen = (x < 56) ? (56 - x) : (120 - x);
|
||||
MD5_Update(ctx, PADDING, padLen);
|
||||
|
||||
/* Append length (before padding) */
|
||||
MD5_Update(ctx, bits, 8);
|
||||
|
||||
/* Store state in digest */
|
||||
Encode(digest, ctx->state, MD5_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5 basic transformation. Transforms state based on block.
|
||||
*/
|
||||
static void MD5Transform(uint32_t state[4], const uint8_t block[64])
|
||||
{
|
||||
uint32_t a = state[0], b = state[1], c = state[2],
|
||||
d = state[3], x[MD5_SIZE];
|
||||
|
||||
Decode(x, block, 64);
|
||||
|
||||
/* Round 1 */
|
||||
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
||||
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
||||
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
||||
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
||||
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
||||
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
||||
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
||||
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
||||
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
||||
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
||||
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
||||
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
||||
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
||||
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
||||
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
||||
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
||||
|
||||
/* Round 2 */
|
||||
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
||||
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
||||
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
||||
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
||||
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
||||
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
||||
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
||||
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
||||
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
||||
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
||||
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
||||
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
||||
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
||||
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
||||
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
||||
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
||||
|
||||
/* Round 3 */
|
||||
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
||||
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
||||
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
||||
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
||||
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
||||
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
||||
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
||||
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
||||
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
||||
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
||||
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
||||
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
||||
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
||||
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
||||
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
||||
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
||||
|
||||
/* Round 4 */
|
||||
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
||||
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
||||
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
||||
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
||||
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
||||
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
||||
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
||||
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
||||
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
||||
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
||||
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
||||
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
||||
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
||||
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
||||
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
||||
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
||||
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes input (uint32_t) into output (uint8_t). Assumes len is
|
||||
* a multiple of 4.
|
||||
*/
|
||||
static void Encode(uint8_t *output, uint32_t *input, uint32_t len)
|
||||
{
|
||||
uint32_t i, j;
|
||||
|
||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||
{
|
||||
output[j] = (uint8_t)(input[i] & 0xff);
|
||||
output[j+1] = (uint8_t)((input[i] >> 8) & 0xff);
|
||||
output[j+2] = (uint8_t)((input[i] >> 16) & 0xff);
|
||||
output[j+3] = (uint8_t)((input[i] >> 24) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes input (uint8_t) into output (uint32_t). Assumes len is
|
||||
* a multiple of 4.
|
||||
*/
|
||||
static void Decode(uint32_t *output, const uint8_t *input, uint32_t len)
|
||||
{
|
||||
uint32_t i, j;
|
||||
|
||||
for (i = 0, j = 0; j < len; i++, j += 4)
|
||||
output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
|
||||
(((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
|
||||
}
|
||||
81
Argyll_V3.5.0/ccast/axTLS/os_int.h
Normal file
81
Argyll_V3.5.0/ccast/axTLS/os_int.h
Normal file
@@ -0,0 +1,81 @@
|
||||
|
||||
/* Ensure a consistent bit size */
|
||||
|
||||
/*************************************************************************
|
||||
Copyright 2014 Graeme W. Gill
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef HEADER_OS_INT_H
|
||||
#define HEADER_OS_INT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if (__STDC_VERSION__ >= 199901L) /* C99 */ \
|
||||
|| defined(_STDINT_H_) || defined(_STDINT_H) \
|
||||
|| defined(_SYS_TYPES_H) \
|
||||
|| (defined(__APPLE__) && defined(__POWERPC__))
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define CF64PREC(NNN) NNN##LL /* 64 bit Constant precision specifier */
|
||||
|
||||
#else /* !__STDC_VERSION__ */
|
||||
#ifdef _MSC_VER
|
||||
|
||||
typedef __int8 int8_t;
|
||||
typedef __int16 int16_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
|
||||
#define CF64PREC(NNN) NNN##i64 /* 64 bit Constant precision specifier */
|
||||
|
||||
#else /* !_MSC_VER */
|
||||
|
||||
/* The following works on a lot of modern systems, including */
|
||||
/* LLP64 and LP64 models, but won't work with ILP64 which needs int32 */
|
||||
|
||||
typedef signed char int8_t;
|
||||
typedef signed short int16_t;
|
||||
typedef signed int int32_t;
|
||||
typedef long long int64_t;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
|
||||
#define CF64PREC(NNN) NNN##LL /* 64 bit Constant precision specifier */
|
||||
|
||||
#endif /* !_MSC_VER */
|
||||
#endif /* !__STDC_VERSION__ */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
95
Argyll_V3.5.0/ccast/axTLS/os_port.c
Normal file
95
Argyll_V3.5.0/ccast/axTLS/os_port.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file os_port.c
|
||||
*
|
||||
* OS specific functions.
|
||||
*/
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include "os_port.h"
|
||||
|
||||
#ifdef WIN32
|
||||
/**
|
||||
* gettimeofday() not in Win32
|
||||
*/
|
||||
EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
|
||||
{
|
||||
#if defined(_WIN32_WCE)
|
||||
t->tv_sec = time(NULL);
|
||||
t->tv_usec = 0; /* 1sec precision only */
|
||||
#else
|
||||
struct _timeb timebuffer;
|
||||
_ftime(&timebuffer);
|
||||
t->tv_sec = (long)timebuffer.time;
|
||||
t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef __GNUC__ /* But is in MingW */
|
||||
/**
|
||||
* strcasecmp() not in Win32
|
||||
*/
|
||||
EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
|
||||
{
|
||||
while (tolower(*s1) == tolower(*s2++))
|
||||
{
|
||||
if (*s1++ == '\0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
|
||||
}
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
|
||||
{
|
||||
HKEY hKey;
|
||||
unsigned long datatype;
|
||||
unsigned long bufferlength = buf_size;
|
||||
|
||||
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
|
||||
TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
|
||||
0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
|
||||
return -1;
|
||||
|
||||
RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
|
||||
RegCloseKey(hKey);
|
||||
return 0;
|
||||
}
|
||||
#else /* ! WIN32 */
|
||||
char *os_port_placeholder = "os_port"; /* Supress warning of empty .o file... */
|
||||
#endif /* !WIN32 */
|
||||
|
||||
202
Argyll_V3.5.0/ccast/axTLS/os_port.h
Normal file
202
Argyll_V3.5.0/ccast/axTLS/os_port.h
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file os_port.h
|
||||
*
|
||||
* Some stuff to minimise the differences between windows and linux/unix
|
||||
*/
|
||||
|
||||
#ifndef HEADER_OS_PORT_H
|
||||
#define HEADER_OS_PORT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
//#include "os_int.h" // GWG
|
||||
#include "axTLS_config.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef STDCALL // GWG
|
||||
# define STDCALL
|
||||
#endif
|
||||
#ifndef EXP_FUNC
|
||||
# define EXP_FUNC
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
#undef WIN32
|
||||
#define WIN32
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
/* Windows CE stuff */
|
||||
#if defined(_WIN32_WCE)
|
||||
#include <basetsd.h>
|
||||
#define abort() exit(1)
|
||||
#else
|
||||
#include <io.h>
|
||||
#include <process.h>
|
||||
#include <sys/timeb.h>
|
||||
#include <fcntl.h>
|
||||
#endif /* _WIN32_WCE */
|
||||
|
||||
#include <winsock2.h> // GWG
|
||||
#include <direct.h>
|
||||
#undef getpid
|
||||
#undef open
|
||||
#undef close
|
||||
#undef sleep
|
||||
#undef gettimeofday
|
||||
#undef dup2
|
||||
#undef unlink
|
||||
|
||||
#define SOCKET_READ(A,B,C) recv(A,B,C,0)
|
||||
#define SOCKET_WRITE(A,B,C) send(A,B,C,0)
|
||||
#define SOCKET_CLOSE(A) closesocket(A)
|
||||
#define srandom(A) srand(A)
|
||||
#define random() rand()
|
||||
#define getpid() _getpid()
|
||||
#define snprintf _snprintf
|
||||
#define open(A,B) _open(A,B)
|
||||
#define dup2(A,B) _dup2(A,B)
|
||||
#define unlink(A) _unlink(A)
|
||||
#define close(A) _close(A)
|
||||
#define read(A,B,C) _read(A,B,C)
|
||||
#define write(A,B,C) _write(A,B,C)
|
||||
#define sleep(A) Sleep(A*1000)
|
||||
#define usleep(A) Sleep(A/1000)
|
||||
#define strdup(A) _strdup(A)
|
||||
#define chroot(A) _chdir(A)
|
||||
#define chdir(A) _chdir(A)
|
||||
//#define alloca(A) _alloca(A) // GWG
|
||||
#ifndef lseek
|
||||
#define lseek(A,B,C) _lseek(A,B,C)
|
||||
#endif
|
||||
|
||||
#ifndef __GNUC__
|
||||
# define be64toh(x) _byteswap_uint64(x)
|
||||
#else
|
||||
# define be64toh(x) __builtin_bswap64(x)
|
||||
#endif
|
||||
|
||||
/* This fix gets around a problem where a win32 application on a cygwin xterm
|
||||
doesn't display regular output (until a certain buffer limit) - but it works
|
||||
fine under a normal DOS window. This is a hack to get around the issue -
|
||||
see http://www.khngai.com/emacs/tty.php */
|
||||
#define TTY_FLUSH() if (!_isatty(_fileno(stdout))) fflush(stdout);
|
||||
|
||||
/*
|
||||
* automatically build some library dependencies.
|
||||
*/
|
||||
#pragma comment(lib, "WS2_32.lib")
|
||||
#pragma comment(lib, "AdvAPI32.lib")
|
||||
|
||||
typedef int socklen_t;
|
||||
|
||||
EXP_FUNC void STDCALL gettimeofday(struct timeval* t,void* timezone);
|
||||
#ifndef __GNUC__
|
||||
EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2);
|
||||
#endif
|
||||
EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
|
||||
|
||||
#else /* Not Win32 */
|
||||
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <netdb.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#if defined(__APPLE__)
|
||||
# include <libkern/OSByteOrder.h>
|
||||
# define be64toh(x) OSSwapBigToHostInt64(x)
|
||||
#else
|
||||
# include <asm/byteorder.h>
|
||||
# ifndef be64toh
|
||||
# define be64toh(x) __be64_to_cpu(x)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define SOCKET_READ(A,B,C) read(A,B,C)
|
||||
#define SOCKET_WRITE(A,B,C) write(A,B,C)
|
||||
#define SOCKET_CLOSE(A) if (A >= 0) close(A)
|
||||
#define TTY_FLUSH()
|
||||
|
||||
|
||||
#endif /* Not Win32 */
|
||||
|
||||
#include "os_int.h" // GWG
|
||||
|
||||
/* some functions to mutate the way these work */
|
||||
EXP_FUNC int STDCALL ax_open(const char *pathname, int flags);
|
||||
|
||||
#ifdef CONFIG_PLATFORM_LINUX
|
||||
void exit_now(const char *format, ...) __attribute((noreturn));
|
||||
#else
|
||||
void exit_now(const char *format, ...);
|
||||
#endif
|
||||
|
||||
/* Mutexing definitions */
|
||||
#if defined(CONFIG_SSL_CTX_MUTEXING)
|
||||
#if defined(WIN32)
|
||||
#define SSL_CTX_MUTEX_TYPE HANDLE
|
||||
#define SSL_CTX_MUTEX_INIT(A) A=CreateMutex(0, FALSE, 0)
|
||||
#define SSL_CTX_MUTEX_DESTROY(A) CloseHandle(A)
|
||||
#define SSL_CTX_LOCK(A) WaitForSingleObject(A, INFINITE)
|
||||
#define SSL_CTX_UNLOCK(A) ReleaseMutex(A)
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#define SSL_CTX_MUTEX_TYPE pthread_mutex_t
|
||||
#define SSL_CTX_MUTEX_INIT(A) pthread_mutex_init(&A, NULL)
|
||||
#define SSL_CTX_MUTEX_DESTROY(A) pthread_mutex_destroy(&A)
|
||||
#define SSL_CTX_LOCK(A) pthread_mutex_lock(&A)
|
||||
#define SSL_CTX_UNLOCK(A) pthread_mutex_unlock(&A)
|
||||
#endif
|
||||
#else /* no mutexing */
|
||||
#define SSL_CTX_MUTEX_INIT(A)
|
||||
#define SSL_CTX_MUTEX_DESTROY(A)
|
||||
#define SSL_CTX_LOCK(A)
|
||||
#define SSL_CTX_UNLOCK(A)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
483
Argyll_V3.5.0/ccast/axTLS/p12.c
Normal file
483
Argyll_V3.5.0/ccast/axTLS/p12.c
Normal file
@@ -0,0 +1,483 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Process PKCS#8/PKCS#12 keys.
|
||||
*
|
||||
* The decoding of a PKCS#12 key is fairly specific - this code was tested on a
|
||||
* key generated with:
|
||||
*
|
||||
* openssl pkcs12 -export -in axTLS.x509_1024.pem -inkey axTLS.key_1024.pem
|
||||
* -keypbe PBE-SHA1-RC4-128 -certpbe PBE-SHA1-RC4-128
|
||||
* -name "p12_withoutCA" -out axTLS.withoutCA.p12 -password pass:abcd
|
||||
*
|
||||
* or with a certificate chain:
|
||||
*
|
||||
* openssl pkcs12 -export -in axTLS.x509_1024.pem -inkey axTLS.key_1024.pem
|
||||
* -certfile axTLS.ca_x509.pem -keypbe PBE-SHA1-RC4-128 -certpbe
|
||||
* PBE-SHA1-RC4-128 -name "p12_withCA" -out axTLS.withCA.p12 -password pass:abcd
|
||||
*
|
||||
* Note that the PBE has to be specified with PBE-SHA1-RC4-128. The
|
||||
* private/public keys/certs have to use RSA encryption. Both the integrity
|
||||
* and privacy passwords are the same.
|
||||
*
|
||||
* The PKCS#8 files were generated with something like:
|
||||
*
|
||||
* PEM format:
|
||||
* openssl pkcs8 -in axTLS.key_512.pem -passout pass:abcd -topk8 -v1
|
||||
* PBE-SHA1-RC4-128 -out axTLS.encrypted_pem.p8
|
||||
*
|
||||
* DER format:
|
||||
* openssl pkcs8 -in axTLS.key_512.pem -passout pass:abcd -topk8 -outform DER
|
||||
* -v1 PBE-SHA1-RC4-128 -out axTLS.encrypted.p8
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "os_port.h"
|
||||
#include "ssl.h"
|
||||
|
||||
/* all commented out if not used */
|
||||
#ifdef CONFIG_SSL_USE_PKCS12
|
||||
|
||||
#define BLOCK_SIZE 64
|
||||
#define PKCS12_KEY_ID 1
|
||||
#define PKCS12_IV_ID 2
|
||||
#define PKCS12_MAC_ID 3
|
||||
|
||||
static char *make_uni_pass(const char *password, int *uni_pass_len);
|
||||
static int p8_decrypt(const char *uni_pass, int uni_pass_len,
|
||||
const uint8_t *salt, int iter,
|
||||
uint8_t *priv_key, int priv_key_len, int id);
|
||||
static int p8_add_key(SSL_CTX *ssl_ctx, uint8_t *priv_key);
|
||||
static int get_pbe_params(uint8_t *buf, int *offset,
|
||||
const uint8_t **salt, int *iterations);
|
||||
|
||||
/*
|
||||
* Take a raw pkcs8 block and then decrypt it and turn it into a normal key.
|
||||
*/
|
||||
int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password)
|
||||
{
|
||||
uint8_t *buf = ssl_obj->buf;
|
||||
int len, offset = 0;
|
||||
int iterations;
|
||||
int ret = SSL_NOT_OK;
|
||||
uint8_t *version = NULL;
|
||||
const uint8_t *salt;
|
||||
uint8_t *priv_key;
|
||||
int uni_pass_len;
|
||||
char *uni_pass = make_uni_pass(password, &uni_pass_len);
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0)
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: Invalid p8 ASN.1 file\n");
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* unencrypted key? */
|
||||
if (asn1_get_big_int(buf, &offset, &version) > 0 && *version == 0)
|
||||
{
|
||||
ret = p8_add_key(ssl_ctx, buf);
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (get_pbe_params(buf, &offset, &salt, &iterations) < 0)
|
||||
goto error;
|
||||
|
||||
if ((len = asn1_next_obj(buf, &offset, ASN1_OCTET_STRING)) < 0)
|
||||
goto error;
|
||||
|
||||
priv_key = &buf[offset];
|
||||
|
||||
p8_decrypt(uni_pass, uni_pass_len, salt,
|
||||
iterations, priv_key, len, PKCS12_KEY_ID);
|
||||
ret = p8_add_key(ssl_ctx, priv_key);
|
||||
|
||||
error:
|
||||
free(version);
|
||||
free(uni_pass);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Take the unencrypted pkcs8 and turn it into a private key
|
||||
*/
|
||||
static int p8_add_key(SSL_CTX *ssl_ctx, uint8_t *priv_key)
|
||||
{
|
||||
uint8_t *buf = priv_key;
|
||||
int len, offset = 0;
|
||||
int ret = SSL_NOT_OK;
|
||||
|
||||
/* Skip the preamble and go straight to the private key.
|
||||
We only support rsaEncryption (1.2.840.113549.1.1.1) */
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_skip_obj(buf, &offset, ASN1_INTEGER) < 0 ||
|
||||
asn1_skip_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OCTET_STRING)) < 0)
|
||||
goto error;
|
||||
|
||||
ret = asn1_get_private_key(&buf[offset], len, &ssl_ctx->rsa_ctx);
|
||||
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create the unicode password
|
||||
*/
|
||||
static char *make_uni_pass(const char *password, int *uni_pass_len)
|
||||
{
|
||||
int pass_len = 0, i;
|
||||
char *uni_pass;
|
||||
|
||||
if (password == NULL)
|
||||
{
|
||||
password = "";
|
||||
}
|
||||
|
||||
uni_pass = (char *)malloc((strlen(password)+1)*2);
|
||||
|
||||
/* modify the password into a unicode version */
|
||||
for (i = 0; i < (int)strlen(password); i++)
|
||||
{
|
||||
uni_pass[pass_len++] = 0;
|
||||
uni_pass[pass_len++] = password[i];
|
||||
}
|
||||
|
||||
uni_pass[pass_len++] = 0; /* null terminate */
|
||||
uni_pass[pass_len++] = 0;
|
||||
*uni_pass_len = pass_len;
|
||||
return uni_pass;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decrypt a pkcs8 block.
|
||||
*/
|
||||
static int p8_decrypt(const char *uni_pass, int uni_pass_len,
|
||||
const uint8_t *salt, int iter,
|
||||
uint8_t *priv_key, int priv_key_len, int id)
|
||||
{
|
||||
uint8_t p[BLOCK_SIZE*2];
|
||||
uint8_t d[BLOCK_SIZE];
|
||||
uint8_t Ai[SHA1_SIZE];
|
||||
SHA1_CTX sha_ctx;
|
||||
RC4_CTX rc4_ctx;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < BLOCK_SIZE; i++)
|
||||
{
|
||||
p[i] = salt[i % SALT_SIZE];
|
||||
p[BLOCK_SIZE+i] = uni_pass[i % uni_pass_len];
|
||||
d[i] = id;
|
||||
}
|
||||
|
||||
/* get the key - no IV since we are using RC4 */
|
||||
SHA1_Init(&sha_ctx);
|
||||
SHA1_Update(&sha_ctx, d, sizeof(d));
|
||||
SHA1_Update(&sha_ctx, p, sizeof(p));
|
||||
SHA1_Final(Ai, &sha_ctx);
|
||||
|
||||
for (i = 1; i < iter; i++)
|
||||
{
|
||||
SHA1_Init(&sha_ctx);
|
||||
SHA1_Update(&sha_ctx, Ai, SHA1_SIZE);
|
||||
SHA1_Final(Ai, &sha_ctx);
|
||||
}
|
||||
|
||||
/* do the decryption */
|
||||
if (id == PKCS12_KEY_ID)
|
||||
{
|
||||
RC4_setup(&rc4_ctx, Ai, 16);
|
||||
RC4_crypt(&rc4_ctx, priv_key, priv_key, priv_key_len);
|
||||
}
|
||||
else /* MAC */
|
||||
memcpy(priv_key, Ai, SHA1_SIZE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Take a raw pkcs12 block and the decrypt it and turn it into a certificate(s)
|
||||
* and keys.
|
||||
*/
|
||||
int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password)
|
||||
{
|
||||
uint8_t *buf = ssl_obj->buf;
|
||||
int len, iterations, auth_safes_start,
|
||||
auth_safes_end, auth_safes_len, key_offset, offset = 0;
|
||||
int all_certs = 0;
|
||||
uint8_t *version = NULL, *auth_safes = NULL, *cert, *orig_mac;
|
||||
uint8_t key[SHA1_SIZE];
|
||||
uint8_t mac[SHA1_SIZE];
|
||||
const uint8_t *salt;
|
||||
int uni_pass_len, ret = SSL_OK;
|
||||
char *uni_pass = make_uni_pass(password, &uni_pass_len);
|
||||
static const uint8_t pkcs_data[] = /* pkc7 data */
|
||||
{ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01 };
|
||||
static const uint8_t pkcs_encrypted[] = /* pkc7 encrypted */
|
||||
{ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x06 };
|
||||
static const uint8_t pkcs8_key_bag[] = /* 1.2.840.113549.1.12.10.1.2 */
|
||||
{ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x0a, 0x01, 0x02 };
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0)
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: Invalid p12 ASN.1 file\n");
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (asn1_get_big_int(buf, &offset, &version) < 0 || *version != 3)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_VERSION;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* remove all the boring pcks7 bits */
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OID)) < 0 ||
|
||||
len != sizeof(pkcs_data) ||
|
||||
memcmp(&buf[offset], pkcs_data, sizeof(pkcs_data)))
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_EXPLICIT_TAG) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_OCTET_STRING) < 0)
|
||||
goto error;
|
||||
|
||||
/* work out the MAC start/end points (done on AuthSafes) */
|
||||
auth_safes_start = offset;
|
||||
auth_safes_end = offset;
|
||||
if (asn1_skip_obj(buf, &auth_safes_end, ASN1_SEQUENCE) < 0)
|
||||
goto error;
|
||||
|
||||
auth_safes_len = auth_safes_end - auth_safes_start;
|
||||
auth_safes = malloc(auth_safes_len);
|
||||
|
||||
memcpy(auth_safes, &buf[auth_safes_start], auth_safes_len);
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OID)) < 0 ||
|
||||
(len != sizeof(pkcs_encrypted) ||
|
||||
memcmp(&buf[offset], pkcs_encrypted, sizeof(pkcs_encrypted))))
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_EXPLICIT_TAG) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_skip_obj(buf, &offset, ASN1_INTEGER) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OID)) < 0 ||
|
||||
len != sizeof(pkcs_data) ||
|
||||
memcmp(&buf[offset], pkcs_data, sizeof(pkcs_data)))
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
/* work out the salt for the certificate */
|
||||
if (get_pbe_params(buf, &offset, &salt, &iterations) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_IMPLICIT_TAG)) < 0)
|
||||
goto error;
|
||||
|
||||
/* decrypt the certificate */
|
||||
cert = &buf[offset];
|
||||
if ((ret = p8_decrypt(uni_pass, uni_pass_len, salt, iterations, cert,
|
||||
len, PKCS12_KEY_ID)) < 0)
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
/* load the certificate */
|
||||
key_offset = 0;
|
||||
all_certs = asn1_next_obj(cert, &key_offset, ASN1_SEQUENCE);
|
||||
|
||||
/* keep going until all certs are loaded */
|
||||
while (key_offset < all_certs)
|
||||
{
|
||||
int cert_offset = key_offset;
|
||||
|
||||
if (asn1_skip_obj(cert, &cert_offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_next_obj(cert, &key_offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_skip_obj(cert, &key_offset, ASN1_OID) < 0 ||
|
||||
asn1_next_obj(cert, &key_offset, ASN1_EXPLICIT_TAG) < 0 ||
|
||||
asn1_next_obj(cert, &key_offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_skip_obj(cert, &key_offset, ASN1_OID) < 0 ||
|
||||
asn1_next_obj(cert, &key_offset, ASN1_EXPLICIT_TAG) < 0 ||
|
||||
(len = asn1_next_obj(cert, &key_offset, ASN1_OCTET_STRING)) < 0)
|
||||
goto error;
|
||||
|
||||
if ((ret = add_cert(ssl_ctx, &cert[key_offset], len)) < 0)
|
||||
goto error;
|
||||
|
||||
key_offset = cert_offset;
|
||||
}
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OID)) < 0 ||
|
||||
len != sizeof(pkcs_data) ||
|
||||
memcmp(&buf[offset], pkcs_data, sizeof(pkcs_data)))
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
if (asn1_next_obj(buf, &offset, ASN1_EXPLICIT_TAG) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_OCTET_STRING) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OID)) < 0 ||
|
||||
(len != sizeof(pkcs8_key_bag)) ||
|
||||
memcmp(&buf[offset], pkcs8_key_bag, sizeof(pkcs8_key_bag)))
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
/* work out the salt for the private key */
|
||||
if (asn1_next_obj(buf, &offset, ASN1_EXPLICIT_TAG) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
get_pbe_params(buf, &offset, &salt, &iterations) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OCTET_STRING)) < 0)
|
||||
goto error;
|
||||
|
||||
/* decrypt the private key */
|
||||
cert = &buf[offset];
|
||||
if ((ret = p8_decrypt(uni_pass, uni_pass_len, salt, iterations, cert,
|
||||
len, PKCS12_KEY_ID)) < 0)
|
||||
goto error;
|
||||
|
||||
offset += len;
|
||||
|
||||
/* load the private key */
|
||||
if ((ret = p8_add_key(ssl_ctx, cert)) < 0)
|
||||
goto error;
|
||||
|
||||
/* miss out on friendly name, local key id etc */
|
||||
if (asn1_skip_obj(buf, &offset, ASN1_SET) < 0)
|
||||
goto error;
|
||||
|
||||
/* work out the MAC */
|
||||
if (asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_next_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
asn1_skip_obj(buf, &offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, &offset, ASN1_OCTET_STRING)) < 0 ||
|
||||
len != SHA1_SIZE)
|
||||
goto error;
|
||||
|
||||
orig_mac = &buf[offset];
|
||||
offset += len;
|
||||
|
||||
/* get the salt */
|
||||
if ((len = asn1_next_obj(buf, &offset, ASN1_OCTET_STRING)) < 0 || len != 8)
|
||||
goto error;
|
||||
|
||||
salt = &buf[offset];
|
||||
|
||||
/* work out what the mac should be */
|
||||
if ((ret = p8_decrypt(uni_pass, uni_pass_len, salt, iterations,
|
||||
key, SHA1_SIZE, PKCS12_MAC_ID)) < 0)
|
||||
goto error;
|
||||
|
||||
hmac_sha1(auth_safes, auth_safes_len, key, SHA1_SIZE, mac);
|
||||
|
||||
if (memcmp(mac, orig_mac, SHA1_SIZE))
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_HMAC;
|
||||
goto error;
|
||||
}
|
||||
|
||||
error:
|
||||
free(version);
|
||||
free(uni_pass);
|
||||
free(auth_safes);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve the salt/iteration details from a PBE block.
|
||||
*/
|
||||
static int get_pbe_params(uint8_t *buf, int *offset,
|
||||
const uint8_t **salt, int *iterations)
|
||||
{
|
||||
static const uint8_t pbeSH1RC4[] = /* pbeWithSHAAnd128BitRC4 */
|
||||
{ 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x0c, 0x01, 0x01 };
|
||||
|
||||
int i, len;
|
||||
uint8_t *iter = NULL;
|
||||
int error_code = SSL_ERROR_NOT_SUPPORTED;
|
||||
|
||||
/* Get the PBE type */
|
||||
if (asn1_next_obj(buf, offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, offset, ASN1_OID)) < 0)
|
||||
goto error;
|
||||
|
||||
/* we expect pbeWithSHAAnd128BitRC4 (1.2.840.113549.1.12.1.1)
|
||||
which is the only algorithm we support */
|
||||
if (len != sizeof(pbeSH1RC4) ||
|
||||
memcmp(&buf[*offset], pbeSH1RC4, sizeof(pbeSH1RC4)))
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: pkcs8/pkcs12 must use \"PBE-SHA1-RC4-128\"\n");
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
|
||||
*offset += len;
|
||||
|
||||
if (asn1_next_obj(buf, offset, ASN1_SEQUENCE) < 0 ||
|
||||
(len = asn1_next_obj(buf, offset, ASN1_OCTET_STRING)) < 0 ||
|
||||
len != 8)
|
||||
goto error;
|
||||
|
||||
*salt = &buf[*offset];
|
||||
*offset += len;
|
||||
|
||||
if ((len = asn1_get_big_int(buf, offset, &iter)) < 0)
|
||||
goto error;
|
||||
|
||||
*iterations = 0;
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
(*iterations) <<= 8;
|
||||
(*iterations) += iter[i];
|
||||
}
|
||||
|
||||
free(iter);
|
||||
error_code = SSL_OK; /* got here - we are ok */
|
||||
|
||||
error:
|
||||
return error_code;
|
||||
}
|
||||
|
||||
#endif
|
||||
54
Argyll_V3.5.0/ccast/axTLS/private_key.h
Normal file
54
Argyll_V3.5.0/ccast/axTLS/private_key.h
Normal file
@@ -0,0 +1,54 @@
|
||||
unsigned char default_private_key[] = {
|
||||
0x30, 0x82, 0x02, 0x5d, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbd,
|
||||
0x0f, 0xd4, 0x42, 0xa8, 0x74, 0x87, 0x54, 0xaa, 0xb9, 0x3a, 0x1f, 0x8b,
|
||||
0xce, 0xbd, 0xb7, 0x65, 0xfb, 0x40, 0x3d, 0xd0, 0x11, 0x9a, 0x9c, 0xdc,
|
||||
0x82, 0x7c, 0xea, 0xa8, 0x17, 0xe1, 0x74, 0xf3, 0x05, 0x0e, 0x61, 0xc1,
|
||||
0xc1, 0x78, 0x8a, 0xb2, 0xba, 0x15, 0x22, 0x5a, 0xff, 0x9b, 0xb8, 0x7a,
|
||||
0x2e, 0x0f, 0x88, 0xb7, 0x74, 0xde, 0x04, 0x99, 0xa5, 0xa2, 0x99, 0x53,
|
||||
0x8b, 0xad, 0x78, 0x5a, 0x31, 0xed, 0xbc, 0x01, 0xe7, 0xdf, 0xe9, 0xec,
|
||||
0x2f, 0xa0, 0x5d, 0x53, 0xf6, 0xe6, 0x8a, 0xa0, 0xc8, 0x6d, 0x41, 0x45,
|
||||
0x63, 0x23, 0xb3, 0xcf, 0x4e, 0x50, 0x1f, 0x28, 0xdf, 0x36, 0xe2, 0x73,
|
||||
0xdf, 0xd6, 0xa1, 0xb3, 0x46, 0x4f, 0x6e, 0xbb, 0x0d, 0x9b, 0xef, 0xa8,
|
||||
0xf9, 0x4c, 0xa5, 0x71, 0xa1, 0x88, 0xdd, 0x07, 0xa9, 0x86, 0x0d, 0x3f,
|
||||
0xcd, 0x99, 0x23, 0xa2, 0x84, 0x77, 0x0f, 0x02, 0x03, 0x01, 0x00, 0x01,
|
||||
0x02, 0x81, 0x80, 0x26, 0x3f, 0xec, 0x96, 0xab, 0xd4, 0x1f, 0x89, 0x0e,
|
||||
0x9d, 0x38, 0xd8, 0x27, 0x05, 0xe5, 0xb6, 0x14, 0x08, 0xd7, 0xff, 0x69,
|
||||
0x78, 0x16, 0x4a, 0xc4, 0x06, 0x16, 0x55, 0xb7, 0x3a, 0x55, 0x9f, 0xbe,
|
||||
0x86, 0xf8, 0x58, 0xe8, 0xc5, 0x46, 0xa8, 0xf0, 0xed, 0xda, 0xd6, 0xbf,
|
||||
0x88, 0x55, 0x2d, 0xe6, 0x72, 0x29, 0x2c, 0x64, 0xc9, 0x5d, 0x1d, 0x9b,
|
||||
0x24, 0x3a, 0x98, 0x40, 0xa1, 0xd2, 0xaf, 0x5c, 0xab, 0x23, 0xe4, 0x33,
|
||||
0xd0, 0xea, 0x60, 0x52, 0xe7, 0x7a, 0x9e, 0x73, 0x5f, 0x2e, 0x80, 0xd1,
|
||||
0xdc, 0x6f, 0x47, 0x0f, 0x97, 0x80, 0x36, 0xd2, 0x30, 0x07, 0xdd, 0xd6,
|
||||
0xd7, 0x15, 0x89, 0x2b, 0x74, 0xd5, 0x7e, 0x8a, 0xbc, 0x63, 0x42, 0x0a,
|
||||
0xf2, 0x31, 0x29, 0xbf, 0xf9, 0xf9, 0xf0, 0x88, 0x8f, 0x8a, 0xc2, 0x22,
|
||||
0x6e, 0x15, 0x26, 0xb7, 0x5e, 0x5b, 0x58, 0x44, 0x1c, 0x3b, 0x79, 0x02,
|
||||
0x41, 0x00, 0xe1, 0xf1, 0xb2, 0xe5, 0xc8, 0x80, 0x93, 0x40, 0x50, 0x74,
|
||||
0x14, 0xdd, 0xb2, 0xf2, 0x27, 0x5c, 0x0c, 0x3d, 0xc0, 0x5f, 0xee, 0x9c,
|
||||
0x45, 0x6c, 0x13, 0x00, 0xdf, 0xd0, 0xd9, 0x83, 0xfa, 0x90, 0x2c, 0x84,
|
||||
0xf2, 0xaa, 0xc2, 0xdd, 0xfb, 0xcf, 0x03, 0x41, 0x88, 0x10, 0xc6, 0xbb,
|
||||
0x5e, 0xb7, 0xb6, 0x2e, 0xa6, 0x1d, 0xaa, 0xba, 0xfb, 0x4a, 0x72, 0xd8,
|
||||
0x9a, 0xad, 0x88, 0x0d, 0x6a, 0x15, 0x02, 0x41, 0x00, 0xd6, 0x36, 0x23,
|
||||
0xf3, 0x5d, 0x77, 0xc8, 0xd3, 0x49, 0xc1, 0x93, 0xfe, 0xca, 0x0d, 0xeb,
|
||||
0x9b, 0xda, 0xbd, 0x47, 0x28, 0x73, 0x97, 0xa0, 0x50, 0xd7, 0x4c, 0x24,
|
||||
0xdf, 0x9b, 0x0b, 0x37, 0xae, 0xc3, 0x31, 0xb5, 0x4f, 0x62, 0x08, 0xca,
|
||||
0xe5, 0xef, 0x97, 0x7b, 0x43, 0xa0, 0xda, 0x2b, 0x1f, 0xbf, 0xa8, 0x08,
|
||||
0x93, 0xd2, 0x16, 0x1c, 0x89, 0x99, 0xf1, 0xdf, 0x26, 0xd1, 0x42, 0x99,
|
||||
0x93, 0x02, 0x41, 0x00, 0xb1, 0x41, 0xe4, 0x7e, 0xdf, 0x20, 0xf7, 0xe4,
|
||||
0xf1, 0xf9, 0x4f, 0xd1, 0x6a, 0x2d, 0x0d, 0xf1, 0xe9, 0xec, 0x9c, 0x3a,
|
||||
0xe6, 0xc0, 0x94, 0xba, 0x27, 0xe2, 0x7c, 0xb4, 0xa5, 0xa1, 0x23, 0xf6,
|
||||
0xed, 0xe6, 0x53, 0x56, 0xe2, 0x50, 0x32, 0xd8, 0x02, 0x8e, 0xeb, 0xc7,
|
||||
0x75, 0x91, 0xd3, 0xca, 0x3e, 0xd4, 0x34, 0x20, 0x7c, 0x2b, 0xfb, 0x2f,
|
||||
0x3a, 0x10, 0x72, 0xb1, 0x07, 0x56, 0xb6, 0xcd, 0x02, 0x40, 0x1e, 0x3b,
|
||||
0xf2, 0x03, 0x0d, 0x74, 0x34, 0xb2, 0x2d, 0xbc, 0xd6, 0xc8, 0xa5, 0x78,
|
||||
0x25, 0x83, 0x0f, 0xf2, 0x9b, 0x32, 0x88, 0x6e, 0x24, 0x40, 0x84, 0xc2,
|
||||
0xc8, 0x89, 0x8e, 0xf6, 0x9c, 0x5b, 0x5c, 0x4d, 0x8d, 0xcb, 0xb0, 0x88,
|
||||
0x91, 0x2a, 0xb7, 0x10, 0x68, 0x63, 0x79, 0x36, 0x91, 0xd3, 0x9f, 0x57,
|
||||
0x76, 0x2e, 0x76, 0xfe, 0x8b, 0xf4, 0x97, 0xf7, 0xdd, 0x89, 0x3b, 0x0b,
|
||||
0xed, 0x65, 0x02, 0x41, 0x00, 0xb9, 0xaf, 0xbf, 0x09, 0xc9, 0x90, 0x26,
|
||||
0xf3, 0x72, 0x8b, 0xbf, 0xb3, 0x7c, 0xe7, 0x6f, 0x6f, 0x5b, 0xa3, 0x95,
|
||||
0xb8, 0x9e, 0x03, 0xb9, 0xcf, 0xa0, 0x53, 0xba, 0x32, 0xc1, 0xd3, 0xad,
|
||||
0x85, 0xbb, 0x79, 0x48, 0x09, 0xd6, 0x3f, 0x9c, 0xd9, 0x37, 0x91, 0x11,
|
||||
0x0d, 0x04, 0xd5, 0x3b, 0xca, 0x74, 0x5d, 0x1c, 0x91, 0x8d, 0x3d, 0xf1,
|
||||
0xf8, 0xf9, 0xbe, 0x35, 0xd7, 0xb2, 0x53, 0x50, 0x1d
|
||||
};
|
||||
unsigned int default_private_key_len = 609;
|
||||
97
Argyll_V3.5.0/ccast/axTLS/rc4.c
Normal file
97
Argyll_V3.5.0/ccast/axTLS/rc4.c
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* An implementation of the RC4/ARC4 algorithm.
|
||||
* Originally written by Christophe Devine.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
/* only used for PKCS12 now */
|
||||
#ifdef CONFIG_SSL_USE_PKCS12
|
||||
|
||||
/**
|
||||
* Get ready for an encrypt/decrypt operation
|
||||
*/
|
||||
void RC4_setup(RC4_CTX *ctx, const uint8_t *key, int length)
|
||||
{
|
||||
int i, j = 0, k = 0, a;
|
||||
uint8_t *m;
|
||||
|
||||
ctx->x = 0;
|
||||
ctx->y = 0;
|
||||
m = ctx->m;
|
||||
|
||||
for (i = 0; i < 256; i++)
|
||||
m[i] = i;
|
||||
|
||||
for (i = 0; i < 256; i++)
|
||||
{
|
||||
a = m[i];
|
||||
j = (uint8_t)(j + a + key[k]);
|
||||
m[i] = m[j];
|
||||
m[j] = a;
|
||||
|
||||
if (++k >= length)
|
||||
k = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the encrypt/decrypt operation (can use it for either since
|
||||
* this is a stream cipher).
|
||||
* NOTE: *msg and *out must be the same pointer (performance tweak)
|
||||
*/
|
||||
void RC4_crypt(RC4_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
|
||||
{
|
||||
int i;
|
||||
uint8_t *m, x, y, a, b;
|
||||
|
||||
x = ctx->x;
|
||||
y = ctx->y;
|
||||
m = ctx->m;
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
a = m[++x];
|
||||
y += a;
|
||||
m[x] = b = m[y];
|
||||
m[y] = a;
|
||||
out[i] ^= m[(uint8_t)(a + b)];
|
||||
}
|
||||
|
||||
ctx->x = x;
|
||||
ctx->y = y;
|
||||
}
|
||||
|
||||
#endif
|
||||
339
Argyll_V3.5.0/ccast/axTLS/rsa.c
Normal file
339
Argyll_V3.5.0/ccast/axTLS/rsa.c
Normal file
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2014, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements the RSA public encryption algorithm. Uses the bigint library to
|
||||
* perform its calculations.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
void RSA_priv_key_new(RSA_CTX **ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
const uint8_t *pub_exp, int pub_len,
|
||||
const uint8_t *priv_exp, int priv_len
|
||||
#if 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
|
||||
)
|
||||
{
|
||||
RSA_CTX *rsa_ctx;
|
||||
BI_CTX *bi_ctx;
|
||||
RSA_pub_key_new(ctx, modulus, mod_len, pub_exp, pub_len);
|
||||
rsa_ctx = *ctx;
|
||||
bi_ctx = rsa_ctx->bi_ctx;
|
||||
rsa_ctx->d = bi_import(bi_ctx, priv_exp, priv_len);
|
||||
bi_permanent(rsa_ctx->d);
|
||||
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
rsa_ctx->p = bi_import(bi_ctx, p, p_len);
|
||||
rsa_ctx->q = bi_import(bi_ctx, q, q_len);
|
||||
rsa_ctx->dP = bi_import(bi_ctx, dP, dP_len);
|
||||
rsa_ctx->dQ = bi_import(bi_ctx, dQ, dQ_len);
|
||||
rsa_ctx->qInv = bi_import(bi_ctx, qInv, qInv_len);
|
||||
bi_permanent(rsa_ctx->dP);
|
||||
bi_permanent(rsa_ctx->dQ);
|
||||
bi_permanent(rsa_ctx->qInv);
|
||||
bi_set_mod(bi_ctx, rsa_ctx->p, BIGINT_P_OFFSET);
|
||||
bi_set_mod(bi_ctx, rsa_ctx->q, BIGINT_Q_OFFSET);
|
||||
#endif
|
||||
}
|
||||
|
||||
void RSA_pub_key_new(RSA_CTX **ctx,
|
||||
const uint8_t *modulus, int mod_len,
|
||||
const uint8_t *pub_exp, int pub_len)
|
||||
{
|
||||
RSA_CTX *rsa_ctx;
|
||||
BI_CTX *bi_ctx;
|
||||
|
||||
if (*ctx) /* if we load multiple certs, dump the old one */
|
||||
RSA_free(*ctx);
|
||||
|
||||
bi_ctx = bi_initialize();
|
||||
*ctx = (RSA_CTX *)calloc(1, sizeof(RSA_CTX));
|
||||
rsa_ctx = *ctx;
|
||||
rsa_ctx->bi_ctx = bi_ctx;
|
||||
rsa_ctx->num_octets = mod_len;
|
||||
rsa_ctx->m = bi_import(bi_ctx, modulus, mod_len);
|
||||
bi_set_mod(bi_ctx, rsa_ctx->m, BIGINT_M_OFFSET);
|
||||
rsa_ctx->e = bi_import(bi_ctx, pub_exp, pub_len);
|
||||
bi_permanent(rsa_ctx->e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free up any RSA context resources.
|
||||
*/
|
||||
void RSA_free(RSA_CTX *rsa_ctx)
|
||||
{
|
||||
BI_CTX *bi_ctx;
|
||||
if (rsa_ctx == NULL) /* deal with ptrs that are null */
|
||||
return;
|
||||
|
||||
bi_ctx = rsa_ctx->bi_ctx;
|
||||
|
||||
bi_depermanent(rsa_ctx->e);
|
||||
bi_free(bi_ctx, rsa_ctx->e);
|
||||
bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);
|
||||
|
||||
if (rsa_ctx->d)
|
||||
{
|
||||
bi_depermanent(rsa_ctx->d);
|
||||
bi_free(bi_ctx, rsa_ctx->d);
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
bi_depermanent(rsa_ctx->dP);
|
||||
bi_depermanent(rsa_ctx->dQ);
|
||||
bi_depermanent(rsa_ctx->qInv);
|
||||
bi_free(bi_ctx, rsa_ctx->dP);
|
||||
bi_free(bi_ctx, rsa_ctx->dQ);
|
||||
bi_free(bi_ctx, rsa_ctx->qInv);
|
||||
bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
|
||||
bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
|
||||
#endif
|
||||
}
|
||||
|
||||
bi_terminate(bi_ctx);
|
||||
free(rsa_ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Use PKCS1.5 for decryption/verification.
|
||||
* @param ctx [in] The context
|
||||
* @param in_data [in] The data to decrypt (must be < modulus size-11)
|
||||
* @param out_data [out] The decrypted data.
|
||||
* @param out_len [int] The size of the decrypted buffer in bytes
|
||||
* @param is_decryption [in] Decryption or verify operation.
|
||||
* @return The number of bytes that were originally encrypted. -1 on error.
|
||||
* @see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
|
||||
*/
|
||||
int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data,
|
||||
uint8_t *out_data, int out_len, int is_decryption)
|
||||
{
|
||||
const int byte_size = ctx->num_octets;
|
||||
int i = 0, size;
|
||||
bigint *decrypted_bi, *dat_bi;
|
||||
uint8_t *block = (uint8_t *)malloc(byte_size);
|
||||
int pad_count = 0;
|
||||
|
||||
if (out_len < byte_size) /* check output has enough size */
|
||||
return -1;
|
||||
|
||||
memset(out_data, 0, out_len); /* initialise */
|
||||
|
||||
/* decrypt */
|
||||
dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
decrypted_bi = is_decryption ? /* decrypt or verify? */
|
||||
RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi);
|
||||
#else /* always a decryption */
|
||||
decrypted_bi = RSA_private(ctx, dat_bi);
|
||||
#endif
|
||||
|
||||
/* convert to a normal block */
|
||||
bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
|
||||
|
||||
if (block[i++] != 0) { /* leading 0? */
|
||||
free(block);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
|
||||
{
|
||||
if (block[i++] != 0x01) { /* BT correct? */
|
||||
free(block);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (block[i++] == 0xff && i < byte_size)
|
||||
pad_count++;
|
||||
}
|
||||
else /* PKCS1.5 encryption padding is random */
|
||||
#endif
|
||||
{
|
||||
if (block[i++] != 0x02) { /* BT correct? */
|
||||
free(block);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (block[i++] && i < byte_size)
|
||||
pad_count++;
|
||||
}
|
||||
|
||||
/* check separator byte 0x00 - and padding must be 8 or more bytes */
|
||||
if (i == byte_size || pad_count < 8) {
|
||||
free(block);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size = byte_size - i;
|
||||
|
||||
/* get only the bit we want */
|
||||
memcpy(out_data, &block[i], size);
|
||||
free(block);
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* GWG
|
||||
* @brief Use PKCS1.5 for decryption.
|
||||
* @param ctx [in] The context
|
||||
* @param in_data [in] The data to encrypt
|
||||
* @param out_data [out] The decrypted data.
|
||||
* @return The number of bytes that were originally encrypted. -1 on error.
|
||||
*/
|
||||
int RSA_decrypt2(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data)
|
||||
{
|
||||
const int byte_size = ctx->num_octets;
|
||||
int i, size;
|
||||
bigint *decrypted_bi, *dat_bi;
|
||||
uint8_t *block = (uint8_t *)malloc(byte_size);
|
||||
|
||||
/* decrypt */
|
||||
dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
|
||||
|
||||
decrypted_bi = RSA_public(ctx, dat_bi); /* Frees dat_bi and exponent ? */
|
||||
|
||||
/* convert to a normal block (frees decrypted_bi) */
|
||||
bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
|
||||
|
||||
|
||||
/* We assume this is padded with "0001ff....ff00" */
|
||||
i = 2;
|
||||
while (block[i++] == 0xff && i < byte_size)
|
||||
;
|
||||
|
||||
/* Skip last 0x00 */
|
||||
if (i < byte_size && block[i] == 0x00)
|
||||
i++;
|
||||
|
||||
size = byte_size - i;
|
||||
|
||||
/* get only the bit we want */
|
||||
if (size > 0) {
|
||||
memcpy(out_data, &block[i], size);
|
||||
}
|
||||
|
||||
free(block);
|
||||
|
||||
return size ? size : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs m = c^d mod n
|
||||
*/
|
||||
bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg)
|
||||
{
|
||||
#ifdef CONFIG_BIGINT_CRT
|
||||
return bi_crt(c->bi_ctx, bi_msg, c->dP, c->dQ, c->p, c->q, c->qInv);
|
||||
#else
|
||||
BI_CTX *ctx = c->bi_ctx;
|
||||
ctx->mod_offset = BIGINT_M_OFFSET;
|
||||
return bi_mod_power(ctx, bi_msg, c->d);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
/**
|
||||
* Used for diagnostics.
|
||||
*/
|
||||
void RSA_print(const RSA_CTX *rsa_ctx)
|
||||
{
|
||||
if (rsa_ctx == NULL)
|
||||
return;
|
||||
|
||||
printf("----------------- RSA DEBUG ----------------\n");
|
||||
printf("Size:\t%d\n", rsa_ctx->num_octets);
|
||||
bi_print("Modulus", rsa_ctx->m);
|
||||
bi_print("Public Key", rsa_ctx->e);
|
||||
bi_print("Private Key", rsa_ctx->d);
|
||||
}
|
||||
#endif
|
||||
|
||||
//#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT) // GWG
|
||||
/**
|
||||
* Performs c = m^e mod n
|
||||
*/
|
||||
bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg)
|
||||
{
|
||||
c->bi_ctx->mod_offset = BIGINT_M_OFFSET;
|
||||
return bi_mod_power(c->bi_ctx, bi_msg, c->e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use PKCS1.5 for encryption/signing.
|
||||
* see http://www.rsasecurity.com/rsalabs/node.asp?id=2125
|
||||
*/
|
||||
int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
|
||||
uint8_t *out_data, int is_signing)
|
||||
{
|
||||
int byte_size = ctx->num_octets;
|
||||
int num_pads_needed = byte_size-in_len-3;
|
||||
bigint *dat_bi, *encrypt_bi;
|
||||
|
||||
/* note: in_len+11 must be > byte_size */
|
||||
out_data[0] = 0; /* ensure encryption block is < modulus */
|
||||
|
||||
if (is_signing)
|
||||
{
|
||||
out_data[1] = 1; /* PKCS1.5 signing pads with "0xff"'s */
|
||||
memset(&out_data[2], 0xff, num_pads_needed);
|
||||
}
|
||||
else /* randomize the encryption padding with non-zero bytes */
|
||||
{
|
||||
out_data[1] = 2;
|
||||
if (get_random_NZ(num_pads_needed, &out_data[2]) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
out_data[2+num_pads_needed] = 0;
|
||||
memcpy(&out_data[3+num_pads_needed], in_data, 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);
|
||||
bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
|
||||
|
||||
/* save a few bytes of memory */
|
||||
bi_clear_cache(ctx->bi_ctx);
|
||||
return byte_size;
|
||||
}
|
||||
|
||||
//#endif
|
||||
|
||||
249
Argyll_V3.5.0/ccast/axTLS/sha1.c
Normal file
249
Argyll_V3.5.0/ccast/axTLS/sha1.c
Normal file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* SHA1 implementation - as defined in FIPS PUB 180-1 published April 17, 1995.
|
||||
* This code was originally taken from RFC3174
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
/*
|
||||
* Define the SHA1 circular left shift macro
|
||||
*/
|
||||
#define SHA1CircularShift(bits,word) \
|
||||
(((word) << (bits)) | ((word) >> (32-(bits))))
|
||||
|
||||
/* ----- static functions ----- */
|
||||
static void SHA1PadMessage(SHA1_CTX *ctx);
|
||||
static void SHA1ProcessMessageBlock(SHA1_CTX *ctx);
|
||||
|
||||
/**
|
||||
* Initialize the SHA1 context
|
||||
*/
|
||||
void SHA1_Init(SHA1_CTX *ctx)
|
||||
{
|
||||
ctx->Length_Low = 0;
|
||||
ctx->Length_High = 0;
|
||||
ctx->Message_Block_Index = 0;
|
||||
ctx->Intermediate_Hash[0] = 0x67452301;
|
||||
ctx->Intermediate_Hash[1] = 0xEFCDAB89;
|
||||
ctx->Intermediate_Hash[2] = 0x98BADCFE;
|
||||
ctx->Intermediate_Hash[3] = 0x10325476;
|
||||
ctx->Intermediate_Hash[4] = 0xC3D2E1F0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an array of octets as the next portion of the message.
|
||||
*/
|
||||
void SHA1_Update(SHA1_CTX *ctx, const uint8_t *msg, int len)
|
||||
{
|
||||
while (len--)
|
||||
{
|
||||
ctx->Message_Block[ctx->Message_Block_Index++] = (*msg & 0xFF);
|
||||
ctx->Length_Low += 8;
|
||||
|
||||
if (ctx->Length_Low == 0)
|
||||
ctx->Length_High++;
|
||||
|
||||
if (ctx->Message_Block_Index == 64)
|
||||
SHA1ProcessMessageBlock(ctx);
|
||||
|
||||
msg++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 160-bit message digest into the user's array
|
||||
*/
|
||||
void SHA1_Final(uint8_t *digest, SHA1_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
|
||||
SHA1PadMessage(ctx);
|
||||
memset(ctx->Message_Block, 0, 64);
|
||||
ctx->Length_Low = 0; /* and clear length */
|
||||
ctx->Length_High = 0;
|
||||
|
||||
for (i = 0; i < SHA1_SIZE; i++)
|
||||
{
|
||||
digest[i] = ctx->Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the next 512 bits of the message stored in the array.
|
||||
*/
|
||||
static void SHA1ProcessMessageBlock(SHA1_CTX *ctx)
|
||||
{
|
||||
const uint32_t K[] = { /* Constants defined in SHA-1 */
|
||||
0x5A827999,
|
||||
0x6ED9EBA1,
|
||||
0x8F1BBCDC,
|
||||
0xCA62C1D6
|
||||
};
|
||||
int t; /* Loop counter */
|
||||
uint32_t temp; /* Temporary word value */
|
||||
uint32_t W[80]; /* Word sequence */
|
||||
uint32_t A, B, C, D, E; /* Word buffers */
|
||||
|
||||
/*
|
||||
* Initialize the first 16 words in the array W
|
||||
*/
|
||||
for (t = 0; t < 16; t++)
|
||||
{
|
||||
W[t] = ctx->Message_Block[t * 4] << 24;
|
||||
W[t] |= ctx->Message_Block[t * 4 + 1] << 16;
|
||||
W[t] |= ctx->Message_Block[t * 4 + 2] << 8;
|
||||
W[t] |= ctx->Message_Block[t * 4 + 3];
|
||||
}
|
||||
|
||||
for (t = 16; t < 80; t++)
|
||||
{
|
||||
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
|
||||
}
|
||||
|
||||
A = ctx->Intermediate_Hash[0];
|
||||
B = ctx->Intermediate_Hash[1];
|
||||
C = ctx->Intermediate_Hash[2];
|
||||
D = ctx->Intermediate_Hash[3];
|
||||
E = ctx->Intermediate_Hash[4];
|
||||
|
||||
for (t = 0; t < 20; t++)
|
||||
{
|
||||
temp = SHA1CircularShift(5,A) +
|
||||
((B & C) | ((~B) & D)) + E + W[t] + K[0];
|
||||
E = D;
|
||||
D = C;
|
||||
C = SHA1CircularShift(30,B);
|
||||
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
for (t = 20; t < 40; t++)
|
||||
{
|
||||
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
|
||||
E = D;
|
||||
D = C;
|
||||
C = SHA1CircularShift(30,B);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
for (t = 40; t < 60; t++)
|
||||
{
|
||||
temp = SHA1CircularShift(5,A) +
|
||||
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
|
||||
E = D;
|
||||
D = C;
|
||||
C = SHA1CircularShift(30,B);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
for (t = 60; t < 80; t++)
|
||||
{
|
||||
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
|
||||
E = D;
|
||||
D = C;
|
||||
C = SHA1CircularShift(30,B);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
ctx->Intermediate_Hash[0] += A;
|
||||
ctx->Intermediate_Hash[1] += B;
|
||||
ctx->Intermediate_Hash[2] += C;
|
||||
ctx->Intermediate_Hash[3] += D;
|
||||
ctx->Intermediate_Hash[4] += E;
|
||||
ctx->Message_Block_Index = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* According to the standard, the message must be padded to an even
|
||||
* 512 bits. The first padding bit must be a '1'. The last 64
|
||||
* bits represent the length of the original message. All bits in
|
||||
* between should be 0. This function will pad the message
|
||||
* according to those rules by filling the Message_Block array
|
||||
* accordingly. It will also call the ProcessMessageBlock function
|
||||
* provided appropriately. When it returns, it can be assumed that
|
||||
* the message digest has been computed.
|
||||
*
|
||||
* @param ctx [in, out] The SHA1 context
|
||||
*/
|
||||
static void SHA1PadMessage(SHA1_CTX *ctx)
|
||||
{
|
||||
/*
|
||||
* Check to see if the current message block is too small to hold
|
||||
* the initial padding bits and length. If so, we will pad the
|
||||
* block, process it, and then continue padding into a second
|
||||
* block.
|
||||
*/
|
||||
if (ctx->Message_Block_Index > 55)
|
||||
{
|
||||
ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
|
||||
while(ctx->Message_Block_Index < 64)
|
||||
{
|
||||
ctx->Message_Block[ctx->Message_Block_Index++] = 0;
|
||||
}
|
||||
|
||||
SHA1ProcessMessageBlock(ctx);
|
||||
|
||||
while (ctx->Message_Block_Index < 56)
|
||||
{
|
||||
ctx->Message_Block[ctx->Message_Block_Index++] = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
|
||||
while(ctx->Message_Block_Index < 56)
|
||||
{
|
||||
|
||||
ctx->Message_Block[ctx->Message_Block_Index++] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the message length as the last 8 octets
|
||||
*/
|
||||
ctx->Message_Block[56] = ctx->Length_High >> 24;
|
||||
ctx->Message_Block[57] = ctx->Length_High >> 16;
|
||||
ctx->Message_Block[58] = ctx->Length_High >> 8;
|
||||
ctx->Message_Block[59] = ctx->Length_High;
|
||||
ctx->Message_Block[60] = ctx->Length_Low >> 24;
|
||||
ctx->Message_Block[61] = ctx->Length_Low >> 16;
|
||||
ctx->Message_Block[62] = ctx->Length_Low >> 8;
|
||||
ctx->Message_Block[63] = ctx->Length_Low;
|
||||
SHA1ProcessMessageBlock(ctx);
|
||||
}
|
||||
274
Argyll_V3.5.0/ccast/axTLS/sha256.c
Normal file
274
Argyll_V3.5.0/ccast/axTLS/sha256.c
Normal file
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
#define GET_UINT32(n,b,i) \
|
||||
{ \
|
||||
(n) = ((uint32_t) (b)[(i) ] << 24) \
|
||||
| ((uint32_t) (b)[(i) + 1] << 16) \
|
||||
| ((uint32_t) (b)[(i) + 2] << 8) \
|
||||
| ((uint32_t) (b)[(i) + 3] ); \
|
||||
}
|
||||
|
||||
#define PUT_UINT32(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (uint8_t) ((n) >> 24); \
|
||||
(b)[(i) + 1] = (uint8_t) ((n) >> 16); \
|
||||
(b)[(i) + 2] = (uint8_t) ((n) >> 8); \
|
||||
(b)[(i) + 3] = (uint8_t) ((n) ); \
|
||||
}
|
||||
|
||||
static const uint8_t sha256_padding[64] =
|
||||
{
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the SHA256 context
|
||||
*/
|
||||
void SHA256_Init(SHA256_CTX *ctx)
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x6A09E667;
|
||||
ctx->state[1] = 0xBB67AE85;
|
||||
ctx->state[2] = 0x3C6EF372;
|
||||
ctx->state[3] = 0xA54FF53A;
|
||||
ctx->state[4] = 0x510E527F;
|
||||
ctx->state[5] = 0x9B05688C;
|
||||
ctx->state[6] = 0x1F83D9AB;
|
||||
ctx->state[7] = 0x5BE0CD19;
|
||||
}
|
||||
|
||||
static void SHA256_Process(const uint8_t digest[64], SHA256_CTX *ctx)
|
||||
{
|
||||
uint32_t temp1, temp2, W[64];
|
||||
uint32_t A, B, C, D, E, F, G, H;
|
||||
|
||||
GET_UINT32(W[0], digest, 0);
|
||||
GET_UINT32(W[1], digest, 4);
|
||||
GET_UINT32(W[2], digest, 8);
|
||||
GET_UINT32(W[3], digest, 12);
|
||||
GET_UINT32(W[4], digest, 16);
|
||||
GET_UINT32(W[5], digest, 20);
|
||||
GET_UINT32(W[6], digest, 24);
|
||||
GET_UINT32(W[7], digest, 28);
|
||||
GET_UINT32(W[8], digest, 32);
|
||||
GET_UINT32(W[9], digest, 36);
|
||||
GET_UINT32(W[10], digest, 40);
|
||||
GET_UINT32(W[11], digest, 44);
|
||||
GET_UINT32(W[12], digest, 48);
|
||||
GET_UINT32(W[13], digest, 52);
|
||||
GET_UINT32(W[14], digest, 56);
|
||||
GET_UINT32(W[15], digest, 60);
|
||||
|
||||
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
|
||||
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
|
||||
|
||||
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
|
||||
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
|
||||
|
||||
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
|
||||
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
|
||||
|
||||
#define F0(x,y,z) ((x & y) | (z & (x | y)))
|
||||
#define F1(x,y,z) (z ^ (x & (y ^ z)))
|
||||
|
||||
#define R(t) \
|
||||
( \
|
||||
W[t] = S1(W[t - 2]) + W[t - 7] + \
|
||||
S0(W[t - 15]) + W[t - 16] \
|
||||
)
|
||||
|
||||
#define P(a,b,c,d,e,f,g,h,x,K) \
|
||||
{ \
|
||||
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
|
||||
temp2 = S2(a) + F0(a,b,c); \
|
||||
d += temp1; h = temp1 + temp2; \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
F = ctx->state[5];
|
||||
G = ctx->state[6];
|
||||
H = ctx->state[7];
|
||||
|
||||
P(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98);
|
||||
P(H, A, B, C, D, E, F, G, W[ 1], 0x71374491);
|
||||
P(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF);
|
||||
P(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5);
|
||||
P(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B);
|
||||
P(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1);
|
||||
P(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4);
|
||||
P(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5);
|
||||
P(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98);
|
||||
P(H, A, B, C, D, E, F, G, W[ 9], 0x12835B01);
|
||||
P(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
|
||||
P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
|
||||
P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
|
||||
P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
|
||||
P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
|
||||
P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
|
||||
P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
|
||||
P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
|
||||
P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
|
||||
P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
|
||||
P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
|
||||
P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
|
||||
P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
|
||||
P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
|
||||
P(A, B, C, D, E, F, G, H, R(24), 0x983E5152);
|
||||
P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
|
||||
P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
|
||||
P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
|
||||
P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
|
||||
P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
|
||||
P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
|
||||
P(B, C, D, E, F, G, H, A, R(31), 0x14292967);
|
||||
P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
|
||||
P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
|
||||
P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
|
||||
P(F, G, H, A, B, C, D, E, R(35), 0x53380D13);
|
||||
P(E, F, G, H, A, B, C, D, R(36), 0x650A7354);
|
||||
P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
|
||||
P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
|
||||
P(B, C, D, E, F, G, H, A, R(39), 0x92722C85);
|
||||
P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
|
||||
P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
|
||||
P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
|
||||
P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
|
||||
P(E, F, G, H, A, B, C, D, R(44), 0xD192E819);
|
||||
P(D, E, F, G, H, A, B, C, R(45), 0xD6990624);
|
||||
P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
|
||||
P(B, C, D, E, F, G, H, A, R(47), 0x106AA070);
|
||||
P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
|
||||
P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
|
||||
P(G, H, A, B, C, D, E, F, R(50), 0x2748774C);
|
||||
P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
|
||||
P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
|
||||
P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
|
||||
P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
|
||||
P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
|
||||
P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
|
||||
P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
|
||||
P(G, H, A, B, C, D, E, F, R(58), 0x84C87814);
|
||||
P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
|
||||
P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
|
||||
P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
|
||||
P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
|
||||
P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
ctx->state[4] += E;
|
||||
ctx->state[5] += F;
|
||||
ctx->state[6] += G;
|
||||
ctx->state[7] += H;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an array of octets as the next portion of the message.
|
||||
*/
|
||||
void SHA256_Update(SHA256_CTX *ctx, const uint8_t * msg, int len)
|
||||
{
|
||||
uint32_t left = ctx->total[0] & 0x3F;
|
||||
uint32_t fill = 64 - left;
|
||||
|
||||
ctx->total[0] += len;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if (ctx->total[0] < len)
|
||||
ctx->total[1]++;
|
||||
|
||||
if (left && len >= fill)
|
||||
{
|
||||
memcpy((void *) (ctx->buffer + left), (void *)msg, fill);
|
||||
SHA256_Process(ctx->buffer, ctx);
|
||||
len -= fill;
|
||||
msg += fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while (len >= 64)
|
||||
{
|
||||
SHA256_Process(msg, ctx);
|
||||
len -= 64;
|
||||
msg += 64;
|
||||
}
|
||||
|
||||
if (len)
|
||||
{
|
||||
memcpy((void *) (ctx->buffer + left), (void *) msg, len);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 256-bit message digest into the user's array
|
||||
*/
|
||||
void SHA256_Final(uint8_t *digest, SHA256_CTX *ctx)
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
uint8_t msglen[8];
|
||||
|
||||
high = (ctx->total[0] >> 29)
|
||||
| (ctx->total[1] << 3);
|
||||
low = (ctx->total[0] << 3);
|
||||
|
||||
PUT_UINT32(high, msglen, 0);
|
||||
PUT_UINT32(low, msglen, 4);
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||
|
||||
SHA256_Update(ctx, sha256_padding, padn);
|
||||
SHA256_Update(ctx, msglen, 8);
|
||||
|
||||
PUT_UINT32(ctx->state[0], digest, 0);
|
||||
PUT_UINT32(ctx->state[1], digest, 4);
|
||||
PUT_UINT32(ctx->state[2], digest, 8);
|
||||
PUT_UINT32(ctx->state[3], digest, 12);
|
||||
PUT_UINT32(ctx->state[4], digest, 16);
|
||||
PUT_UINT32(ctx->state[5], digest, 20);
|
||||
PUT_UINT32(ctx->state[6], digest, 24);
|
||||
PUT_UINT32(ctx->state[7], digest, 28);
|
||||
}
|
||||
77
Argyll_V3.5.0/ccast/axTLS/sha384.c
Normal file
77
Argyll_V3.5.0/ccast/axTLS/sha384.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
/**
|
||||
* Initialize the SHA384 context
|
||||
*/
|
||||
void SHA384_Init(SHA384_CTX *ctx)
|
||||
{
|
||||
//Set initial hash value
|
||||
ctx->h_dig.h[0] = CF64PREC(0xCBBB9D5DC1059ED8);
|
||||
ctx->h_dig.h[1] = CF64PREC(0x629A292A367CD507);
|
||||
ctx->h_dig.h[2] = CF64PREC(0x9159015A3070DD17);
|
||||
ctx->h_dig.h[3] = CF64PREC(0x152FECD8F70E5939);
|
||||
ctx->h_dig.h[4] = CF64PREC(0x67332667FFC00B31);
|
||||
ctx->h_dig.h[5] = CF64PREC(0x8EB44A8768581511);
|
||||
ctx->h_dig.h[6] = CF64PREC(0xDB0C2E0D64F98FA7);
|
||||
ctx->h_dig.h[7] = CF64PREC(0x47B5481DBEFA4FA4);
|
||||
|
||||
// Number of bytes in the buffer
|
||||
ctx->size = 0;
|
||||
// Total length of the message
|
||||
ctx->totalSize = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an array of octets as the next portion of the message.
|
||||
*/
|
||||
void SHA384_Update(SHA384_CTX *ctx, const uint8_t * msg, int len)
|
||||
{
|
||||
// The function is defined in the exact same manner as SHA-512
|
||||
SHA512_Update(ctx, msg, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 384-bit message digest into the user's array
|
||||
*/
|
||||
void SHA384_Final(uint8_t *digest, SHA384_CTX *ctx)
|
||||
{
|
||||
// The function is defined in the exact same manner as SHA-512
|
||||
SHA512_Final(NULL, ctx);
|
||||
|
||||
// Copy the resulting digest
|
||||
if (digest != NULL)
|
||||
memcpy(digest, ctx->h_dig.digest, SHA384_SIZE);
|
||||
}
|
||||
|
||||
220
Argyll_V3.5.0/ccast/axTLS/sha512.c
Normal file
220
Argyll_V3.5.0/ccast/axTLS/sha512.c
Normal file
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 <string.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
|
||||
#define SHR64(a, n) ((a) >> (n))
|
||||
#define ROR64(a, n) (((a) >> (n)) | ((a) << (64 - (n))))
|
||||
#define CH(x, y, z) (((x) & (y)) | (~(x) & (z)))
|
||||
#define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
|
||||
#define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39))
|
||||
#define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41))
|
||||
#define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ SHR64(x, 7))
|
||||
#define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(x, 6))
|
||||
#define MIN(x, y) ((x) < (y) ? x : y)
|
||||
|
||||
static const uint8_t padding[128] =
|
||||
{
|
||||
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
static const uint64_t k[80] =
|
||||
{
|
||||
CF64PREC(0x428A2F98D728AE22), CF64PREC(0x7137449123EF65CD), CF64PREC(0xB5C0FBCFEC4D3B2F), CF64PREC(0xE9B5DBA58189DBBC),
|
||||
CF64PREC(0x3956C25BF348B538), CF64PREC(0x59F111F1B605D019), CF64PREC(0x923F82A4AF194F9B), CF64PREC(0xAB1C5ED5DA6D8118),
|
||||
CF64PREC(0xD807AA98A3030242), CF64PREC(0x12835B0145706FBE), CF64PREC(0x243185BE4EE4B28C), CF64PREC(0x550C7DC3D5FFB4E2),
|
||||
CF64PREC(0x72BE5D74F27B896F), CF64PREC(0x80DEB1FE3B1696B1), CF64PREC(0x9BDC06A725C71235), CF64PREC(0xC19BF174CF692694),
|
||||
CF64PREC(0xE49B69C19EF14AD2), CF64PREC(0xEFBE4786384F25E3), CF64PREC(0x0FC19DC68B8CD5B5), CF64PREC(0x240CA1CC77AC9C65),
|
||||
CF64PREC(0x2DE92C6F592B0275), CF64PREC(0x4A7484AA6EA6E483), CF64PREC(0x5CB0A9DCBD41FBD4), CF64PREC(0x76F988DA831153B5),
|
||||
CF64PREC(0x983E5152EE66DFAB), CF64PREC(0xA831C66D2DB43210), CF64PREC(0xB00327C898FB213F), CF64PREC(0xBF597FC7BEEF0EE4),
|
||||
CF64PREC(0xC6E00BF33DA88FC2), CF64PREC(0xD5A79147930AA725), CF64PREC(0x06CA6351E003826F), CF64PREC(0x142929670A0E6E70),
|
||||
CF64PREC(0x27B70A8546D22FFC), CF64PREC(0x2E1B21385C26C926), CF64PREC(0x4D2C6DFC5AC42AED), CF64PREC(0x53380D139D95B3DF),
|
||||
CF64PREC(0x650A73548BAF63DE), CF64PREC(0x766A0ABB3C77B2A8), CF64PREC(0x81C2C92E47EDAEE6), CF64PREC(0x92722C851482353B),
|
||||
CF64PREC(0xA2BFE8A14CF10364), CF64PREC(0xA81A664BBC423001), CF64PREC(0xC24B8B70D0F89791), CF64PREC(0xC76C51A30654BE30),
|
||||
CF64PREC(0xD192E819D6EF5218), CF64PREC(0xD69906245565A910), CF64PREC(0xF40E35855771202A), CF64PREC(0x106AA07032BBD1B8),
|
||||
CF64PREC(0x19A4C116B8D2D0C8), CF64PREC(0x1E376C085141AB53), CF64PREC(0x2748774CDF8EEB99), CF64PREC(0x34B0BCB5E19B48A8),
|
||||
CF64PREC(0x391C0CB3C5C95A63), CF64PREC(0x4ED8AA4AE3418ACB), CF64PREC(0x5B9CCA4F7763E373), CF64PREC(0x682E6FF3D6B2B8A3),
|
||||
CF64PREC(0x748F82EE5DEFB2FC), CF64PREC(0x78A5636F43172F60), CF64PREC(0x84C87814A1F0AB72), CF64PREC(0x8CC702081A6439EC),
|
||||
CF64PREC(0x90BEFFFA23631E28), CF64PREC(0xA4506CEBDE82BDE9), CF64PREC(0xBEF9A3F7B2C67915), CF64PREC(0xC67178F2E372532B),
|
||||
CF64PREC(0xCA273ECEEA26619C), CF64PREC(0xD186B8C721C0C207), CF64PREC(0xEADA7DD6CDE0EB1E), CF64PREC(0xF57D4F7FEE6ED178),
|
||||
CF64PREC(0x06F067AA72176FBA), CF64PREC(0x0A637DC5A2C898A6), CF64PREC(0x113F9804BEF90DAE), CF64PREC(0x1B710B35131C471B),
|
||||
CF64PREC(0x28DB77F523047D84), CF64PREC(0x32CAAB7B40C72493), CF64PREC(0x3C9EBE0A15C9BEBC), CF64PREC(0x431D67C49C100D4C),
|
||||
CF64PREC(0x4CC5D4BECB3E42B6), CF64PREC(0x597F299CFC657E2A), CF64PREC(0x5FCB6FAB3AD6FAEC), CF64PREC(0x6C44198C4A475817)
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the SHA512 context
|
||||
*/
|
||||
void SHA512_Init(SHA512_CTX *ctx)
|
||||
{
|
||||
ctx->h_dig.h[0] = CF64PREC(0x6A09E667F3BCC908);
|
||||
ctx->h_dig.h[1] = CF64PREC(0xBB67AE8584CAA73B);
|
||||
ctx->h_dig.h[2] = CF64PREC(0x3C6EF372FE94F82B);
|
||||
ctx->h_dig.h[3] = CF64PREC(0xA54FF53A5F1D36F1);
|
||||
ctx->h_dig.h[4] = CF64PREC(0x510E527FADE682D1);
|
||||
ctx->h_dig.h[5] = CF64PREC(0x9B05688C2B3E6C1F);
|
||||
ctx->h_dig.h[6] = CF64PREC(0x1F83D9ABFB41BD6B);
|
||||
ctx->h_dig.h[7] = CF64PREC(0x5BE0CD19137E2179);
|
||||
ctx->size = 0;
|
||||
ctx->totalSize = 0;
|
||||
}
|
||||
|
||||
static void SHA512_Process(SHA512_CTX *ctx)
|
||||
{
|
||||
int t;
|
||||
uint64_t temp1;
|
||||
uint64_t temp2;
|
||||
|
||||
// Initialize the 8 working registers
|
||||
uint64_t a = ctx->h_dig.h[0];
|
||||
uint64_t b = ctx->h_dig.h[1];
|
||||
uint64_t c = ctx->h_dig.h[2];
|
||||
uint64_t d = ctx->h_dig.h[3];
|
||||
uint64_t e = ctx->h_dig.h[4];
|
||||
uint64_t f = ctx->h_dig.h[5];
|
||||
uint64_t g = ctx->h_dig.h[6];
|
||||
uint64_t h = ctx->h_dig.h[7];
|
||||
|
||||
// Process message in 16-word blocks
|
||||
uint64_t *w = ctx->w_buf.w;
|
||||
|
||||
// Convert from big-endian byte order to host byte order
|
||||
for (t = 0; t < 16; t++)
|
||||
w[t] = be64toh(w[t]);
|
||||
|
||||
// Prepare the message schedule
|
||||
for (t = 16; t < 80; t++)
|
||||
w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16];
|
||||
|
||||
// SHA-512 hash computation
|
||||
for (t = 0; t < 80; t++)
|
||||
{
|
||||
// Calculate T1 and T2
|
||||
temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + w[t];
|
||||
temp2 = SIGMA1(a) + MAJ(a, b, c);
|
||||
|
||||
// Update the working registers
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
|
||||
// Update the hash value
|
||||
ctx->h_dig.h[0] += a;
|
||||
ctx->h_dig.h[1] += b;
|
||||
ctx->h_dig.h[2] += c;
|
||||
ctx->h_dig.h[3] += d;
|
||||
ctx->h_dig.h[4] += e;
|
||||
ctx->h_dig.h[5] += f;
|
||||
ctx->h_dig.h[6] += g;
|
||||
ctx->h_dig.h[7] += h;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts an array of octets as the next portion of the message.
|
||||
*/
|
||||
void SHA512_Update(SHA512_CTX *ctx, const uint8_t * msg, int len)
|
||||
{
|
||||
// Process the incoming data
|
||||
while (len > 0)
|
||||
{
|
||||
// The buffer can hold at most 128 bytes
|
||||
size_t n = MIN(len, 128 - ctx->size);
|
||||
|
||||
// Copy the data to the buffer
|
||||
memcpy(ctx->w_buf.buffer + ctx->size, msg, n);
|
||||
|
||||
// Update the SHA-512 ctx
|
||||
ctx->size += n;
|
||||
ctx->totalSize += n;
|
||||
// Advance the data pointer
|
||||
msg = (uint8_t *) msg + n;
|
||||
// Remaining bytes to process
|
||||
len -= n;
|
||||
|
||||
// Process message in 16-word blocks
|
||||
if (ctx->size == 128)
|
||||
{
|
||||
// Transform the 16-word block
|
||||
SHA512_Process(ctx);
|
||||
// Empty the buffer
|
||||
ctx->size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the 512-bit message digest into the user's array
|
||||
*/
|
||||
void SHA512_Final(uint8_t *digest, SHA512_CTX *ctx)
|
||||
{
|
||||
int i;
|
||||
size_t paddingSize;
|
||||
uint64_t totalSize;
|
||||
|
||||
// Length of the original message (before padding)
|
||||
totalSize = ctx->totalSize * 8;
|
||||
|
||||
// Pad the message so that its length is congruent to 112 modulo 128
|
||||
paddingSize = (ctx->size < 112) ? (112 - ctx->size) :
|
||||
(128 + 112 - ctx->size);
|
||||
// Append padding
|
||||
SHA512_Update(ctx, padding, paddingSize);
|
||||
|
||||
// Append the length of the original message
|
||||
ctx->w_buf.w[14] = 0;
|
||||
ctx->w_buf.w[15] = be64toh(totalSize);
|
||||
|
||||
// Calculate the message digest
|
||||
SHA512_Process(ctx);
|
||||
|
||||
// Convert from host byte order to big-endian byte order
|
||||
for (i = 0; i < 8; i++)
|
||||
ctx->h_dig.h[i] = be64toh(ctx->h_dig.h[i]);
|
||||
|
||||
// Copy the resulting digest
|
||||
if (digest != NULL)
|
||||
memcpy(digest, ctx->h_dig.digest, SHA512_SIZE);
|
||||
}
|
||||
|
||||
556
Argyll_V3.5.0/ccast/axTLS/ssl.h
Normal file
556
Argyll_V3.5.0/ccast/axTLS/ssl.h
Normal file
@@ -0,0 +1,556 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @mainpage axTLS API
|
||||
*
|
||||
* @image html axolotl.jpg
|
||||
*
|
||||
* The axTLS library has features such as:
|
||||
* - The TLSv1 SSL client/server protocol
|
||||
* - No requirement to use any openssl libraries.
|
||||
* - A choice between AES block (128/256 bit) and RC4 (128 bit) stream ciphers.
|
||||
* - RSA encryption/decryption with variable sized keys (up to 4096 bits).
|
||||
* - Certificate chaining and peer authentication.
|
||||
* - Session resumption, session renegotiation.
|
||||
* - ASN.1, X.509, PKCS#8, PKCS#12 keys/certificates with DER/PEM encoding.
|
||||
* - Highly configurable compile time options.
|
||||
* - Portable across many platforms (written in ANSI C), and has language
|
||||
* bindings in C, C#, VB.NET, Java, Perl and Lua.
|
||||
* - Partial openssl API compatibility (via a wrapper).
|
||||
* - A very small footprint (around 50-60kB for the library in 'server-only'
|
||||
* mode).
|
||||
* - No dependencies on sockets - can use serial connections for example.
|
||||
* - A very simple API - ~ 20 functions/methods.
|
||||
*
|
||||
* A list of these functions/methods are described below.
|
||||
*
|
||||
* @ref c_api
|
||||
*
|
||||
* @ref bigint_api
|
||||
*
|
||||
* @ref csharp_api
|
||||
*
|
||||
* @ref java_api
|
||||
*/
|
||||
#ifndef HEADER_SSL_H
|
||||
#define HEADER_SSL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
|
||||
/* need to predefine before ssl_lib.h gets to it */
|
||||
#define SSL_SESSION_ID_SIZE 32
|
||||
|
||||
#include "tls1.h"
|
||||
|
||||
/* The optional parameters that can be given to the client/server SSL engine */
|
||||
#define SSL_CLIENT_AUTHENTICATION 0x00010000
|
||||
#define SSL_SERVER_VERIFY_LATER 0x00020000
|
||||
#define SSL_NO_DEFAULT_KEY 0x00040000
|
||||
#define SSL_DISPLAY_STATES 0x00080000
|
||||
#define SSL_DISPLAY_BYTES 0x00100000
|
||||
#define SSL_DISPLAY_CERTS 0x00200000
|
||||
#define SSL_DISPLAY_RSA 0x00400000
|
||||
#define SSL_CONNECT_IN_PARTS 0x00800000
|
||||
|
||||
/* errors that can be generated */
|
||||
#define SSL_OK 0
|
||||
#define SSL_NOT_OK -1
|
||||
#define SSL_ERROR_DEAD -2
|
||||
#define SSL_CLOSE_NOTIFY -3
|
||||
#define SSL_TIMEDOUT -4 // GWG
|
||||
#define SSL_ERROR_CONN_LOST -256
|
||||
#define SSL_ERROR_RECORD_OVERFLOW -257
|
||||
#define SSL_ERROR_SOCK_SETUP_FAILURE -258
|
||||
#define SSL_ERROR_INVALID_HANDSHAKE -260
|
||||
#define SSL_ERROR_INVALID_PROT_MSG -261
|
||||
#define SSL_ERROR_INVALID_HMAC -262
|
||||
#define SSL_ERROR_INVALID_VERSION -263
|
||||
#define SSL_ERROR_UNSUPPORTED_EXTENSION -264
|
||||
#define SSL_ERROR_INVALID_SESSION -265
|
||||
#define SSL_ERROR_NO_CIPHER -266
|
||||
#define SSL_ERROR_INVALID_CERT_HASH_ALG -267
|
||||
#define SSL_ERROR_BAD_CERTIFICATE -268
|
||||
#define SSL_ERROR_INVALID_KEY -269
|
||||
#define SSL_ERROR_FINISHED_INVALID -271
|
||||
#define SSL_ERROR_NO_CERT_DEFINED -272
|
||||
#define SSL_ERROR_NO_CLIENT_RENOG -273
|
||||
#define SSL_ERROR_NOT_SUPPORTED -274
|
||||
#define SSL_X509_OFFSET -512
|
||||
#define SSL_X509_ERROR(A) (SSL_X509_OFFSET+A)
|
||||
|
||||
/* alert types that are recognized */
|
||||
#define SSL_ALERT_TYPE_WARNING 1
|
||||
#define SLL_ALERT_TYPE_FATAL 2
|
||||
|
||||
/* these are all the alerts that are recognized */
|
||||
#define SSL_ALERT_CLOSE_NOTIFY 0
|
||||
#define SSL_ALERT_UNEXPECTED_MESSAGE 10
|
||||
#define SSL_ALERT_BAD_RECORD_MAC 20
|
||||
#define SSL_ALERT_RECORD_OVERFLOW 22
|
||||
#define SSL_ALERT_HANDSHAKE_FAILURE 40
|
||||
#define SSL_ALERT_BAD_CERTIFICATE 42
|
||||
#define SSL_ALERT_UNSUPPORTED_CERTIFICATE 43
|
||||
#define SSL_ALERT_CERTIFICATE_EXPIRED 45
|
||||
#define SSL_ALERT_CERTIFICATE_UNKNOWN 46
|
||||
#define SSL_ALERT_ILLEGAL_PARAMETER 47
|
||||
#define SSL_ALERT_UNKNOWN_CA 48
|
||||
#define SSL_ALERT_DECODE_ERROR 50
|
||||
#define SSL_ALERT_DECRYPT_ERROR 51
|
||||
#define SSL_ALERT_INVALID_VERSION 70
|
||||
#define SSL_ALERT_NO_RENEGOTIATION 100
|
||||
#define SSL_ALERT_UNSUPPORTED_EXTENSION 110
|
||||
|
||||
/* The ciphers that are supported */
|
||||
#define SSL_AES128_SHA 0x2f
|
||||
#define SSL_AES256_SHA 0x35
|
||||
#define SSL_AES128_SHA256 0x3c
|
||||
#define SSL_AES256_SHA256 0x3d
|
||||
|
||||
/* build mode ids' */
|
||||
#define SSL_BUILD_SKELETON_MODE 0x01
|
||||
#define SSL_BUILD_SERVER_ONLY 0x02
|
||||
#define SSL_BUILD_ENABLE_VERIFICATION 0x03
|
||||
#define SSL_BUILD_ENABLE_CLIENT 0x04
|
||||
#define SSL_BUILD_FULL_MODE 0x05
|
||||
|
||||
/* offsets to retrieve configuration information */
|
||||
#define SSL_BUILD_MODE 0
|
||||
#define SSL_MAX_CERT_CFG_OFFSET 1
|
||||
#define SSL_MAX_CA_CERT_CFG_OFFSET 2
|
||||
#define SSL_HAS_PEM 3
|
||||
|
||||
/* default session sizes */
|
||||
#define SSL_DEFAULT_SVR_SESS 5
|
||||
#define SSL_DEFAULT_CLNT_SESS 1
|
||||
|
||||
/* X.509/X.520 distinguished name types */
|
||||
#define SSL_X509_CERT_COMMON_NAME 0
|
||||
#define SSL_X509_CERT_ORGANIZATION 1
|
||||
#define SSL_X509_CERT_ORGANIZATIONAL_NAME 2
|
||||
#define SSL_X509_CERT_LOCATION 3
|
||||
#define SSL_X509_CERT_COUNTRY 4
|
||||
#define SSL_X509_CERT_STATE 5
|
||||
#define SSL_X509_CA_CERT_COMMON_NAME 6
|
||||
#define SSL_X509_CA_CERT_ORGANIZATION 7
|
||||
#define SSL_X509_CA_CERT_ORGANIZATIONAL_NAME 8
|
||||
#define SSL_X509_CA_CERT_LOCATION 9
|
||||
#define SSL_X509_CA_CERT_COUNTRY 10
|
||||
#define SSL_X509_CA_CERT_STATE 11
|
||||
|
||||
/* SSL object loader types */
|
||||
#define SSL_OBJ_X509_CERT 1
|
||||
#define SSL_OBJ_X509_CACERT 2
|
||||
#define SSL_OBJ_RSA_KEY 3
|
||||
#define SSL_OBJ_PKCS8 4
|
||||
#define SSL_OBJ_PKCS12 5
|
||||
|
||||
/**
|
||||
* @defgroup c_api Standard C API
|
||||
* @brief The standard interface in C.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Establish a new client/server context.
|
||||
*
|
||||
* This function is called before any client/server SSL connections are made.
|
||||
*
|
||||
* Each new connection will use the this context's private key and
|
||||
* certificate chain. If a different certificate chain is required, then a
|
||||
* different context needs to be be used.
|
||||
*
|
||||
* There are two threading models supported - a single thread with one
|
||||
* SSL_CTX can support any number of SSL connections - and multiple threads can
|
||||
* support one SSL_CTX object each (the default). But if a single SSL_CTX
|
||||
* object uses many SSL objects in individual threads, then the
|
||||
* CONFIG_SSL_CTX_MUTEXING option needs to be configured.
|
||||
*
|
||||
* @param options [in] Any particular options. At present the options
|
||||
* supported are:
|
||||
* - SSL_SERVER_VERIFY_LATER (client only): Don't stop a handshake if the server
|
||||
* authentication fails. The certificate can be authenticated later with a
|
||||
* call to ssl_verify_cert().
|
||||
* - SSL_CLIENT_AUTHENTICATION (server only): Enforce client authentication
|
||||
* i.e. each handshake will include a "certificate request" message from the
|
||||
* server. Only available if verification has been enabled.
|
||||
* - SSL_DISPLAY_BYTES (full mode build only): Display the byte sequences
|
||||
* during the handshake.
|
||||
* - SSL_DISPLAY_STATES (full mode build only): Display the state changes
|
||||
* during the handshake.
|
||||
* - SSL_DISPLAY_CERTS (full mode build only): Display the certificates that
|
||||
* are passed during a handshake.
|
||||
* - SSL_DISPLAY_RSA (full mode build only): Display the RSA key details that
|
||||
* are passed during a handshake.
|
||||
* - SSL_CONNECT_IN_PARTS (client only): To use a non-blocking version of
|
||||
* ssl_client_new().
|
||||
* @param num_sessions [in] The number of sessions to be used for session
|
||||
* caching. If this value is 0, then there is no session caching. This option
|
||||
* is not used in skeleton mode.
|
||||
* @return A client/server context.
|
||||
*/
|
||||
EXP_FUNC SSL_CTX * STDCALL ssl_ctx_new(uint32_t options, int num_sessions);
|
||||
|
||||
/**
|
||||
* @brief Remove a client/server context.
|
||||
*
|
||||
* Frees any used resources used by this context. Each connection will be
|
||||
* sent a "Close Notify" alert (if possible).
|
||||
* @param ssl_ctx [in] The client/server context.
|
||||
*/
|
||||
EXP_FUNC void STDCALL ssl_ctx_free(SSL_CTX *ssl_ctx);
|
||||
|
||||
/**
|
||||
* @brief Allocates new SSL extensions structure and returns pointer to it
|
||||
*
|
||||
* Needed for the SNI support.
|
||||
* Needed for the Max Fragment Size Extension.
|
||||
*
|
||||
* @return ssl_ext Pointer to SSL_EXTENSIONS structure
|
||||
*
|
||||
*/
|
||||
EXP_FUNC SSL_EXTENSIONS * STDCALL ssl_ext_new(void);
|
||||
|
||||
/**
|
||||
* @brief Frees SSL extensions structure
|
||||
*
|
||||
* @param ssl_ext [in] Pointer to SSL_EXTENSION structure
|
||||
*
|
||||
*/
|
||||
EXP_FUNC void STDCALL ssl_ext_free(SSL_EXTENSIONS *ssl_ext);
|
||||
|
||||
/**
|
||||
* @brief (server only) Establish a new SSL connection to an SSL client.
|
||||
*
|
||||
* It is up to the application to establish the logical connection (whether it
|
||||
* is a socket, serial connection etc).
|
||||
* @param ssl_ctx [in] The server context.
|
||||
* @param client_fd [in] The client's file descriptor.
|
||||
* @return An SSL object reference.
|
||||
*/
|
||||
EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd);
|
||||
|
||||
/**
|
||||
* @brief (client only) Establish a new SSL connection to an SSL server.
|
||||
*
|
||||
* It is up to the application to establish the initial logical connection
|
||||
* (whether it is a socket, serial connection etc).
|
||||
*
|
||||
* This is a normally a blocking call - it will finish when the handshake is
|
||||
* complete (or has failed). To use in non-blocking mode, set
|
||||
* SSL_CONNECT_IN_PARTS in ssl_ctx_new().
|
||||
* @param ssl_ctx [in] The client context.
|
||||
* @param client_fd [in] The client's file descriptor.
|
||||
* @param session_id [in] A 32 byte session id for session resumption. This
|
||||
* can be null if no session resumption is being used or required. This option
|
||||
* is not used in skeleton mode.
|
||||
* @param sess_id_size The size of the session id (max 32)
|
||||
* @param ssl_ext pointer to a structure with the activated SSL extensions
|
||||
* and their values
|
||||
* @return An SSL object reference. Use ssl_handshake_status() to check
|
||||
* if a handshake succeeded.
|
||||
*/
|
||||
EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const uint8_t *session_id, uint8_t sess_id_size, SSL_EXTENSIONS* ssl_ext);
|
||||
|
||||
/**
|
||||
* @brief Free any used resources on this connection.
|
||||
|
||||
* A "Close Notify" message is sent on this connection (if possible). It is up
|
||||
* to the application to close the socket or file descriptor.
|
||||
* @param ssl [in] The ssl object reference.
|
||||
*/
|
||||
EXP_FUNC void STDCALL ssl_free(SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Read the SSL data stream.
|
||||
* If the socket is non-blocking and data is blocked then SSO_OK will be
|
||||
* returned.
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @param in_data [out] If the read was successful, a pointer to the read
|
||||
* buffer will be here. Do NOT ever free this memory as this buffer is used in
|
||||
* sucessive calls. If the call was unsuccessful, this value will be null.
|
||||
* @return The number of decrypted bytes:
|
||||
* - if > 0, then the handshaking is complete and we are returning the number
|
||||
* of decrypted bytes.
|
||||
* - SSL_OK if the handshaking stage is successful (but not yet complete).
|
||||
* - < 0 if an error.
|
||||
* @see ssl.h for the error code list.
|
||||
* @note Use in_data before doing any successive ssl calls.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data);
|
||||
|
||||
/* Synchronous internal version of above used for protocol negotiation - GWG */
|
||||
EXP_FUNC int STDCALL ssl_readi(SSL *ssl, uint8_t **in_data);
|
||||
|
||||
/**
|
||||
* @brief Write to the SSL data stream.
|
||||
* if the socket is non-blocking and data is blocked then a check is made
|
||||
* to ensure that all data is sent (i.e. blocked mode is forced).
|
||||
* @param ssl [in] An SSL obect reference.
|
||||
* @param out_data [in] The data to be written
|
||||
* @param out_len [in] The number of bytes to be written.
|
||||
* @return The number of bytes sent, or if < 0 if an error.
|
||||
* @see ssl.h for the error code list.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len);
|
||||
|
||||
/**
|
||||
* @brief Find an ssl object based on a file descriptor.
|
||||
*
|
||||
* Goes through the list of SSL objects maintained in a client/server context
|
||||
* to look for a file descriptor match.
|
||||
* @param ssl_ctx [in] The client/server context.
|
||||
* @param client_fd [in] The file descriptor.
|
||||
* @return A reference to the SSL object. Returns null if the object could not
|
||||
* be found.
|
||||
*/
|
||||
EXP_FUNC SSL * STDCALL ssl_find(SSL_CTX *ssl_ctx, int client_fd);
|
||||
|
||||
/**
|
||||
* @brief Get the session id for a handshake.
|
||||
*
|
||||
* This will be a 32 byte sequence and is available after the first
|
||||
* handshaking messages are sent.
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @return The session id as a 32 byte sequence.
|
||||
* @note A SSLv23 handshake may have only 16 valid bytes.
|
||||
*/
|
||||
EXP_FUNC const uint8_t * STDCALL ssl_get_session_id(const SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Get the session id size for a handshake.
|
||||
*
|
||||
* This will normally be 32 but could be 0 (no session id) or something else.
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @return The size of the session id.
|
||||
*/
|
||||
EXP_FUNC uint8_t STDCALL ssl_get_session_id_size(const SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Return the cipher id (in the SSL form).
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @return The cipher id. This will be one of the following:
|
||||
* - SSL_AES128_SHA (0x2f)
|
||||
* - SSL_AES256_SHA (0x35)
|
||||
* - SSL_AES128_SHA256 (0x3c)
|
||||
* - SSL_AES256_SHA256 (0x3d)
|
||||
*/
|
||||
EXP_FUNC uint8_t STDCALL ssl_get_cipher_id(const SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Return the status of the handshake.
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @return SSL_OK if the handshake is complete and ok.
|
||||
* @see ssl.h for the error code list.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_handshake_status(const SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Retrieve various parameters about the axTLS engine.
|
||||
* @param offset [in] The configuration offset. It will be one of the following:
|
||||
* - SSL_BUILD_MODE The build mode. This will be one of the following:
|
||||
* - SSL_BUILD_SERVER_ONLY (basic server mode)
|
||||
* - SSL_BUILD_ENABLE_VERIFICATION (server can do client authentication)
|
||||
* - SSL_BUILD_ENABLE_CLIENT (client/server capabilties)
|
||||
* - SSL_BUILD_FULL_MODE (client/server with diagnostics)
|
||||
* - SSL_BUILD_SKELETON_MODE (skeleton mode)
|
||||
* - SSL_MAX_CERT_CFG_OFFSET The maximum number of certificates allowed.
|
||||
* - SSL_MAX_CA_CERT_CFG_OFFSET The maximum number of CA certificates allowed.
|
||||
* - SSL_HAS_PEM 1 if supported
|
||||
* @return The value of the requested parameter.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_get_config(int offset);
|
||||
|
||||
/**
|
||||
* GWG
|
||||
* @brief Return a string explaining why the handshake failed.
|
||||
*
|
||||
* This call is can be used by diagnostics elsewhere.
|
||||
* @param error_code [in] An error code.
|
||||
* @return The appropriate string, which may be static.
|
||||
* @see ssl.h for the error code list.
|
||||
*/
|
||||
EXP_FUNC const char * STDCALL ssl_error_string(int error_code);
|
||||
|
||||
/**
|
||||
* @brief Display why the handshake failed.
|
||||
*
|
||||
* This call is only useful in a 'full mode' build. The output is to stdout.
|
||||
* @param error_code [in] An error code.
|
||||
* @see ssl.h for the error code list.
|
||||
*/
|
||||
EXP_FUNC void STDCALL ssl_display_error(int error_code);
|
||||
|
||||
/**
|
||||
* @brief Authenticate a received certificate.
|
||||
*
|
||||
* This call is usually made by a client after a handshake is complete and the
|
||||
* context is in SSL_SERVER_VERIFY_LATER mode.
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @return SSL_OK if the certificate is verified.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_verify_cert(const SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Retrieve an X.509 distinguished name component.
|
||||
*
|
||||
* When a handshake is complete and a certificate has been exchanged, then the
|
||||
* details of the remote certificate can be retrieved.
|
||||
*
|
||||
* This will usually be used by a client to check that the server's common
|
||||
* name matches the URL.
|
||||
*
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @param component [in] one of:
|
||||
* - SSL_X509_CERT_COMMON_NAME
|
||||
* - SSL_X509_CERT_ORGANIZATION
|
||||
* - SSL_X509_CERT_ORGANIZATIONAL_NAME
|
||||
* - SSL_X509_CERT_LOCATION
|
||||
* - SSL_X509_CERT_COUNTRY
|
||||
* - SSL_X509_CERT_STATE
|
||||
* - SSL_X509_CA_CERT_COMMON_NAME
|
||||
* - SSL_X509_CA_CERT_ORGANIZATION
|
||||
* - SSL_X509_CA_CERT_ORGANIZATIONAL_NAME
|
||||
* - SSL_X509_CA_CERT_LOCATION
|
||||
* - SSL_X509_CA_CERT_COUNTRY
|
||||
* - SSL_X509_CA_CERT_STATE
|
||||
* @return The appropriate string (or null if not defined)
|
||||
* @note Verification build mode must be enabled.
|
||||
*/
|
||||
EXP_FUNC const char * STDCALL ssl_get_cert_dn(const SSL *ssl, int component);
|
||||
|
||||
/**
|
||||
* @brief Retrieve a Subject Alternative DNSName
|
||||
*
|
||||
* When a handshake is complete and a certificate has been exchanged, then the
|
||||
* details of the remote certificate can be retrieved.
|
||||
*
|
||||
* This will usually be used by a client to check that the server's DNS
|
||||
* name matches the URL.
|
||||
*
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @param dnsindex [in] The index of the DNS name to retrieve.
|
||||
* @return The appropriate string (or null if not defined)
|
||||
* @note Verification build mode must be enabled.
|
||||
*/
|
||||
EXP_FUNC const char * STDCALL ssl_get_cert_subject_alt_dnsname(const SSL *ssl, int dnsindex);
|
||||
|
||||
/**
|
||||
* @brief Force the client to perform its handshake again.
|
||||
*
|
||||
* For a client this involves sending another "client hello" message.
|
||||
* For the server is means sending a "hello request" message.
|
||||
*
|
||||
* This is a blocking call on the client (until the handshake completes).
|
||||
*
|
||||
* @param ssl [in] An SSL object reference.
|
||||
* @return SSL_OK if renegotiation instantiation was ok
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_renegotiate(SSL *ssl);
|
||||
|
||||
/**
|
||||
* @brief Process a file that is in binary DER or ASCII PEM format.
|
||||
*
|
||||
* These are temporary objects that are used to load private keys,
|
||||
* certificates etc into memory.
|
||||
* @param ssl_ctx [in] The client/server context.
|
||||
* @param obj_type [in] The format of the file. Can be one of:
|
||||
* - SSL_OBJ_X509_CERT (no password required)
|
||||
* - SSL_OBJ_X509_CACERT (no password required)
|
||||
* - SSL_OBJ_RSA_KEY (AES128/AES256 PEM encryption supported)
|
||||
* - SSL_OBJ_PKCS8 (RC4-128 encrypted data supported)
|
||||
* - SSL_OBJ_PKCS12 (RC4-128 encrypted data supported)
|
||||
*
|
||||
* PEM files are automatically detected (if supported). The object type is
|
||||
* also detected, and so is not relevant for these types of files.
|
||||
* @param filename [in] The location of a file in DER/PEM format.
|
||||
* @param password [in] The password used. Can be null if not required.
|
||||
* @return SSL_OK if all ok
|
||||
* @note Not available in skeleton build mode.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_obj_load(SSL_CTX *ssl_ctx, int obj_type, const char *filename, const char *password);
|
||||
|
||||
/**
|
||||
* @brief Process binary data.
|
||||
*
|
||||
* These are temporary objects that are used to load private keys,
|
||||
* certificates etc into memory.
|
||||
* @param ssl_ctx [in] The client/server context.
|
||||
* @param obj_type [in] The format of the memory data.
|
||||
* @param data [in] The binary data to be loaded.
|
||||
* @param len [in] The amount of data to be loaded.
|
||||
* @param password [in] The password used. Can be null if not required.
|
||||
* @return SSL_OK if all ok
|
||||
* @see ssl_obj_load for more details on obj_type.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_obj_memory_load(SSL_CTX *ssl_ctx, int obj_type, const uint8_t *data, int len, const char *password);
|
||||
|
||||
#ifdef CONFIG_SSL_GENERATE_X509_CERT
|
||||
/**
|
||||
* @brief Create an X.509 certificate.
|
||||
*
|
||||
* This certificate is a self-signed v1 cert with a fixed start/stop validity
|
||||
* times. It is signed with an internal private key in ssl_ctx.
|
||||
*
|
||||
* @param ssl_ctx [in] The client/server context.
|
||||
* @param options [in] Not used yet.
|
||||
* @param dn [in] An array of distinguished name strings. The array is defined
|
||||
* by:
|
||||
* - SSL_X509_CERT_COMMON_NAME (0)
|
||||
* - If SSL_X509_CERT_COMMON_NAME is empty or not defined, then the
|
||||
* hostname will be used.
|
||||
* - SSL_X509_CERT_ORGANIZATION (1)
|
||||
* - If SSL_X509_CERT_ORGANIZATION is empty or not defined, then $USERNAME
|
||||
* will be used.
|
||||
* - SSL_X509_CERT_ORGANIZATIONAL_NAME (2)
|
||||
* - SSL_X509_CERT_ORGANIZATIONAL_NAME is optional.
|
||||
* @param cert_data [out] The certificate as a sequence of bytes.
|
||||
* @return < 0 if an error, or the size of the certificate in bytes.
|
||||
* @note cert_data must be freed when there is no more need for it.
|
||||
*/
|
||||
EXP_FUNC int STDCALL ssl_x509_create(SSL_CTX *ssl_ctx, uint32_t options, const char * dn[], uint8_t **cert_data);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Return the axTLS library version as a string.
|
||||
*/
|
||||
EXP_FUNC const char * STDCALL ssl_version(void);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
2741
Argyll_V3.5.0/ccast/axTLS/tls1.c
Normal file
2741
Argyll_V3.5.0/ccast/axTLS/tls1.c
Normal file
File diff suppressed because it is too large
Load Diff
333
Argyll_V3.5.0/ccast/axTLS/tls1.h
Normal file
333
Argyll_V3.5.0/ccast/axTLS/tls1.h
Normal file
@@ -0,0 +1,333 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file tls1.h
|
||||
*
|
||||
* @brief The definitions for the TLS library.
|
||||
*/
|
||||
#ifndef HEADER_SSL_LIB_H
|
||||
#define HEADER_SSL_LIB_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "axTLS_version.h"
|
||||
#include "axTLS_config.h"
|
||||
#include "os_int.h"
|
||||
#include "os_port.h"
|
||||
#include "crypto.h"
|
||||
#include "crypto_misc.h"
|
||||
|
||||
#define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */
|
||||
#define SSL_PROTOCOL_VERSION_MAX 0x33 /* TLS v1.3 */
|
||||
#define SSL_PROTOCOL_VERSION_TLS1_1 0x32 /* TLS v1.1 */
|
||||
#define SSL_PROTOCOL_VERSION_TLS1_2 0x33 /* TLS v1.2 */
|
||||
#define SSL_RANDOM_SIZE 32
|
||||
#define SSL_SECRET_SIZE 48
|
||||
#define SSL_FINISHED_HASH_SIZE 12
|
||||
#define SSL_RECORD_SIZE 5
|
||||
#define SSL_SERVER_READ 0
|
||||
#define SSL_SERVER_WRITE 1
|
||||
#define SSL_CLIENT_READ 2
|
||||
#define SSL_CLIENT_WRITE 3
|
||||
#define SSL_HS_HDR_SIZE 4
|
||||
|
||||
/* the flags we use while establishing a connection */
|
||||
#define SSL_NEED_RECORD 0x0001
|
||||
#define SSL_TX_ENCRYPTED 0x0002
|
||||
#define SSL_RX_ENCRYPTED 0x0004
|
||||
#define SSL_SESSION_RESUME 0x0008
|
||||
#define SSL_IS_CLIENT 0x0010
|
||||
#define SSL_HAS_CERT_REQ 0x0020
|
||||
#define SSL_SENT_CLOSE_NOTIFY 0x0040
|
||||
|
||||
/* some macros to muck around with flag bits */
|
||||
#define SET_SSL_FLAG(A) (ssl->flag |= A)
|
||||
#define CLR_SSL_FLAG(A) (ssl->flag &= ~A)
|
||||
#define IS_SET_SSL_FLAG(A) (ssl->flag & A)
|
||||
|
||||
#define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */
|
||||
#define RT_MAX_PLAIN_LENGTH 16384
|
||||
#define RT_EXTRA 1024
|
||||
#define BM_RECORD_OFFSET 5
|
||||
|
||||
#define NUM_PROTOCOLS 4
|
||||
|
||||
#define MAX_SIG_ALGORITHMS 4
|
||||
#define SIG_ALG_SHA1 2
|
||||
#define SIG_ALG_SHA256 4
|
||||
#define SIG_ALG_SHA384 5
|
||||
#define SIG_ALG_SHA512 6
|
||||
#define SIG_ALG_RSA 1
|
||||
|
||||
#define PARANOIA_CHECK(A, B) if (A < B) { \
|
||||
ret = SSL_ERROR_INVALID_HANDSHAKE; goto error; }
|
||||
|
||||
/* protocol types */
|
||||
enum
|
||||
{
|
||||
PT_CHANGE_CIPHER_SPEC = 20,
|
||||
PT_ALERT_PROTOCOL,
|
||||
PT_HANDSHAKE_PROTOCOL,
|
||||
PT_APP_PROTOCOL_DATA
|
||||
};
|
||||
|
||||
/* handshaking types */
|
||||
enum
|
||||
{
|
||||
HS_HELLO_REQUEST,
|
||||
HS_CLIENT_HELLO,
|
||||
HS_SERVER_HELLO,
|
||||
HS_CERTIFICATE = 11,
|
||||
HS_SERVER_KEY_XCHG,
|
||||
HS_CERT_REQ,
|
||||
HS_SERVER_HELLO_DONE,
|
||||
HS_CERT_VERIFY,
|
||||
HS_CLIENT_KEY_XCHG,
|
||||
HS_FINISHED = 20
|
||||
};
|
||||
|
||||
/* SSL extension types */
|
||||
enum
|
||||
{
|
||||
SSL_EXT_SERVER_NAME = 0,
|
||||
SSL_EXT_MAX_FRAGMENT_SIZE,
|
||||
SSL_EXT_SIG_ALG = 0x0d,
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t cipher;
|
||||
uint8_t key_size;
|
||||
uint8_t iv_size;
|
||||
uint8_t padding_size;
|
||||
uint8_t digest_size;
|
||||
uint8_t key_block_size;
|
||||
hmac_func hmac;
|
||||
crypt_func encrypt;
|
||||
crypt_func decrypt;
|
||||
} cipher_info_t;
|
||||
|
||||
struct _SSLObjLoader
|
||||
{
|
||||
uint8_t *buf;
|
||||
int len;
|
||||
};
|
||||
|
||||
typedef struct _SSLObjLoader SSLObjLoader;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
time_t conn_time;
|
||||
uint8_t session_id[SSL_SESSION_ID_SIZE];
|
||||
uint8_t master_secret[SSL_SECRET_SIZE];
|
||||
} SSL_SESSION;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t *buf;
|
||||
int size;
|
||||
uint8_t hash_alg;
|
||||
} SSL_CERT;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
MD5_CTX md5_ctx;
|
||||
SHA1_CTX sha1_ctx;
|
||||
SHA256_CTX sha256_ctx;
|
||||
uint8_t client_random[SSL_RANDOM_SIZE]; /* client's random sequence */
|
||||
uint8_t server_random[SSL_RANDOM_SIZE]; /* server's random sequence */
|
||||
uint8_t final_finish_mac[128];
|
||||
uint8_t master_secret[SSL_SECRET_SIZE];
|
||||
uint8_t key_block[256];
|
||||
uint16_t bm_proc_index;
|
||||
uint8_t key_block_generated;
|
||||
} DISPOSABLE_CTX;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *host_name; /* Needed for the SNI support */
|
||||
/* Needed for the Max Fragment Size Extension.
|
||||
Allowed values: 2^9, 2^10 .. 2^14 */
|
||||
uint16_t max_fragment_size;
|
||||
} SSL_EXTENSIONS;
|
||||
|
||||
struct _SSL
|
||||
{
|
||||
uint32_t flag;
|
||||
uint16_t need_bytes;
|
||||
uint16_t got_bytes;
|
||||
uint8_t record_type;
|
||||
uint8_t cipher;
|
||||
uint8_t sess_id_size;
|
||||
uint8_t version;
|
||||
uint8_t client_version;
|
||||
int16_t next_state;
|
||||
int16_t hs_status;
|
||||
DISPOSABLE_CTX *dc; /* temporary data which we'll get rid of soon */
|
||||
int client_fd;
|
||||
const cipher_info_t *cipher_info;
|
||||
void *encrypt_ctx;
|
||||
void *decrypt_ctx;
|
||||
uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
|
||||
uint8_t *bm_data;
|
||||
uint16_t bm_index;
|
||||
uint16_t bm_read_index;
|
||||
uint8_t sig_algs[MAX_SIG_ALGORITHMS];
|
||||
uint8_t num_sig_algs;
|
||||
struct _SSL *next; /* doubly linked list */
|
||||
struct _SSL *prev;
|
||||
struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
|
||||
|
||||
/* asynchronous client ssl_read specific duplicates of above: */
|
||||
uint16_t rneed_bytes;
|
||||
uint16_t rgot_bytes;
|
||||
uint8_t bm_read_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
|
||||
uint8_t *bm_rdata;
|
||||
uint16_t bm_rread_index;
|
||||
uint8_t hmac_rheader[SSL_RECORD_SIZE]; /* client rx hmac */
|
||||
uint8_t rrecord_type;
|
||||
int need_record;
|
||||
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
uint16_t session_index;
|
||||
SSL_SESSION *session;
|
||||
#endif
|
||||
//#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
X509_CTX *x509_ctx;
|
||||
//#endif
|
||||
|
||||
uint8_t session_id[SSL_SESSION_ID_SIZE];
|
||||
uint8_t client_mac[SHA256_SIZE]; /* for HMAC verification */
|
||||
uint8_t server_mac[SHA256_SIZE]; /* for HMAC verification */
|
||||
uint8_t read_sequence[8]; /* 64 bit sequence number */
|
||||
uint8_t write_sequence[8]; /* 64 bit sequence number */
|
||||
uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
|
||||
SSL_EXTENSIONS *extensions; /* Contains the SSL (client) extensions */
|
||||
};
|
||||
|
||||
typedef struct _SSL SSL;
|
||||
|
||||
struct _SSL_CTX
|
||||
{
|
||||
uint32_t options;
|
||||
uint8_t chain_length;
|
||||
RSA_CTX *rsa_ctx;
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
CA_CERT_CTX *ca_cert_ctx;
|
||||
#endif
|
||||
SSL *head;
|
||||
SSL *tail;
|
||||
SSL_CERT certs[CONFIG_SSL_MAX_CERTS];
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
uint16_t num_sessions;
|
||||
SSL_SESSION **ssl_sessions;
|
||||
#endif
|
||||
#ifdef CONFIG_SSL_CTX_MUTEXING
|
||||
SSL_CTX_MUTEX_TYPE mutex;
|
||||
#endif
|
||||
#ifdef CONFIG_OPENSSL_COMPATIBLE
|
||||
void *bonus_attr;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef struct _SSL_CTX SSL_CTX;
|
||||
|
||||
/* backwards compatibility */
|
||||
typedef struct _SSL_CTX SSLCTX;
|
||||
|
||||
extern const uint8_t ssl_prot_prefs[NUM_PROTOCOLS];
|
||||
|
||||
SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd);
|
||||
void disposable_new(SSL *ssl);
|
||||
void disposable_free(SSL *ssl);
|
||||
int send_packet(SSL *ssl, uint8_t protocol,
|
||||
const uint8_t *in, int length);
|
||||
int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
|
||||
int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len);
|
||||
int process_finished(SSL *ssl, uint8_t *buf, int hs_len);
|
||||
int process_sslv23_client_hello(SSL *ssl);
|
||||
int send_alert(SSL *ssl, int error_code);
|
||||
int send_finished(SSL *ssl);
|
||||
int send_certificate(SSL *ssl);
|
||||
int basic_read(SSL *ssl, uint8_t **in_data);
|
||||
int basic_readi(SSL *ssl, uint8_t **in_data);
|
||||
int send_change_cipher_spec(SSL *ssl);
|
||||
int finished_digest(SSL *ssl, const char *label, uint8_t *digest);
|
||||
void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret);
|
||||
void add_packet(SSL *ssl, const uint8_t *pkt, int len);
|
||||
int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
|
||||
int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj);
|
||||
void ssl_obj_free(SSLObjLoader *ssl_obj);
|
||||
int pkcs8_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
|
||||
int pkcs12_decode(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj, const char *password);
|
||||
int load_key_certs(SSL_CTX *ssl_ctx);
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len);
|
||||
void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
|
||||
#endif
|
||||
#ifdef CONFIG_SSL_ENABLE_CLIENT
|
||||
int do_client_connect(SSL *ssl);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);
|
||||
void DISPLAY_BYTES(SSL *ssl, const char *format,
|
||||
const uint8_t *data, int size, ...);
|
||||
void DISPLAY_CERT(SSL *ssl, const X509_CTX *x509_ctx);
|
||||
void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx);
|
||||
void DISPLAY_ALERT(SSL *ssl, int alert);
|
||||
#else
|
||||
#define DISPLAY_STATE(A,B,C,D)
|
||||
#define DISPLAY_CERT(A,B)
|
||||
#define DISPLAY_RSA(A,B)
|
||||
#define DISPLAY_ALERT(A, B)
|
||||
#ifdef WIN32
|
||||
void DISPLAY_BYTES(SSL *ssl, const char *format,/* win32 has no variadic macros */
|
||||
const uint8_t *data, int size, ...);
|
||||
#else
|
||||
#define DISPLAY_BYTES(A,B,C,D,...)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int process_certificate(SSL *ssl, X509_CTX **x509_ctx);
|
||||
|
||||
SSL_SESSION *ssl_session_update(int max_sessions,
|
||||
SSL_SESSION *ssl_sessions[], SSL *ssl,
|
||||
const uint8_t *session_id);
|
||||
void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
541
Argyll_V3.5.0/ccast/axTLS/tls1_clnt.c
Normal file
541
Argyll_V3.5.0/ccast/axTLS/tls1_clnt.c
Normal file
@@ -0,0 +1,541 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2016, 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 <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include "os_port.h"
|
||||
#include "ssl.h"
|
||||
|
||||
#ifdef CONFIG_SSL_ENABLE_CLIENT /* all commented out if no client */
|
||||
|
||||
/* support sha512/384/256/1 RSA */
|
||||
static const uint8_t g_sig_alg[] = {
|
||||
0x00, SSL_EXT_SIG_ALG,
|
||||
0x00, 0x0a, 0x00, 0x08,
|
||||
SIG_ALG_SHA512, SIG_ALG_RSA,
|
||||
SIG_ALG_SHA384, SIG_ALG_RSA,
|
||||
SIG_ALG_SHA256, SIG_ALG_RSA,
|
||||
SIG_ALG_SHA1, SIG_ALG_RSA
|
||||
};
|
||||
|
||||
static const uint8_t g_asn1_sha256[] =
|
||||
{
|
||||
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
|
||||
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
|
||||
};
|
||||
|
||||
static int send_client_hello(SSL *ssl);
|
||||
static int process_server_hello(SSL *ssl);
|
||||
static int process_server_hello_done(SSL *ssl);
|
||||
static int send_client_key_xchg(SSL *ssl);
|
||||
static int process_cert_req(SSL *ssl);
|
||||
static int send_cert_verify(SSL *ssl);
|
||||
|
||||
/*
|
||||
* Establish a new SSL connection to an SSL server.
|
||||
*/
|
||||
EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
|
||||
uint8_t *session_id, uint8_t sess_id_size, SSL_EXTENSIONS* ssl_ext)
|
||||
{
|
||||
SSL *ssl = ssl_new(ssl_ctx, client_fd);
|
||||
ssl->version = SSL_PROTOCOL_VERSION_MAX; /* try top version first */
|
||||
|
||||
if (session_id && ssl_ctx->num_sessions)
|
||||
{
|
||||
if (sess_id_size > SSL_SESSION_ID_SIZE) /* validity check */
|
||||
{
|
||||
ssl_free(ssl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(ssl->session_id, session_id, sess_id_size);
|
||||
ssl->sess_id_size = sess_id_size;
|
||||
SET_SSL_FLAG(SSL_SESSION_RESUME); /* just flag for later */
|
||||
}
|
||||
|
||||
ssl->extensions = ssl_ext;
|
||||
|
||||
SET_SSL_FLAG(SSL_IS_CLIENT);
|
||||
do_client_connect(ssl);
|
||||
return ssl;
|
||||
}
|
||||
|
||||
/*
|
||||
* Process the handshake record.
|
||||
*/
|
||||
int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
|
||||
{
|
||||
int ret = SSL_OK;
|
||||
|
||||
/* To get here the state must be valid */
|
||||
switch (handshake_type)
|
||||
{
|
||||
case HS_SERVER_HELLO:
|
||||
ret = process_server_hello(ssl);
|
||||
break;
|
||||
|
||||
case HS_CERTIFICATE:
|
||||
//#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
ret = process_certificate(ssl, &ssl->x509_ctx);
|
||||
//#endif
|
||||
break;
|
||||
|
||||
case HS_SERVER_HELLO_DONE:
|
||||
if ((ret = process_server_hello_done(ssl)) == SSL_OK)
|
||||
{
|
||||
if (IS_SET_SSL_FLAG(SSL_HAS_CERT_REQ))
|
||||
{
|
||||
if ((ret = send_certificate(ssl)) == SSL_OK &&
|
||||
(ret = send_client_key_xchg(ssl)) == SSL_OK)
|
||||
{
|
||||
send_cert_verify(ssl);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = send_client_key_xchg(ssl);
|
||||
}
|
||||
|
||||
if (ret == SSL_OK &&
|
||||
(ret = send_change_cipher_spec(ssl)) == SSL_OK)
|
||||
{
|
||||
ret = send_finished(ssl);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case HS_CERT_REQ:
|
||||
ret = process_cert_req(ssl);
|
||||
break;
|
||||
|
||||
case HS_FINISHED:
|
||||
ret = process_finished(ssl, buf, hs_len);
|
||||
disposable_free(ssl); /* free up some memory */
|
||||
/* note: client renegotiation is not allowed after this */
|
||||
break;
|
||||
|
||||
case HS_HELLO_REQUEST:
|
||||
disposable_new(ssl);
|
||||
ret = do_client_connect(ssl);
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = SSL_ERROR_INVALID_HANDSHAKE;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Do the handshaking from the beginning.
|
||||
*/
|
||||
int do_client_connect(SSL *ssl)
|
||||
{
|
||||
int ret = SSL_OK;
|
||||
|
||||
send_client_hello(ssl); /* send the client hello */
|
||||
ssl->bm_read_index = 0;
|
||||
ssl->next_state = HS_SERVER_HELLO;
|
||||
ssl->hs_status = SSL_NOT_OK; /* not connected */
|
||||
|
||||
/* sit in a loop until it all looks good */
|
||||
if (!IS_SET_SSL_FLAG(SSL_CONNECT_IN_PARTS))
|
||||
{
|
||||
while (ssl->hs_status != SSL_OK)
|
||||
{
|
||||
/* Use internal, synchronous ssl_read - GWG */
|
||||
ret = ssl_readi(ssl, NULL);
|
||||
|
||||
if (ret < SSL_OK)
|
||||
break;
|
||||
}
|
||||
|
||||
ssl->hs_status = ret; /* connected? */
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Send the initial client hello.
|
||||
*/
|
||||
static int send_client_hello(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = ssl->bm_data;
|
||||
time_t tm = time(NULL);
|
||||
uint8_t *tm_ptr = &buf[6]; /* time will go here */
|
||||
int i, offset, ext_offset;
|
||||
int ext_len = 0;
|
||||
|
||||
|
||||
buf[0] = HS_CLIENT_HELLO;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
/* byte 3 is calculated later */
|
||||
buf[4] = 0x03;
|
||||
buf[5] = ssl->version & 0x0f;
|
||||
|
||||
/* client random value - spec says that 1st 4 bytes are big endian time */
|
||||
*tm_ptr++ = (uint8_t)(((long)tm & 0xff000000) >> 24);
|
||||
*tm_ptr++ = (uint8_t)(((long)tm & 0x00ff0000) >> 16);
|
||||
*tm_ptr++ = (uint8_t)(((long)tm & 0x0000ff00) >> 8);
|
||||
*tm_ptr++ = (uint8_t)(((long)tm & 0x000000ff));
|
||||
if (get_random(SSL_RANDOM_SIZE-4, &buf[10]) < 0)
|
||||
return SSL_NOT_OK;
|
||||
|
||||
memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
|
||||
offset = 6 + SSL_RANDOM_SIZE;
|
||||
|
||||
/* give session resumption a go */
|
||||
if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME)) /* set initially by user */
|
||||
{
|
||||
buf[offset++] = ssl->sess_id_size;
|
||||
memcpy(&buf[offset], ssl->session_id, ssl->sess_id_size);
|
||||
offset += ssl->sess_id_size;
|
||||
CLR_SSL_FLAG(SSL_SESSION_RESUME); /* clear so we can set later */
|
||||
}
|
||||
else
|
||||
{
|
||||
/* no session id - because no session resumption just yet */
|
||||
buf[offset++] = 0;
|
||||
}
|
||||
|
||||
buf[offset++] = 0; /* number of ciphers */
|
||||
buf[offset++] = NUM_PROTOCOLS*2;/* number of ciphers */
|
||||
|
||||
/* put all our supported protocols in our request */
|
||||
for (i = 0; i < NUM_PROTOCOLS; i++)
|
||||
{
|
||||
buf[offset++] = 0; /* cipher we are using */
|
||||
buf[offset++] = ssl_prot_prefs[i];
|
||||
}
|
||||
|
||||
buf[offset++] = 1; /* no compression */
|
||||
buf[offset++] = 0;
|
||||
|
||||
ext_offset = offset;
|
||||
|
||||
buf[offset++] = 0; /* total length of extensions */
|
||||
buf[offset++] = 0;
|
||||
|
||||
/* send the signature algorithm extension for TLS 1.2+ */
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2)
|
||||
{
|
||||
memcpy(&buf[offset], g_sig_alg, sizeof(g_sig_alg));
|
||||
offset += sizeof(g_sig_alg);
|
||||
ext_len += sizeof(g_sig_alg);
|
||||
}
|
||||
|
||||
if (ssl->extensions != NULL)
|
||||
{
|
||||
/* send the host name if specified */
|
||||
if (ssl->extensions->host_name != NULL)
|
||||
{
|
||||
size_t host_len = strlen(ssl->extensions->host_name);
|
||||
buf[offset++] = 0;
|
||||
buf[offset++] = SSL_EXT_SERVER_NAME; /* server_name(0) (65535) */
|
||||
buf[offset++] = 0;
|
||||
buf[offset++] = host_len + 5; /* server_name length */
|
||||
buf[offset++] = 0;
|
||||
buf[offset++] = host_len + 3; /* server_list length */
|
||||
buf[offset++] = 0; /* host_name(0) (255) */
|
||||
buf[offset++] = 0;
|
||||
buf[offset++] = host_len; /* host_name length */
|
||||
strncpy((char*) &buf[offset], ssl->extensions->host_name, host_len);
|
||||
offset += host_len;
|
||||
ext_len += host_len + 9;
|
||||
}
|
||||
|
||||
if (ssl->extensions->max_fragment_size)
|
||||
{
|
||||
buf[offset++] = 0;
|
||||
buf[offset++] = SSL_EXT_MAX_FRAGMENT_SIZE;
|
||||
|
||||
buf[offset++] = 0; // size of data
|
||||
buf[offset++] = 2;
|
||||
|
||||
buf[offset++] = (uint8_t)
|
||||
((ssl->extensions->max_fragment_size >> 8) & 0xff);
|
||||
buf[offset++] = (uint8_t)
|
||||
(ssl->extensions->max_fragment_size & 0xff);
|
||||
ext_len += 6;
|
||||
}
|
||||
}
|
||||
|
||||
if (ext_len > 0)
|
||||
{
|
||||
// update the extensions length value
|
||||
buf[ext_offset] = (uint8_t) ((ext_len >> 8) & 0xff);
|
||||
buf[ext_offset + 1] = (uint8_t) (ext_len & 0xff);
|
||||
}
|
||||
|
||||
buf[3] = offset - 4; /* handshake size */
|
||||
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
|
||||
}
|
||||
|
||||
/*
|
||||
* Process the server hello.
|
||||
*/
|
||||
static int process_server_hello(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = ssl->bm_data;
|
||||
int pkt_size = ssl->bm_index;
|
||||
int num_sessions = ssl->ssl_ctx->num_sessions;
|
||||
uint8_t sess_id_size;
|
||||
int offset, ret = SSL_OK;
|
||||
|
||||
/* check that we are talking to a TLSv1 server */
|
||||
uint8_t version = (buf[4] << 4) + buf[5];
|
||||
if (version > SSL_PROTOCOL_VERSION_MAX)
|
||||
{
|
||||
version = SSL_PROTOCOL_VERSION_MAX;
|
||||
}
|
||||
else if (ssl->version < SSL_PROTOCOL_MIN_VERSION)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_VERSION;
|
||||
ssl_display_error(ret);
|
||||
goto error;
|
||||
}
|
||||
|
||||
ssl->version = version;
|
||||
|
||||
/* get the server random value */
|
||||
memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE);
|
||||
offset = 6 + SSL_RANDOM_SIZE; /* skip of session id size */
|
||||
sess_id_size = buf[offset++];
|
||||
|
||||
if (sess_id_size > SSL_SESSION_ID_SIZE)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_SESSION;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (num_sessions)
|
||||
{
|
||||
ssl->session = ssl_session_update(num_sessions,
|
||||
ssl->ssl_ctx->ssl_sessions, ssl, &buf[offset]);
|
||||
memcpy(ssl->session->session_id, &buf[offset], sess_id_size);
|
||||
|
||||
/* pad the rest with 0's */
|
||||
if (sess_id_size < SSL_SESSION_ID_SIZE)
|
||||
{
|
||||
memset(&ssl->session->session_id[sess_id_size], 0,
|
||||
SSL_SESSION_ID_SIZE-sess_id_size);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(ssl->session_id, &buf[offset], sess_id_size);
|
||||
ssl->sess_id_size = sess_id_size;
|
||||
offset += sess_id_size;
|
||||
|
||||
/* get the real cipher we are using - ignore MSB */
|
||||
ssl->cipher = buf[++offset];
|
||||
ssl->next_state = IS_SET_SSL_FLAG(SSL_SESSION_RESUME) ?
|
||||
HS_FINISHED : HS_CERTIFICATE;
|
||||
|
||||
offset += 2; // ignore compression
|
||||
PARANOIA_CHECK(pkt_size, offset);
|
||||
|
||||
ssl->dc->bm_proc_index = offset;
|
||||
PARANOIA_CHECK(pkt_size, offset);
|
||||
|
||||
// no extensions
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the server hello done message.
|
||||
*/
|
||||
static int process_server_hello_done(SSL *ssl)
|
||||
{
|
||||
ssl->next_state = HS_FINISHED;
|
||||
return SSL_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Send a client key exchange message.
|
||||
*/
|
||||
static int send_client_key_xchg(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = ssl->bm_data;
|
||||
uint8_t premaster_secret[SSL_SECRET_SIZE];
|
||||
int enc_secret_size = -1;
|
||||
|
||||
buf[0] = HS_CLIENT_KEY_XCHG;
|
||||
buf[1] = 0;
|
||||
|
||||
// spec says client must use the what is initially negotiated -
|
||||
// and this is our current version
|
||||
premaster_secret[0] = 0x03;
|
||||
premaster_secret[1] = SSL_PROTOCOL_VERSION_MAX & 0x0f;
|
||||
if (get_random(SSL_SECRET_SIZE-2, &premaster_secret[2]) < 0)
|
||||
return SSL_NOT_OK;
|
||||
|
||||
DISPLAY_RSA(ssl, ssl->x509_ctx->rsa_ctx);
|
||||
|
||||
/* rsa_ctx->bi_ctx is not thread-safe */
|
||||
SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
|
||||
enc_secret_size = RSA_encrypt(ssl->x509_ctx->rsa_ctx, premaster_secret,
|
||||
SSL_SECRET_SIZE, &buf[6], 0);
|
||||
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
|
||||
|
||||
buf[2] = (enc_secret_size + 2) >> 8;
|
||||
buf[3] = (enc_secret_size + 2) & 0xff;
|
||||
buf[4] = enc_secret_size >> 8;
|
||||
buf[5] = enc_secret_size & 0xff;
|
||||
|
||||
generate_master_secret(ssl, premaster_secret);
|
||||
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, enc_secret_size+6);
|
||||
}
|
||||
|
||||
/*
|
||||
* Process the certificate request.
|
||||
*/
|
||||
static int process_cert_req(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
|
||||
int ret = SSL_OK;
|
||||
int cert_req_size = (buf[2]<<8) + buf[3];
|
||||
int offset = 4;
|
||||
int pkt_size = ssl->bm_index;
|
||||
uint8_t cert_type_len, sig_alg_len;
|
||||
|
||||
PARANOIA_CHECK(pkt_size, offset + cert_req_size);
|
||||
ssl->dc->bm_proc_index = cert_req_size;
|
||||
|
||||
/* don't do any processing - we will send back an RSA certificate anyway */
|
||||
ssl->next_state = HS_SERVER_HELLO_DONE;
|
||||
SET_SSL_FLAG(SSL_HAS_CERT_REQ);
|
||||
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
|
||||
{
|
||||
// supported certificate types
|
||||
cert_type_len = buf[offset++];
|
||||
PARANOIA_CHECK(pkt_size, offset + cert_type_len);
|
||||
offset += cert_type_len;
|
||||
|
||||
// supported signature algorithms
|
||||
sig_alg_len = buf[offset++] << 8;
|
||||
sig_alg_len += buf[offset++];
|
||||
PARANOIA_CHECK(pkt_size, offset + sig_alg_len);
|
||||
|
||||
while (sig_alg_len > 0)
|
||||
{
|
||||
uint8_t hash_alg = buf[offset++];
|
||||
uint8_t sig_alg = buf[offset++];
|
||||
sig_alg_len -= 2;
|
||||
|
||||
if (sig_alg == SIG_ALG_RSA &&
|
||||
(hash_alg == SIG_ALG_SHA1 ||
|
||||
hash_alg == SIG_ALG_SHA256 ||
|
||||
hash_alg == SIG_ALG_SHA384 ||
|
||||
hash_alg == SIG_ALG_SHA512))
|
||||
{
|
||||
ssl->sig_algs[ssl->num_sig_algs++] = hash_alg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Send a certificate verify message.
|
||||
*/
|
||||
static int send_cert_verify(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = ssl->bm_data;
|
||||
uint8_t dgst[SHA1_SIZE+MD5_SIZE+15];
|
||||
RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
|
||||
int n = 0, ret;
|
||||
int offset = 0;
|
||||
uint16_t dgst_len;
|
||||
|
||||
if (rsa_ctx == NULL)
|
||||
return SSL_OK;
|
||||
|
||||
DISPLAY_RSA(ssl, rsa_ctx);
|
||||
|
||||
buf[0] = HS_CERT_VERIFY;
|
||||
buf[1] = 0;
|
||||
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
|
||||
{
|
||||
buf[4] = SIG_ALG_SHA256;
|
||||
buf[5] = SIG_ALG_RSA;
|
||||
offset = 6;
|
||||
memcpy(dgst, g_asn1_sha256, sizeof(g_asn1_sha256));
|
||||
dgst_len = finished_digest(ssl, NULL, &dgst[sizeof(g_asn1_sha256)]) +
|
||||
sizeof(g_asn1_sha256);
|
||||
}
|
||||
else
|
||||
{
|
||||
offset = 4;
|
||||
dgst_len = finished_digest(ssl, NULL, dgst);
|
||||
}
|
||||
|
||||
/* rsa_ctx->bi_ctx is not thread-safe */
|
||||
if (rsa_ctx)
|
||||
{
|
||||
SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
|
||||
n = RSA_encrypt(rsa_ctx, dgst, dgst_len, &buf[offset + 2], 1);
|
||||
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
|
||||
|
||||
if (n == 0)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
buf[offset] = n >> 8; /* add the RSA size */
|
||||
buf[offset+1] = n & 0xff;
|
||||
n += 2;
|
||||
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
|
||||
{
|
||||
n += 2; // sig/alg
|
||||
offset -= 2;
|
||||
}
|
||||
|
||||
buf[2] = n >> 8;
|
||||
buf[3] = n & 0xff;
|
||||
ret = send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, n + offset);
|
||||
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SSL_ENABLE_CLIENT */
|
||||
532
Argyll_V3.5.0/ccast/axTLS/tls1_svr.c
Normal file
532
Argyll_V3.5.0/ccast/axTLS/tls1_svr.c
Normal file
@@ -0,0 +1,532 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2017, 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 <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "os_port.h"
|
||||
#include "ssl.h"
|
||||
|
||||
static const uint8_t g_hello_done[] = { HS_SERVER_HELLO_DONE, 0, 0, 0 };
|
||||
static const uint8_t g_asn1_sha256[] =
|
||||
{
|
||||
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
|
||||
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
|
||||
};
|
||||
|
||||
static int process_client_hello(SSL *ssl);
|
||||
static int send_server_hello_sequence(SSL *ssl);
|
||||
static int send_server_hello(SSL *ssl);
|
||||
static int send_server_hello_done(SSL *ssl);
|
||||
static int process_client_key_xchg(SSL *ssl);
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
static int send_certificate_request(SSL *ssl);
|
||||
static int process_cert_verify(SSL *ssl);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Establish a new SSL connection to an SSL client.
|
||||
*/
|
||||
EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd)
|
||||
{
|
||||
SSL *ssl;
|
||||
|
||||
ssl = ssl_new(ssl_ctx, client_fd);
|
||||
ssl->next_state = HS_CLIENT_HELLO;
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (ssl_ctx->chain_length == 0)
|
||||
printf("Warning - no server certificate defined\n"); TTY_FLUSH();
|
||||
#endif
|
||||
|
||||
return ssl;
|
||||
}
|
||||
|
||||
/*
|
||||
* Process the handshake record.
|
||||
*/
|
||||
int do_svr_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
|
||||
{
|
||||
int ret = SSL_OK;
|
||||
ssl->hs_status = SSL_NOT_OK; /* not connected */
|
||||
|
||||
/* To get here the state must be valid */
|
||||
switch (handshake_type)
|
||||
{
|
||||
case HS_CLIENT_HELLO:
|
||||
if ((ret = process_client_hello(ssl)) == SSL_OK)
|
||||
ret = send_server_hello_sequence(ssl);
|
||||
break;
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
case HS_CERTIFICATE:/* the client sends its cert */
|
||||
ret = process_certificate(ssl, &ssl->x509_ctx);
|
||||
|
||||
if (ret == SSL_OK) /* verify the cert */
|
||||
{
|
||||
int cert_res;
|
||||
int pathLenConstraint = 0;
|
||||
|
||||
cert_res = x509_verify(ssl->ssl_ctx->ca_cert_ctx,
|
||||
ssl->x509_ctx, &pathLenConstraint);
|
||||
ret = (cert_res == 0) ? SSL_OK : SSL_X509_ERROR(cert_res);
|
||||
}
|
||||
break;
|
||||
|
||||
case HS_CERT_VERIFY:
|
||||
ret = process_cert_verify(ssl);
|
||||
add_packet(ssl, buf, hs_len); /* needs to be done after */
|
||||
break;
|
||||
#endif
|
||||
case HS_CLIENT_KEY_XCHG:
|
||||
ret = process_client_key_xchg(ssl);
|
||||
break;
|
||||
|
||||
case HS_FINISHED:
|
||||
ret = process_finished(ssl, buf, hs_len);
|
||||
disposable_free(ssl); /* free up some memory */
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Process a client hello message.
|
||||
*/
|
||||
static int process_client_hello(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = ssl->bm_data;
|
||||
int pkt_size = ssl->bm_index;
|
||||
int i, j, cs_len, id_len, offset = 6 + SSL_RANDOM_SIZE;
|
||||
int ret = SSL_OK;
|
||||
|
||||
uint8_t version = (buf[4] << 4) + buf[5];
|
||||
ssl->version = ssl->client_version = version;
|
||||
|
||||
if (version > SSL_PROTOCOL_VERSION_MAX)
|
||||
{
|
||||
/* use client's version instead */
|
||||
ssl->version = SSL_PROTOCOL_VERSION_MAX;
|
||||
}
|
||||
else if (version < SSL_PROTOCOL_MIN_VERSION) /* old version supported? */
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_VERSION;
|
||||
ssl_display_error(ret);
|
||||
goto error;
|
||||
}
|
||||
|
||||
memcpy(ssl->dc->client_random, &buf[6], SSL_RANDOM_SIZE);
|
||||
|
||||
/* process the session id */
|
||||
id_len = buf[offset++];
|
||||
if (id_len > SSL_SESSION_ID_SIZE)
|
||||
{
|
||||
return SSL_ERROR_INVALID_SESSION;
|
||||
}
|
||||
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
ssl->session = ssl_session_update(ssl->ssl_ctx->num_sessions,
|
||||
ssl->ssl_ctx->ssl_sessions, ssl, id_len ? &buf[offset] : NULL);
|
||||
#endif
|
||||
|
||||
offset += id_len;
|
||||
cs_len = (buf[offset]<<8) + buf[offset+1];
|
||||
offset += 2;
|
||||
|
||||
PARANOIA_CHECK(pkt_size, offset + cs_len);
|
||||
|
||||
/* work out what cipher suite we are going to use - client defines
|
||||
the preference */
|
||||
for (i = 0; i < cs_len; i += 2)
|
||||
{
|
||||
/* only support ciphersuites with the form (0x00, xxxx) */
|
||||
if (buf[offset+i])
|
||||
continue;
|
||||
|
||||
for (j = 0; j < NUM_PROTOCOLS; j++)
|
||||
{
|
||||
if (ssl_prot_prefs[j] == buf[offset+i+1]) /* got a match? */
|
||||
{
|
||||
ssl->cipher = ssl_prot_prefs[j];
|
||||
goto do_compression;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ouch! protocol is not supported */
|
||||
return SSL_ERROR_NO_CIPHER;
|
||||
|
||||
/* completely ignore compression */
|
||||
do_compression:
|
||||
offset += cs_len;
|
||||
id_len = buf[offset++];
|
||||
offset += id_len;
|
||||
PARANOIA_CHECK(pkt_size, offset + id_len);
|
||||
|
||||
if (offset == pkt_size)
|
||||
{
|
||||
/* no extensions */
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* extension size */
|
||||
id_len = buf[offset++] << 8;
|
||||
id_len += buf[offset++];
|
||||
PARANOIA_CHECK(pkt_size, offset + id_len);
|
||||
|
||||
// Check for extensions from the client - only the signature algorithm
|
||||
// is supported
|
||||
while (offset < pkt_size)
|
||||
{
|
||||
int ext, ext_len;
|
||||
|
||||
ext = buf[offset++] << 8;
|
||||
ext += buf[offset++];
|
||||
ext_len = buf[offset++] << 8;
|
||||
ext_len += buf[offset++];
|
||||
PARANOIA_CHECK(pkt_size, offset + ext_len);
|
||||
|
||||
if (ext == SSL_EXT_SIG_ALG)
|
||||
{
|
||||
while (ext_len > 0)
|
||||
{
|
||||
uint8_t hash_alg = buf[offset++];
|
||||
uint8_t sig_alg = buf[offset++];
|
||||
ext_len -= 2;
|
||||
|
||||
if (sig_alg == SIG_ALG_RSA &&
|
||||
(hash_alg == SIG_ALG_SHA1 ||
|
||||
hash_alg == SIG_ALG_SHA256 ||
|
||||
hash_alg == SIG_ALG_SHA384 ||
|
||||
hash_alg == SIG_ALG_SHA512))
|
||||
{
|
||||
ssl->sig_algs[ssl->num_sig_algs++] = hash_alg;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
offset += ext_len;
|
||||
}
|
||||
}
|
||||
|
||||
/* default is RSA/SHA1 */
|
||||
if (ssl->num_sig_algs == 0)
|
||||
{
|
||||
ssl->sig_algs[ssl->num_sig_algs++] = SIG_ALG_SHA1;
|
||||
}
|
||||
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Send the entire server hello sequence
|
||||
*/
|
||||
static int send_server_hello_sequence(SSL *ssl)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if ((ret = send_server_hello(ssl)) == SSL_OK)
|
||||
{
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
/* resume handshake? */
|
||||
if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
|
||||
{
|
||||
if ((ret = send_change_cipher_spec(ssl)) == SSL_OK)
|
||||
{
|
||||
ret = send_finished(ssl);
|
||||
ssl->next_state = HS_FINISHED;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if ((ret = send_certificate(ssl)) == SSL_OK)
|
||||
{
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
/* ask the client for its certificate */
|
||||
if (IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION))
|
||||
{
|
||||
if ((ret = send_certificate_request(ssl)) == SSL_OK)
|
||||
{
|
||||
ret = send_server_hello_done(ssl);
|
||||
ssl->next_state = HS_CERTIFICATE;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
ret = send_server_hello_done(ssl);
|
||||
ssl->next_state = HS_CLIENT_KEY_XCHG;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Send a server hello message.
|
||||
*/
|
||||
static int send_server_hello(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = ssl->bm_data;
|
||||
int offset = 0;
|
||||
|
||||
buf[0] = HS_SERVER_HELLO;
|
||||
buf[1] = 0;
|
||||
buf[2] = 0;
|
||||
/* byte 3 is calculated later */
|
||||
buf[4] = 0x03;
|
||||
buf[5] = ssl->version & 0x0f;
|
||||
|
||||
/* server random value */
|
||||
if (get_random(SSL_RANDOM_SIZE, &buf[6]) < 0)
|
||||
return SSL_NOT_OK;
|
||||
|
||||
memcpy(ssl->dc->server_random, &buf[6], SSL_RANDOM_SIZE);
|
||||
offset = 6 + SSL_RANDOM_SIZE;
|
||||
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
if (IS_SET_SSL_FLAG(SSL_SESSION_RESUME))
|
||||
{
|
||||
/* retrieve id from session cache */
|
||||
buf[offset++] = SSL_SESSION_ID_SIZE;
|
||||
memcpy(&buf[offset], ssl->session->session_id, SSL_SESSION_ID_SIZE);
|
||||
memcpy(ssl->session_id, ssl->session->session_id, SSL_SESSION_ID_SIZE);
|
||||
ssl->sess_id_size = SSL_SESSION_ID_SIZE;
|
||||
offset += SSL_SESSION_ID_SIZE;
|
||||
}
|
||||
else /* generate our own session id */
|
||||
#endif
|
||||
{
|
||||
#ifndef CONFIG_SSL_SKELETON_MODE
|
||||
buf[offset++] = SSL_SESSION_ID_SIZE;
|
||||
get_random(SSL_SESSION_ID_SIZE, &buf[offset]);
|
||||
memcpy(ssl->session_id, &buf[offset], SSL_SESSION_ID_SIZE);
|
||||
ssl->sess_id_size = SSL_SESSION_ID_SIZE;
|
||||
|
||||
/* store id in session cache */
|
||||
if (ssl->ssl_ctx->num_sessions)
|
||||
{
|
||||
memcpy(ssl->session->session_id,
|
||||
ssl->session_id, SSL_SESSION_ID_SIZE);
|
||||
}
|
||||
|
||||
offset += SSL_SESSION_ID_SIZE;
|
||||
#else
|
||||
buf[offset++] = 0; /* don't bother with session id in skelton mode */
|
||||
#endif
|
||||
}
|
||||
|
||||
buf[offset++] = 0; /* cipher we are using */
|
||||
buf[offset++] = ssl->cipher;
|
||||
buf[offset++] = 0; /* no compression and no extensions supported */
|
||||
buf[3] = offset - 4; /* handshake size */
|
||||
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
|
||||
}
|
||||
|
||||
/*
|
||||
* Send the server hello done message.
|
||||
*/
|
||||
static int send_server_hello_done(SSL *ssl)
|
||||
{
|
||||
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
|
||||
g_hello_done, sizeof(g_hello_done));
|
||||
}
|
||||
|
||||
/*
|
||||
* Pull apart a client key exchange message. Decrypt the pre-master key (using
|
||||
* our RSA private key) and then work out the master key. Initialise the
|
||||
* ciphers.
|
||||
*/
|
||||
static int process_client_key_xchg(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
|
||||
int pkt_size = ssl->bm_index;
|
||||
int premaster_size, secret_length = (buf[2] << 8) + buf[3];
|
||||
uint8_t premaster_secret[MAX_KEY_BYTE_SIZE];
|
||||
RSA_CTX *rsa_ctx = ssl->ssl_ctx->rsa_ctx;
|
||||
int offset = 4;
|
||||
int ret = SSL_OK;
|
||||
|
||||
if (rsa_ctx == NULL)
|
||||
{
|
||||
ret = SSL_ERROR_NO_CERT_DEFINED;
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* is there an extra size field? */
|
||||
if ((secret_length - 2) == rsa_ctx->num_octets)
|
||||
offset += 2;
|
||||
|
||||
PARANOIA_CHECK(pkt_size, rsa_ctx->num_octets+offset);
|
||||
|
||||
/* rsa_ctx->bi_ctx is not thread-safe */
|
||||
SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
|
||||
premaster_size = RSA_decrypt(rsa_ctx, &buf[offset], premaster_secret,
|
||||
sizeof(premaster_secret), 1);
|
||||
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
|
||||
|
||||
if (premaster_size != SSL_SECRET_SIZE ||
|
||||
premaster_secret[0] != 0x03 || /* must be the same as client
|
||||
offered version */
|
||||
premaster_secret[1] != (ssl->client_version & 0x0f))
|
||||
{
|
||||
/* guard against a Bleichenbacher attack */
|
||||
if (get_random(SSL_SECRET_SIZE, premaster_secret) < 0)
|
||||
return SSL_NOT_OK;
|
||||
|
||||
/* and continue - will die eventually when checking the mac */
|
||||
}
|
||||
|
||||
generate_master_secret(ssl, premaster_secret);
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
ssl->next_state = IS_SET_SSL_FLAG(SSL_CLIENT_AUTHENTICATION) ?
|
||||
HS_CERT_VERIFY : HS_FINISHED;
|
||||
#else
|
||||
ssl->next_state = HS_FINISHED;
|
||||
#endif
|
||||
|
||||
ssl->dc->bm_proc_index += rsa_ctx->num_octets+offset;
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
static const uint8_t g_cert_request[] = { HS_CERT_REQ, 0,
|
||||
0, 0x0e,
|
||||
1, 1, // rsa sign
|
||||
0x00, 0x08,
|
||||
SIG_ALG_SHA256, SIG_ALG_RSA,
|
||||
SIG_ALG_SHA512, SIG_ALG_RSA,
|
||||
SIG_ALG_SHA384, SIG_ALG_RSA,
|
||||
SIG_ALG_SHA1, SIG_ALG_RSA,
|
||||
0, 0
|
||||
};
|
||||
|
||||
static const uint8_t g_cert_request_v1[] = { HS_CERT_REQ, 0, 0, 4, 1, 0, 0, 0 };
|
||||
|
||||
/*
|
||||
* Send the certificate request message.
|
||||
*/
|
||||
static int send_certificate_request(SSL *ssl)
|
||||
{
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
|
||||
{
|
||||
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
|
||||
g_cert_request, sizeof(g_cert_request));
|
||||
}
|
||||
else
|
||||
{
|
||||
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
|
||||
g_cert_request_v1, sizeof(g_cert_request_v1));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Ensure the client has the private key by first decrypting the packet and
|
||||
* then checking the packet digests.
|
||||
*/
|
||||
static int process_cert_verify(SSL *ssl)
|
||||
{
|
||||
uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
|
||||
int pkt_size = ssl->bm_index;
|
||||
uint8_t dgst_buf[MAX_KEY_BYTE_SIZE];
|
||||
uint8_t dgst[MD5_SIZE + SHA1_SIZE];
|
||||
X509_CTX *x509_ctx = ssl->x509_ctx;
|
||||
int ret = SSL_OK;
|
||||
int offset = 6;
|
||||
int rsa_len;
|
||||
int n;
|
||||
|
||||
DISPLAY_RSA(ssl, x509_ctx->rsa_ctx);
|
||||
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
|
||||
{
|
||||
// TODO: should really need to be able to handle other algorihms. An
|
||||
// assumption is made on RSA/SHA256 and appears to be OK.
|
||||
//uint8_t hash_alg = buf[4];
|
||||
//uint8_t sig_alg = buf[5];
|
||||
offset = 8;
|
||||
rsa_len = (buf[6] << 8) + buf[7];
|
||||
}
|
||||
else
|
||||
{
|
||||
rsa_len = (buf[4] << 8) + buf[5];
|
||||
}
|
||||
|
||||
PARANOIA_CHECK(pkt_size, offset + rsa_len);
|
||||
|
||||
/* rsa_ctx->bi_ctx is not thread-safe */
|
||||
SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
|
||||
n = RSA_decrypt(x509_ctx->rsa_ctx, &buf[offset], dgst_buf,
|
||||
sizeof(dgst_buf), 0);
|
||||
SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
|
||||
|
||||
if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
|
||||
{
|
||||
if (memcmp(dgst_buf, g_asn1_sha256, sizeof(g_asn1_sha256)))
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
goto error;
|
||||
}
|
||||
|
||||
finished_digest(ssl, NULL, dgst); /* calculate the digest */
|
||||
if (memcmp(&dgst_buf[sizeof(g_asn1_sha256)], dgst, SHA256_SIZE))
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
else // TLS1.0/1.1
|
||||
{
|
||||
if (n != SHA1_SIZE + MD5_SIZE)
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
goto end_cert_vfy;
|
||||
}
|
||||
|
||||
finished_digest(ssl, NULL, dgst); /* calculate the digest */
|
||||
if (memcmp(dgst_buf, dgst, MD5_SIZE + SHA1_SIZE))
|
||||
{
|
||||
ret = SSL_ERROR_INVALID_KEY;
|
||||
}
|
||||
}
|
||||
|
||||
end_cert_vfy:
|
||||
ssl->next_state = HS_FINISHED;
|
||||
error:
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
924
Argyll_V3.5.0/ccast/axTLS/x509.c
Normal file
924
Argyll_V3.5.0/ccast/axTLS/x509.c
Normal file
@@ -0,0 +1,924 @@
|
||||
/*
|
||||
* Copyright (c) 2007-2017, 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file x509.c
|
||||
*
|
||||
* Certificate processing.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "os_port.h"
|
||||
#include "crypto_misc.h"
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
static int x509_v3_subject_alt_name(const uint8_t *cert, int offset,
|
||||
X509_CTX *x509_ctx);
|
||||
static int x509_v3_basic_constraints(const uint8_t *cert, int offset,
|
||||
X509_CTX *x509_ctx);
|
||||
static int x509_v3_key_usage(const uint8_t *cert, int offset,
|
||||
X509_CTX *x509_ctx);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Construct a new x509 object.
|
||||
* @return 0 if ok. < 0 if there was a problem.
|
||||
*/
|
||||
int x509_new(const uint8_t *cert, int *len, X509_CTX **ctx)
|
||||
{
|
||||
int begin_tbs, end_tbs;
|
||||
int ret = X509_NOT_OK, offset = 0, cert_size = 0;
|
||||
int version = 0;
|
||||
X509_CTX *x509_ctx;
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
|
||||
BI_CTX *bi_ctx;
|
||||
#endif
|
||||
|
||||
*ctx = (X509_CTX *)calloc(1, sizeof(X509_CTX));
|
||||
x509_ctx = *ctx;
|
||||
|
||||
/* get the certificate size */
|
||||
asn1_skip_obj(cert, &cert_size, ASN1_SEQUENCE);
|
||||
|
||||
if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
|
||||
goto end_cert;
|
||||
|
||||
begin_tbs = offset; /* start of the tbs */
|
||||
end_tbs = begin_tbs; /* work out the end of the tbs */
|
||||
asn1_skip_obj(cert, &end_tbs, ASN1_SEQUENCE);
|
||||
|
||||
if (asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
|
||||
goto end_cert;
|
||||
|
||||
/* optional version */
|
||||
if (cert[offset] == ASN1_EXPLICIT_TAG &&
|
||||
asn1_version(cert, &offset, &version) == X509_NOT_OK)
|
||||
goto end_cert;
|
||||
|
||||
if (asn1_skip_obj(cert, &offset, ASN1_INTEGER) || /* serial number */
|
||||
asn1_next_obj(cert, &offset, ASN1_SEQUENCE) < 0)
|
||||
goto end_cert;
|
||||
|
||||
/* make sure the signature is ok */
|
||||
if (asn1_signature_type(cert, &offset, x509_ctx))
|
||||
{
|
||||
ret = X509_VFY_ERROR_UNSUPPORTED_DIGEST;
|
||||
goto end_cert;
|
||||
}
|
||||
|
||||
if (asn1_name(cert, &offset, x509_ctx->ca_cert_dn) ||
|
||||
asn1_validity(cert, &offset, x509_ctx) ||
|
||||
asn1_name(cert, &offset, x509_ctx->cert_dn) ||
|
||||
asn1_public_key(cert, &offset, x509_ctx))
|
||||
{
|
||||
goto end_cert;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
|
||||
bi_ctx = x509_ctx->rsa_ctx->bi_ctx;
|
||||
|
||||
/* use the appropriate signature algorithm */
|
||||
switch (x509_ctx->sig_type)
|
||||
{
|
||||
case SIG_TYPE_MD5:
|
||||
{
|
||||
MD5_CTX md5_ctx;
|
||||
uint8_t md5_dgst[MD5_SIZE];
|
||||
MD5_Init(&md5_ctx);
|
||||
MD5_Update(&md5_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
|
||||
MD5_Final(md5_dgst, &md5_ctx);
|
||||
x509_ctx->digest = bi_import(bi_ctx, md5_dgst, MD5_SIZE);
|
||||
}
|
||||
break;
|
||||
|
||||
case SIG_TYPE_SHA1:
|
||||
{
|
||||
SHA1_CTX sha_ctx;
|
||||
uint8_t sha_dgst[SHA1_SIZE];
|
||||
SHA1_Init(&sha_ctx);
|
||||
SHA1_Update(&sha_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
|
||||
SHA1_Final(sha_dgst, &sha_ctx);
|
||||
x509_ctx->digest = bi_import(bi_ctx, sha_dgst, SHA1_SIZE);
|
||||
}
|
||||
break;
|
||||
|
||||
case SIG_TYPE_SHA256:
|
||||
{
|
||||
SHA256_CTX sha256_ctx;
|
||||
uint8_t sha256_dgst[SHA256_SIZE];
|
||||
SHA256_Init(&sha256_ctx);
|
||||
SHA256_Update(&sha256_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
|
||||
SHA256_Final(sha256_dgst, &sha256_ctx);
|
||||
x509_ctx->digest = bi_import(bi_ctx, sha256_dgst, SHA256_SIZE);
|
||||
}
|
||||
break;
|
||||
|
||||
case SIG_TYPE_SHA384:
|
||||
{
|
||||
SHA384_CTX sha384_ctx;
|
||||
uint8_t sha384_dgst[SHA384_SIZE];
|
||||
SHA384_Init(&sha384_ctx);
|
||||
SHA384_Update(&sha384_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
|
||||
SHA384_Final(sha384_dgst, &sha384_ctx);
|
||||
x509_ctx->digest = bi_import(bi_ctx, sha384_dgst, SHA384_SIZE);
|
||||
}
|
||||
break;
|
||||
|
||||
case SIG_TYPE_SHA512:
|
||||
{
|
||||
SHA512_CTX sha512_ctx;
|
||||
uint8_t sha512_dgst[SHA512_SIZE];
|
||||
SHA512_Init(&sha512_ctx);
|
||||
SHA512_Update(&sha512_ctx, &cert[begin_tbs], end_tbs-begin_tbs);
|
||||
SHA512_Final(sha512_dgst, &sha512_ctx);
|
||||
x509_ctx->digest = bi_import(bi_ctx, sha512_dgst, SHA512_SIZE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (version == 2 && asn1_next_obj(cert, &offset, ASN1_V3_DATA) > 0)
|
||||
{
|
||||
x509_v3_subject_alt_name(cert, offset, x509_ctx);
|
||||
x509_v3_basic_constraints(cert, offset, x509_ctx);
|
||||
x509_v3_key_usage(cert, offset, x509_ctx);
|
||||
}
|
||||
|
||||
offset = end_tbs; /* skip the rest of v3 data */
|
||||
if (asn1_skip_obj(cert, &offset, ASN1_SEQUENCE) ||
|
||||
asn1_signature(cert, &offset, x509_ctx))
|
||||
goto end_cert;
|
||||
#endif
|
||||
ret = X509_OK;
|
||||
end_cert:
|
||||
if (len)
|
||||
{
|
||||
*len = cert_size;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
{
|
||||
#ifdef CONFIG_DEBUG
|
||||
printf("Error: Invalid X509 ASN.1 file (%s)\n",
|
||||
x509_display_error(ret));
|
||||
#endif
|
||||
x509_free(x509_ctx);
|
||||
*ctx = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION /* only care if doing verification */
|
||||
static int x509_v3_subject_alt_name(const uint8_t *cert, int offset,
|
||||
X509_CTX *x509_ctx)
|
||||
{
|
||||
if ((offset = asn1_is_subject_alt_name(cert, offset)) > 0)
|
||||
{
|
||||
x509_ctx->subject_alt_name_present = true;
|
||||
x509_ctx->subject_alt_name_is_critical =
|
||||
asn1_is_critical_ext(cert, &offset);
|
||||
|
||||
if (asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) > 0)
|
||||
{
|
||||
int altlen;
|
||||
|
||||
if ((altlen = asn1_next_obj(cert, &offset, ASN1_SEQUENCE)) > 0)
|
||||
{
|
||||
int endalt = offset + altlen;
|
||||
int totalnames = 0;
|
||||
|
||||
while (offset < endalt)
|
||||
{
|
||||
int type = cert[offset++];
|
||||
int dnslen = get_asn1_length(cert, &offset);
|
||||
|
||||
if (type == ASN1_CONTEXT_DNSNAME)
|
||||
{
|
||||
/* sanity check the hostname due to
|
||||
https://tools.cisco.com/security/center/viewAlert.x?alertId=18852
|
||||
*/
|
||||
if (strnlen((const char *)&cert[offset], dnslen) !=
|
||||
dnslen)
|
||||
{
|
||||
return X509_VFY_ERROR_NO_TRUSTED_CERT;
|
||||
}
|
||||
|
||||
x509_ctx->subject_alt_dnsnames = (char**)
|
||||
realloc(x509_ctx->subject_alt_dnsnames,
|
||||
(totalnames + 2) * sizeof(char*));
|
||||
x509_ctx->subject_alt_dnsnames[totalnames] =
|
||||
(char*)malloc(dnslen + 1);
|
||||
x509_ctx->subject_alt_dnsnames[totalnames+1] = NULL;
|
||||
memcpy(x509_ctx->subject_alt_dnsnames[totalnames],
|
||||
cert + offset, dnslen);
|
||||
x509_ctx->subject_alt_dnsnames[totalnames][dnslen] = 0;
|
||||
totalnames++;
|
||||
}
|
||||
|
||||
offset += dnslen;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return X509_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constraints - see https://tools.ietf.org/html/rfc5280#page-39
|
||||
*/
|
||||
static int x509_v3_basic_constraints(const uint8_t *cert, int offset,
|
||||
X509_CTX *x509_ctx)
|
||||
{
|
||||
int ret = X509_OK;
|
||||
int lenSeq = 0;
|
||||
|
||||
if ((offset = asn1_is_basic_constraints(cert, offset)) == 0)
|
||||
goto end_contraints;
|
||||
|
||||
x509_ctx->basic_constraint_present = true;
|
||||
x509_ctx->basic_constraint_is_critical =
|
||||
asn1_is_critical_ext(cert, &offset);
|
||||
|
||||
/* Assign Defaults in case not specified
|
||||
basic_constraint_cA will already by zero by virtue of the calloc */
|
||||
x509_ctx->basic_constraint_cA = 0;
|
||||
/* basic_constraint_pathLenConstraint is unlimited by default.
|
||||
10000 is just a large number (limits.h is not already included) */
|
||||
x509_ctx->basic_constraint_pathLenConstraint = 10000;
|
||||
|
||||
if ((asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) < 0) ||
|
||||
((lenSeq = asn1_next_obj(cert, &offset, ASN1_SEQUENCE)) < 0))
|
||||
{
|
||||
ret = X509_NOT_OK;
|
||||
}
|
||||
|
||||
/* If the Sequence Length is greater than zero,
|
||||
continue with the basic_constraint_cA */
|
||||
if ((lenSeq>0)&&(asn1_get_bool(cert, &offset,
|
||||
&x509_ctx->basic_constraint_cA) < 0))
|
||||
{
|
||||
ret = X509_NOT_OK;
|
||||
}
|
||||
|
||||
/* If the Sequence Length is greater than 3, it has more content than
|
||||
the basic_constraint_cA bool, so grab the pathLenConstraint */
|
||||
if ((lenSeq>3) && (asn1_get_int(cert, &offset,
|
||||
&x509_ctx->basic_constraint_pathLenConstraint) < 0))
|
||||
{
|
||||
ret = X509_NOT_OK;
|
||||
}
|
||||
|
||||
end_contraints:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Key usage - see https://tools.ietf.org/html/rfc5280#section-4.2.1.3
|
||||
*/
|
||||
static int x509_v3_key_usage(const uint8_t *cert, int offset,
|
||||
X509_CTX *x509_ctx)
|
||||
{
|
||||
int ret = X509_OK;
|
||||
|
||||
if ((offset = asn1_is_key_usage(cert, offset)) == 0)
|
||||
goto end_key_usage;
|
||||
|
||||
x509_ctx->key_usage_present = true;
|
||||
x509_ctx->key_usage_is_critical = asn1_is_critical_ext(cert, &offset);
|
||||
|
||||
if (asn1_next_obj(cert, &offset, ASN1_OCTET_STRING) < 0 ||
|
||||
asn1_get_bit_string_as_int(cert, &offset, &x509_ctx->key_usage))
|
||||
{
|
||||
ret = X509_NOT_OK;
|
||||
}
|
||||
|
||||
end_key_usage:
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Free an X.509 object's resources.
|
||||
*/
|
||||
void x509_free(X509_CTX *x509_ctx)
|
||||
{
|
||||
X509_CTX *next;
|
||||
int i;
|
||||
|
||||
if (x509_ctx == NULL) /* if already null, then don't bother */
|
||||
return;
|
||||
|
||||
for (i = 0; i < X509_NUM_DN_TYPES; i++)
|
||||
{
|
||||
free(x509_ctx->ca_cert_dn[i]);
|
||||
free(x509_ctx->cert_dn[i]);
|
||||
}
|
||||
|
||||
free(x509_ctx->signature);
|
||||
|
||||
#ifdef CONFIG_SSL_CERT_VERIFICATION
|
||||
if (x509_ctx->digest)
|
||||
{
|
||||
bi_free(x509_ctx->rsa_ctx->bi_ctx, x509_ctx->digest);
|
||||
}
|
||||
|
||||
if (x509_ctx->subject_alt_dnsnames)
|
||||
{
|
||||
for (i = 0; x509_ctx->subject_alt_dnsnames[i]; ++i)
|
||||
free(x509_ctx->subject_alt_dnsnames[i]);
|
||||
|
||||
free(x509_ctx->subject_alt_dnsnames);
|
||||
}
|
||||
#endif
|
||||
|
||||
RSA_free(x509_ctx->rsa_ctx);
|
||||
next = x509_ctx->next;
|
||||
free(x509_ctx);
|
||||
x509_free(next); /* clear the chain */
|
||||
}
|
||||
|
||||
#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_DEBUG)
|
||||
|
||||
static const uint8_t sig_prefix_md5[] = {0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10};
|
||||
static const uint8_t sig_prefix_sha1[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14};
|
||||
static const uint8_t sig_prefix_sha256[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
|
||||
static const uint8_t sig_prefix_sha384[] = {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30};
|
||||
static const uint8_t sig_prefix_sha512[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
|
||||
|
||||
/**
|
||||
* Take a signature and decrypt it.
|
||||
*/
|
||||
static bigint *sig_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
|
||||
uint8_t sig_type, bigint *modulus, bigint *pub_exp)
|
||||
{
|
||||
int i;
|
||||
bigint *decrypted_bi, *dat_bi;
|
||||
bigint *bir = NULL;
|
||||
uint8_t *block = (uint8_t *)malloc(sig_len);
|
||||
|
||||
const uint8_t *sig_prefix = NULL;
|
||||
uint8_t sig_prefix_size = 0, hash_len = 0;
|
||||
/* adjust our expections */
|
||||
switch (sig_type)
|
||||
{
|
||||
case SIG_TYPE_MD5:
|
||||
sig_prefix = sig_prefix_md5;
|
||||
sig_prefix_size = sizeof(sig_prefix_md5);
|
||||
break;
|
||||
case SIG_TYPE_SHA1:
|
||||
sig_prefix = sig_prefix_sha1;
|
||||
sig_prefix_size = sizeof(sig_prefix_sha1);
|
||||
break;
|
||||
case SIG_TYPE_SHA256:
|
||||
sig_prefix = sig_prefix_sha256;
|
||||
sig_prefix_size = sizeof(sig_prefix_sha256);
|
||||
break;
|
||||
case SIG_TYPE_SHA384:
|
||||
sig_prefix = sig_prefix_sha384;
|
||||
sig_prefix_size = sizeof(sig_prefix_sha384);
|
||||
break;
|
||||
case SIG_TYPE_SHA512:
|
||||
sig_prefix = sig_prefix_sha512;
|
||||
sig_prefix_size = sizeof(sig_prefix_sha512);
|
||||
break;
|
||||
}
|
||||
|
||||
if (sig_prefix)
|
||||
hash_len = sig_prefix[sig_prefix_size - 1];
|
||||
|
||||
/* check length (#A) */
|
||||
if (sig_len < 2 + 8 + 1 + sig_prefix_size + hash_len)
|
||||
goto err;
|
||||
|
||||
/* 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;
|
||||
|
||||
/* check the first 2 bytes */
|
||||
if (block[0] != 0 || block[1] != 1)
|
||||
goto err;
|
||||
|
||||
/* check the padding */
|
||||
i = 2; /* start at the first padding byte */
|
||||
while (i < sig_len - 1 - sig_prefix_size - hash_len)
|
||||
{ /* together with (#A), we require at least 8 bytes of padding */
|
||||
if (block[i++] != 0xFF)
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* check end of padding */
|
||||
if (block[i++] != 0)
|
||||
goto err;
|
||||
|
||||
/* check the ASN.1 metadata */
|
||||
if (memcmp(block+i, sig_prefix, sig_prefix_size))
|
||||
goto err;
|
||||
|
||||
/* now we can get the hash we need */
|
||||
bir = bi_import(ctx, block + i + sig_prefix_size, hash_len);
|
||||
|
||||
err:
|
||||
free(block);
|
||||
/* save a few bytes of memory */
|
||||
bi_clear_cache(ctx);
|
||||
return bir;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SSL_CERT_VERIFICATION */
|
||||
|
||||
#if defined (CONFIG_DEBUG)
|
||||
|
||||
/**
|
||||
* Do some basic checks on the certificate chain.
|
||||
*
|
||||
* Certificate verification consists of a number of checks:
|
||||
* - The date of the certificate is after the start date.
|
||||
* - The date of the certificate is before the finish date.
|
||||
* - A root certificate exists in the certificate store.
|
||||
* - That the certificate(s) are not self-signed.
|
||||
* - The certificate chain is valid.
|
||||
* - The signature of the certificate is valid.
|
||||
* - Basic constraints
|
||||
*/
|
||||
int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert,
|
||||
int *pathLenConstraint)
|
||||
{
|
||||
int ret = X509_OK, i = 0;
|
||||
bigint *cert_sig;
|
||||
X509_CTX *next_cert = NULL;
|
||||
BI_CTX *ctx = NULL;
|
||||
bigint *mod = NULL, *expn = NULL;
|
||||
int match_ca_cert = 0;
|
||||
struct timeval tv;
|
||||
uint8_t is_self_signed = 0;
|
||||
|
||||
if (cert == NULL)
|
||||
{
|
||||
ret = X509_VFY_ERROR_NO_TRUSTED_CERT;
|
||||
goto end_verify;
|
||||
}
|
||||
|
||||
/* a self-signed certificate that is not in the CA store - use this
|
||||
to check the signature */
|
||||
if (asn1_compare_dn(cert->ca_cert_dn, cert->cert_dn) == 0)
|
||||
{
|
||||
is_self_signed = 1;
|
||||
ctx = cert->rsa_ctx->bi_ctx;
|
||||
mod = cert->rsa_ctx->m;
|
||||
expn = cert->rsa_ctx->e;
|
||||
}
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
/* check the not before date */
|
||||
if (tv.tv_sec < cert->not_before)
|
||||
{
|
||||
ret = X509_VFY_ERROR_NOT_YET_VALID;
|
||||
goto end_verify;
|
||||
}
|
||||
|
||||
/* check the not after date */
|
||||
if (tv.tv_sec > cert->not_after)
|
||||
{
|
||||
ret = X509_VFY_ERROR_EXPIRED;
|
||||
goto end_verify;
|
||||
}
|
||||
|
||||
if (cert->basic_constraint_present)
|
||||
{
|
||||
/* If the cA boolean is not asserted,
|
||||
then the keyCertSign bit in the key usage extension MUST NOT be
|
||||
asserted. */
|
||||
if (!cert->basic_constraint_cA &&
|
||||
IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN))
|
||||
{
|
||||
ret = X509_VFY_ERROR_BASIC_CONSTRAINT;
|
||||
goto end_verify;
|
||||
}
|
||||
|
||||
/* The pathLenConstraint field is meaningful only if the cA boolean is
|
||||
asserted and the key usage extension, if present, asserts the
|
||||
keyCertSign bit. In this case, it gives the maximum number of
|
||||
non-self-issued intermediate certificates that may follow this
|
||||
certificate in a valid certification path. */
|
||||
if (cert->basic_constraint_cA &&
|
||||
(!cert->key_usage_present ||
|
||||
IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN)) &&
|
||||
(cert->basic_constraint_pathLenConstraint+1) < *pathLenConstraint)
|
||||
{
|
||||
ret = X509_VFY_ERROR_BASIC_CONSTRAINT;
|
||||
goto end_verify;
|
||||
}
|
||||
}
|
||||
|
||||
next_cert = cert->next;
|
||||
|
||||
/* last cert in the chain - look for a trusted cert */
|
||||
if (next_cert == NULL)
|
||||
{
|
||||
if (ca_cert_ctx != NULL)
|
||||
{
|
||||
/* go thru the CA store */
|
||||
while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
|
||||
{
|
||||
/* the extension is present but the cA boolean is not
|
||||
asserted, then the certified public key MUST NOT be used
|
||||
to verify certificate signatures. */
|
||||
if (cert->basic_constraint_present &&
|
||||
!ca_cert_ctx->cert[i]->basic_constraint_cA)
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (asn1_compare_dn(cert->ca_cert_dn,
|
||||
ca_cert_ctx->cert[i]->cert_dn) == 0)
|
||||
{
|
||||
/* use this CA certificate for signature verification */
|
||||
match_ca_cert = true;
|
||||
ctx = ca_cert_ctx->cert[i]->rsa_ctx->bi_ctx;
|
||||
mod = ca_cert_ctx->cert[i]->rsa_ctx->m;
|
||||
expn = ca_cert_ctx->cert[i]->rsa_ctx->e;
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/* couldn't find a trusted cert (& let self-signed errors
|
||||
be returned) */
|
||||
if (!match_ca_cert && !is_self_signed)
|
||||
{
|
||||
ret = X509_VFY_ERROR_NO_TRUSTED_CERT;
|
||||
goto end_verify;
|
||||
}
|
||||
}
|
||||
else if (asn1_compare_dn(cert->ca_cert_dn, next_cert->cert_dn) != 0)
|
||||
{
|
||||
/* check the chain */
|
||||
ret = X509_VFY_ERROR_INVALID_CHAIN;
|
||||
goto end_verify;
|
||||
}
|
||||
else /* use the next certificate in the chain for signature verify */
|
||||
{
|
||||
ctx = next_cert->rsa_ctx->bi_ctx;
|
||||
mod = next_cert->rsa_ctx->m;
|
||||
expn = next_cert->rsa_ctx->e;
|
||||
}
|
||||
|
||||
/* cert is self signed */
|
||||
if (!match_ca_cert && is_self_signed)
|
||||
{
|
||||
ret = X509_VFY_ERROR_SELF_SIGNED;
|
||||
goto end_verify;
|
||||
}
|
||||
|
||||
/* check the signature */
|
||||
cert_sig = sig_verify(ctx, cert->signature, cert->sig_len, cert->sig_type,
|
||||
bi_clone(ctx, mod), bi_clone(ctx, expn));
|
||||
|
||||
if (cert_sig && cert->digest)
|
||||
{
|
||||
if (bi_compare(cert_sig, cert->digest) != 0)
|
||||
ret = X509_VFY_ERROR_BAD_SIGNATURE;
|
||||
|
||||
|
||||
bi_free(ctx, cert_sig);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = X509_VFY_ERROR_BAD_SIGNATURE;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
goto end_verify;
|
||||
|
||||
/* go down the certificate chain using recursion. */
|
||||
if (next_cert != NULL)
|
||||
{
|
||||
(*pathLenConstraint)++; /* don't include last certificate */
|
||||
ret = x509_verify(ca_cert_ctx, next_cert, pathLenConstraint);
|
||||
}
|
||||
|
||||
end_verify:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for diagnostics.
|
||||
*/
|
||||
static const char *not_part_of_cert = "<Not Part Of Certificate>";
|
||||
void x509_print(const X509_CTX *cert, CA_CERT_CTX *ca_cert_ctx)
|
||||
{
|
||||
if (cert == NULL)
|
||||
return;
|
||||
|
||||
printf("=== CERTIFICATE ISSUED TO ===\n");
|
||||
printf("Common Name (CN):\t\t");
|
||||
printf("%s\n", cert->cert_dn[X509_COMMON_NAME] ?
|
||||
cert->cert_dn[X509_COMMON_NAME] : not_part_of_cert);
|
||||
|
||||
printf("Organization (O):\t\t");
|
||||
printf("%s\n", cert->cert_dn[X509_ORGANIZATION] ?
|
||||
cert->cert_dn[X509_ORGANIZATION] : not_part_of_cert);
|
||||
|
||||
if (cert->cert_dn[X509_ORGANIZATIONAL_UNIT])
|
||||
{
|
||||
printf("Organizational Unit (OU):\t");
|
||||
printf("%s\n", cert->cert_dn[X509_ORGANIZATIONAL_UNIT]);
|
||||
}
|
||||
|
||||
if (cert->cert_dn[X509_LOCATION])
|
||||
{
|
||||
printf("Location (L):\t\t\t");
|
||||
printf("%s\n", cert->cert_dn[X509_LOCATION]);
|
||||
}
|
||||
|
||||
if (cert->cert_dn[X509_COUNTRY])
|
||||
{
|
||||
printf("Country (C):\t\t\t");
|
||||
printf("%s\n", cert->cert_dn[X509_COUNTRY]);
|
||||
}
|
||||
|
||||
if (cert->cert_dn[X509_STATE])
|
||||
{
|
||||
printf("State (ST):\t\t\t");
|
||||
printf("%s\n", cert->cert_dn[X509_STATE]);
|
||||
}
|
||||
|
||||
if (cert->basic_constraint_present)
|
||||
{
|
||||
printf("Basic Constraints:\t\t%sCA:%s, pathlen:%d\n",
|
||||
cert->basic_constraint_is_critical ?
|
||||
"critical, " : "",
|
||||
cert->basic_constraint_cA? "TRUE" : "FALSE",
|
||||
cert->basic_constraint_pathLenConstraint);
|
||||
}
|
||||
|
||||
if (cert->key_usage_present)
|
||||
{
|
||||
printf("Key Usage:\t\t\t%s", cert->key_usage_is_critical ?
|
||||
"critical, " : "");
|
||||
bool has_started = false;
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DIGITAL_SIGNATURE))
|
||||
{
|
||||
printf("Digital Signature");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_NON_REPUDIATION))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Non Repudiation");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_ENCIPHERMENT))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Key Encipherment");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DATA_ENCIPHERMENT))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Data Encipherment");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_AGREEMENT))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Key Agreement");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_KEY_CERT_SIGN))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Key Cert Sign");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_CRL_SIGN))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("CRL Sign");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_ENCIPHER_ONLY))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Encipher Only");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
if (IS_SET_KEY_USAGE_FLAG(cert, KEY_USAGE_DECIPHER_ONLY))
|
||||
{
|
||||
if (has_started)
|
||||
printf(", ");
|
||||
|
||||
printf("Decipher Only");
|
||||
has_started = true;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (cert->subject_alt_name_present)
|
||||
{
|
||||
printf("Subject Alt Name:\t\t%s", cert->subject_alt_name_is_critical
|
||||
? "critical, " : "");
|
||||
if (cert->subject_alt_dnsnames)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (cert->subject_alt_dnsnames[i])
|
||||
printf("%s ", cert->subject_alt_dnsnames[i++]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
}
|
||||
|
||||
printf("=== CERTIFICATE ISSUED BY ===\n");
|
||||
printf("Common Name (CN):\t\t");
|
||||
printf("%s\n", cert->ca_cert_dn[X509_COMMON_NAME] ?
|
||||
cert->ca_cert_dn[X509_COMMON_NAME] : not_part_of_cert);
|
||||
|
||||
printf("Organization (O):\t\t");
|
||||
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATION] ?
|
||||
cert->ca_cert_dn[X509_ORGANIZATION] : not_part_of_cert);
|
||||
|
||||
if (cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT])
|
||||
{
|
||||
printf("Organizational Unit (OU):\t");
|
||||
printf("%s\n", cert->ca_cert_dn[X509_ORGANIZATIONAL_UNIT]);
|
||||
}
|
||||
|
||||
if (cert->ca_cert_dn[X509_LOCATION])
|
||||
{
|
||||
printf("Location (L):\t\t\t");
|
||||
printf("%s\n", cert->ca_cert_dn[X509_LOCATION]);
|
||||
}
|
||||
|
||||
if (cert->ca_cert_dn[X509_COUNTRY])
|
||||
{
|
||||
printf("Country (C):\t\t\t");
|
||||
printf("%s\n", cert->ca_cert_dn[X509_COUNTRY]);
|
||||
}
|
||||
|
||||
if (cert->ca_cert_dn[X509_STATE])
|
||||
{
|
||||
printf("State (ST):\t\t\t");
|
||||
printf("%s\n", cert->ca_cert_dn[X509_STATE]);
|
||||
}
|
||||
|
||||
printf("Not Before:\t\t\t%s", ctime(&cert->not_before));
|
||||
printf("Not After:\t\t\t%s", ctime(&cert->not_after));
|
||||
printf("RSA bitsize:\t\t\t%d\n", cert->rsa_ctx->num_octets*8);
|
||||
printf("Sig Type:\t\t\t");
|
||||
switch (cert->sig_type)
|
||||
{
|
||||
case SIG_TYPE_MD5:
|
||||
printf("MD5\n");
|
||||
break;
|
||||
case SIG_TYPE_SHA1:
|
||||
printf("SHA1\n");
|
||||
break;
|
||||
case SIG_TYPE_SHA256:
|
||||
printf("SHA256\n");
|
||||
break;
|
||||
case SIG_TYPE_SHA384:
|
||||
printf("SHA384\n");
|
||||
break;
|
||||
case SIG_TYPE_SHA512:
|
||||
printf("SHA512\n");
|
||||
break;
|
||||
default:
|
||||
printf("Unrecognized: %d\n", cert->sig_type);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ca_cert_ctx)
|
||||
{
|
||||
int pathLenConstraint = 0;
|
||||
printf("Verify:\t\t\t\t%s\n",
|
||||
x509_display_error(x509_verify(ca_cert_ctx, cert,
|
||||
&pathLenConstraint)));
|
||||
}
|
||||
|
||||
#if 0
|
||||
print_blob("Signature", cert->signature, cert->sig_len);
|
||||
bi_print("Modulus", cert->rsa_ctx->m);
|
||||
bi_print("Pub Exp", cert->rsa_ctx->e);
|
||||
#endif
|
||||
|
||||
if (ca_cert_ctx)
|
||||
{
|
||||
x509_print(cert->next, ca_cert_ctx);
|
||||
}
|
||||
|
||||
TTY_FLUSH();
|
||||
}
|
||||
#endif /* CONFIG_DEBUG */
|
||||
|
||||
/* Information function */
|
||||
const char * x509_display_error(int error)
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case X509_OK:
|
||||
return "Certificate verify successful";
|
||||
|
||||
case X509_NOT_OK:
|
||||
return "X509 not ok";
|
||||
|
||||
case X509_VFY_ERROR_NO_TRUSTED_CERT:
|
||||
return "No trusted cert is available";
|
||||
|
||||
case X509_VFY_ERROR_BAD_SIGNATURE:
|
||||
return "Bad signature";
|
||||
|
||||
case X509_VFY_ERROR_NOT_YET_VALID:
|
||||
return "Cert is not yet valid";
|
||||
|
||||
case X509_VFY_ERROR_EXPIRED:
|
||||
return "Cert has expired";
|
||||
|
||||
case X509_VFY_ERROR_SELF_SIGNED:
|
||||
return "Cert is self-signed";
|
||||
|
||||
case X509_VFY_ERROR_INVALID_CHAIN:
|
||||
return "Chain is invalid (check order of certs)";
|
||||
|
||||
case X509_VFY_ERROR_UNSUPPORTED_DIGEST:
|
||||
return "Unsupported digest";
|
||||
|
||||
case X509_INVALID_PRIV_KEY:
|
||||
return "Invalid private key";
|
||||
|
||||
case X509_VFY_ERROR_BASIC_CONSTRAINT:
|
||||
return "Basic constraint invalid";
|
||||
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user