This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / apps / s_cb.c
1 /* apps/s_cb.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #define USE_SOCKETS
62 #define NON_MAIN
63 #include "apps.h"
64 #undef NON_MAIN
65 #undef USE_SOCKETS
66 #include "err.h"
67 #include "x509.h"
68 #include "ssl.h"
69 #include "s_apps.h"
70
71 int verify_depth=0;
72 int verify_error=X509_V_OK;
73
74 int MS_CALLBACK verify_callback(ok, ctx)
75 int ok;
76 X509_STORE_CTX *ctx;
77         {
78         char buf[256];
79         X509 *err_cert;
80         int err,depth;
81
82         err_cert=X509_STORE_CTX_get_current_cert(ctx);
83         err=    X509_STORE_CTX_get_error(ctx);
84         depth=  X509_STORE_CTX_get_error_depth(ctx);
85
86         X509_NAME_oneline(X509_get_subject_name(err_cert),buf,256);
87         BIO_printf(bio_err,"depth=%d %s\n",depth,buf);
88         if (!ok)
89                 {
90                 BIO_printf(bio_err,"verify error:num=%d:%s\n",err,
91                         X509_verify_cert_error_string(err));
92                 if (verify_depth >= depth)
93                         {
94                         ok=1;
95                         verify_error=X509_V_OK;
96                         }
97                 else
98                         {
99                         ok=0;
100                         verify_error=X509_V_ERR_CERT_CHAIN_TOO_LONG;
101                         }
102                 }
103         switch (ctx->error)
104                 {
105         case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
106                 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert),buf,256);
107                 BIO_printf(bio_err,"issuer= %s\n",buf);
108                 break;
109         case X509_V_ERR_CERT_NOT_YET_VALID:
110         case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
111                 BIO_printf(bio_err,"notBefore=");
112                 ASN1_UTCTIME_print(bio_err,X509_get_notBefore(ctx->current_cert));
113                 BIO_printf(bio_err,"\n");
114                 break;
115         case X509_V_ERR_CERT_HAS_EXPIRED:
116         case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
117                 BIO_printf(bio_err,"notAfter=");
118                 ASN1_UTCTIME_print(bio_err,X509_get_notAfter(ctx->current_cert));
119                 BIO_printf(bio_err,"\n");
120                 break;
121                 }
122         BIO_printf(bio_err,"verify return:%d\n",ok);
123         return(ok);
124         }
125
126 int set_cert_stuff(ctx, cert_file, key_file)
127 SSL_CTX *ctx;
128 char *cert_file;
129 char *key_file;
130         {
131         if (cert_file != NULL)
132                 {
133                 SSL *ssl;
134                 X509 *x509;
135
136                 if (SSL_CTX_use_certificate_file(ctx,cert_file,
137                         SSL_FILETYPE_PEM) <= 0)
138                         {
139                         BIO_printf(bio_err,"unable to get certificate from '%s'\n",cert_file);
140                         ERR_print_errors(bio_err);
141                         return(0);
142                         }
143                 if (key_file == NULL) key_file=cert_file;
144                 if (SSL_CTX_use_PrivateKey_file(ctx,key_file,
145                         SSL_FILETYPE_PEM) <= 0)
146                         {
147                         BIO_printf(bio_err,"unable to get private key from '%s'\n",key_file);
148                         ERR_print_errors(bio_err);
149                         return(0);
150                         }
151
152                 ssl=SSL_new(ctx);
153                 x509=SSL_get_certificate(ssl);
154
155                 if (x509 != NULL)
156                         EVP_PKEY_copy_parameters(X509_get_pubkey(x509),
157                                 SSL_get_privatekey(ssl));
158                 SSL_free(ssl);
159
160                 /* If we are using DSA, we can copy the parameters from
161                  * the private key */
162                 
163                 
164                 /* Now we know that a key and cert have been set against
165                  * the SSL context */
166                 if (!SSL_CTX_check_private_key(ctx))
167                         {
168                         BIO_printf(bio_err,"Private key does not match the certificate public key\n");
169                         return(0);
170                         }
171                 }
172         return(1);
173         }
174
175 long MS_CALLBACK bio_dump_cb(bio,cmd,argp,argi,argl,ret)
176 BIO *bio;
177 int cmd;
178 char *argp;
179 int argi;
180 long argl;
181 long ret;
182         {
183         BIO *out;
184
185         out=(BIO *)BIO_get_callback_arg(bio);
186         if (out == NULL) return(ret);
187
188         if (cmd == (BIO_CB_READ|BIO_CB_RETURN))
189                 {
190                 BIO_printf(out,"read from %08X [%08lX] (%d bytes => %ld (0x%X))\n",
191                         bio,argp,argi,ret,ret);
192                 BIO_dump(out,argp,(int)ret);
193                 return(ret);
194                 }
195         else if (cmd == (BIO_CB_WRITE|BIO_CB_RETURN))
196                 {
197                 BIO_printf(out,"write to %08X [%08lX] (%d bytes => %ld (0x%X))\n",
198                         bio,argp,argi,ret,ret);
199                 BIO_dump(out,argp,(int)ret);
200                 }
201         return(ret);
202         }
203
204 void MS_CALLBACK apps_ssl_info_callback(s,where,ret)
205 SSL *s;
206 int where;
207 int ret;
208         {
209         char *str;
210         int w;
211
212         w=where& ~SSL_ST_MASK;
213
214         if (w & SSL_ST_CONNECT) str="SSL_connect";
215         else if (w & SSL_ST_ACCEPT) str="SSL_accept";
216         else str="undefined";
217
218         if (where & SSL_CB_LOOP)
219                 {
220                 BIO_printf(bio_err,"%s:%s\n",str,SSL_state_string_long(s));
221                 }
222         else if (where & SSL_CB_ALERT)
223                 {
224                 str=(where & SSL_CB_READ)?"read":"write";
225                 BIO_printf(bio_err,"SSL3 alert %s:%s:%s\n",
226                         str,
227                         SSL_alert_type_string_long(ret),
228                         SSL_alert_desc_string_long(ret));
229                 }
230         else if (where & SSL_CB_EXIT)
231                 {
232                 if (ret == 0)
233                         BIO_printf(bio_err,"%s:failed in %s\n",
234                                 str,SSL_state_string_long(s));
235                 else if (ret < 0)
236                         {
237                         BIO_printf(bio_err,"%s:error in %s\n",
238                                 str,SSL_state_string_long(s));
239                         }
240                 }
241         }
242