Skip to content

Commit eb90a48

Browse files
author
Ben Laurie
committedFeb 28, 1999
Add functions to add certs to stacks, used for CA file/path stuff in servers.
1 parent 49bc262 commit eb90a48

File tree

9 files changed

+161
-48
lines changed

9 files changed

+161
-48
lines changed
 

‎CHANGES

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
Changes between 0.9.1c and 0.9.2
77

8+
*) Add new certificate file to stack functions, SSL_add_cert_file_to_stack()
9+
and SSL_add_cert_dir_to_stack(). These largely supplant
10+
SSL_load_client_CA_file(), and can be used to add multiple certs easily to
11+
a stack (usually this is then handed to SSL_CTX_set_client_CA_list()).
12+
This means that Apache-SSL and similar packages don't have to mess around
13+
to add as many CAs as they want to the preferred list.
14+
[Ben Laurie]
15+
816
*) Experiment with doxygen documentation. Currently only partially applied to
917
ssl/ssl_lib.c.
1018
See http://www.stack.nl/~dimitri/doxygen/index.html, and run doxygen with

‎crypto/bio/bio.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ int BIO_get_ex_new_index(long argl, char *argp, int (*new_func)(),
383383
#define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL)
384384
#define BIO_tell(b) (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL)
385385

386+
/* name is cast to lose const, but might be better to route through a function
387+
so we can do it safely */
386388
#define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
387-
BIO_CLOSE|BIO_FP_READ,name)
389+
BIO_CLOSE|BIO_FP_READ,(char *)name)
388390
#define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
389391
BIO_CLOSE|BIO_FP_WRITE,name)
390392
#define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \

‎crypto/stack/stack.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@ char *STACK_version="Stack part of OpenSSL 0.9.2 31-Dec-1998";
8282

8383
#include <errno.h>
8484

85-
void sk_set_cmp_func(sk,c)
85+
int (*sk_set_cmp_func(sk,c))()
8686
STACK *sk;
8787
int (*c)();
8888
{
89+
int (*old)()=sk->comp;
90+
8991
if (sk->comp != c)
9092
sk->sorted=0;
9193
sk->comp=c;
94+
95+
return old;
9296
}
9397

9498
STACK *sk_dup(sk)

‎crypto/stack/stack.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int sk_unshift(STACK *st,char *data);
9191
char *sk_shift(STACK *st);
9292
char *sk_pop(STACK *st);
9393
void sk_zero(STACK *st);
94-
void sk_set_cmp_func(STACK *sk, int (*c)());
94+
int (*sk_set_cmp_func(STACK *sk, int (*c)()))();
9595
STACK *sk_dup(STACK *st);
9696

9797
#else
@@ -108,7 +108,7 @@ int sk_unshift();
108108
char *sk_shift();
109109
char *sk_pop();
110110
void sk_zero();
111-
void sk_set_cmp_func();
111+
int (*sk_set_cmp_func())();
112112
STACK *sk_dup();
113113

114114
#endif

‎openssl.doxy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PROJECT_NAME=OpenSSL
22
GENERATE_LATEX=no
33
OUTPUT_DIRECTORY=doxygen
4-
INPUT=ssl
4+
INPUT=ssl include
55
FILE_PATTERNS=*.c *.h
66
RECURSIVE=yes
77
PREDEFINED=DOXYGEN

‎ssl/ssl.err

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM 130
3535
#define SSL_F_SSL3_CLIENT_HELLO 131
3636
#define SSL_F_SSL3_CONNECT 132
37+
#define SSL_F_SSL3_CTRL 213
3738
#define SSL_F_SSL3_CTX_CTRL 133
3839
#define SSL_F_SSL3_ENC 134
3940
#define SSL_F_SSL3_GET_CERTIFICATE_REQUEST 135
@@ -61,8 +62,11 @@
6162
#define SSL_F_SSL3_SETUP_KEY_BLOCK 157
6263
#define SSL_F_SSL3_WRITE_BYTES 158
6364
#define SSL_F_SSL3_WRITE_PENDING 159
65+
#define SSL_F_SSL_ADD_CERT_DIR_TO_STACK 215
66+
#define SSL_F_SSL_ADD_CERT_FILE_TO_STACK 216
6467
#define SSL_F_SSL_BAD_METHOD 160
6568
#define SSL_F_SSL_BYTES_TO_CIPHER_LIST 161
69+
#define SSL_F_SSL_CERT_INSTANTIATE 214
6670
#define SSL_F_SSL_CERT_NEW 162
6771
#define SSL_F_SSL_CHECK_PRIVATE_KEY 163
6872
#define SSL_F_SSL_CLEAR 164
@@ -113,8 +117,6 @@
113117
#define SSL_F_TLS1_ENC 210
114118
#define SSL_F_TLS1_SETUP_KEY_BLOCK 211
115119
#define SSL_F_WRITE_PENDING 212
116-
#define SSL_F_SSL3_CTRL 213
117-
#define SSL_F_SSL_CERT_INSTANTIATE 214
118120

