Nanang Izzuddin | 2fb937e | 2010-02-24 05:43:34 +0000 | [diff] [blame^] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include <pj/ssl_sock.h> |
| 20 | #include <pj/errno.h> |
| 21 | #include <pj/os.h> |
| 22 | #include <pj/string.h> |
| 23 | |
| 24 | |
| 25 | /* Only build when PJ_HAS_SSL_SOCK is enabled */ |
| 26 | #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK!=0 |
| 27 | |
| 28 | #define THIS_FILE "ssl_sock_dump.c" |
| 29 | |
| 30 | #define CHECK_BUF_LEN() \ |
| 31 | if ((len < 0) || ((p+=len) >= end)) { \ |
| 32 | *(p-1) = '\0'; \ |
| 33 | return PJ_ETOOSMALL; \ |
| 34 | } |
| 35 | |
| 36 | PJ_DEF(pj_status_t) pj_ssl_cert_info_dump(const pj_ssl_cert_info *ci, |
| 37 | const char *prefix, |
| 38 | char *buf, |
| 39 | pj_size_t buf_size) |
| 40 | { |
| 41 | const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
| 42 | pj_parsed_time pt1; |
| 43 | pj_parsed_time pt2; |
| 44 | unsigned i; |
| 45 | int len = 0; |
| 46 | char *p, *end; |
| 47 | |
| 48 | p = buf; |
| 49 | end = buf + buf_size; |
| 50 | |
| 51 | pj_time_decode(&ci->validity.start, &pt1); |
| 52 | pj_time_decode(&ci->validity.end, &pt2); |
| 53 | |
| 54 | /* Version */ |
| 55 | len = pj_ansi_snprintf(p, end-p, "%sVersion : v%d\n", |
| 56 | prefix, ci->version); |
| 57 | CHECK_BUF_LEN(); |
| 58 | |
| 59 | /* Serial number */ |
| 60 | len = pj_ansi_snprintf(p, end-p, "%sSerial : ", prefix); |
| 61 | CHECK_BUF_LEN(); |
| 62 | |
| 63 | for (i = 0; i < sizeof(ci->serial_no) && !ci->serial_no[i]; ++i); |
| 64 | for (; i < sizeof(ci->serial_no); ++i) { |
| 65 | len = pj_ansi_snprintf(p, end-p, "%02X ", ci->serial_no[i]); |
| 66 | CHECK_BUF_LEN(); |
| 67 | } |
| 68 | *(p-1) = '\n'; |
| 69 | |
| 70 | /* Subject */ |
| 71 | len = pj_ansi_snprintf( p, end-p, "%sSubject : %.*s\n", prefix, |
| 72 | ci->subject.cn.slen, |
| 73 | ci->subject.cn.ptr); |
| 74 | CHECK_BUF_LEN(); |
| 75 | len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", prefix, |
| 76 | ci->subject.info.slen, |
| 77 | ci->subject.info.ptr); |
| 78 | CHECK_BUF_LEN(); |
| 79 | |
| 80 | /* Issuer */ |
| 81 | len = pj_ansi_snprintf( p, end-p, "%sIssuer : %.*s\n", prefix, |
| 82 | ci->issuer.cn.slen, |
| 83 | ci->issuer.cn.ptr); |
| 84 | CHECK_BUF_LEN(); |
| 85 | len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", prefix, |
| 86 | ci->issuer.info.slen, |
| 87 | ci->issuer.info.ptr); |
| 88 | CHECK_BUF_LEN(); |
| 89 | |
| 90 | /* Validity period */ |
| 91 | len = pj_ansi_snprintf( p, end-p, "%sValid from : %s %4d-%02d-%02d " |
| 92 | "%02d:%02d:%02d.%03d %s\n", prefix, |
| 93 | wdays[pt1.wday], pt1.year, pt1.mon+1, pt1.day, |
| 94 | pt1.hour, pt1.min, pt1.sec, pt1.msec, |
| 95 | (ci->validity.gmt? "GMT":"")); |
| 96 | CHECK_BUF_LEN(); |
| 97 | |
| 98 | len = pj_ansi_snprintf( p, end-p, "%sValid to : %s %4d-%02d-%02d " |
| 99 | "%02d:%02d:%02d.%03d %s\n", prefix, |
| 100 | wdays[pt2.wday], pt2.year, pt2.mon+1, pt2.day, |
| 101 | pt2.hour, pt2.min, pt2.sec, pt2.msec, |
| 102 | (ci->validity.gmt? "GMT":"")); |
| 103 | CHECK_BUF_LEN(); |
| 104 | |
| 105 | /* Subject alternative name extension */ |
| 106 | if (ci->subj_alt_name.cnt) { |
| 107 | unsigned i; |
| 108 | |
| 109 | len = pj_ansi_snprintf(p, end-p, "%ssubjectAltName extension\n", |
| 110 | prefix); |
| 111 | CHECK_BUF_LEN(); |
| 112 | |
| 113 | for (i = 0; i < ci->subj_alt_name.cnt; ++i) { |
| 114 | const char *type = NULL; |
| 115 | |
| 116 | switch(ci->subj_alt_name.entry[i].type) { |
| 117 | case PJ_SSL_CERT_NAME_RFC822: |
| 118 | type = "MAIL"; |
| 119 | break; |
| 120 | case PJ_SSL_CERT_NAME_DNS: |
| 121 | type = " DNS"; |
| 122 | break; |
| 123 | case PJ_SSL_CERT_NAME_URI: |
| 124 | type = " URI"; |
| 125 | break; |
| 126 | case PJ_SSL_CERT_NAME_IP: |
| 127 | type = " IP"; |
| 128 | break; |
| 129 | default: |
| 130 | break; |
| 131 | } |
| 132 | if (type) { |
| 133 | len = pj_ansi_snprintf( p, end-p, "%s %s : %.*s\n", prefix, |
| 134 | type, |
| 135 | ci->subj_alt_name.entry[i].name.slen, |
| 136 | ci->subj_alt_name.entry[i].name.ptr); |
| 137 | CHECK_BUF_LEN(); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return PJ_SUCCESS; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | #endif /* PJ_HAS_SSL_SOCK */ |
| 147 | |