Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2009-2011 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) || (len >= end-p)) { \ |
| 32 | *p = '\0'; \ |
| 33 | return -1; \ |
| 34 | } \ |
| 35 | p += len; |
| 36 | |
| 37 | PJ_DEF(pj_ssize_t) pj_ssl_cert_info_dump(const pj_ssl_cert_info *ci, |
| 38 | const char *indent, |
| 39 | char *buf, |
| 40 | pj_size_t buf_size) |
| 41 | { |
| 42 | const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
| 43 | pj_parsed_time pt1; |
| 44 | pj_parsed_time pt2; |
| 45 | unsigned i; |
| 46 | int len = 0; |
| 47 | char *p, *end; |
| 48 | |
| 49 | p = buf; |
| 50 | end = buf + buf_size; |
| 51 | |
| 52 | pj_time_decode(&ci->validity.start, &pt1); |
| 53 | pj_time_decode(&ci->validity.end, &pt2); |
| 54 | |
| 55 | /* Version */ |
| 56 | len = pj_ansi_snprintf(p, end-p, "%sVersion : v%d\n", |
| 57 | indent, ci->version); |
| 58 | CHECK_BUF_LEN(); |
| 59 | |
| 60 | /* Serial number */ |
| 61 | len = pj_ansi_snprintf(p, end-p, "%sSerial : ", indent); |
| 62 | CHECK_BUF_LEN(); |
| 63 | |
| 64 | for (i = 0; i < sizeof(ci->serial_no) && !ci->serial_no[i]; ++i); |
| 65 | for (; i < sizeof(ci->serial_no); ++i) { |
| 66 | len = pj_ansi_snprintf(p, end-p, "%02X ", ci->serial_no[i]); |
| 67 | CHECK_BUF_LEN(); |
| 68 | } |
| 69 | *(p-1) = '\n'; |
| 70 | |
| 71 | /* Subject */ |
| 72 | len = pj_ansi_snprintf( p, end-p, "%sSubject : %.*s\n", indent, |
| 73 | (int)ci->subject.cn.slen, |
| 74 | ci->subject.cn.ptr); |
| 75 | CHECK_BUF_LEN(); |
| 76 | len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", indent, |
| 77 | (int)ci->subject.info.slen, |
| 78 | ci->subject.info.ptr); |
| 79 | CHECK_BUF_LEN(); |
| 80 | |
| 81 | /* Issuer */ |
| 82 | len = pj_ansi_snprintf( p, end-p, "%sIssuer : %.*s\n", indent, |
| 83 | (int)ci->issuer.cn.slen, |
| 84 | ci->issuer.cn.ptr); |
| 85 | CHECK_BUF_LEN(); |
| 86 | len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", indent, |
| 87 | (int)ci->issuer.info.slen, |
| 88 | ci->issuer.info.ptr); |
| 89 | CHECK_BUF_LEN(); |
| 90 | |
| 91 | /* Validity period */ |
| 92 | len = pj_ansi_snprintf( p, end-p, "%sValid from : %s %4d-%02d-%02d " |
| 93 | "%02d:%02d:%02d.%03d %s\n", indent, |
| 94 | wdays[pt1.wday], pt1.year, pt1.mon+1, pt1.day, |
| 95 | pt1.hour, pt1.min, pt1.sec, pt1.msec, |
| 96 | (ci->validity.gmt? "GMT":"")); |
| 97 | CHECK_BUF_LEN(); |
| 98 | |
| 99 | len = pj_ansi_snprintf( p, end-p, "%sValid to : %s %4d-%02d-%02d " |
| 100 | "%02d:%02d:%02d.%03d %s\n", indent, |
| 101 | wdays[pt2.wday], pt2.year, pt2.mon+1, pt2.day, |
| 102 | pt2.hour, pt2.min, pt2.sec, pt2.msec, |
| 103 | (ci->validity.gmt? "GMT":"")); |
| 104 | CHECK_BUF_LEN(); |
| 105 | |
| 106 | /* Subject alternative name extension */ |
| 107 | if (ci->subj_alt_name.cnt) { |
| 108 | unsigned i; |
| 109 | |
| 110 | len = pj_ansi_snprintf(p, end-p, "%ssubjectAltName extension\n", |
| 111 | indent); |
| 112 | CHECK_BUF_LEN(); |
| 113 | |
| 114 | for (i = 0; i < ci->subj_alt_name.cnt; ++i) { |
| 115 | const char *type = NULL; |
| 116 | |
| 117 | switch(ci->subj_alt_name.entry[i].type) { |
| 118 | case PJ_SSL_CERT_NAME_RFC822: |
| 119 | type = "MAIL"; |
| 120 | break; |
| 121 | case PJ_SSL_CERT_NAME_DNS: |
| 122 | type = " DNS"; |
| 123 | break; |
| 124 | case PJ_SSL_CERT_NAME_URI: |
| 125 | type = " URI"; |
| 126 | break; |
| 127 | case PJ_SSL_CERT_NAME_IP: |
| 128 | type = " IP"; |
| 129 | break; |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | if (type) { |
| 134 | len = pj_ansi_snprintf( p, end-p, "%s %s : %.*s\n", indent, |
| 135 | type, |
| 136 | (int)ci->subj_alt_name.entry[i].name.slen, |
| 137 | ci->subj_alt_name.entry[i].name.ptr); |
| 138 | CHECK_BUF_LEN(); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return (p-buf); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | #endif /* PJ_HAS_SSL_SOCK */ |
| 148 | |