119121
/* Reason codes. */
120122
#define SSL_R_APP_DATA_IN_HANDSHAKE 100
@@ -216,6 +218,7 @@
216218
#define SSL_R_NULL_SSL_METHOD_PASSED 196
217219
#define SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED 197
218220
#define SSL_R_PACKET_LENGTH_TOO_LONG 198
221+
#define SSL_R_PATH_TOO_LONG 270
219222
#define SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE 199
220223
#define SSL_R_PEER_ERROR 200
221224
#define SSL_R_PEER_ERROR_CERTIFICATE 201

‎ssl/ssl.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,8 @@ int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, char *file, int type);
859859
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, char *file, int type);
860860
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, char *file, int type);
861861
STACK * SSL_load_client_CA_file(char *file);
862+
int SSL_add_cert_file_to_stack(STACK *stackCAs,const char *file);
863+
int SSL_add_cert_dir_to_stack(STACK *stackCAs,const char *dir);
862864
#endif
863865

864866
void ERR_load_SSL_strings(void );
@@ -1111,6 +1113,8 @@ int SSL_CTX_use_RSAPrivateKey_file();
11111113
int SSL_CTX_use_PrivateKey_file();
11121114
int SSL_CTX_use_certificate_file();
11131115
STACK * SSL_load_client_CA_file();
1116+
int SSL_add_cert_file_to_stack();
1117+
int SSL_add_cert_dir_to_stack();
11141118
#endif
11151119

11161120
void ERR_load_SSL_strings();
@@ -1317,6 +1321,7 @@ void SSL_set_tmp_dh_callback();
13171321
#define SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM 130
13181322
#define SSL_F_SSL3_CLIENT_HELLO 131
13191323
#define SSL_F_SSL3_CONNECT 132
1324+
#define SSL_F_SSL3_CTRL 213
13201325
#define SSL_F_SSL3_CTX_CTRL 133
13211326
#define SSL_F_SSL3_ENC 134
13221327
#define SSL_F_SSL3_GET_CERTIFICATE_REQUEST 135
@@ -1344,8 +1349,11 @@ void SSL_set_tmp_dh_callback();
13441349
#define SSL_F_SSL3_SETUP_KEY_BLOCK 157
13451350
#define SSL_F_SSL3_WRITE_BYTES 158
13461351
#define SSL_F_SSL3_WRITE_PENDING 159
1352+
#define SSL_F_SSL_ADD_CERT_DIR_TO_STACK 215
1353+
#define SSL_F_SSL_ADD_CERT_FILE_TO_STACK 216
13471354
#define SSL_F_SSL_BAD_METHOD 160
13481355
#define SSL_F_SSL_BYTES_TO_CIPHER_LIST 161
1356+
#define SSL_F_SSL_CERT_INSTANTIATE 214
13491357
#define SSL_F_SSL_CERT_NEW 162
13501358
#define SSL_F_SSL_CHECK_PRIVATE_KEY 163
13511359
#define SSL_F_SSL_CLEAR 164
@@ -1396,8 +1404,6 @@ void SSL_set_tmp_dh_callback();
13961404
#define SSL_F_TLS1_ENC 210
13971405
#define SSL_F_TLS1_SETUP_KEY_BLOCK 211
13981406
#define SSL_F_WRITE_PENDING 212
1399-
#define SSL_F_SSL3_CTRL 213
1400-
#define SSL_F_SSL_CERT_INSTANTIATE 214
14011407

