| 1 |
/* |
| 2 |
* gnome-keyring |
| 3 |
* |
| 4 |
* Copyright 2010 (C) Collabora Ltd. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "egg/egg-asn1x.h" |
| 26 |
#include "egg/egg-dn.h" |
| 27 |
#include "egg/egg-error.h" |
| 28 |
#include "egg/egg-asn1-defs.h" |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
#include "xdg-store/gkm-xdg-asn1-defs.h" |
| 33 |
|
| 34 |
static void |
| 35 |
barf_and_die (const gchar *msg, const gchar *detail) |
| 36 |
{ |
| 37 |
if (detail) |
| 38 |
g_printerr ("diddle-trust-file: %s: %s\n", msg, detail); |
| 39 |
else |
| 40 |
g_printerr ("diddle-trust-file: %s\n", msg); |
| 41 |
exit (1); |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
create_trust_file_for_certificate (const gchar *filename, const gchar *certificate) |
| 46 |
{ |
| 47 |
GError *err = NULL; |
| 48 |
GNode *asn, *cert, *choice, *ref; |
| 49 |
GBytes *bytes, *result; |
| 50 |
gchar *data; |
| 51 |
gsize n_data; |
| 52 |
|
| 53 |
if (!g_file_get_contents (certificate, &data, &n_data, &err)) |
| 54 |
barf_and_die ("couldn't read certificate file", egg_error_message (err)); |
| 55 |
|
| 56 |
/* Make sure the certificate is */ |
| 57 |
cert = egg_asn1x_create (pkix_asn1_tab, "Certificate"); |
| 58 |
g_return_if_fail (cert); |
| 59 |
|
| 60 |
bytes = g_bytes_new_take (data, n_data); |
| 61 |
if (!egg_asn1x_decode (cert, bytes)) |
| 62 |
barf_and_die ("couldn't parse der certificate file", egg_asn1x_message (cert)); |
| 63 |
|
| 64 |
asn = egg_asn1x_create (xdg_asn1_tab, "trust-1"); |
| 65 |
g_return_if_fail (asn); |
| 66 |
|
| 67 |
ref = egg_asn1x_node (asn, "reference", NULL); |
| 68 |
choice = egg_asn1x_node (ref, "certComplete", NULL); |
| 69 |
|
| 70 |
if (!egg_asn1x_set_choice (ref, choice) || !egg_asn1x_set_any_raw (choice, bytes)) |
| 71 |
g_return_if_reached (); |
| 72 |
|
| 73 |
g_bytes_unref (bytes); |
| 74 |
|
| 75 |
result = egg_asn1x_encode (asn, NULL); |
| 76 |
if (result == NULL) |
| 77 |
barf_and_die ("couldn't encode the trust file", egg_asn1x_message (asn)); |
| 78 |
|
| 79 |
egg_asn1x_destroy (asn); |
| 80 |
egg_asn1x_destroy (cert); |
| 81 |
|
| 82 |
if (!g_file_set_contents (filename, g_bytes_get_data (result, NULL), |
| 83 |
g_bytes_get_size (result), &err)) |
| 84 |
barf_and_die ("couldn't write trust file", egg_error_message (err)); |
| 85 |
|
| 86 |
g_bytes_unref (result); |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
create_trust_file_for_issuer_and_serial (const gchar *filename, const gchar *certificate) |
| 91 |
{ |
| 92 |
GError *err = NULL; |
| 93 |
GNode *asn, *cert, *choice, *ref; |
| 94 |
GNode *issuer, *serial; |
| 95 |
gchar *data; |
| 96 |
GBytes *result; |
| 97 |
GBytes *value; |
| 98 |
GBytes *element; |
| 99 |
gsize n_data; |
| 100 |
GBytes *bytes; |
| 101 |
|
| 102 |
if (!g_file_get_contents (certificate, &data, &n_data, &err)) |
| 103 |
barf_and_die ("couldn't read certificate file", egg_error_message (err)); |
| 104 |
|
| 105 |
/* Make sure the certificate is */ |
| 106 |
cert = egg_asn1x_create (pkix_asn1_tab, "Certificate"); |
| 107 |
g_return_if_fail (cert); |
| 108 |
|
| 109 |
bytes = g_bytes_new_take (data, n_data); |
| 110 |
if (!egg_asn1x_decode (cert, bytes)) |
| 111 |
barf_and_die ("couldn't parse der certificate file", egg_asn1x_message (cert)); |
| 112 |
g_bytes_unref (bytes); |
| 113 |
|
| 114 |
/* Dig out the issuer and serial */ |
| 115 |
issuer = egg_asn1x_node (cert, "tbsCertificate", "issuer", NULL); |
| 116 |
serial = egg_asn1x_node (cert, "tbsCertificate", "serialNumber", NULL); |
| 117 |
g_return_if_fail (issuer && serial); |
| 118 |
|
| 119 |
/* Create up the trust structure */ |
| 120 |
asn = egg_asn1x_create (xdg_asn1_tab, "trust-1"); |
| 121 |
g_return_if_fail (asn); |
| 122 |
|
| 123 |
/* Setup the type of trust assertion */ |
| 124 |
ref = egg_asn1x_node (asn, "reference", NULL); |
| 125 |
choice = egg_asn1x_node (ref, "certReference", NULL); |
| 126 |
if (!egg_asn1x_set_choice (ref, choice)) |
| 127 |
g_return_if_reached (); |
| 128 |
|
| 129 |
/* Copy over the serial and issuer */ |
| 130 |
element = egg_asn1x_get_element_raw (issuer); |
| 131 |
if (!egg_asn1x_set_any_raw (egg_asn1x_node (choice, "issuer", NULL), element)) |
| 132 |
g_return_if_reached (); |
| 133 |
g_bytes_unref (element); |
| 134 |
|
| 135 |
value = egg_asn1x_get_integer_as_raw (serial); |
| 136 |
egg_asn1x_set_integer_as_raw (egg_asn1x_node (choice, "serialNumber", NULL), value); |
| 137 |
g_bytes_unref (value); |
| 138 |
|
| 139 |
result = egg_asn1x_encode (asn, NULL); |
| 140 |
if (result == NULL) |
| 141 |
barf_and_die ("couldn't encode the trust file", egg_asn1x_message (asn)); |
| 142 |
|
| 143 |
g_free (data); |
| 144 |
egg_asn1x_destroy (cert); |
| 145 |
egg_asn1x_destroy (asn); |
| 146 |
|
| 147 |
if (!g_file_set_contents (filename, g_bytes_get_data (result, NULL), |
| 148 |
g_bytes_get_size (result), &err)) |
| 149 |
barf_and_die ("couldn't write trust file", egg_error_message (err)); |
| 150 |
|
| 151 |
g_bytes_unref (result); |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
add_trust_purpose_to_file (const gchar *filename, const gchar *purpose) |
| 156 |
{ |
| 157 |
GError *err = NULL; |
| 158 |
gchar *data; |
| 159 |
GBytes *result; |
| 160 |
gsize n_data; |
| 161 |
GNode *asn, *assertion; |
| 162 |
GBytes *bytes; |
| 163 |
|
| 164 |
if (!g_file_get_contents (filename, &data, &n_data, &err)) |
| 165 |
barf_and_die ("couldn't read trust file", egg_error_message (err)); |
| 166 |
|
| 167 |
/* Create up the trust structure */ |
| 168 |
asn = egg_asn1x_create (xdg_asn1_tab, "trust-1"); |
| 169 |
g_return_if_fail (asn); |
| 170 |
|
| 171 |
/* And parse it */ |
| 172 |
bytes = g_bytes_new_take (data, n_data); |
| 173 |
if (!egg_asn1x_decode (asn, bytes)) |
| 174 |
barf_and_die ("couldn't parse trust file", egg_asn1x_message (asn)); |
| 175 |
g_bytes_unref (bytes); |
| 176 |
|
| 177 |
assertion = egg_asn1x_append (egg_asn1x_node (asn, "assertions", NULL)); |
| 178 |
g_return_if_fail (assertion); |
| 179 |
|
| 180 |
if (!egg_asn1x_set_string_as_utf8 (egg_asn1x_node (assertion, "purpose", NULL), g_strdup (purpose), g_free)) |
| 181 |
g_return_if_reached (); |
| 182 |
egg_asn1x_set_enumerated (egg_asn1x_node (assertion, "level", NULL), g_quark_from_string ("trusted")); |
| 183 |
|
| 184 |
result = egg_asn1x_encode (asn, NULL); |
| 185 |
if (result == NULL) |
| 186 |
barf_and_die ("couldn't encode trust file", egg_asn1x_message (asn)); |
| 187 |
|
| 188 |
egg_asn1x_destroy (asn); |
| 189 |
|
| 190 |
if (!g_file_set_contents (filename, g_bytes_get_data (result, NULL), |
| 191 |
g_bytes_get_size (result), &err)) |
| 192 |
barf_and_die ("couldn't write trust file", egg_error_message (err)); |
| 193 |
|
| 194 |
g_bytes_unref (result); |
| 195 |
} |
| 196 |
|
| 197 |
/* -------------------------------------------------------------------------------- |
| 198 |
* MAIN |
| 199 |
*/ |
| 200 |
|
| 201 |
static gchar *create_for_file = NULL; |
| 202 |
static gchar *refer_for_file = NULL; |
| 203 |
static gchar *add_trust_purpose = NULL; |
| 204 |
|
| 205 |
static GOptionEntry option_entries[] = { |
| 206 |
{ "create", '\0', 0, G_OPTION_ARG_FILENAME, &create_for_file, |
| 207 |
"Create trust file for full certificate.", "certificate" }, |
| 208 |
{ "refer", '\0', 0, G_OPTION_ARG_FILENAME, &refer_for_file, |
| 209 |
"Create trust file for issuer+serial certificate", "certificate" }, |
| 210 |
{ "add-trust", '\0', 0, G_OPTION_ARG_STRING, &add_trust_purpose, |
| 211 |
"Add trust purpose to trust file", "purpose" }, |
| 212 |
{ NULL } |
| 213 |
}; |
| 214 |
|
| 215 |
int |
| 216 |
main(int argc, char* argv[]) |
| 217 |
{ |
| 218 |
GError *err = NULL; |
| 219 |
GOptionContext *context; |
| 220 |
|
| 221 |
context = g_option_context_new ("trust-file"); |
| 222 |
g_option_context_add_main_entries (context, option_entries, GETTEXT_PACKAGE); |
| 223 |
if (!g_option_context_parse (context, &argc, &argv, &err)) |
| 224 |
barf_and_die (egg_error_message (err), NULL); |
| 225 |
|
| 226 |
g_option_context_free (context); |
| 227 |
|
| 228 |
if (argc != 2) |
| 229 |
barf_and_die ("specify trust-file", NULL); |
| 230 |
|
| 231 |
if (((create_for_file ? 1 : 0) + |
| 232 |
(refer_for_file ? 1 : 0) + |
| 233 |
(add_trust_purpose ? 1 : 0)) > 1) |
| 234 |
barf_and_die ("incompatible options specified", NULL); |
| 235 |
|
| 236 |
if (create_for_file) |
| 237 |
create_trust_file_for_certificate (argv[1], create_for_file); |
| 238 |
else if (refer_for_file) |
| 239 |
create_trust_file_for_issuer_and_serial (argv[1], refer_for_file); |
| 240 |
else if (add_trust_purpose) |
| 241 |
add_trust_purpose_to_file (argv[1], add_trust_purpose); |
| 242 |
|
| 243 |
g_free (create_for_file); |
| 244 |
g_free (refer_for_file); |
| 245 |
g_free (add_trust_purpose); |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |