Skip to content

Commit 8ac6264

Browse files
author
cameronrich
committed
looks like some stuff didn't get checked in
git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@226 9a5d90b5-6617-0410-8a86-bb477d3ed2e3
1 parent fec170a commit 8ac6264

File tree

9 files changed

+113
-59
lines changed

9 files changed

+113
-59
lines changed

crypto/crypto.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ void RSA_print(const RSA_CTX *ctx);
217217
/**************************************************************************
218218
* RNG declarations
219219
**************************************************************************/
220-
EXP_FUNC void STDCALL RNG_initialize(const uint8_t *seed_buf, int size);
220+
EXP_FUNC void STDCALL RNG_initialize(void);
221+
EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
221222
EXP_FUNC void STDCALL RNG_terminate(void);
222223
EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
223224
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);

crypto/crypto_misc.c

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ static HCRYPTPROV gCryptProv;
5656
static uint8_t entropy_pool[ENTROPY_POOL_SIZE];
5757
#endif
5858

59-
static int rng_ref_count;
6059
const char * const unsupported_str = "Error: Feature not supported\n";
6160

6261
#ifndef CONFIG_SSL_SKELETON_MODE
@@ -102,61 +101,56 @@ int get_file(const char *filename, uint8_t **buf)
102101
* - On Linux use /dev/urandom
103102
* - If none of these work then use a custom RNG.
104103
*/
105-
EXP_FUNC void STDCALL RNG_initialize(const uint8_t *seed_buf, int size)
104+
EXP_FUNC void STDCALL RNG_initialize()
106105
{
107-
if (rng_ref_count == 0)
108-
{
109106
#if !defined(WIN32) && defined(CONFIG_USE_DEV_URANDOM)
110-
rng_fd = ax_open("/dev/urandom", O_RDONLY);
107+
rng_fd = ax_open("/dev/urandom", O_RDONLY);
111108
#elif defined(WIN32) && defined(CONFIG_WIN32_USE_CRYPTO_LIB)
112-
if (!CryptAcquireContext(&gCryptProv,
113-
NULL, NULL, PROV_RSA_FULL, 0))
109+
if (!CryptAcquireContext(&gCryptProv,
110+
NULL, NULL, PROV_RSA_FULL, 0))
111+
{
112+
if (GetLastError() == NTE_BAD_KEYSET &&
113+
!CryptAcquireContext(&gCryptProv,
114+
NULL,
115+
NULL,
116+
PROV_RSA_FULL,
117+
CRYPT_NEWKEYSET))
114118
{
115-
if (GetLastError() == NTE_BAD_KEYSET &&
116-
!CryptAcquireContext(&gCryptProv,
117-
NULL,
118-
NULL,
119-
PROV_RSA_FULL,
120-
CRYPT_NEWKEYSET))
121-
{
122-
printf("CryptoLib: %x\n", unsupported_str, GetLastError());
123-
exit(1);
124-
}
119+
printf("CryptoLib: %x\n", unsupported_str, GetLastError());
120+
exit(1);
125121
}
126-
#else
127-
int i;
128-
uint32_t seed_addr_val = (uint32_t)&seed_buf;
129-
uint32_t *ep = (uint32_t *)entropy_pool;
130-
131-
/* help start the entropy with the user's private key - this is
132-
a number that should be hard to find, due to the fact that it
133-
relies on knowing the private key */
134-
memcpy(entropy_pool, seed_buf, ENTROPY_POOL_SIZE);
135-
srand((long)entropy_pool);
122+
}
123+
#else
124+
/* start of with a stack to copy across */
125+
int i;
126+
memcpy(entropy_pool, &i, ENTROPY_POOL_SIZE);
127+
srand(&i);
128+
#endif
129+
}
136130

137-
/* mix it up a little with a stack address */
138-
for (i = 0; i < ENTROPY_POOL_SIZE/4; i++)
139-
ep[i] ^= seed_addr_val;
131+
/**
132+
* If no /dev/urandom, then initialise the RNG with something interesting.
133+
*/
134+
EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size)
135+
{
136+
#if defined(WIN32) || defined(CONFIG_WIN32_USE_CRYPTO_LIB)
137+
int i;
140138

139+
for (i = 0; i < ENTROPY_POOL_SIZE && i < size; i++)
140+
entropy_pool[i] ^= seed_buf[i];
141141
#endif
142-
}
143-
144-
rng_ref_count++;
145142
}
146143

147144
/**
148145
* Terminate the RNG engine.
149146
*/
150147
EXP_FUNC void STDCALL RNG_terminate(void)
151148
{
152-
if (--rng_ref_count == 0)
153-
{
154149
#ifndef WIN32
155-
close(rng_fd);
150+
close(rng_fd);
156151
#elif defined(CONFIG_WIN32_USE_CRYPTO_LIB)
157-
CryptReleaseContext(gCryptProv, 0);
152+
CryptReleaseContext(gCryptProv, 0);
158153
#endif
159-
}
160154
}
161155