14021408
/* Reason codes. */
14031409
#define SSL_R_APP_DATA_IN_HANDSHAKE 100
@@ -1499,6 +1505,7 @@ void SSL_set_tmp_dh_callback();
14991505
#define SSL_R_NULL_SSL_METHOD_PASSED 196
15001506
#define SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED 197
15011507
#define SSL_R_PACKET_LENGTH_TOO_LONG 198
1508+
#define SSL_R_PATH_TOO_LONG 270
15021509
#define SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE 199
15031510
#define SSL_R_PEER_ERROR 200
15041511
#define SSL_R_PEER_ERROR_CERTIFICATE 201

‎ssl/ssl_cert.c

+123-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* ssl/ssl_cert.c */
1+
/*! \file ssl/ssl_cert.c */
22
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
33
* All rights reserved.
44
*
@@ -57,6 +57,8 @@
5757
*/
5858

5959
#include <stdio.h>
60+
#include <sys/types.h>
61+
#include <dirent.h>
6062
#include "objects.h"
6163
#include "bio.h"
6264
#include "pem.h"
@@ -100,8 +102,7 @@ CERT *ssl_cert_new()
100102
return(ret);
101103
}
102104

103-
void ssl_cert_free(c)
104-
CERT *c;
105+
void ssl_cert_free(CERT *c)
105106
{
106107
int i;
107108

@@ -165,17 +166,13 @@ int ssl_cert_instantiate(CERT **o, CERT *d)
165166
return(1);
166167
}
167168

168-
int ssl_set_cert_type(c, type)
169-
CERT *c;
170-
int type;
169+
int ssl_set_cert_type(CERT *c,int type)
171170
{
172171
c->cert_type=type;
173172
return(1);
174173
}
175174

176-
int ssl_verify_cert_chain(s,sk)
177-
SSL *s;
178-
STACK *sk;
175+
int ssl_verify_cert_chain(SSL *s,STACK *sk)
179176
{
180177
X509 *x;
181178
int i;
@@ -208,18 +205,15 @@ STACK *sk;
208205
return(i);
209206
}
210207

211-
static void set_client_CA_list(ca_list,list)
212-
STACK **ca_list;
213-
STACK *list;
208+
static void set_client_CA_list(STACK **ca_list,STACK *list)
214209
{
215210
if (*ca_list != NULL)
216211
sk_pop_free(*ca_list,X509_NAME_free);
217212

218213
*ca_list=list;
219214
}
220215

221-
STACK *SSL_dup_CA_list(sk)
222-
STACK *sk;
216+
STACK *SSL_dup_CA_list(STACK *sk)
223217
{
224218
int i;
225219
STACK *ret;
@@ -238,28 +232,22 @@ STACK *sk;
238232
return(ret);
239233
}
240234

241-
void SSL_set_client_CA_list(s,list)
242-
SSL *s;
243-
STACK *list;
235+
void SSL_set_client_CA_list(SSL *s,STACK *list)
244236
{
245237
set_client_CA_list(&(s->client_CA),list);
246238
}
247239

248-
void SSL_CTX_set_client_CA_list(ctx,list)
249-
SSL_CTX *ctx;
250-
STACK *list;
240+
void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK *list)
251241
{
252242
set_client_CA_list(&(ctx->client_CA),list);
253243
}
254244

255-
STACK *SSL_CTX_get_client_CA_list(ctx)
256-
SSL_CTX *ctx;
245+
STACK *SSL_CTX_get_client_CA_list(SSL_CTX *ctx)
257246
{
258247
return(ctx->client_CA);
259248
}
260249

261-
STACK *SSL_get_client_CA_list(s)
262-
SSL *s;
250+
STACK *SSL_get_client_CA_list(SSL *s)
263251
{
264252
if (s->type == SSL_ST_CONNECT)
265253
{ /* we are in the client */
@@ -278,9 +266,7 @@ SSL *s;
278266
}
279267
}
280268

281-
static int add_client_CA(sk,x)
282-
STACK **sk;
283-
X509 *x;
269+
static int add_client_CA(STACK **sk,X509 *x)
284270
{
285271
X509_NAME *name;
286272

@@ -299,29 +285,31 @@ X509 *x;
299285
return(1);
300286
}
301287

302-
int SSL_add_client_CA(ssl,x)
303-
SSL *ssl;
304-
X509 *x;
288+
int SSL_add_client_CA(SSL *ssl,X509 *x)
305289
{
306290
return(add_client_CA(&(ssl->client_CA),x));
307291
}
308292

