| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* gkm-data-der.c - parsing and serializing of common crypto DER structures |
| 3 |
|
| 4 |
Copyright (C) 2007 Stefan Walter |
| 5 |
|
| 6 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "gkm-data-asn1.h" |
| 26 |
#include "gkm-data-der.h" |
| 27 |
#include "gkm-data-types.h" |
| 28 |
#include "gkm-sexp.h" |
| 29 |
|
| 30 |
#include "egg/egg-asn1x.h" |
| 31 |
#include "egg/egg-asn1-defs.h" |
| 32 |
#include "egg/egg-secure-memory.h" |
| 33 |
#include "egg/egg-symkey.h" |
| 34 |
|
| 35 |
#include <glib.h> |
| 36 |
#include <gcrypt.h> |
| 37 |
|
| 38 |
EGG_SECURE_DECLARE (data_der); |
| 39 |
|
| 40 |
/* ----------------------------------------------------------------------------- |
| 41 |
* QUARKS |
| 42 |
*/ |
| 43 |
|
| 44 |
static GQuark OID_PKIX1_RSA; |
| 45 |
static GQuark OID_PKIX1_DSA; |
| 46 |
static GQuark OID_PKIX1_ECDSA; |
| 47 |
static GQuark OID_PKCS12_PBE_3DES_SHA1; |
| 48 |
static GQuark OID_ANSI_SECP256R1; |
| 49 |
static GQuark OID_ANSI_SECP384R1; |
| 50 |
static GQuark OID_ANSI_SECP521R1; |
| 51 |
|
| 52 |
static void |
| 53 |
init_quarks (void) |
| 54 |
{ |
| 55 |
static gsize quarks_inited = 0; |
| 56 |
|
| 57 |
if (g_once_init_enter (&quarks_inited)) { |
| 58 |
|
| 59 |
#define QUARK(name, value) \ |
| 60 |
name = g_quark_from_static_string(value) |
| 61 |
|
| 62 |
QUARK (OID_PKIX1_RSA, "1.2.840.113549.1.1.1"); |
| 63 |
QUARK (OID_PKIX1_DSA, "1.2.840.10040.4.1"); |
| 64 |
QUARK (OID_PKIX1_ECDSA, "1.2.840.10045.2.1"); |
| 65 |
QUARK (OID_PKCS12_PBE_3DES_SHA1, "1.2.840.113549.1.12.1.3"); |
| 66 |
QUARK (OID_ANSI_SECP256R1, "1.2.840.10045.3.1.7"); |
| 67 |
QUARK (OID_ANSI_SECP384R1, "1.3.132.0.34"); |
| 68 |
QUARK (OID_ANSI_SECP521R1, "1.3.132.0.35"); |
| 69 |
|
| 70 |
#undef QUARK |
| 71 |
|
| 72 |
g_once_init_leave (&quarks_inited, 1); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
const gchar * |
| 77 |
gkm_data_der_oid_to_curve (GQuark oid) |
| 78 |
{ |
| 79 |
if (oid == OID_ANSI_SECP256R1) |
| 80 |
return "NIST P-256"; |
| 81 |
else if (oid == OID_ANSI_SECP384R1) |
| 82 |
return "NIST P-384"; |
| 83 |
else if (oid == OID_ANSI_SECP521R1) |
| 84 |
return "NIST P-521"; |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Convert ecc->curve values from libgcrypt representation to internal one. |
| 90 |
* Ignore duplicates and alternative names, since S-expressions are created |
| 91 |
* only by us throught this code. |
| 92 |
*/ |
| 93 |
static GQuark |
| 94 |
gkm_data_der_curve_to_oid (const gchar *curve) |
| 95 |
{ |
| 96 |
if (g_str_equal (curve, "NIST P-256")) |
| 97 |
return OID_ANSI_SECP256R1; |
| 98 |
else if (g_str_equal (curve, "NIST P-384")) |
| 99 |
return OID_ANSI_SECP384R1; |
| 100 |
else if (g_str_equal (curve, "NIST P-521")) |
| 101 |
return OID_ANSI_SECP521R1; |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
GQuark |
| 107 |
gkm_data_der_oid_from_ec_params (GBytes *params) |
| 108 |
{ |
| 109 |
GNode *asn; |
| 110 |
GQuark oid; |
| 111 |
|
| 112 |
init_quarks (); |
| 113 |
|
| 114 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "Parameters", params); |
| 115 |
if (!asn) |
| 116 |
return 0; |
| 117 |
|
| 118 |
oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "namedCurve", NULL)); |
| 119 |
|
| 120 |
egg_asn1x_destroy (asn); |
| 121 |
return oid; |
| 122 |
} |
| 123 |
|
| 124 |
GBytes * |
| 125 |
gkm_data_der_get_ec_params (GQuark oid) |
| 126 |
{ |
| 127 |
GNode *asn; |
| 128 |
GBytes *params = NULL; |
| 129 |
GNode *named_curve; |
| 130 |
|
| 131 |
asn = egg_asn1x_create (pk_asn1_tab, "Parameters"); |
| 132 |
if (!asn) |
| 133 |
goto done; |
| 134 |
|
| 135 |
named_curve = egg_asn1x_node (asn, "namedCurve", NULL); |
| 136 |
|
| 137 |
if (!egg_asn1x_set_oid_as_quark (named_curve, oid)) |
| 138 |
goto done; |
| 139 |
|
| 140 |
if (!egg_asn1x_set_choice (asn, named_curve)) |
| 141 |
goto done; |
| 142 |
|
| 143 |
params = egg_asn1x_encode (asn, NULL); |
| 144 |
|
| 145 |
done: |
| 146 |
egg_asn1x_destroy (asn); |
| 147 |
return params; |
| 148 |
} |
| 149 |
|
| 150 |
/* wrapper so we do not have to export the GQuark magic */ |
| 151 |
GBytes * |
| 152 |
gkm_data_der_curve_to_ec_params (const gchar *curve_name) |
| 153 |
{ |
| 154 |
GQuark oid; |
| 155 |
|
| 156 |
init_quarks (); |
| 157 |
|
| 158 |
oid = gkm_data_der_curve_to_oid (curve_name); |
| 159 |
if (oid == 0) |
| 160 |
return NULL; |
| 161 |
|
| 162 |
return gkm_data_der_get_ec_params (oid); |
| 163 |
} |
| 164 |
|
| 165 |
GBytes * |
| 166 |
gkm_data_der_encode_ecdsa_q_str (const guchar *data, gsize data_len) |
| 167 |
{ |
| 168 |
GNode *asn = NULL; |
| 169 |
g_autoptr(GBytes) bytes = NULL; |
| 170 |
GBytes *result = NULL; |
| 171 |
|
| 172 |
asn = egg_asn1x_create (pk_asn1_tab, "ECKeyQ"); |
| 173 |
g_return_val_if_fail (asn, FALSE); |
| 174 |
|
| 175 |
bytes = g_bytes_new_static (data, data_len); |
| 176 |
if (!gkm_data_asn1_write_string (asn, bytes)) |
| 177 |
goto done; |
| 178 |
|
| 179 |
result = egg_asn1x_encode (asn, g_realloc); |
| 180 |
if (result == NULL) |
| 181 |
g_warning ("couldn't encode Q into the PKCS#11 structure: %s", egg_asn1x_message (asn)); |
| 182 |
done: |
| 183 |
egg_asn1x_destroy (asn); |
| 184 |
return result; |
| 185 |
} |
| 186 |
|
| 187 |
gboolean |
| 188 |
gkm_data_der_encode_ecdsa_q (gcry_mpi_t q, GBytes **result) |
| 189 |
{ |
| 190 |
gcry_error_t gcry; |
| 191 |
guchar data[1024]; |
| 192 |
gsize data_len = 1024; |
| 193 |
gboolean rv = TRUE; |
| 194 |
|
| 195 |
g_assert (q); |
| 196 |
g_assert (result); |
| 197 |
|
| 198 |
gcry = gcry_mpi_print (GCRYMPI_FMT_USG, data, data_len, &data_len, q); |
| 199 |
g_return_val_if_fail (gcry == 0, FALSE); |
| 200 |
|
| 201 |
*result = gkm_data_der_encode_ecdsa_q_str (data, data_len); |
| 202 |
if (*result == NULL) |
| 203 |
rv = FALSE; |
| 204 |
|
| 205 |
return rv; |
| 206 |
} |
| 207 |
|
| 208 |
gboolean |
| 209 |
gkm_data_der_decode_ecdsa_q (GBytes *data, GBytes **result) |
| 210 |
{ |
| 211 |
GNode *asn = NULL; |
| 212 |
gboolean rv = TRUE; |
| 213 |
|
| 214 |
g_assert (data); |
| 215 |
g_assert (result); |
| 216 |
|
| 217 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "ECKeyQ", data); |
| 218 |
/* workaround a bug in gcr (not DER encoding the MPI) */ |
| 219 |
if (!asn) { |
| 220 |
*result = data; |
| 221 |
return rv; |
| 222 |
} |
| 223 |
|
| 224 |
rv = gkm_data_asn1_read_string (asn, result); |
| 225 |
|
| 226 |
egg_asn1x_destroy (asn); |
| 227 |
return rv; |
| 228 |
} |
| 229 |
|
| 230 |
/* ----------------------------------------------------------------------------- |
| 231 |
* KEY PARSING |
| 232 |
*/ |
| 233 |
|
| 234 |
#define SEXP_PUBLIC_RSA \ |
| 235 |
"(public-key" \ |
| 236 |
" (rsa" \ |
| 237 |
" (n %m)" \ |
| 238 |
" (e %m)))" |
| 239 |
|
| 240 |
GkmDataResult |
| 241 |
gkm_data_der_read_public_key_rsa (GBytes *data, |
| 242 |
gcry_sexp_t *s_key) |
| 243 |
{ |
| 244 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 245 |
GNode *asn = NULL; |
| 246 |
gcry_mpi_t n, e; |
| 247 |
int res; |
| 248 |
|
| 249 |
n = e = NULL; |
| 250 |
|
| 251 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "RSAPublicKey", data); |
| 252 |
if (!asn) |
| 253 |
goto done; |
| 254 |
|
| 255 |
ret = GKM_DATA_FAILURE; |
| 256 |
|
| 257 |
if (!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "modulus", NULL), &n) || |
| 258 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "publicExponent", NULL), &e)) |
| 259 |
goto done; |
| 260 |
|
| 261 |
res = gcry_sexp_build (s_key, NULL, SEXP_PUBLIC_RSA, n, e); |
| 262 |
if (res) |
| 263 |
goto done; |
| 264 |
|
| 265 |
g_assert (*s_key); |
| 266 |
ret = GKM_DATA_SUCCESS; |
| 267 |
|
| 268 |
done: |
| 269 |
egg_asn1x_destroy (asn); |
| 270 |
gcry_mpi_release (n); |
| 271 |
gcry_mpi_release (e); |
| 272 |
|
| 273 |
if (ret == GKM_DATA_FAILURE) |
| 274 |
g_message ("invalid RSA public key"); |
| 275 |
|
| 276 |
return ret; |
| 277 |
} |
| 278 |
|
| 279 |
#define SEXP_PRIVATE_RSA \ |
| 280 |
"(private-key" \ |
| 281 |
" (rsa" \ |
| 282 |
" (n %m)" \ |
| 283 |
" (e %m)" \ |
| 284 |
" (d %m)" \ |
| 285 |
" (p %m)" \ |
| 286 |
" (q %m)" \ |
| 287 |
" (u %m)))" |
| 288 |
|
| 289 |
GkmDataResult |
| 290 |
gkm_data_der_read_private_key_rsa (GBytes *data, |
| 291 |
gcry_sexp_t *s_key) |
| 292 |
{ |
| 293 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 294 |
gcry_mpi_t n, e, d, p, q, u; |
| 295 |
gcry_mpi_t tmp; |
| 296 |
gulong version; |
| 297 |
GNode *asn = NULL; |
| 298 |
int res; |
| 299 |
|
| 300 |
n = e = d = p = q = u = NULL; |
| 301 |
|
| 302 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "RSAPrivateKey", data); |
| 303 |
if (!asn) |
| 304 |
goto done; |
| 305 |
|
| 306 |
ret = GKM_DATA_FAILURE; |
| 307 |
|
| 308 |
if (!egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), &version)) |
| 309 |
goto done; |
| 310 |
|
| 311 |
/* We only support simple version */ |
| 312 |
if (version != 0) { |
| 313 |
ret = GKM_DATA_UNRECOGNIZED; |
| 314 |
g_message ("unsupported version of RSA key: %lu", version); |
| 315 |
goto done; |
| 316 |
} |
| 317 |
|
| 318 |
if (!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "modulus", NULL), &n) || |
| 319 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "publicExponent", NULL), &e) || |
| 320 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "privateExponent", NULL), &d) || |
| 321 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "prime1", NULL), &p) || |
| 322 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "prime2", NULL), &q) || |
| 323 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "coefficient", NULL), &u)) |
| 324 |
goto done; |
| 325 |
|
| 326 |
/* Fix up the incoming key so gcrypt likes it */ |
| 327 |
if (gcry_mpi_cmp (p, q) > 0) { |
| 328 |
/* P shall be smaller then Q! Swap primes. iqmp becomes u. */ |
| 329 |
tmp = p; |
| 330 |
p = q; |
| 331 |
q = tmp; |
| 332 |
} else { |
| 333 |
/* U needs to be recomputed. */ |
| 334 |
gcry_mpi_invm (u, p, q); |
| 335 |
} |
| 336 |
|
| 337 |
res = gcry_sexp_build (s_key, NULL, SEXP_PRIVATE_RSA, n, e, d, p, q, u); |
| 338 |
if (res) |
| 339 |
goto done; |
| 340 |
|
| 341 |
g_assert (*s_key); |
| 342 |
ret = GKM_DATA_SUCCESS; |
| 343 |
|
| 344 |
done: |
| 345 |
egg_asn1x_destroy (asn); |
| 346 |
gcry_mpi_release (n); |
| 347 |
gcry_mpi_release (e); |
| 348 |
gcry_mpi_release (d); |
| 349 |
gcry_mpi_release (p); |
| 350 |
gcry_mpi_release (q); |
| 351 |
gcry_mpi_release (u); |
| 352 |
|
| 353 |
if (ret == GKM_DATA_FAILURE) |
| 354 |
g_message ("invalid RSA key"); |
| 355 |
|
| 356 |
return ret; |
| 357 |
} |
| 358 |
|
| 359 |
#define SEXP_PUBLIC_DSA \ |
| 360 |
"(public-key" \ |
| 361 |
" (dsa" \ |
| 362 |
" (p %m)" \ |
| 363 |
" (q %m)" \ |
| 364 |
" (g %m)" \ |
| 365 |
" (y %m)))" |
| 366 |
|
| 367 |
GkmDataResult |
| 368 |
gkm_data_der_read_public_key_dsa (GBytes *data, |
| 369 |
gcry_sexp_t *s_key) |
| 370 |
{ |
| 371 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 372 |
GNode *asn = NULL; |
| 373 |
gcry_mpi_t p, q, g, y; |
| 374 |
int res; |
| 375 |
|
| 376 |
p = q = g = y = NULL; |
| 377 |
|
| 378 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "DSAPublicKey", data); |
| 379 |
if (!asn) |
| 380 |
goto done; |
| 381 |
|
| 382 |
ret = GKM_DATA_FAILURE; |
| 383 |
|
| 384 |
if (!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "p", NULL), &p) || |
| 385 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "q", NULL), &q) || |
| 386 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "g", NULL), &g) || |
| 387 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "Y", NULL), &y)) |
| 388 |
goto done; |
| 389 |
|
| 390 |
res = gcry_sexp_build (s_key, NULL, SEXP_PUBLIC_DSA, p, q, g, y); |
| 391 |
if (res) |
| 392 |
goto done; |
| 393 |
|
| 394 |
g_assert (*s_key); |
| 395 |
ret = GKM_DATA_SUCCESS; |
| 396 |
|
| 397 |
done: |
| 398 |
egg_asn1x_destroy (asn); |
| 399 |
gcry_mpi_release (p); |
| 400 |
gcry_mpi_release (q); |
| 401 |
gcry_mpi_release (g); |
| 402 |
gcry_mpi_release (y); |
| 403 |
|
| 404 |
if (ret == GKM_DATA_FAILURE) |
| 405 |
g_message ("invalid public DSA key"); |
| 406 |
|
| 407 |
return ret; |
| 408 |
} |
| 409 |
|
| 410 |
GkmDataResult |
| 411 |
gkm_data_der_read_public_key_dsa_parts (GBytes *keydata, |
| 412 |
GBytes *params, |
| 413 |
gcry_sexp_t *s_key) |
| 414 |
{ |
| 415 |
gcry_mpi_t p, q, g, y; |
| 416 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 417 |
GNode *asn_params = NULL; |
| 418 |
GNode *asn_key = NULL; |
| 419 |
int res; |
| 420 |
|
| 421 |
p = q = g = y = NULL; |
| 422 |
|
| 423 |
asn_params = egg_asn1x_create_and_decode (pk_asn1_tab, "DSAParameters", params); |
| 424 |
asn_key = egg_asn1x_create_and_decode (pk_asn1_tab, "DSAPublicPart", keydata); |
| 425 |
if (!asn_params || !asn_key) |
| 426 |
goto done; |
| 427 |
|
| 428 |
ret = GKM_DATA_FAILURE; |
| 429 |
|
| 430 |
if (!gkm_data_asn1_read_mpi (egg_asn1x_node (asn_params, "p", NULL), &p) || |
| 431 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn_params, "q", NULL), &q) || |
| 432 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn_params, "g", NULL), &g)) |
| 433 |
goto done; |
| 434 |
|
| 435 |
if (!gkm_data_asn1_read_mpi (asn_key, &y)) |
| 436 |
goto done; |
| 437 |
|
| 438 |
res = gcry_sexp_build (s_key, NULL, SEXP_PUBLIC_DSA, p, q, g, y); |
| 439 |
if (res) |
| 440 |
goto done; |
| 441 |
|
| 442 |
g_assert (*s_key); |
| 443 |
ret = GKM_DATA_SUCCESS; |
| 444 |
|
| 445 |
done: |
| 446 |
egg_asn1x_destroy (asn_key); |
| 447 |
egg_asn1x_destroy (asn_params); |
| 448 |
gcry_mpi_release (p); |
| 449 |
gcry_mpi_release (q); |
| 450 |
gcry_mpi_release (g); |
| 451 |
gcry_mpi_release (y); |
| 452 |
|
| 453 |
if (ret == GKM_DATA_FAILURE) |
| 454 |
g_message ("invalid DSA key"); |
| 455 |
|
| 456 |
return ret; |
| 457 |
} |
| 458 |
|
| 459 |
#define SEXP_PRIVATE_DSA \ |
| 460 |
"(private-key" \ |
| 461 |
" (dsa" \ |
| 462 |
" (p %m)" \ |
| 463 |
" (q %m)" \ |
| 464 |
" (g %m)" \ |
| 465 |
" (y %m)" \ |
| 466 |
" (x %m)))" |
| 467 |
|
| 468 |
GkmDataResult |
| 469 |
gkm_data_der_read_private_key_dsa (GBytes *data, |
| 470 |
gcry_sexp_t *s_key) |
| 471 |
{ |
| 472 |
gcry_mpi_t p, q, g, y, x; |
| 473 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 474 |
int res; |
| 475 |
GNode *asn = NULL; |
| 476 |
|
| 477 |
p = q = g = y = x = NULL; |
| 478 |
|
| 479 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "DSAPrivateKey", data); |
| 480 |
if (!asn) |
| 481 |
goto done; |
| 482 |
|
| 483 |
ret = GKM_DATA_FAILURE; |
| 484 |
|
| 485 |
if (!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "p", NULL), &p) || |
| 486 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "q", NULL), &q) || |
| 487 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "g", NULL), &g) || |
| 488 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "Y", NULL), &y) || |
| 489 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "priv", NULL), &x)) |
| 490 |
goto done; |
| 491 |
|
| 492 |
res = gcry_sexp_build (s_key, NULL, SEXP_PRIVATE_DSA, p, q, g, y, x); |
| 493 |
if (res) |
| 494 |
goto done; |
| 495 |
|
| 496 |
g_assert (*s_key); |
| 497 |
ret = GKM_DATA_SUCCESS; |
| 498 |
|
| 499 |
done: |
| 500 |
egg_asn1x_destroy (asn); |
| 501 |
gcry_mpi_release (p); |
| 502 |
gcry_mpi_release (q); |
| 503 |
gcry_mpi_release (g); |
| 504 |
gcry_mpi_release (y); |
| 505 |
gcry_mpi_release (x); |
| 506 |
|
| 507 |
if (ret == GKM_DATA_FAILURE) |
| 508 |
g_message ("invalid DSA key"); |
| 509 |
|
| 510 |
return ret; |
| 511 |
} |
| 512 |
|
| 513 |
GkmDataResult |
| 514 |
gkm_data_der_read_private_key_dsa_parts (GBytes *keydata, |
| 515 |
GBytes *params, |
| 516 |
gcry_sexp_t *s_key) |
| 517 |
{ |
| 518 |
gcry_mpi_t p, q, g, y, x; |
| 519 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 520 |
int res; |
| 521 |
GNode *asn_params = NULL; |
| 522 |
GNode *asn_key = NULL; |
| 523 |
|
| 524 |
p = q = g = y = x = NULL; |
| 525 |
|
| 526 |
asn_params = egg_asn1x_create_and_decode (pk_asn1_tab, "DSAParameters", params); |
| 527 |
asn_key = egg_asn1x_create_and_decode (pk_asn1_tab, "DSAPrivatePart", keydata); |
| 528 |
if (!asn_params || !asn_key) |
| 529 |
goto done; |
| 530 |
|
| 531 |
ret = GKM_DATA_FAILURE; |
| 532 |
|
| 533 |
if (!gkm_data_asn1_read_mpi (egg_asn1x_node (asn_params, "p", NULL), &p) || |
| 534 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn_params, "q", NULL), &q) || |
| 535 |
!gkm_data_asn1_read_mpi (egg_asn1x_node (asn_params, "g", NULL), &g)) |
| 536 |
goto done; |
| 537 |
|
| 538 |
if (!gkm_data_asn1_read_mpi (asn_key, &x)) |
| 539 |
goto done; |
| 540 |
|
| 541 |
/* Now we calculate y */ |
| 542 |
y = gcry_mpi_snew (1024); |
| 543 |
gcry_mpi_powm (y, g, x, p); |
| 544 |
|
| 545 |
res = gcry_sexp_build (s_key, NULL, SEXP_PRIVATE_DSA, p, q, g, y, x); |
| 546 |
if (res) |
| 547 |
goto done; |
| 548 |
|
| 549 |
g_assert (*s_key); |
| 550 |
ret = GKM_DATA_SUCCESS; |
| 551 |
|
| 552 |
done: |
| 553 |
egg_asn1x_destroy (asn_key); |
| 554 |
egg_asn1x_destroy (asn_params); |
| 555 |
gcry_mpi_release (p); |
| 556 |
gcry_mpi_release (q); |
| 557 |
gcry_mpi_release (g); |
| 558 |
gcry_mpi_release (y); |
| 559 |
gcry_mpi_release (x); |
| 560 |
|
| 561 |
if (ret == GKM_DATA_FAILURE) |
| 562 |
g_message ("invalid DSA key"); |
| 563 |
|
| 564 |
return ret; |
| 565 |
} |
| 566 |
|
| 567 |
#define SEXP_PUBLIC_ECDSA \ |
| 568 |
"(public-key" \ |
| 569 |
" (ecdsa" \ |
| 570 |
" (curve %s)" \ |
| 571 |
" (q %b)))" |
| 572 |
|
| 573 |
GkmDataResult |
| 574 |
gkm_data_der_read_public_key_ecdsa (GBytes *data, |
| 575 |
gcry_sexp_t *s_key) |
| 576 |
{ |
| 577 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 578 |
int res; |
| 579 |
GNode *asn = NULL; |
| 580 |
GBytes *q = NULL; |
| 581 |
gsize q_bits; |
| 582 |
GQuark oid; |
| 583 |
const gchar *curve = NULL; |
| 584 |
|
| 585 |
init_quarks (); |
| 586 |
|
| 587 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "ECPublicKey", data); |
| 588 |
if (!asn) |
| 589 |
goto done; |
| 590 |
|
| 591 |
ret = GKM_DATA_FAILURE; |
| 592 |
|
| 593 |
if (!gkm_data_asn1_read_oid (egg_asn1x_node (asn, "parameters", "namedCurve", NULL), &oid) || |
| 594 |
!gkm_data_asn1_read_bit_string (egg_asn1x_node (asn, "q", NULL), &q, &q_bits)) |
| 595 |
goto done; |
| 596 |
|
| 597 |
curve = gkm_data_der_oid_to_curve (oid); |
| 598 |
if (curve == NULL) |
| 599 |
goto done; |
| 600 |
|
| 601 |
res = gcry_sexp_build (s_key, NULL, SEXP_PUBLIC_ECDSA, |
| 602 |
curve, |
| 603 |
g_bytes_get_size(q), g_bytes_get_data(q, NULL)); |
| 604 |
if (res) |
| 605 |
goto done; |
| 606 |
|
| 607 |
g_assert (*s_key); |
| 608 |
ret = GKM_DATA_SUCCESS; |
| 609 |
|
| 610 |
done: |
| 611 |
egg_asn1x_destroy (asn); |
| 612 |
g_bytes_unref (q); |
| 613 |
|
| 614 |
if (ret == GKM_DATA_FAILURE) |
| 615 |
g_message ("invalid ECDSA key"); |
| 616 |
|
| 617 |
return ret; |
| 618 |
} |
| 619 |
|
| 620 |
#define SEXP_PRIVATE_ECDSA \ |
| 621 |
"(private-key" \ |
| 622 |
" (ecdsa" \ |
| 623 |
" (curve %s)" \ |
| 624 |
" (q %b)" \ |
| 625 |
" (d %m)))" |
| 626 |
|
| 627 |
GkmDataResult |
| 628 |
gkm_data_der_read_private_key_ecdsa (GBytes *data, |
| 629 |
gcry_sexp_t *s_key) |
| 630 |
{ |
| 631 |
gcry_mpi_t d = NULL; |
| 632 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 633 |
int res; |
| 634 |
GNode *asn = NULL; |
| 635 |
GBytes *q = NULL; |
| 636 |
gsize q_bits; |
| 637 |
GQuark oid; |
| 638 |
const gchar *curve = NULL; |
| 639 |
|
| 640 |
init_quarks (); |
| 641 |
|
| 642 |
asn = egg_asn1x_create_and_decode (pk_asn1_tab, "ECPrivateKey", data); |
| 643 |
if (!asn) |
| 644 |
goto done; |
| 645 |
|
| 646 |
ret = GKM_DATA_FAILURE; |
| 647 |
|
| 648 |
if (!gkm_data_asn1_read_string_mpi (egg_asn1x_node (asn, "d", NULL), &d) || |
| 649 |
!gkm_data_asn1_read_oid (egg_asn1x_node (asn, "parameters", "namedCurve", NULL), &oid) || |
| 650 |
!gkm_data_asn1_read_bit_string (egg_asn1x_node (asn, "q", NULL), &q, &q_bits)) |
| 651 |
goto done; |
| 652 |
|
| 653 |
curve = gkm_data_der_oid_to_curve (oid); |
| 654 |
if (curve == NULL) |
| 655 |
goto done; |
| 656 |
|
| 657 |
res = gcry_sexp_build (s_key, NULL, SEXP_PRIVATE_ECDSA, |
| 658 |
curve, |
| 659 |
g_bytes_get_size(q), g_bytes_get_data(q, NULL), d); |
| 660 |
if (res) |
| 661 |
goto done; |
| 662 |
|
| 663 |
g_assert (*s_key); |
| 664 |
ret = GKM_DATA_SUCCESS; |
| 665 |
|
| 666 |
done: |
| 667 |
egg_asn1x_destroy (asn); |
| 668 |
gcry_mpi_release (d); |
| 669 |
g_bytes_unref (q); |
| 670 |
|
| 671 |
if (ret == GKM_DATA_FAILURE) |
| 672 |
g_message ("invalid ECDSA key"); |
| 673 |
|
| 674 |
return ret; |
| 675 |
} |
| 676 |
|
| 677 |
GkmDataResult |
| 678 |
gkm_data_der_read_public_key (GBytes *data, gcry_sexp_t *s_key) |
| 679 |
{ |
| 680 |
GkmDataResult res; |
| 681 |
|
| 682 |
res = gkm_data_der_read_public_key_rsa (data, s_key); |
| 683 |
if (res == GKM_DATA_UNRECOGNIZED) |
| 684 |
res = gkm_data_der_read_public_key_ecdsa (data, s_key); |
| 685 |
if (res == GKM_DATA_UNRECOGNIZED) |
| 686 |
res = gkm_data_der_read_public_key_dsa (data, s_key); |
| 687 |
|
| 688 |
return res; |
| 689 |
} |
| 690 |
|
| 691 |
GkmDataResult |
| 692 |
gkm_data_der_read_public_key_info (GBytes *data, |
| 693 |
gcry_sexp_t* s_key) |
| 694 |
{ |
| 695 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 696 |
GQuark oid; |
| 697 |
GNode *asn = NULL; |
| 698 |
GBytes *params; |
| 699 |
GBytes *key = NULL; |
| 700 |
guint n_bits; |
| 701 |
|
| 702 |
init_quarks (); |
| 703 |
|
| 704 |
asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "SubjectPublicKeyInfo", data); |
| 705 |
if (!asn) |
| 706 |
goto done; |
| 707 |
|
| 708 |
ret = GKM_DATA_FAILURE; |
| 709 |
|
| 710 |
/* Figure out the algorithm */ |
| 711 |
oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "algorithm", "algorithm", NULL)); |
| 712 |
if (!oid) |
| 713 |
goto done; |
| 714 |
|
| 715 |
/* A bit string so we cannot process in place */ |
| 716 |
key = egg_asn1x_get_bits_as_raw (egg_asn1x_node (asn, "subjectPublicKey", NULL), &n_bits); |
| 717 |
if (!key) |
| 718 |
goto done; |
| 719 |
if (n_bits % 8 != 0) { |
| 720 |
g_message ("invalid bit length for public key: %u", n_bits); |
| 721 |
goto done; |
| 722 |
} |
| 723 |
|
| 724 |
/* An RSA key is simple */ |
| 725 |
if (oid == OID_PKIX1_RSA) { |
| 726 |
ret = gkm_data_der_read_public_key_rsa (key, s_key); |
| 727 |
|
| 728 |
/* A DSA key paramaters are stored separately */ |
| 729 |
} else if (oid == OID_PKIX1_DSA) { |
| 730 |
params = egg_asn1x_get_element_raw (egg_asn1x_node (asn, "algorithm", "parameters", NULL)); |
| 731 |
if (!params) |
| 732 |
goto done; |
| 733 |
ret = gkm_data_der_read_public_key_dsa_parts (key, params, s_key); |
| 734 |
g_bytes_unref (params); |
| 735 |
|
| 736 |
/* A ECDSA key */ |
| 737 |
} else if (oid == OID_PKIX1_ECDSA) { |
| 738 |
ret = gkm_data_der_read_public_key_ecdsa (key, s_key); |
| 739 |
|
| 740 |
} else { |
| 741 |
g_message ("unsupported key algorithm in certificate: %s", g_quark_to_string (oid)); |
| 742 |
ret = GKM_DATA_UNRECOGNIZED; |
| 743 |
goto done; |
| 744 |
} |
| 745 |
|
| 746 |
done: |
| 747 |
egg_asn1x_destroy (asn); |
| 748 |
if (key) |
| 749 |
g_bytes_unref (key); |
| 750 |
|
| 751 |
if (ret == GKM_DATA_FAILURE) |
| 752 |
g_message ("invalid subject public-key info"); |
| 753 |
|
| 754 |
return ret; |
| 755 |
} |
| 756 |
|
| 757 |
GkmDataResult |
| 758 |
gkm_data_der_read_private_key (GBytes *data, |
| 759 |
gcry_sexp_t *s_key) |
| 760 |
{ |
| 761 |
GkmDataResult res; |
| 762 |
|
| 763 |
res = gkm_data_der_read_private_key_rsa (data, s_key); |
| 764 |
if (res == GKM_DATA_UNRECOGNIZED) |
| 765 |
res = gkm_data_der_read_private_key_dsa (data, s_key); |
| 766 |
if (res == GKM_DATA_UNRECOGNIZED) |
| 767 |
res = gkm_data_der_read_private_key_ecdsa (data, s_key); |
| 768 |
|
| 769 |
return res; |
| 770 |
} |
| 771 |
|
| 772 |
GkmDataResult |
| 773 |
gkm_data_der_read_private_pkcs8_plain (GBytes *data, |
| 774 |
gcry_sexp_t *s_key) |
| 775 |
{ |
| 776 |
GNode *asn = NULL; |
| 777 |
GkmDataResult ret; |
| 778 |
int algorithm; |
| 779 |
GQuark key_algo; |
| 780 |
GBytes *keydata = NULL; |
| 781 |
GBytes *params = NULL; |
| 782 |
|
| 783 |
ret = GKM_DATA_UNRECOGNIZED; |
| 784 |
|
| 785 |
init_quarks (); |
| 786 |
|
| 787 |
asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "pkcs-8-PrivateKeyInfo", data); |
| 788 |
if (!asn) |
| 789 |
goto done; |
| 790 |
|
| 791 |
ret = GKM_DATA_FAILURE; |
| 792 |
algorithm = 0; |
| 793 |
|
| 794 |
key_algo = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "privateKeyAlgorithm", "algorithm", NULL)); |
| 795 |
if (!key_algo) |
| 796 |
goto done; |
| 797 |
else if (key_algo == OID_PKIX1_RSA) |
| 798 |
algorithm = GCRY_PK_RSA; |
| 799 |
else if (key_algo == OID_PKIX1_DSA) |
| 800 |
algorithm = GCRY_PK_DSA; |
| 801 |
else if (key_algo == OID_PKIX1_ECDSA) |
| 802 |
algorithm = GCRY_PK_ECC; |
| 803 |
|
| 804 |
if (!algorithm) { |
| 805 |
ret = GKM_DATA_UNRECOGNIZED; |
| 806 |
goto done; |
| 807 |
} |
| 808 |
|
| 809 |
keydata = egg_asn1x_get_string_as_bytes (egg_asn1x_node (asn, "privateKey", NULL)); |
| 810 |
if (!keydata) |
| 811 |
goto done; |
| 812 |
|
| 813 |
params = egg_asn1x_get_element_raw (egg_asn1x_node (asn, "privateKeyAlgorithm", "parameters", NULL)); |
| 814 |
|
| 815 |
ret = GKM_DATA_SUCCESS; |
| 816 |
|
| 817 |
done: |
| 818 |
if (ret == GKM_DATA_SUCCESS) { |
| 819 |
switch (algorithm) { |
| 820 |
case GCRY_PK_RSA: |
| 821 |
ret = gkm_data_der_read_private_key_rsa (keydata, s_key); |
| 822 |
break; |
| 823 |
case GCRY_PK_DSA: |
| 824 |
/* Try the normal one block format */ |
| 825 |
ret = gkm_data_der_read_private_key_dsa (keydata, s_key); |
| 826 |
|
| 827 |
/* Otherwise try the two part format that everyone seems to like */ |
| 828 |
if (ret == GKM_DATA_UNRECOGNIZED && params) |
| 829 |
ret = gkm_data_der_read_private_key_dsa_parts (keydata, params, s_key); |
| 830 |
break; |
| 831 |
case GCRY_PK_ECC: |
| 832 |
ret = gkm_data_der_read_private_key_ecdsa (keydata, s_key); |
| 833 |
break; |
| 834 |
default: |
| 835 |
g_message ("invalid or unsupported key type in PKCS#8 key"); |
| 836 |
ret = GKM_DATA_UNRECOGNIZED; |
| 837 |
break; |
| 838 |
}; |
| 839 |
|
| 840 |
} else if (ret == GKM_DATA_FAILURE) { |
| 841 |
g_message ("invalid PKCS#8 key"); |
| 842 |
} |
| 843 |
|
| 844 |
if (params) |
| 845 |
g_bytes_unref (params); |
| 846 |
if (keydata) |
| 847 |
g_bytes_unref (keydata); |
| 848 |
egg_asn1x_destroy (asn); |
| 849 |
return ret; |
| 850 |
} |
| 851 |
|
| 852 |
GkmDataResult |
| 853 |
gkm_data_der_read_private_pkcs8_crypted (GBytes *data, |
| 854 |
const gchar *password, |
| 855 |
gsize n_password, |
| 856 |
gcry_sexp_t *s_key) |
| 857 |
{ |
| 858 |
GNode *asn = NULL; |
| 859 |
gcry_cipher_hd_t cih = NULL; |
| 860 |
gcry_error_t gcry; |
| 861 |
GkmDataResult ret, r; |
| 862 |
GQuark scheme; |
| 863 |
guchar *crypted = NULL; |
| 864 |
GNode *params; |
| 865 |
GBytes *bytes; |
| 866 |
gsize n_crypted; |
| 867 |
gint l; |
| 868 |
|
| 869 |
init_quarks (); |
| 870 |
|
| 871 |
ret = GKM_DATA_UNRECOGNIZED; |
| 872 |
|
| 873 |
asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "pkcs-8-EncryptedPrivateKeyInfo", data); |
| 874 |
if (!asn) |
| 875 |
goto done; |
| 876 |
|
| 877 |
ret = GKM_DATA_FAILURE; |
| 878 |
|
| 879 |
/* Figure out the type of encryption */ |
| 880 |
scheme = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "encryptionAlgorithm", "algorithm", NULL)); |
| 881 |
if (!scheme) |
| 882 |
goto done; |
| 883 |
|
| 884 |
params = egg_asn1x_node (asn, "encryptionAlgorithm", "parameters", NULL); |
| 885 |
if (!params) |
| 886 |
goto done; |
| 887 |
|
| 888 |
/* |
| 889 |
* Parse the encryption stuff into a cipher. |
| 890 |
*/ |
| 891 |
r = egg_symkey_read_cipher (scheme, password, n_password, params, &cih); |
| 892 |
|
| 893 |
if (r == GKM_DATA_UNRECOGNIZED) { |
| 894 |
ret = GKM_DATA_FAILURE; |
| 895 |
goto done; |
| 896 |
} else if (r != GKM_DATA_SUCCESS) { |
| 897 |
ret = r; |
| 898 |
goto done; |
| 899 |
} |
| 900 |
|
| 901 |
crypted = egg_asn1x_get_string_as_raw (egg_asn1x_node (asn, "encryptedData", NULL), |
| 902 |
egg_secure_realloc, &n_crypted); |
| 903 |
if (!crypted) |
| 904 |
goto done; |
| 905 |
|
| 906 |
gcry = gcry_cipher_decrypt (cih, crypted, n_crypted, NULL, 0); |
| 907 |
gcry_cipher_close (cih); |
| 908 |
cih = NULL; |
| 909 |
|
| 910 |
if (gcry != 0) { |
| 911 |
g_warning ("couldn't decrypt pkcs8 data: %s", gcry_strerror (gcry)); |
| 912 |
goto done; |
| 913 |
} |
| 914 |
|
| 915 |
/* Unpad the DER data */ |
| 916 |
l = egg_asn1x_element_length (crypted, n_crypted); |
| 917 |
if (l <= 0 || l > n_crypted) { |
| 918 |
ret = GKM_DATA_LOCKED; |
| 919 |
goto done; |
| 920 |
} |
| 921 |
n_crypted = l; |
| 922 |
|
| 923 |
bytes = g_bytes_new_with_free_func (crypted, n_crypted, egg_secure_free, crypted); |
| 924 |
crypted = NULL; |
| 925 |
|
| 926 |
/* Try to parse the resulting key */ |
| 927 |
ret = gkm_data_der_read_private_pkcs8_plain (bytes, s_key); |
| 928 |
g_bytes_unref (bytes); |
| 929 |
|
| 930 |
/* If unrecognized we assume bad password */ |
| 931 |
if (ret == GKM_DATA_UNRECOGNIZED) |
| 932 |
ret = GKM_DATA_LOCKED; |
| 933 |
|
| 934 |
done: |
| 935 |
if (cih) |
| 936 |
gcry_cipher_close (cih); |
| 937 |
egg_asn1x_destroy (asn); |
| 938 |
egg_secure_free (crypted); |
| 939 |
|
| 940 |
return ret; |
| 941 |
} |
| 942 |
|
| 943 |
GkmDataResult |
| 944 |
gkm_data_der_read_private_pkcs8 (GBytes *data, |
| 945 |
const gchar *password, |
| 946 |
gsize n_password, |
| 947 |
gcry_sexp_t *s_key) |
| 948 |
{ |
| 949 |
GkmDataResult res; |
| 950 |
|
| 951 |
res = gkm_data_der_read_private_pkcs8_crypted (data, password, n_password, s_key); |
| 952 |
if (res == GKM_DATA_UNRECOGNIZED) |
| 953 |
res = gkm_data_der_read_private_pkcs8_plain (data, s_key); |
| 954 |
|
| 955 |
return res; |
| 956 |
} |
| 957 |
|
| 958 |
GBytes * |
| 959 |
gkm_data_der_write_public_key_rsa (gcry_sexp_t s_key) |
| 960 |
{ |
| 961 |
GNode *asn = NULL; |
| 962 |
gcry_mpi_t n, e; |
| 963 |
GBytes *result = NULL; |
| 964 |
|
| 965 |
n = e = NULL; |
| 966 |
|
| 967 |
asn = egg_asn1x_create (pk_asn1_tab, "RSAPublicKey"); |
| 968 |
g_return_val_if_fail (asn, NULL); |
| 969 |
|
| 970 |
if (!gkm_sexp_extract_mpi (s_key, &n, "rsa", "n", NULL) || |
| 971 |
!gkm_sexp_extract_mpi (s_key, &e, "rsa", "e", NULL)) |
| 972 |
goto done; |
| 973 |
|
| 974 |
if (!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "modulus", NULL), n) || |
| 975 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "publicExponent", NULL), e)) |
| 976 |
goto done; |
| 977 |
|
| 978 |
result = egg_asn1x_encode (asn, NULL); |
| 979 |
if (result == NULL) |
| 980 |
g_warning ("couldn't encode public rsa key: %s", egg_asn1x_message (asn)); |
| 981 |
|
| 982 |
done: |
| 983 |
egg_asn1x_destroy (asn); |
| 984 |
gcry_mpi_release (n); |
| 985 |
gcry_mpi_release (e); |
| 986 |
|
| 987 |
return result; |
| 988 |
} |
| 989 |
|
| 990 |
GBytes * |
| 991 |
gkm_data_der_write_private_key_rsa (gcry_sexp_t s_key) |
| 992 |
{ |
| 993 |
GNode *asn = NULL; |
| 994 |
gcry_mpi_t n, e, d, p, q, u, e1, e2, tmp; |
| 995 |
GBytes *result = NULL; |
| 996 |
|
| 997 |
n = e = d = p = q = u = e1 = e2 = tmp = NULL; |
| 998 |
|
| 999 |
asn = egg_asn1x_create (pk_asn1_tab, "RSAPrivateKey"); |
| 1000 |
g_return_val_if_fail (asn, NULL); |
| 1001 |
|
| 1002 |
if (!gkm_sexp_extract_mpi (s_key, &n, "rsa", "n", NULL) || |
| 1003 |
!gkm_sexp_extract_mpi (s_key, &e, "rsa", "e", NULL) || |
| 1004 |
!gkm_sexp_extract_mpi (s_key, &d, "rsa", "d", NULL) || |
| 1005 |
!gkm_sexp_extract_mpi (s_key, &p, "rsa", "p", NULL) || |
| 1006 |
!gkm_sexp_extract_mpi (s_key, &q, "rsa", "q", NULL) || |
| 1007 |
!gkm_sexp_extract_mpi (s_key, &u, "rsa", "u", NULL)) |
| 1008 |
goto done; |
| 1009 |
|
| 1010 |
if (!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "modulus", NULL), n) || |
| 1011 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "publicExponent", NULL), e) || |
| 1012 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "privateExponent", NULL), d) || |
| 1013 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "prime1", NULL), p) || |
| 1014 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "prime2", NULL), q) || |
| 1015 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "coefficient", NULL), u)) |
| 1016 |
goto done; |
| 1017 |
|
| 1018 |
/* Calculate e1 and e2 */ |
| 1019 |
tmp = gcry_mpi_snew (1024); |
| 1020 |
gcry_mpi_sub_ui (tmp, p, 1); |
| 1021 |
e1 = gcry_mpi_snew (1024); |
| 1022 |
gcry_mpi_mod (e1, d, tmp); |
| 1023 |
gcry_mpi_sub_ui (tmp, q, 1); |
| 1024 |
e2 = gcry_mpi_snew (1024); |
| 1025 |
gcry_mpi_mod (e2, d, tmp); |
| 1026 |
|
| 1027 |
/* Write out calculated */ |
| 1028 |
if (!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "exponent1", NULL), e1) || |
| 1029 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "exponent2", NULL), e2)) |
| 1030 |
goto done; |
| 1031 |
|
| 1032 |
/* Write out the version */ |
| 1033 |
egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), 0); |
| 1034 |
|
| 1035 |
result = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1036 |
if (result == NULL) |
| 1037 |
g_warning ("couldn't encode private rsa key: %s", egg_asn1x_message (asn)); |
| 1038 |
|
| 1039 |
done: |
| 1040 |
egg_asn1x_destroy (asn); |
| 1041 |
gcry_mpi_release (n); |
| 1042 |
gcry_mpi_release (e); |
| 1043 |
gcry_mpi_release (d); |
| 1044 |
gcry_mpi_release (p); |
| 1045 |
gcry_mpi_release (q); |
| 1046 |
gcry_mpi_release (u); |
| 1047 |
|
| 1048 |
gcry_mpi_release (tmp); |
| 1049 |
gcry_mpi_release (e1); |
| 1050 |
gcry_mpi_release (e2); |
| 1051 |
|
| 1052 |
return result; |
| 1053 |
} |
| 1054 |
|
| 1055 |
GBytes * |
| 1056 |
gkm_data_der_write_public_key_dsa (gcry_sexp_t s_key) |
| 1057 |
{ |
| 1058 |
GNode *asn = NULL; |
| 1059 |
gcry_mpi_t p, q, g, y; |
| 1060 |
GBytes *result = NULL; |
| 1061 |
|
| 1062 |
p = q = g = y = NULL; |
| 1063 |
|
| 1064 |
asn = egg_asn1x_create (pk_asn1_tab, "DSAPublicKey"); |
| 1065 |
g_return_val_if_fail (asn, NULL); |
| 1066 |
|
| 1067 |
if (!gkm_sexp_extract_mpi (s_key, &p, "dsa", "p", NULL) || |
| 1068 |
!gkm_sexp_extract_mpi (s_key, &q, "dsa", "q", NULL) || |
| 1069 |
!gkm_sexp_extract_mpi (s_key, &g, "dsa", "g", NULL) || |
| 1070 |
!gkm_sexp_extract_mpi (s_key, &y, "dsa", "y", NULL)) |
| 1071 |
goto done; |
| 1072 |
|
| 1073 |
if (!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "p", NULL), p) || |
| 1074 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "q", NULL), q) || |
| 1075 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "g", NULL), g) || |
| 1076 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "Y", NULL), y)) |
| 1077 |
goto done; |
| 1078 |
|
| 1079 |
egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), 0); |
| 1080 |
|
| 1081 |
result = egg_asn1x_encode (asn, NULL); |
| 1082 |
if (result == NULL) |
| 1083 |
g_warning ("couldn't encode public dsa key: %s", egg_asn1x_message (asn)); |
| 1084 |
|
| 1085 |
done: |
| 1086 |
egg_asn1x_destroy (asn); |
| 1087 |
gcry_mpi_release (p); |
| 1088 |
gcry_mpi_release (q); |
| 1089 |
gcry_mpi_release (g); |
| 1090 |
gcry_mpi_release (y); |
| 1091 |
|
| 1092 |
return result; |
| 1093 |
} |
| 1094 |
|
| 1095 |
GBytes * |
| 1096 |
gkm_data_der_write_private_key_dsa_part (gcry_sexp_t skey) |
| 1097 |
{ |
| 1098 |
GNode *asn = NULL; |
| 1099 |
gcry_mpi_t x; |
| 1100 |
GBytes *result = NULL; |
| 1101 |
|
| 1102 |
x = NULL; |
| 1103 |
|
| 1104 |
asn = egg_asn1x_create (pk_asn1_tab, "DSAPrivatePart"); |
| 1105 |
g_return_val_if_fail (asn, NULL); |
| 1106 |
|
| 1107 |
if (!gkm_sexp_extract_mpi (skey, &x, "dsa", "x", NULL)) |
| 1108 |
goto done; |
| 1109 |
|
| 1110 |
if (!gkm_data_asn1_write_mpi (asn, x)) |
| 1111 |
goto done; |
| 1112 |
|
| 1113 |
result = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1114 |
if (result == NULL) |
| 1115 |
g_warning ("couldn't encode private dsa key: %s", egg_asn1x_message (asn)); |
| 1116 |
|
| 1117 |
done: |
| 1118 |
egg_asn1x_destroy (asn); |
| 1119 |
gcry_mpi_release (x); |
| 1120 |
|
| 1121 |
return result; |
| 1122 |
} |
| 1123 |
|
| 1124 |
GBytes * |
| 1125 |
gkm_data_der_write_private_key_dsa_params (gcry_sexp_t skey) |
| 1126 |
{ |
| 1127 |
GNode *asn = NULL; |
| 1128 |
gcry_mpi_t p, q, g; |
| 1129 |
GBytes *result = NULL; |
| 1130 |
|
| 1131 |
p = q = g = NULL; |
| 1132 |
|
| 1133 |
asn = egg_asn1x_create (pk_asn1_tab, "DSAParameters"); |
| 1134 |
g_return_val_if_fail (asn, NULL); |
| 1135 |
|
| 1136 |
if (!gkm_sexp_extract_mpi (skey, &p, "dsa", "p", NULL) || |
| 1137 |
!gkm_sexp_extract_mpi (skey, &q, "dsa", "q", NULL) || |
| 1138 |
!gkm_sexp_extract_mpi (skey, &g, "dsa", "g", NULL)) |
| 1139 |
goto done; |
| 1140 |
|
| 1141 |
if (!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "p", NULL), p) || |
| 1142 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "q", NULL), q) || |
| 1143 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "g", NULL), g)) |
| 1144 |
goto done; |
| 1145 |
|
| 1146 |
result = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1147 |
if (result == NULL) |
| 1148 |
g_warning ("couldn't encode private dsa params: %s", egg_asn1x_message (asn)); |
| 1149 |
|
| 1150 |
done: |
| 1151 |
egg_asn1x_destroy (asn); |
| 1152 |
gcry_mpi_release (p); |
| 1153 |
gcry_mpi_release (q); |
| 1154 |
gcry_mpi_release (g); |
| 1155 |
|
| 1156 |
return result; |
| 1157 |
} |
| 1158 |
|
| 1159 |
GBytes * |
| 1160 |
gkm_data_der_write_private_key_dsa (gcry_sexp_t s_key) |
| 1161 |
{ |
| 1162 |
GNode *asn = NULL; |
| 1163 |
gcry_mpi_t p, q, g, y, x; |
| 1164 |
GBytes *result = NULL; |
| 1165 |
|
| 1166 |
p = q = g = y = x = NULL; |
| 1167 |
|
| 1168 |
asn = egg_asn1x_create (pk_asn1_tab, "DSAPrivateKey"); |
| 1169 |
g_return_val_if_fail (asn, NULL); |
| 1170 |
|
| 1171 |
if (!gkm_sexp_extract_mpi (s_key, &p, "dsa", "p", NULL) || |
| 1172 |
!gkm_sexp_extract_mpi (s_key, &q, "dsa", "q", NULL) || |
| 1173 |
!gkm_sexp_extract_mpi (s_key, &g, "dsa", "g", NULL) || |
| 1174 |
!gkm_sexp_extract_mpi (s_key, &y, "dsa", "y", NULL) || |
| 1175 |
!gkm_sexp_extract_mpi (s_key, &x, "dsa", "x", NULL)) |
| 1176 |
goto done; |
| 1177 |
|
| 1178 |
if (!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "p", NULL), p) || |
| 1179 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "q", NULL), q) || |
| 1180 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "g", NULL), g) || |
| 1181 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "Y", NULL), y) || |
| 1182 |
!gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "priv", NULL), x)) |
| 1183 |
goto done; |
| 1184 |
|
| 1185 |
egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), 0); |
| 1186 |
|
| 1187 |
result = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1188 |
if (result == NULL) |
| 1189 |
g_warning ("couldn't encode private dsa key: %s", egg_asn1x_message (asn)); |
| 1190 |
|
| 1191 |
done: |
| 1192 |
egg_asn1x_destroy (asn); |
| 1193 |
gcry_mpi_release (p); |
| 1194 |
gcry_mpi_release (q); |
| 1195 |
gcry_mpi_release (g); |
| 1196 |
gcry_mpi_release (y); |
| 1197 |
gcry_mpi_release (x); |
| 1198 |
|
| 1199 |
return result; |
| 1200 |
} |
| 1201 |
|
| 1202 |
GBytes * |
| 1203 |
gkm_data_der_write_public_key_ecdsa (gcry_sexp_t s_key) |
| 1204 |
{ |
| 1205 |
GNode *asn = NULL, *named_curve; |
| 1206 |
gcry_mpi_t d = NULL; |
| 1207 |
GBytes *result = NULL, *q = NULL; |
| 1208 |
gchar *q_data = NULL; |
| 1209 |
GQuark oid; |
| 1210 |
gchar *curve = NULL; |
| 1211 |
gsize q_size; |
| 1212 |
|
| 1213 |
init_quarks (); |
| 1214 |
|
| 1215 |
asn = egg_asn1x_create (pk_asn1_tab, "ECPublicKey"); |
| 1216 |
g_return_val_if_fail (asn, NULL); |
| 1217 |
|
| 1218 |
if (!gkm_sexp_extract_buffer (s_key, &q_data, &q_size, "ecdsa", "q", NULL) || |
| 1219 |
!gkm_sexp_extract_string (s_key, &curve, "ecdsa", "curve", NULL)) |
| 1220 |
goto done; |
| 1221 |
|
| 1222 |
oid = gkm_data_der_curve_to_oid (curve); |
| 1223 |
g_free (curve); |
| 1224 |
if (oid == 0) |
| 1225 |
goto done; |
| 1226 |
|
| 1227 |
q = g_bytes_new_take (q_data, q_size); |
| 1228 |
if (q == NULL) |
| 1229 |
goto done; |
| 1230 |
|
| 1231 |
named_curve = egg_asn1x_node (asn, "parameters", "namedCurve", NULL); |
| 1232 |
|
| 1233 |
/* XXX the bit size does not have to match byte size * 8 exactly |
| 1234 |
* it would be good to cover this with more tests |
| 1235 |
*/ |
| 1236 |
if (!gkm_data_asn1_write_bit_string (egg_asn1x_node (asn, "q", NULL), q, q_size*8) || |
| 1237 |
!gkm_data_asn1_write_oid (named_curve, oid)) |
| 1238 |
goto done; |
| 1239 |
|
| 1240 |
if (!egg_asn1x_set_choice (egg_asn1x_node (asn, "parameters", NULL), named_curve)) |
| 1241 |
goto done; |
| 1242 |
|
| 1243 |
result = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1244 |
if (result == NULL) |
| 1245 |
g_warning ("couldn't encode public ecdsa key: %s", egg_asn1x_message (asn)); |
| 1246 |
|
| 1247 |
done: |
| 1248 |
egg_asn1x_destroy (asn); |
| 1249 |
gcry_mpi_release (d); |
| 1250 |
g_bytes_unref (q); |
| 1251 |
|
| 1252 |
return result; |
| 1253 |
} |
| 1254 |
|
| 1255 |
GBytes * |
| 1256 |
gkm_data_der_write_private_key_ecdsa (gcry_sexp_t s_key) |
| 1257 |
{ |
| 1258 |
GNode *asn = NULL, *named_curve; |
| 1259 |
gcry_mpi_t d = NULL; |
| 1260 |
GBytes *result = NULL, *q = NULL; |
| 1261 |
gchar *q_data = NULL; |
| 1262 |
GQuark oid; |
| 1263 |
gchar *curve = NULL; |
| 1264 |
gsize q_size; |
| 1265 |
|
| 1266 |
init_quarks (); |
| 1267 |
|
| 1268 |
asn = egg_asn1x_create (pk_asn1_tab, "ECPrivateKey"); |
| 1269 |
g_return_val_if_fail (asn, NULL); |
| 1270 |
|
| 1271 |
if (!gkm_sexp_extract_mpi (s_key, &d, "ecdsa", "d", NULL) || |
| 1272 |
!gkm_sexp_extract_buffer (s_key, &q_data, &q_size, "ecdsa", "q", NULL) || |
| 1273 |
!gkm_sexp_extract_string (s_key, &curve, "ecdsa", "curve", NULL)) |
| 1274 |
goto done; |
| 1275 |
|
| 1276 |
oid = gkm_data_der_curve_to_oid (curve); |
| 1277 |
g_free (curve); |
| 1278 |
if (oid == 0) |
| 1279 |
goto done; |
| 1280 |
|
| 1281 |
q = g_bytes_new_take (q_data, q_size); |
| 1282 |
if (q == NULL) |
| 1283 |
goto done; |
| 1284 |
|
| 1285 |
named_curve = egg_asn1x_node (asn, "parameters", "namedCurve", NULL); |
| 1286 |
|
| 1287 |
if (!gkm_data_asn1_write_string_mpi (egg_asn1x_node (asn, "d", NULL), d) || |
| 1288 |
!gkm_data_asn1_write_bit_string (egg_asn1x_node (asn, "q", NULL), q, q_size*8) || |
| 1289 |
!gkm_data_asn1_write_oid (named_curve, oid)) |
| 1290 |
goto done; |
| 1291 |
|
| 1292 |
if (!egg_asn1x_set_choice (egg_asn1x_node (asn, "parameters", NULL), named_curve)) |
| 1293 |
goto done; |
| 1294 |
|
| 1295 |
egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), 1); |
| 1296 |
|
| 1297 |
result = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1298 |
if (result == NULL) |
| 1299 |
g_warning ("couldn't encode private ecdsa key: %s", egg_asn1x_message (asn)); |
| 1300 |
|
| 1301 |
done: |
| 1302 |
egg_asn1x_destroy (asn); |
| 1303 |
gcry_mpi_release (d); |
| 1304 |
g_bytes_unref (q); |
| 1305 |
|
| 1306 |
return result; |
| 1307 |
} |
| 1308 |
|
| 1309 |
GBytes * |
| 1310 |
gkm_data_der_write_public_key (gcry_sexp_t s_key) |
| 1311 |
{ |
| 1312 |
gboolean is_priv; |
| 1313 |
int algorithm; |
| 1314 |
|
| 1315 |
g_return_val_if_fail (s_key != NULL, NULL); |
| 1316 |
|
| 1317 |
if (!gkm_sexp_parse_key (s_key, &algorithm, &is_priv, NULL)) |
| 1318 |
g_return_val_if_reached (NULL); |
| 1319 |
|
| 1320 |
g_return_val_if_fail (!is_priv, NULL); |
| 1321 |
|
| 1322 |
switch (algorithm) { |
| 1323 |
case GCRY_PK_RSA: |
| 1324 |
return gkm_data_der_write_public_key_rsa (s_key); |
| 1325 |
case GCRY_PK_DSA: |
| 1326 |
return gkm_data_der_write_public_key_dsa (s_key); |
| 1327 |
case GCRY_PK_ECC: |
| 1328 |
return gkm_data_der_write_public_key_ecdsa (s_key); |
| 1329 |
default: |
| 1330 |
g_return_val_if_reached (NULL); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
GBytes * |
| 1335 |
gkm_data_der_write_private_key (gcry_sexp_t s_key) |
| 1336 |
{ |
| 1337 |
gboolean is_priv; |
| 1338 |
int algorithm; |
| 1339 |
|
| 1340 |
g_return_val_if_fail (s_key != NULL, NULL); |
| 1341 |
|
| 1342 |
if (!gkm_sexp_parse_key (s_key, &algorithm, &is_priv, NULL)) |
| 1343 |
g_return_val_if_reached (NULL); |
| 1344 |
|
| 1345 |
g_return_val_if_fail (is_priv, NULL); |
| 1346 |
|
| 1347 |
switch (algorithm) { |
| 1348 |
case GCRY_PK_RSA: |
| 1349 |
return gkm_data_der_write_private_key_rsa (s_key); |
| 1350 |
case GCRY_PK_DSA: |
| 1351 |
return gkm_data_der_write_private_key_dsa (s_key); |
| 1352 |
case GCRY_PK_ECC: |
| 1353 |
return gkm_data_der_write_private_key_ecdsa (s_key); |
| 1354 |
default: |
| 1355 |
g_return_val_if_reached (NULL); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
static gcry_cipher_hd_t |
| 1360 |
prepare_and_encode_pkcs8_cipher (GNode *asn, const gchar *password, |
| 1361 |
gsize n_password, gsize *n_block) |
| 1362 |
{ |
| 1363 |
GNode *asn1_params = NULL; |
| 1364 |
gcry_cipher_hd_t cih; |
| 1365 |
guchar *salt; |
| 1366 |
gsize n_salt; |
| 1367 |
gcry_error_t gcry; |
| 1368 |
guchar *key, *iv; |
| 1369 |
gsize n_key; |
| 1370 |
int iterations; |
| 1371 |
|
| 1372 |
init_quarks (); |
| 1373 |
|
| 1374 |
/* Make sure the encryption algorithm works */ |
| 1375 |
g_return_val_if_fail (gcry_cipher_algo_info (gcry_cipher_map_name (g_quark_to_string (OID_PKCS12_PBE_3DES_SHA1)), |
| 1376 |
GCRYCTL_TEST_ALGO, NULL, 0) == 0, NULL); |
| 1377 |
|
| 1378 |
/* The encryption algorithm */ |
| 1379 |
if(!egg_asn1x_set_oid_as_quark (egg_asn1x_node (asn, "encryptionAlgorithm", "algorithm", NULL), |
| 1380 |
OID_PKCS12_PBE_3DES_SHA1)) |
| 1381 |
g_return_val_if_reached (NULL); |
| 1382 |
|
| 1383 |
/* Randomize some input for the password based secret */ |
| 1384 |
iterations = g_random_int_range (1000, 4096); |
| 1385 |
n_salt = 8; |
| 1386 |
salt = g_malloc (n_salt); |
| 1387 |
gcry_create_nonce (salt, n_salt); |
| 1388 |
|
| 1389 |
/* Allocate space for the key and iv */ |
| 1390 |
n_key = gcry_cipher_get_algo_keylen (GCRY_CIPHER_3DES); |
| 1391 |
*n_block = gcry_cipher_get_algo_blklen (GCRY_MD_SHA1); |
| 1392 |
g_return_val_if_fail (n_key && *n_block, NULL); |
| 1393 |
|
| 1394 |
if (!egg_symkey_generate_pkcs12 (GCRY_CIPHER_3DES, GCRY_MD_SHA1, |
| 1395 |
password, n_password, salt, |
| 1396 |
sizeof (salt), iterations, &key, &iv)) |
| 1397 |
g_return_val_if_reached (NULL); |
| 1398 |
|
| 1399 |
/* Now write out the parameters */ |
| 1400 |
asn1_params = egg_asn1x_create (pkix_asn1_tab, "pkcs-12-PbeParams"); |
| 1401 |
g_return_val_if_fail (asn1_params, NULL); |
| 1402 |
egg_asn1x_set_string_as_raw (egg_asn1x_node (asn1_params, "salt", NULL), salt, n_salt, g_free); |
| 1403 |
egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn1_params, "iterations", NULL), iterations); |
| 1404 |
egg_asn1x_set_any_from (egg_asn1x_node (asn, "encryptionAlgorithm", "parameters", NULL), asn1_params); |
| 1405 |
|
| 1406 |
/* Now make a cipher that matches what we wrote out */ |
| 1407 |
gcry = gcry_cipher_open (&cih, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC, 0); |
| 1408 |
g_return_val_if_fail (gcry == 0, NULL); |
| 1409 |
g_return_val_if_fail (cih, NULL); |
| 1410 |
|
| 1411 |
gcry_cipher_setiv (cih, iv, *n_block); |
| 1412 |
gcry_cipher_setkey (cih, key, n_key); |
| 1413 |
|
| 1414 |
g_free (iv); |
| 1415 |
egg_secure_free (key); |
| 1416 |
egg_asn1x_destroy (asn1_params); |
| 1417 |
|
| 1418 |
return cih; |
| 1419 |
} |
| 1420 |
|
| 1421 |
GBytes * |
| 1422 |
gkm_data_der_write_private_pkcs8_plain (gcry_sexp_t skey) |
| 1423 |
{ |
| 1424 |
GNode *asn = NULL; |
| 1425 |
int algorithm; |
| 1426 |
gboolean is_priv; |
| 1427 |
GQuark oid; |
| 1428 |
GBytes *params; |
| 1429 |
GBytes *key; |
| 1430 |
GBytes *data; |
| 1431 |
|
| 1432 |
init_quarks (); |
| 1433 |
|
| 1434 |
/* Parse and check that the key is for real */ |
| 1435 |
if (!gkm_sexp_parse_key (skey, &algorithm, &is_priv, NULL)) |
| 1436 |
g_return_val_if_reached (NULL); |
| 1437 |
g_return_val_if_fail (is_priv == TRUE, NULL); |
| 1438 |
|
| 1439 |
asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-8-PrivateKeyInfo"); |
| 1440 |
g_return_val_if_fail (asn, NULL); |
| 1441 |
|
| 1442 |
/* Write out the version */ |
| 1443 |
egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), 0); |
| 1444 |
|
| 1445 |
/* Per algorithm differences */ |
| 1446 |
switch (algorithm) |
| 1447 |
{ |
| 1448 |
/* RSA gets encoded in a standard simple way */ |
| 1449 |
case GCRY_PK_RSA: |
| 1450 |
oid = OID_PKIX1_RSA; |
| 1451 |
params = NULL; |
| 1452 |
key = gkm_data_der_write_private_key_rsa (skey); |
| 1453 |
break; |
| 1454 |
|
| 1455 |
/* DSA gets incoded with the params seperate */ |
| 1456 |
case GCRY_PK_DSA: |
| 1457 |
oid = OID_PKIX1_DSA; |
| 1458 |
key = gkm_data_der_write_private_key_dsa_part (skey); |
| 1459 |
params = gkm_data_der_write_private_key_dsa_params (skey); |
| 1460 |
break; |
| 1461 |
|
| 1462 |
case GCRY_PK_ECC: |
| 1463 |
oid = OID_PKIX1_ECDSA; |
| 1464 |
params = NULL; |
| 1465 |
key = gkm_data_der_write_private_key_ecdsa (skey); |
| 1466 |
break; |
| 1467 |
|
| 1468 |
default: |
| 1469 |
g_warning ("trying to serialize unsupported private key algorithm: %d", algorithm); |
| 1470 |
return NULL; |
| 1471 |
}; |
| 1472 |
|
| 1473 |
/* Write out the algorithm */ |
| 1474 |
if (!egg_asn1x_set_oid_as_quark (egg_asn1x_node (asn, "privateKeyAlgorithm", "algorithm", NULL), oid)) |
| 1475 |
g_return_val_if_reached (NULL); |
| 1476 |
|
| 1477 |
/* Write out the parameters */ |
| 1478 |
if (params) { |
| 1479 |
egg_asn1x_set_any_raw (egg_asn1x_node (asn, "privateKeyAlgorithm", "parameters", NULL), params); |
| 1480 |
g_bytes_unref (params); |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* Write out the key portion */ |
| 1484 |
egg_asn1x_set_string_as_bytes (egg_asn1x_node (asn, "privateKey", NULL), key); |
| 1485 |
g_bytes_unref (key); |
| 1486 |
|
| 1487 |
data = egg_asn1x_encode (asn, egg_secure_realloc); |
| 1488 |
if (data == NULL) |
| 1489 |
g_warning ("couldn't encode private pkcs8 key: %s", egg_asn1x_message (asn)); |
| 1490 |
|
| 1491 |
egg_asn1x_destroy (asn); |
| 1492 |
return data; |
| 1493 |
} |
| 1494 |
|
| 1495 |
GBytes * |
| 1496 |
gkm_data_der_write_private_pkcs8_crypted (gcry_sexp_t skey, |
| 1497 |
const gchar *password, |
| 1498 |
gsize n_password) |
| 1499 |
{ |
| 1500 |
gcry_error_t gcry; |
| 1501 |
gcry_cipher_hd_t cih; |
| 1502 |
GNode *asn = NULL; |
| 1503 |
GBytes *key, *data; |
| 1504 |
guchar *raw; |
| 1505 |
gsize n_raw, n_key; |
| 1506 |
gsize block = 0; |
| 1507 |
|
| 1508 |
/* Encode the key in normal pkcs8 fashion */ |
| 1509 |
key = gkm_data_der_write_private_pkcs8_plain (skey); |
| 1510 |
if (key == NULL) |
| 1511 |
return NULL; |
| 1512 |
|
| 1513 |
asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-8-EncryptedPrivateKeyInfo"); |
| 1514 |
g_return_val_if_fail (asn, NULL); |
| 1515 |
|
| 1516 |
/* Create a and write out a cipher used for encryption */ |
| 1517 |
cih = prepare_and_encode_pkcs8_cipher (asn, password, n_password, &block); |
| 1518 |
g_return_val_if_fail (cih, NULL); |
| 1519 |
|
| 1520 |
n_key = g_bytes_get_size (key); |
| 1521 |
|
| 1522 |
/* Pad the block of data */ |
| 1523 |
if(block > 1) { |
| 1524 |
gsize n_pad = block - (n_key % block); |
| 1525 |
if (n_pad == 0) |
| 1526 |
n_pad = block; |
| 1527 |
raw = egg_secure_alloc (n_key + n_pad); |
| 1528 |
memcpy (raw, g_bytes_get_data (key, NULL), n_key); |
| 1529 |
memset (raw + n_key, (int)n_pad, n_pad); |
| 1530 |
n_raw = n_key + n_pad; |
| 1531 |
|
| 1532 |
/* No padding, probably stream cipher */ |
| 1533 |
} else { |
| 1534 |
raw = egg_secure_alloc (n_key); |
| 1535 |
memcpy (raw, g_bytes_get_data (key, NULL), n_key); |
| 1536 |
n_raw = n_key; |
| 1537 |
} |
| 1538 |
|
| 1539 |
g_bytes_unref (key); |
| 1540 |
|
| 1541 |
gcry = gcry_cipher_encrypt (cih, raw, n_raw, NULL, 0); |
| 1542 |
g_return_val_if_fail (gcry == 0, NULL); |
| 1543 |
|
| 1544 |
gcry_cipher_close (cih); |
| 1545 |
key = g_bytes_new_with_free_func (raw, n_raw, egg_secure_free, raw); |
| 1546 |
|
| 1547 |
egg_asn1x_set_string_as_bytes (egg_asn1x_node (asn, "encryptedData", NULL), key); |
| 1548 |
|
| 1549 |
g_bytes_unref (key); |
| 1550 |
|
| 1551 |
data = egg_asn1x_encode (asn, NULL); |
| 1552 |
if (data == NULL) |
| 1553 |
g_warning ("couldn't encode encrypted pkcs8 key: %s", egg_asn1x_message (asn)); |
| 1554 |
|
| 1555 |
egg_asn1x_destroy (asn); |
| 1556 |
return data; |
| 1557 |
} |
| 1558 |
|
| 1559 |
/* ----------------------------------------------------------------------------- |
| 1560 |
* CERTIFICATES |
| 1561 |
*/ |
| 1562 |
|
| 1563 |
GkmDataResult |
| 1564 |
gkm_data_der_read_certificate (GBytes *data, |
| 1565 |
GNode **asn1) |
| 1566 |
{ |
| 1567 |
*asn1 = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data); |
| 1568 |
if (!*asn1) |
| 1569 |
return GKM_DATA_UNRECOGNIZED; |
| 1570 |
|
| 1571 |
return GKM_DATA_SUCCESS; |
| 1572 |
} |
| 1573 |
|
| 1574 |
GkmDataResult |
| 1575 |
gkm_data_der_read_basic_constraints (GBytes *data, |
| 1576 |
gboolean *is_ca, |
| 1577 |
gint *path_len) |
| 1578 |
{ |
| 1579 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 1580 |
GNode *asn = NULL; |
| 1581 |
GNode *node; |
| 1582 |
gulong value; |
| 1583 |
|
| 1584 |
asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "BasicConstraints", data); |
| 1585 |
if (!asn) |
| 1586 |
goto done; |
| 1587 |
|
| 1588 |
ret = GKM_DATA_FAILURE; |
| 1589 |
|
| 1590 |
if (path_len) { |
| 1591 |
node = egg_asn1x_node (asn, "pathLenConstraint", NULL); |
| 1592 |
if (!egg_asn1x_have (node)) |
| 1593 |
*path_len = -1; |
| 1594 |
else if (!egg_asn1x_get_integer_as_ulong (node, &value)) |
| 1595 |
goto done; |
| 1596 |
else |
| 1597 |
*path_len = value; |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (is_ca) { |
| 1601 |
node = egg_asn1x_node (asn, "cA", NULL); |
| 1602 |
if (!egg_asn1x_have (node)) |
| 1603 |
*is_ca = FALSE; |
| 1604 |
else if (!egg_asn1x_get_boolean (node, is_ca)) |
| 1605 |
goto done; |
| 1606 |
} |
| 1607 |
|
| 1608 |
ret = GKM_DATA_SUCCESS; |
| 1609 |
|
| 1610 |
done: |
| 1611 |
egg_asn1x_destroy (asn); |
| 1612 |
if (ret == GKM_DATA_FAILURE) |
| 1613 |
g_message ("invalid basic constraints"); |
| 1614 |
|
| 1615 |
return ret; |
| 1616 |
} |
| 1617 |
|
| 1618 |
GkmDataResult |
| 1619 |
gkm_data_der_read_key_usage (GBytes *data, |
| 1620 |
gulong *key_usage) |
| 1621 |
{ |
| 1622 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 1623 |
GNode *asn = NULL; |
| 1624 |
guint n_bits; |
| 1625 |
|
| 1626 |
asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "KeyUsage", data); |
| 1627 |
if (!asn) |
| 1628 |
goto done; |
| 1629 |
|
| 1630 |
ret = GKM_DATA_FAILURE; |
| 1631 |
|
| 1632 |
if (!egg_asn1x_get_bits_as_ulong (asn, key_usage, &n_bits)) |
| 1633 |
goto done; |
| 1634 |
|
| 1635 |
ret = GKM_DATA_SUCCESS; |
| 1636 |
|
| 1637 |
done: |
| 1638 |
egg_asn1x_destroy (asn); |
| 1639 |
return ret; |
| 1640 |
} |
| 1641 |
|
| 1642 |
GkmDataResult |
| 1643 |
gkm_data_der_read_enhanced_usage (GBytes *data, |
| 1644 |
GQuark **usage_oids) |
| 1645 |
{ |
| 1646 |
GkmDataResult ret = GKM_DATA_UNRECOGNIZED; |
| 1647 |
GNode *asn = NULL; |
| 1648 |
GNode *node; |
| 1649 |
GArray *array; |
| 1650 |
GQuark oid; |
| 1651 |
int i; |
| 1652 |
|
| 1653 |
asn = egg_asn1x_create_and_decode (pkix_asn1_tab, "ExtKeyUsageSyntax", data); |
| 1654 |
if (!asn) |
| 1655 |
goto done; |
| 1656 |
|
| 1657 |
ret = GKM_DATA_FAILURE; |
| 1658 |
|
| 1659 |
array = g_array_new (TRUE, TRUE, sizeof (GQuark)); |
| 1660 |
for (i = 0; TRUE; ++i) { |
| 1661 |
node = egg_asn1x_node (asn, i + 1, NULL); |
| 1662 |
if (node == NULL) |
| 1663 |
break; |
| 1664 |
|
| 1665 |
oid = egg_asn1x_get_oid_as_quark (node); |
| 1666 |
g_array_append_val (array, oid); |
| 1667 |
} |
| 1668 |
|
| 1669 |
*usage_oids = (GQuark*)g_array_free (array, FALSE); |
| 1670 |
ret = GKM_DATA_SUCCESS; |
| 1671 |
|
| 1672 |
done: |
| 1673 |
egg_asn1x_destroy (asn); |
| 1674 |
return ret; |
| 1675 |
} |
| 1676 |
|
| 1677 |
|
| 1678 |
GBytes * |
| 1679 |
gkm_data_der_write_certificate (GNode *asn1) |
| 1680 |
{ |
| 1681 |
GBytes *result; |
| 1682 |
|
| 1683 |
g_return_val_if_fail (asn1, NULL); |
| 1684 |
|
| 1685 |
result = egg_asn1x_encode (asn1, NULL); |
| 1686 |
if (result == NULL) |
| 1687 |
g_warning ("couldn't encode certificate: %s", egg_asn1x_message (asn1)); |
| 1688 |
|
| 1689 |
return result; |
| 1690 |
} |