Debian Codegraph @

Xpjproject/pjlib/src/pj/ssl_sock_dump.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2009-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pj/ssl_sock.h>
19 #include <pj/errno.h>
20 #include <pj/os.h>
21 #include <pj/string.h>
22
23
24 /* Only build when PJ_HAS_SSL_SOCK is enabled */
25 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK!=0
26
27 #define THIS_FILE "ssl_sock_dump.c"
28
29 #define CHECK_BUF_LEN() \
30 if ((len < 0) || (len >= end-p)) { \
31 *p = '\0'; \
32 return -1; \
33 } \
34 p += len;
35
36 PJ_DEF(pj_ssize_t) pj_ssl_cert_info_dump(const pj_ssl_cert_info *ci,
37 const char *indent,
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 indent, ci->version);
57 CHECK_BUF_LEN();
58
59 /* Serial number */
60 len = pj_ansi_snprintf(p, end-p, "%sSerial : ", indent);
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] & 0xFF);
66 CHECK_BUF_LEN();
67 }
68 *(p-1) = '\n';
69
70 /* Subject */
71 len = pj_ansi_snprintf( p, end-p, "%sSubject : %.*s\n", indent,
72 (int)ci->subject.cn.slen,
73 ci->subject.cn.ptr);
74 CHECK_BUF_LEN();
75 len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", indent,
76 (int)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", indent,
82 (int)ci->issuer.cn.slen,
83 ci->issuer.cn.ptr);
84 CHECK_BUF_LEN();
85 len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", indent,
86 (int)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", indent,
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", indent,
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 len = pj_ansi_snprintf(p, end-p, "%ssubjectAltName extension\n",
108 indent);
109 CHECK_BUF_LEN();
110
111 for (i = 0; i < ci->subj_alt_name.cnt; ++i) {
112 const char *type = NULL;
113
114 switch(ci->subj_alt_name.entry[i].type) {
115 case PJ_SSL_CERT_NAME_RFC822:
116 type = "MAIL";
117 break;
118 case PJ_SSL_CERT_NAME_DNS:
119 type = " DNS";
120 break;
121 case PJ_SSL_CERT_NAME_URI:
122 type = " URI";
123 break;
124 case PJ_SSL_CERT_NAME_IP:
125 type = " IP";
126 break;
127 default:
128 break;
129 }
130 if (type) {
131 len = pj_ansi_snprintf( p, end-p, "%s %s : %.*s\n", indent,
132 type,
133 (int)ci->subj_alt_name.entry[i].name.slen,
134 ci->subj_alt_name.entry[i].name.ptr);
135 CHECK_BUF_LEN();
136 }
137 }
138 }
139
140 return (p-buf);
141 }
142
143
144 #endif /* PJ_HAS_SSL_SOCK */
145