summaryrefslogtreecommitdiff
path: root/tool/gkr-tool-trust.c
blob: a407d332729b1f54675e7422bddf4e91c28dc5c2 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gkr-tool-trust.c: Command line pinned certificates

   Copyright (C) 2010 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "gkr-tool.h"

#include "gck/gck.h"

#include "gcr/gcr.h"

#include "egg/egg-hex.h"

#if 0
static gchar **trust_files = NULL;
#endif

#if 0
struct {
	const gchar *name;
	const gchar *purpose;
} purpose_names[] = {
	{ "server", GCR_PURPOSE_SERVER_AUTH },
	{ "client", GCR_PURPOSE_CLIENT_AUTH },
	{ "code", GCR_PURPOSE_CODE_SIGNING },
	{ "email", GCR_PURPOSE_EMAIL },
};

enum {
	MODE_SHOW = 0,
	MODE_ADD = 1,
	MODE_REMOVE = 2
};

static gboolean
purpose_for_string (const gchar *string, GcrPurpose *purpose)
{
	guint i;

	g_assert (string);
	g_assert (purpose);

	for (i = 0; i < G_N_ELEMENTS (purpose_names); ++i) {
		if (g_str_equal (purpose_names[i].name, string)) {
			*purpose = purpose_names[i].purpose;
			return TRUE;
		}
	}

	return FALSE;
}

static gboolean
trust_for_string (const gchar *string, GcrTrust *trust)
{
	guint i;

	g_assert (string);
	g_assert (trust);

	for (i = 0; i < G_N_ELEMENTS (trust_names); ++i) {
		if (g_str_equal (trust_names[i].name, string)) {
			*trust = trust_names[i].trust;
			return TRUE;
		}
	}

	return FALSE;
}

static const gchar*
purpose_to_string (GcrPurpose purpose)
{
	guint i;

	for (i = 0; i < G_N_ELEMENTS (purpose_names); ++i) {
		if (purpose == purpose_names[i].purpose)
			return purpose_names[i].name;
	}

	return NULL;
}

static int
get_pinned_certificate (GcrCertificate *certificate, GcrPurpose purpose)
{
	GError *error = NULL;
	const gchar *string;
	GcrTrust trust;

	trust = gcr_trust_get_certificate_exception (certificate, purpose, NULL, &error);
	if (error != NULL) {
		gkr_tool_handle_error (&error, "retrieving trust exception failed");
		return 1;
	}

	string = purpose_to_string (purpose);
	if (trust == GCR_TRUST_UNKNOWN && !gkr_tool_mode_quiet)
		g_print ("%s: no trust exception\n", string);
	else if (trust == GCR_TRUST_TRUSTED)
		g_print ("%s: certificate is explicitly trusted\n", string);
	else if (trust == GCR_TRUST_DISTRUSTED)
		g_print ("%s: certificate is explicitly distrusted\n", string);

	return 0;
}

static int
set_certificate_exception (GcrCertificate *certificate, GcrPurpose purpose, GcrTrust trust)
{
	GError *error = NULL;
	const gchar *string;

	if (!gcr_trust_set_certificate_exception (certificate, purpose, trust, NULL, &error)) {
		gkr_tool_handle_error (&error, "setting trust exception failed");
		return 1;
	}

	if (!gkr_tool_mode_quiet) {
		string = purpose_to_string (purpose);
		if (trust == GCR_TRUST_UNKNOWN)
			g_print ("%s: no trust exception\n", string);
		else if (trust == GCR_TRUST_TRUSTED)
			g_print ("%s: certificate is explicitly trusted\n", string);
		else if (trust == GCR_TRUST_DISTRUSTED)
			g_print ("%s: certificate is explicitly distrusted\n", string);
	}

	return 0;
}

static gchar *add_trust = NULL;
static gchar *remove_trust = NULL;

static GOptionEntry trust_entries[] = {
	GKR_TOOL_BASIC_OPTIONS
	{ "add", 0, 0, G_OPTION_ARG_STRING, &set_trust, "Add certificate trust exception", "purpose" },
	{ "remove", 0, 0, G_OPTION_ARG_STRING, &set_trust, "Remove certificate trust exception", "purpose" },
	{ NULL }
};
#endif

int
gkr_tool_trust (int argc, char *argv[])
{
	g_assert_not_reached ();
	return 1;
#if 0
	GcrCertificate *certificate = NULL;
	const gchar *purpose;
	GError *error = NULL;
	GArray *purposes = NULL;
	GFile *file = NULL;
	gchar *contents;
	gsize length;
	TrustMode mode;
	int ret = 2;
	guint i;

	ret = gkr_tool_parse_options (&argc, &argv, trust_entries);
	if (ret != 0)
		return ret;

	if (argc < 3) {
		gkr_tool_handle_error (NULL, "specify certificate file followed by one or more purposes");
		goto done;
	}

	purposes = g_array_new (FALSE, TRUE, sizeof (GcrPurpose));
	for (i = 2; i < argc; ++i) {
		if (!purpose_for_string (argv[i], &purpose)) {
			gkr_tool_handle_error (NULL, "invalid purpose: %s", argv[i]);
			goto done;
		}
		g_array_append_val (purposes, purpose);
	}

	if (set_trust) {
		if (!trust_for_string (set_trust, &trust)) {
			gkr_tool_handle_error (NULL, "invalid trust string: %s", set_trust);
			goto done;
		}
	}

	ret = 1;

	file = g_file_new_for_commandline_arg (argv[1]);
	if (!g_file_load_contents (file, NULL, &contents, &length, NULL, &error)) {
		gkr_tool_handle_error (&error, "couldn't read file: %s", argv[1]);
		goto done;
	}

	certificate = gcr_simple_certificate_new (contents, length);
	g_free (contents);

	for (i = 0; i < purposes->len; ++i) {
		purpose = g_array_index (purposes, GcrPurpose, i);
		if (set_trust)
			ret = add_certificate_exception (certificate, purpose, trust);
		else
			ret = is_certificate_exception (certificate, purpose);
		if (ret != 0)
			break;
	}

done:
	if (file != NULL)
		g_object_unref (file);
	if (purposes != NULL)
		g_array_free (purposes, TRUE);
	if (certificate != NULL)
		g_object_unref (certificate);
	g_free (set_trust);
	return ret;
#endif
}