Ensure all EVP calls have their returns checked where appropriate
[openssl.git] / ssl / ssl_stat.c
1 /* ssl/ssl_stat.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  * Copyright 2005 Nokia. All rights reserved.
60  *
61  * The portions of the attached software ("Contribution") is developed by
62  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
63  * license.
64  *
65  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
66  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
67  * support (see RFC 4279) to OpenSSL.
68  *
69  * No patent licenses or other rights except those expressly stated in
70  * the OpenSSL open source license shall be deemed granted or received
71  * expressly, by implication, estoppel, or otherwise.
72  *
73  * No assurances are provided by Nokia that the Contribution does not
74  * infringe the patent or other intellectual property rights of any third
75  * party or that the license provides you with all the necessary rights
76  * to make use of the Contribution.
77  *
78  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
79  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
80  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
81  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
82  * OTHERWISE.
83  */
84
85 #include <stdio.h>
86 #include "ssl_locl.h"
87
88 const char *SSL_state_string_long(const SSL *s)
89 {
90     const char *str;
91
92     if (ossl_statem_in_error(s)) {
93         return "error";
94     }
95
96     switch (SSL_get_state(s)) {
97     case TLS_ST_BEFORE:
98         str = "before SSL initialization";
99         break;
100     case TLS_ST_OK:
101         str = "SSL negotiation finished successfully";
102         break;
103
104 /* SSLv3 additions */
105     case TLS_ST_CW_CLNT_HELLO:
106         str = "SSLv3/TLS write client hello";
107         break;
108     case TLS_ST_CR_SRVR_HELLO:
109         str = "SSLv3/TLS read server hello";
110         break;
111     case TLS_ST_CR_CERT:
112         str = "SSLv3/TLS read server certificate";
113         break;
114     case TLS_ST_CR_KEY_EXCH:
115         str = "SSLv3/TLS read server key exchange";
116         break;
117     case TLS_ST_CR_CERT_REQ:
118         str = "SSLv3/TLS read server certificate request";
119         break;
120     case TLS_ST_CR_SESSION_TICKET:
121         str = "SSLv3/TLS read server session ticket";
122         break;
123     case TLS_ST_CR_SRVR_DONE:
124         str = "SSLv3/TLS read server done";
125         break;
126     case TLS_ST_CW_CERT:
127         str = "SSLv3/TLS write client certificate";
128         break;
129     case TLS_ST_CW_KEY_EXCH:
130         str = "SSLv3/TLS write client key exchange";
131         break;
132     case TLS_ST_CW_CERT_VRFY:
133         str = "SSLv3/TLS write certificate verify";
134         break;
135
136     case TLS_ST_CW_CHANGE:
137     case TLS_ST_SW_CHANGE:
138         str = "SSLv3/TLS write change cipher spec";
139         break;
140     case TLS_ST_CW_FINISHED:
141     case TLS_ST_SW_FINISHED:
142         str = "SSLv3/TLS write finished";
143         break;
144     case TLS_ST_CR_CHANGE:
145     case TLS_ST_SR_CHANGE:
146         str = "SSLv3/TLS read change cipher spec";
147         break;
148     case TLS_ST_CR_FINISHED:
149     case TLS_ST_SR_FINISHED:
150         str = "SSLv3/TLS read finished";
151         break;
152
153     case TLS_ST_SR_CLNT_HELLO:
154         str = "SSLv3/TLS read client hello";
155         break;
156     case TLS_ST_SW_HELLO_REQ:
157         str = "SSLv3/TLS write hello request";
158         break;
159     case TLS_ST_SW_SRVR_HELLO:
160         str = "SSLv3/TLS write server hello";
161         break;
162     case TLS_ST_SW_CERT:
163         str = "SSLv3/TLS write certificate";
164         break;
165     case TLS_ST_SW_KEY_EXCH:
166         str = "SSLv3/TLS write key exchange";
167         break;
168     case TLS_ST_SW_CERT_REQ:
169         str = "SSLv3/TLS write certificate request";
170         break;
171     case TLS_ST_SW_SESSION_TICKET:
172         str = "SSLv3/TLS write session ticket";
173         break;
174     case TLS_ST_SW_SRVR_DONE:
175         str = "SSLv3/TLS write server done";
176         break;
177     case TLS_ST_SR_CERT:
178         str = "SSLv3/TLS read client certificate";
179         break;
180     case TLS_ST_SR_KEY_EXCH:
181         str = "SSLv3/TLS read client key exchange";
182         break;
183     case TLS_ST_SR_CERT_VRFY:
184         str = "SSLv3/TLS read certificate verify";
185         break;
186
187 /* DTLS */
188     case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
189         str = "DTLS1 read hello verify request";
190         break;
191     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
192         str = "DTLS1 write hello verify request";
193         break;
194
195     default:
196         str = "unknown state";
197         break;
198     }
199     return (str);
200 }
201
202
203 const char *SSL_state_string(const SSL *s)
204 {
205     const char *str;
206
207     if (ossl_statem_in_error(s)) {
208         return "SSLERR";
209     }
210
211     switch (SSL_get_state(s)) {
212     case TLS_ST_BEFORE:
213         str = "PINIT ";
214         break;
215     case TLS_ST_OK:
216         str = "SSLOK ";
217         break;
218
219     case TLS_ST_CW_CLNT_HELLO:
220         str = "TWCH";
221         break;
222     case TLS_ST_CR_SRVR_HELLO:
223         str = "TRSH";
224         break;
225     case TLS_ST_CR_CERT:
226         str = "TRSC";
227         break;
228     case TLS_ST_CR_KEY_EXCH:
229         str = "TRSKE";
230         break;
231     case TLS_ST_CR_CERT_REQ:
232         str = "TRCR";
233         break;
234     case TLS_ST_CR_SRVR_DONE:
235         str = "TRSD";
236         break;
237     case TLS_ST_CW_CERT:
238         str = "TWCC";
239         break;
240     case TLS_ST_CW_KEY_EXCH:
241         str = "TWCKE";
242         break;
243     case TLS_ST_CW_CERT_VRFY:
244         str = "TWCV";
245         break;
246
247     case TLS_ST_SW_CHANGE:
248     case TLS_ST_CW_CHANGE:
249         str = "TWCCS";
250         break;
251     case TLS_ST_SW_FINISHED:
252     case TLS_ST_CW_FINISHED:
253         str = "TWFIN";
254         break;
255     case TLS_ST_SR_CHANGE:
256     case TLS_ST_CR_CHANGE:
257         str = "TRCCS";
258         break;
259     case TLS_ST_SR_FINISHED:
260     case TLS_ST_CR_FINISHED:
261         str = "TRFIN";
262         break;
263
264     case TLS_ST_SW_HELLO_REQ:
265         str = "TWHR";
266         break;
267     case TLS_ST_SR_CLNT_HELLO:
268         str = "TRCH";
269         break;
270     case TLS_ST_SW_SRVR_HELLO:
271         str = "TWSH";
272         break;
273     case TLS_ST_SW_CERT:
274         str = "TWSC";
275         break;
276     case TLS_ST_SW_KEY_EXCH:
277         str = "TWSKE";
278         break;
279     case TLS_ST_SW_CERT_REQ:
280         str = "TWCR";
281         break;
282     case TLS_ST_SW_SRVR_DONE:
283         str = "TWSD";
284         break;
285     case TLS_ST_SR_CERT:
286         str = "TRCC";
287         break;
288     case TLS_ST_SR_KEY_EXCH:
289         str = "TRCKE";
290         break;
291     case TLS_ST_SR_CERT_VRFY:
292         str = "TRCV";
293         break;
294
295 /* DTLS */
296     case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
297         str = "DRCHV";
298         break;
299     case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
300         str = "DWCHV";
301         break;
302
303     default:
304         str = "UNKWN ";
305         break;
306     }
307     return (str);
308 }
309
310 const char *SSL_alert_type_string_long(int value)
311 {
312     value >>= 8;
313     if (value == SSL3_AL_WARNING)
314         return ("warning");
315     else if (value == SSL3_AL_FATAL)
316         return ("fatal");
317     else
318         return ("unknown");
319 }
320
321 const char *SSL_alert_type_string(int value)
322 {
323     value >>= 8;
324     if (value == SSL3_AL_WARNING)
325         return ("W");
326     else if (value == SSL3_AL_FATAL)
327         return ("F");
328     else
329         return ("U");
330 }
331
332 const char *SSL_alert_desc_string(int value)
333 {
334     const char *str;
335
336     switch (value & 0xff) {
337     case SSL3_AD_CLOSE_NOTIFY:
338         str = "CN";
339         break;
340     case SSL3_AD_UNEXPECTED_MESSAGE:
341         str = "UM";
342         break;
343     case SSL3_AD_BAD_RECORD_MAC:
344         str = "BM";
345         break;
346     case SSL3_AD_DECOMPRESSION_FAILURE:
347         str = "DF";
348         break;
349     case SSL3_AD_HANDSHAKE_FAILURE:
350         str = "HF";
351         break;
352     case SSL3_AD_NO_CERTIFICATE:
353         str = "NC";
354         break;
355     case SSL3_AD_BAD_CERTIFICATE:
356         str = "BC";
357         break;
358     case SSL3_AD_UNSUPPORTED_CERTIFICATE:
359         str = "UC";
360         break;
361     case SSL3_AD_CERTIFICATE_REVOKED:
362         str = "CR";
363         break;
364     case SSL3_AD_CERTIFICATE_EXPIRED:
365         str = "CE";
366         break;
367     case SSL3_AD_CERTIFICATE_UNKNOWN:
368         str = "CU";
369         break;
370     case SSL3_AD_ILLEGAL_PARAMETER:
371         str = "IP";
372         break;
373     case TLS1_AD_DECRYPTION_FAILED:
374         str = "DC";
375         break;
376     case TLS1_AD_RECORD_OVERFLOW:
377         str = "RO";
378         break;
379     case TLS1_AD_UNKNOWN_CA:
380         str = "CA";
381         break;
382     case TLS1_AD_ACCESS_DENIED:
383         str = "AD";
384         break;
385     case TLS1_AD_DECODE_ERROR:
386         str = "DE";
387         break;
388     case TLS1_AD_DECRYPT_ERROR:
389         str = "CY";
390         break;
391     case TLS1_AD_EXPORT_RESTRICTION:
392         str = "ER";
393         break;
394     case TLS1_AD_PROTOCOL_VERSION:
395         str = "PV";
396         break;
397     case TLS1_AD_INSUFFICIENT_SECURITY:
398         str = "IS";
399         break;
400     case TLS1_AD_INTERNAL_ERROR:
401         str = "IE";
402         break;
403     case TLS1_AD_USER_CANCELLED:
404         str = "US";
405         break;
406     case TLS1_AD_NO_RENEGOTIATION:
407         str = "NR";
408         break;
409     case TLS1_AD_UNSUPPORTED_EXTENSION:
410         str = "UE";
411         break;
412     case TLS1_AD_CERTIFICATE_UNOBTAINABLE:
413         str = "CO";
414         break;
415     case TLS1_AD_UNRECOGNIZED_NAME:
416         str = "UN";
417         break;
418     case TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
419         str = "BR";
420         break;
421     case TLS1_AD_BAD_CERTIFICATE_HASH_VALUE:
422         str = "BH";
423         break;
424     case TLS1_AD_UNKNOWN_PSK_IDENTITY:
425         str = "UP";
426         break;
427     default:
428         str = "UK";
429         break;
430     }
431     return (str);
432 }
433
434 const char *SSL_alert_desc_string_long(int value)
435 {
436     const char *str;
437
438     switch (value & 0xff) {
439     case SSL3_AD_CLOSE_NOTIFY:
440         str = "close notify";
441         break;
442     case SSL3_AD_UNEXPECTED_MESSAGE:
443         str = "unexpected_message";
444         break;
445     case SSL3_AD_BAD_RECORD_MAC:
446         str = "bad record mac";
447         break;
448     case SSL3_AD_DECOMPRESSION_FAILURE:
449         str = "decompression failure";
450         break;
451     case SSL3_AD_HANDSHAKE_FAILURE:
452         str = "handshake failure";
453         break;
454     case SSL3_AD_NO_CERTIFICATE:
455         str = "no certificate";
456         break;
457     case SSL3_AD_BAD_CERTIFICATE:
458         str = "bad certificate";
459         break;
460     case SSL3_AD_UNSUPPORTED_CERTIFICATE:
461         str = "unsupported certificate";
462         break;
463     case SSL3_AD_CERTIFICATE_REVOKED:
464         str = "certificate revoked";
465         break;
466     case SSL3_AD_CERTIFICATE_EXPIRED:
467         str = "certificate expired";
468         break;
469     case SSL3_AD_CERTIFICATE_UNKNOWN:
470         str = "certificate unknown";
471         break;
472     case SSL3_AD_ILLEGAL_PARAMETER:
473         str = "illegal parameter";
474         break;
475     case TLS1_AD_DECRYPTION_FAILED:
476         str = "decryption failed";
477         break;
478     case TLS1_AD_RECORD_OVERFLOW:
479         str = "record overflow";
480         break;
481     case TLS1_AD_UNKNOWN_CA:
482         str = "unknown CA";
483         break;
484     case TLS1_AD_ACCESS_DENIED:
485         str = "access denied";
486         break;
487     case TLS1_AD_DECODE_ERROR:
488         str = "decode error";
489         break;
490     case TLS1_AD_DECRYPT_ERROR:
491         str = "decrypt error";
492         break;
493     case TLS1_AD_EXPORT_RESTRICTION:
494         str = "export restriction";
495         break;
496     case TLS1_AD_PROTOCOL_VERSION:
497         str = "protocol version";
498         break;
499     case TLS1_AD_INSUFFICIENT_SECURITY:
500         str = "insufficient security";
501         break;
502     case TLS1_AD_INTERNAL_ERROR:
503         str = "internal error";
504         break;
505     case TLS1_AD_USER_CANCELLED:
506         str = "user canceled";
507         break;
508     case TLS1_AD_NO_RENEGOTIATION:
509         str = "no renegotiation";
510         break;
511     case TLS1_AD_UNSUPPORTED_EXTENSION:
512         str = "unsupported extension";
513         break;
514     case TLS1_AD_CERTIFICATE_UNOBTAINABLE:
515         str = "certificate unobtainable";
516         break;
517     case TLS1_AD_UNRECOGNIZED_NAME:
518         str = "unrecognized name";
519         break;
520     case TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
521         str = "bad certificate status response";
522         break;
523     case TLS1_AD_BAD_CERTIFICATE_HASH_VALUE:
524         str = "bad certificate hash value";
525         break;
526     case TLS1_AD_UNKNOWN_PSK_IDENTITY:
527         str = "unknown PSK identity";
528         break;
529     default:
530         str = "unknown";
531         break;
532     }
533     return (str);
534 }