summaryrefslogtreecommitdiff
path: root/examples/dump-certificates.c
blob: f0c045a0559286a796c96cf23e7a3359c1ea8938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * dump-certificates.c - Dump all Certificates from TLS Handshake
 * Copyright (C) 2011 Collabora Ltd.
 * @author Stef Walter <stefw@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include <glib.h>

#include <gio/gio.h>
#include <wocky/wocky.h>

#include <gnutls/x509.h>

static GMainLoop *mainloop;

typedef struct {
  WockyTLSHandler parent;
} DumpTLSHandler;

typedef struct {
  WockyTLSHandlerClass parent_class;
} DumpTLSHandlerClass;

GType dump_tls_handler_get_type (void);

G_DEFINE_TYPE (DumpTLSHandler, dump_tls_handler, WOCKY_TYPE_TLS_HANDLER)

static void
dump_tls_handler_init (DumpTLSHandler *self)
{

}

static void
dump_tls_handler_verify_async (WockyTLSHandler *self,
    WockyTLSSession *tls_session,
    const gchar *peername,
    GStrv extra_identities,
    GAsyncReadyCallback callback,
    gpointer user_data)
{
  GSimpleAsyncResult *res;
  GPtrArray *chain;
  gnutls_x509_crt_t cert;
  gnutls_datum_t datum;
  gchar buffer[1024 * 20];
  gsize length;
  guint i;

  chain = wocky_tls_session_get_peers_certificate (tls_session, NULL);

  for (i = 0; i < chain->len; i++)
    {
      GArray *cert_data = g_ptr_array_index (chain, i);
      datum.data = (gpointer)cert_data->data;
      datum.size = cert_data->len;

      if (gnutls_x509_crt_init (&cert) < 0)
        g_assert_not_reached ();
      if (gnutls_x509_crt_import (cert, &datum, GNUTLS_X509_FMT_DER) < 0)
        {
          g_warning ("couldn't parse certificate %u in chain", i);
          gnutls_x509_crt_deinit (cert);
          continue;
        }

      length = sizeof (buffer);
      gnutls_x509_crt_get_dn (cert, buffer, &length);
      g_print ("Subject: %.*s\n", (gint) length, buffer);

      length = sizeof (buffer);
      gnutls_x509_crt_get_issuer_dn (cert, buffer, &length);
      g_print ("Issuer: %.*s\n", (gint) length, buffer);

      length = sizeof (buffer);
      if (gnutls_x509_crt_export (cert, GNUTLS_X509_FMT_PEM, buffer, &length) < 0)
        {
          g_warning ("couldn't export certificate %u in chain", i);
          gnutls_x509_crt_deinit (cert);
          continue;
        }
      g_print ("%.*s\n", (gint) length, buffer);

      gnutls_x509_crt_deinit (cert);
    }

  g_ptr_array_unref (chain);

  res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
      dump_tls_handler_verify_async);
  g_simple_async_result_complete_in_idle (res);
  g_object_unref (res);
}

static gboolean
dump_tls_handler_verify_finish (WockyTLSHandler *self,
    GAsyncResult *result,
    GError **error)
{
  return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),
      error);
}

static void
dump_tls_handler_class_init (DumpTLSHandlerClass *klass)
{
  WockyTLSHandlerClass *handler_class = WOCKY_TLS_HANDLER_CLASS (klass);
  handler_class->verify_async_func = dump_tls_handler_verify_async;
  handler_class->verify_finish_func = dump_tls_handler_verify_finish;
}

static void
connected_cb (
    GObject *source,
    GAsyncResult *res,
    gpointer user_data)
{
  g_main_loop_quit (mainloop);
}

int
main (int argc,
    char **argv)
{
  char *jid, *password;
  WockyConnector *connector;
  WockyTLSHandler *handler;

  gchar* server = NULL;
  guint port = 5222;

  g_type_init ();
  wocky_init ();

  if (!(argc == 2 || argc == 4))
    {
      g_printerr ("Usage: %s <jid> [<server> <port>]\n", argv[0]);
      return -1;
    }

  jid = argv[1];

  if (argc == 4)
    {
      server = argv[2];
      port = atoi (argv[3]);
    }

  /* This example doesn't use your real password because it does not actually
   * validate certificates: it just dumps them then declares them valid.
   */
  password = "not a chance";

  mainloop = g_main_loop_new (NULL, FALSE);
  handler = g_object_new (dump_tls_handler_get_type (), NULL);
  connector = wocky_connector_new (jid, password, NULL, NULL, handler);
  if (argc == 4)
    g_object_set (G_OBJECT (connector), "xmpp-server", server, "xmpp-port", port, NULL);
  wocky_connector_connect_async (connector, NULL, connected_cb, NULL);

  g_main_loop_run (mainloop);

  g_object_unref (connector);
  g_main_loop_unref (mainloop);
  return 0;
}