162156
/**

crypto/os_int.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2012, Cameron Rich
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* * Neither the name of the axTLS project nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/**
32+
* @file os_int.h
33+
*
34+
* Ensure a consistent bit size
35+
*/
36+
37+
#ifndef HEADER_OS_INT_H
38+
#define HEADER_OS_INT_H
39+
40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
44+
#if defined(WIN32)
45+
typedef UINT8 uint8_t;
46+
typedef INT8 int8_t;
47+
typedef UINT16 uint16_t;
48+
typedef INT16 int16_t;
49+
typedef UINT32 uint32_t;
50+
typedef INT32 int32_t;
51+
typedef UINT64 uint64_t;
52+
typedef INT64 int64_t;
53+
#else /* Not Win32 */
54+
55+
#ifdef CONFIG_PLATFORM_SOLARIS
56+
#include <inttypes.h>
57+
#else
58+
#include <stdint.h>
59+
#endif /* Not Solaris */
60+
61+
#endif /* Not Win32 */
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif

httpd/axhttpd.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ static void addtoservers(int sd)
453453
static void handlenewconnection(int listenfd, int is_ssl)
454454
{
455455
struct sockaddr_in6 their_addr;
456-
int tp = sizeof(their_addr);
456+
socklen_t tp = sizeof(their_addr);
457457
char ipbuf[100];
458458
int connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp);
459459

@@ -506,8 +506,11 @@ static int openlistener(char *address, int port)
506506

507507
my_addr.sin6_family = AF_INET6;
508508
my_addr.sin6_port = htons(port);
509-
my_addr.sin6_addr.s_addr = address == NULL ?
510-
INADDR_ANY : iinet_addr(address);
509+
510+
if (address == NULL)
511+
my_addr.sin6_addr = in6addr_any;
512+
else
513+
inet_pton(AF_INET6, address, &my_addr.sin6_addr);
511514
#endif
512515

513516
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &tp, sizeof(tp));

httpd/htpasswd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int main(int argc, char *argv[])
120120
exit(1);
121121
}
122122

123-
RNG_initialize((uint8_t *)pw, sizeof(pw));
123+
RNG_initialize();
124124
get_random(MD5_SIZE, md5_salt);
125125
RNG_terminate();
126126
base64_encode(md5_salt, MD5_SIZE, b64_salt, sizeof(b64_salt));

ssl/asn1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ int asn1_get_private_key(const uint8_t *buf, int len, RSA_CTX **rsa_ctx)
160160
return X509_INVALID_PRIV_KEY;
161161
}
162162

163-
/* initialise the RNG */
164-
RNG_initialize(buf, len);
163+
/* Use the private key to mix up the RNG if possible. */
164+
RNG_custom_init(buf, len);
165165

166166
mod_len = asn1_get_int(buf, &offset, &modulus);
167167
pub_len = asn1_get_int(buf, &offset, &pub_exp);

ssl/os_port.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
extern "C" {
4242
#endif
4343

44+
#include "os_int.h"
4445
#include <stdio.h>
4546

4647
#if defined(WIN32)
@@ -114,14 +115,6 @@ extern "C" {
114115
#pragma comment(lib, "WS2_32.lib")
115116
#pragma comment(lib, "AdvAPI32.lib")
116117

117-
typedef UINT8 uint8_t;
118-
typedef INT8 int8_t;
119-
typedef UINT16 uint16_t;
120-
typedef INT16 int16_t;
121-
typedef UINT32 uint32_t;
122-
typedef INT32 int32_t;
123-
typedef UINT64 uint64_t;
124-
typedef INT64 int64_t;
125118
typedef int socklen_t;
126119

127120
EXP_FUNC void STDCALL gettimeofday(struct timeval* t,void* timezone);
@@ -130,12 +123,6 @@ EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size);
130123

131124
#else /* Not Win32 */
132125

133-
#ifdef CONFIG_PLATFORM_SOLARIS
134-
#include <inttypes.h>
135-
#else
136-
#include <stdint.h>
137-
#endif /* Not Solaris */
138-
139126
#include <unistd.h>
140127
#include <pwd.h>
141128
#include <netdb.h>

ssl/tls1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ EXP_FUNC SSL_CTX *STDCALL ssl_ctx_new(uint32_t options, int num_sessions)
166166
{
167167
SSL_CTX *ssl_ctx = (SSL_CTX *)calloc(1, sizeof (SSL_CTX));
168168
ssl_ctx->options = options;
169+
RNG_initialize();
169170

170171
if (load_key_certs(ssl_ctx) < 0)
171172
{

ssl/tls1.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern "C" {
4242

4343
#include "version.h"
4444
#include "crypto.h"
45+
#include "os_int.h"
4546
#include "crypto_misc.h"
4647

4748
#define SSL_PROTOCOL_MIN_VERSION 0x31 /* TLS v1.0 */

0 commit comments

Comments
 (0)