309-
int SSL_CTX_add_client_CA(ctx,x)
310-
SSL_CTX *ctx;
311-
X509 *x;
293+
int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
312294
{
313295
return(add_client_CA(&(ctx->client_CA),x));
314296
}
315297

316-
static int name_cmp(a,b)
317-
X509_NAME **a,**b;
298+
static int name_cmp(X509_NAME **a,X509_NAME **b)
318299
{
319300
return(X509_NAME_cmp(*a,*b));
320301
}
321302

322303
#ifndef NO_STDIO
323-
STACK *SSL_load_client_CA_file(file)
324-
char *file;
304+
/*!
305+
* Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
306+
* it doesn't really have anything to do with clients (except that a common use
307+
* for a stack of CAs is to send it to the client). Actually, it doesn't have
308+
* much to do with CAs, either, since it will load any old cert.
309+
* \param file the file containing one or more certs.
310+
* \return a ::STACK containing the certs.
311+
*/
312+
STACK *SSL_load_client_CA_file(char *file)
325313
{
326314
BIO *in;
327315
X509 *x=NULL;
@@ -372,3 +360,101 @@ char *file;
372360
}
373361
#endif
374362

363+
/*!
364+
* Add a file of certs to a stack.
365+
* \param stack the stack to add to.
366+
* \param file the file to add from. All certs in this file that are not
367+
* already in the stack will be added.
368+
* \return 1 for success, 0 for failure. Note that in the case of failure some
369+
* certs may have been added to \c stack.
370+
*/
371+
372+
int SSL_add_cert_file_to_stack(STACK *stack,const char *file)
373+
{
374+
BIO *in;
375+
X509 *x=NULL;
376+
X509_NAME *xn=NULL;
377+
int ret=1;
378+
int (*oldcmp)();
379+
380+
oldcmp=sk_set_cmp_func(stack,name_cmp);
381+
382+
in=BIO_new(BIO_s_file_internal());
383+
384+
if (ret == NULL || in == NULL)
385+
{
386+
SSLerr(SSL_F_SSL_ADD_CERT_FILE_TO_STACK,ERR_R_MALLOC_FAILURE);
387+
goto err;
388+
}
389+
390+
if (!BIO_read_filename(in,file))
391+
goto err;
392+
393+
for (;;)
394+
{
395+
if (PEM_read_bio_X509(in,&x,NULL) == NULL)
396+
break;
397+
if ((xn=X509_get_subject_name(x)) == NULL) goto err;
398+
xn=X509_NAME_dup(xn);
399+
if (xn == NULL) goto err;
400+
if (sk_find(stack,(char *)xn) >= 0)
401+
X509_NAME_free(xn);
402+
else
403+
sk_push(stack,(char *)xn);
404+
}
405+
406+
if (0)
407+
{
408+
err:
409+
ret=0;
410+
}
411+
if(in != NULL)
412+
BIO_free(in);
413+
if(x != NULL)
414+
X509_free(x);
415+
416+
sk_set_cmp_func(stack,oldcmp);
417+
418+
return ret;
419+
}
420+
421+
/*!
422+
* Add a directory of certs to a stack.
423+
* \param stack the stack to append to.
424+
* \param dir the directory to append from. All files in this directory will be
425+
* examined as potential certs. Any that are acceptable to
426+
* SSL_add_cert_file_to_stack() that are not already in the stack will be
427+
* included.
428+
* \return 1 for success, 0 for failure. Note that in the case of failure some
429+
* certs may have been added to \c stack.
430+
*/
431+
432+
int SSL_add_cert_dir_to_stack(STACK *stack,const char *dir)
433+
{
434+
DIR *d=opendir(dir);
435+
struct dirent *dstruct;
436+
437+
/* Note that a side effect is that the CAs will be sorted by name */
438+
if(!d)
439+
{
440+
SSLerr(SSL_F_SSL_ADD_CERT_DIR_TO_STACK,ERR_R_MALLOC_FAILURE);
441+
return 0;
442+
}
443+
444+
while((dstruct=readdir(d)))
445+
{
446+
char buf[1024];
447+
448+
if(strlen(dir)+strlen(dstruct->d_name)+2 > sizeof buf)
449+
{
450+
SSLerr(SSL_F_SSL_ADD_CERT_DIR_TO_STACK,SSL_R_PATH_TOO_LONG);
451+
return 0;
452+
}
453+
454+
sprintf(buf,"%s/%s",dir,dstruct->d_name);
455+
if(!SSL_add_cert_file_to_stack(stack,buf))
456+
return 0;
457+
}
458+
459+
return 1;
460+
}

‎ssl/ssl_err.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
9696
{ERR_PACK(0,SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,0), "SSL3_CHECK_CERT_AND_ALGORITHM"},
9797
{ERR_PACK(0,SSL_F_SSL3_CLIENT_HELLO,0), "SSL3_CLIENT_HELLO"},
9898
{ERR_PACK(0,SSL_F_SSL3_CONNECT,0), "SSL3_CONNECT"},
99+
{ERR_PACK(0,SSL_F_SSL3_CTRL,0), "SSL3_CTRL"},
99100
{ERR_PACK(0,SSL_F_SSL3_CTX_CTRL,0), "SSL3_CTX_CTRL"},
100101
{ERR_PACK(0,SSL_F_SSL3_ENC,0), "SSL3_ENC"},
101102
{ERR_PACK(0,SSL_F_SSL3_GET_CERTIFICATE_REQUEST,0), "SSL3_GET_CERTIFICATE_REQUEST"},
@@ -123,8 +124,11 @@ static ERR_STRING_DATA SSL_str_functs[]=
123124
{ERR_PACK(0,SSL_F_SSL3_SETUP_KEY_BLOCK,0), "SSL3_SETUP_KEY_BLOCK"},
124125
{ERR_PACK(0,SSL_F_SSL3_WRITE_BYTES,0), "SSL3_WRITE_BYTES"},
125126
{ERR_PACK(0,SSL_F_SSL3_WRITE_PENDING,0), "SSL3_WRITE_PENDING"},
127+
{ERR_PACK(0,SSL_F_SSL_ADD_CERT_DIR_TO_STACK,0), "SSL_add_cert_dir_to_stack"},
128+
{ERR_PACK(0,SSL_F_SSL_ADD_CERT_FILE_TO_STACK,0), "SSL_add_cert_file_to_stack"},
126129
{ERR_PACK(0,SSL_F_SSL_BAD_METHOD,0), "SSL_BAD_METHOD"},
127130
{ERR_PACK(0,SSL_F_SSL_BYTES_TO_CIPHER_LIST,0), "SSL_BYTES_TO_CIPHER_LIST"},
131+
{ERR_PACK(0,SSL_F_SSL_CERT_INSTANTIATE,0), "SSL_CERT_INSTANTIATE"},
128132
{ERR_PACK(0,SSL_F_SSL_CERT_NEW,0), "SSL_CERT_NEW"},
129133
{ERR_PACK(0,SSL_F_SSL_CHECK_PRIVATE_KEY,0), "SSL_check_private_key"},
130134
{ERR_PACK(0,SSL_F_SSL_CLEAR,0), "SSL_clear"},
@@ -175,8 +179,6 @@ static ERR_STRING_DATA SSL_str_functs[]=
175179
{ERR_PACK(0,SSL_F_TLS1_ENC,0), "TLS1_ENC"},
176180
{ERR_PACK(0,SSL_F_TLS1_SETUP_KEY_BLOCK,0), "TLS1_SETUP_KEY_BLOCK"},
177181
{ERR_PACK(0,SSL_F_WRITE_PENDING,0), "WRITE_PENDING"},
178-
{ERR_PACK(0,SSL_F_SSL3_CTRL,0), "SSL3_CTRL"},
179-
{ERR_PACK(0,SSL_F_SSL_CERT_INSTANTIATE,0), "SSL_CERT_INSTANTIATE"},
180182
{0,NULL},
181183
};
182184

@@ -281,6 +283,7 @@ static ERR_STRING_DATA SSL_str_reasons[]=
281283
{SSL_R_NULL_SSL_METHOD_PASSED ,"null ssl method passed"},
282284
{SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED ,"old session cipher not returned"},
283285
{SSL_R_PACKET_LENGTH_TOO_LONG ,"packet length too long"},
286+
{SSL_R_PATH_TOO_LONG ,"path too long"},
284287
{SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE ,"peer did not return a certificate"},
285288
{SSL_R_PEER_ERROR ,"peer error"},
286289
{SSL_R_PEER_ERROR_CERTIFICATE ,"peer error certificate"},

0 commit comments

Comments
 (0)
Please sign in to comment.