| 1 |
/* pubkey-util.c - Supporting functions for all pubkey modules. |
| 2 |
* Copyright (C) 1998, 1999, 2000, 2002, 2003, 2005, |
| 3 |
* 2007, 2008, 2011 Free Software Foundation, Inc. |
| 4 |
* Copyright (C) 2013, 2015 g10 Code GmbH |
| 5 |
* |
| 6 |
* This file is part of Libgcrypt. |
| 7 |
* |
| 8 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as |
| 10 |
* published by the Free Software Foundation; either version 2.1 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <config.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "g10lib.h" |
| 28 |
#include "mpi.h" |
| 29 |
#include "cipher.h" |
| 30 |
#include "pubkey-internal.h" |
| 31 |
|
| 32 |
|
| 33 |
/* Callback for the pubkey algorithm code to verify PSS signatures. |
| 34 |
OPAQUE is the data provided by the actual caller. The meaning of |
| 35 |
TMP depends on the actual algorithm (but there is only RSA); now |
| 36 |
for RSA it is the output of running the public key function on the |
| 37 |
input. */ |
| 38 |
static int |
| 39 |
pss_verify_cmp (void *opaque, gcry_mpi_t tmp) |
| 40 |
{ |
| 41 |
struct pk_encoding_ctx *ctx = opaque; |
| 42 |
gcry_mpi_t value = ctx->verify_arg; |
| 43 |
|
| 44 |
return _gcry_rsa_pss_verify (value, !(ctx->flags & PUBKEY_FLAG_PREHASH), |
| 45 |
tmp, ctx->nbits - 1, |
| 46 |
ctx->hash_algo, ctx->saltlen); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/* Parser for a flag list. On return the encoding is stored at |
| 51 |
R_ENCODING and the flags are stored at R_FLAGS. If any of them is |
| 52 |
not needed, NULL may be passed. The function returns 0 on success |
| 53 |
or an error code. */ |
| 54 |
gpg_err_code_t |
| 55 |
_gcry_pk_util_parse_flaglist (gcry_sexp_t list, |
| 56 |
int *r_flags, enum pk_encoding *r_encoding) |
| 57 |
{ |
| 58 |
gpg_err_code_t rc = 0; |
| 59 |
const char *s; |
| 60 |
size_t n; |
| 61 |
int i; |
| 62 |
int encoding = PUBKEY_ENC_UNKNOWN; |
| 63 |
int flags = 0; |
| 64 |
int igninvflag = 0; |
| 65 |
|
| 66 |
for (i = list ? sexp_length (list)-1 : 0; i > 0; i--) |
| 67 |
{ |
| 68 |
s = sexp_nth_data (list, i, &n); |
| 69 |
if (!s) |
| 70 |
continue; /* Not a data element. */ |
| 71 |
|
| 72 |
switch (n) |
| 73 |
{ |
| 74 |
case 3: |
| 75 |
if (!memcmp (s, "pss", 3) && encoding == PUBKEY_ENC_UNKNOWN) |
| 76 |
{ |
| 77 |
encoding = PUBKEY_ENC_PSS; |
| 78 |
flags |= PUBKEY_FLAG_FIXEDLEN; |
| 79 |
} |
| 80 |
else if (!memcmp (s, "raw", 3) && encoding == PUBKEY_ENC_UNKNOWN) |
| 81 |
{ |
| 82 |
encoding = PUBKEY_ENC_RAW; |
| 83 |
flags |= PUBKEY_FLAG_RAW_FLAG; /* Explicitly given. */ |
| 84 |
} |
| 85 |
else if (!memcmp (s, "sm2", 3)) |
| 86 |
{ |
| 87 |
encoding = PUBKEY_ENC_RAW; |
| 88 |
flags |= PUBKEY_FLAG_SM2 | PUBKEY_FLAG_RAW_FLAG; |
| 89 |
} |
| 90 |
else if (!igninvflag) |
| 91 |
rc = GPG_ERR_INV_FLAG; |
| 92 |
break; |
| 93 |
|
| 94 |
case 4: |
| 95 |
if (!memcmp (s, "comp", 4)) |
| 96 |
flags |= PUBKEY_FLAG_COMP; |
| 97 |
else if (!memcmp (s, "oaep", 4) && encoding == PUBKEY_ENC_UNKNOWN) |
| 98 |
{ |
| 99 |
encoding = PUBKEY_ENC_OAEP; |
| 100 |
flags |= PUBKEY_FLAG_FIXEDLEN; |
| 101 |
} |
| 102 |
else if (!memcmp (s, "gost", 4)) |
| 103 |
{ |
| 104 |
encoding = PUBKEY_ENC_RAW; |
| 105 |
flags |= PUBKEY_FLAG_GOST; |
| 106 |
} |
| 107 |
else if (!igninvflag) |
| 108 |
rc = GPG_ERR_INV_FLAG; |
| 109 |
break; |
| 110 |
|
| 111 |
case 5: |
| 112 |
if (!memcmp (s, "eddsa", 5)) |
| 113 |
{ |
| 114 |
encoding = PUBKEY_ENC_RAW; |
| 115 |
flags |= PUBKEY_FLAG_EDDSA; |
| 116 |
flags |= PUBKEY_FLAG_DJB_TWEAK; |
| 117 |
} |
| 118 |
else if (!memcmp (s, "pkcs1", 5) && encoding == PUBKEY_ENC_UNKNOWN) |
| 119 |
{ |
| 120 |
encoding = PUBKEY_ENC_PKCS1; |
| 121 |
flags |= PUBKEY_FLAG_FIXEDLEN; |
| 122 |
} |
| 123 |
else if (!memcmp (s, "param", 5)) |
| 124 |
flags |= PUBKEY_FLAG_PARAM; |
| 125 |
else if (!igninvflag) |
| 126 |
rc = GPG_ERR_INV_FLAG; |
| 127 |
break; |
| 128 |
|
| 129 |
case 6: |
| 130 |
if (!memcmp (s, "nocomp", 6)) |
| 131 |
flags |= PUBKEY_FLAG_NOCOMP; |
| 132 |
else if (!igninvflag) |
| 133 |
rc = GPG_ERR_INV_FLAG; |
| 134 |
break; |
| 135 |
|
| 136 |
case 7: |
| 137 |
if (!memcmp (s, "rfc6979", 7)) |
| 138 |
flags |= PUBKEY_FLAG_RFC6979; |
| 139 |
else if (!memcmp (s, "noparam", 7)) |
| 140 |
; /* Ignore - it is the default. */ |
| 141 |
else if (!memcmp (s, "prehash", 7)) |
| 142 |
flags |= PUBKEY_FLAG_PREHASH; |
| 143 |
else if (!igninvflag) |
| 144 |
rc = GPG_ERR_INV_FLAG; |
| 145 |
break; |
| 146 |
|
| 147 |
case 8: |
| 148 |
if (!memcmp (s, "use-x931", 8)) |
| 149 |
flags |= PUBKEY_FLAG_USE_X931; |
| 150 |
else if (!igninvflag) |
| 151 |
rc = GPG_ERR_INV_FLAG; |
| 152 |
break; |
| 153 |
|
| 154 |
case 9: |
| 155 |
if (!memcmp (s, "pkcs1-raw", 9) && encoding == PUBKEY_ENC_UNKNOWN) |
| 156 |
{ |
| 157 |
encoding = PUBKEY_ENC_PKCS1_RAW; |
| 158 |
flags |= PUBKEY_FLAG_FIXEDLEN; |
| 159 |
} |
| 160 |
else if (!memcmp (s, "djb-tweak", 9)) |
| 161 |
{ |
| 162 |
encoding = PUBKEY_ENC_RAW; |
| 163 |
flags |= PUBKEY_FLAG_DJB_TWEAK; |
| 164 |
} |
| 165 |
else if (!memcmp (s, "no-prefix", 9)) |
| 166 |
flags |= PUBKEY_FLAG_NO_PREFIX; |
| 167 |
else if (!igninvflag) |
| 168 |
rc = GPG_ERR_INV_FLAG; |
| 169 |
break; |
| 170 |
|
| 171 |
case 10: |
| 172 |
if (!memcmp (s, "igninvflag", 10)) |
| 173 |
igninvflag = 1; |
| 174 |
else if (!memcmp (s, "no-keytest", 10)) |
| 175 |
flags |= PUBKEY_FLAG_NO_KEYTEST; |
| 176 |
else if (!igninvflag) |
| 177 |
rc = GPG_ERR_INV_FLAG; |
| 178 |
break; |
| 179 |
|
| 180 |
case 11: |
| 181 |
if (!memcmp (s, "no-blinding", 11)) |
| 182 |
flags |= PUBKEY_FLAG_NO_BLINDING; |
| 183 |
else if (!memcmp (s, "use-fips186", 11)) |
| 184 |
flags |= PUBKEY_FLAG_USE_FIPS186; |
| 185 |
else if (!igninvflag) |
| 186 |
rc = GPG_ERR_INV_FLAG; |
| 187 |
break; |
| 188 |
|
| 189 |
case 13: |
| 190 |
if (!memcmp (s, "use-fips186-2", 13)) |
| 191 |
flags |= PUBKEY_FLAG_USE_FIPS186_2; |
| 192 |
else if (!memcmp (s, "transient-key", 13)) |
| 193 |
flags |= PUBKEY_FLAG_TRANSIENT_KEY; |
| 194 |
else if (!igninvflag) |
| 195 |
rc = GPG_ERR_INV_FLAG; |
| 196 |
break; |
| 197 |
|
| 198 |
default: |
| 199 |
if (!igninvflag) |
| 200 |
rc = GPG_ERR_INV_FLAG; |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (fips_mode () && igninvflag) |
| 206 |
{ |
| 207 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_FLAGS)) |
| 208 |
rc = GPG_ERR_INV_FLAG; |
| 209 |
else |
| 210 |
fips_service_indicator_mark_non_compliant (); |
| 211 |
} |
| 212 |
|
| 213 |
if (r_flags) |
| 214 |
*r_flags = flags; |
| 215 |
if (r_encoding) |
| 216 |
*r_encoding = encoding; |
| 217 |
|
| 218 |
return rc; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
static int |
| 223 |
get_hash_algo (const char *s, size_t n) |
| 224 |
{ |
| 225 |
static const struct { const char *name; int algo; } hashnames[] = { |
| 226 |
{ "sha1", GCRY_MD_SHA1 }, |
| 227 |
{ "md5", GCRY_MD_MD5 }, |
| 228 |
{ "sha256", GCRY_MD_SHA256 }, |
| 229 |
{ "ripemd160", GCRY_MD_RMD160 }, |
| 230 |
{ "rmd160", GCRY_MD_RMD160 }, |
| 231 |
{ "sha384", GCRY_MD_SHA384 }, |
| 232 |
{ "sha512", GCRY_MD_SHA512 }, |
| 233 |
{ "sha224", GCRY_MD_SHA224 }, |
| 234 |
{ "md2", GCRY_MD_MD2 }, |
| 235 |
{ "md4", GCRY_MD_MD4 }, |
| 236 |
{ "tiger", GCRY_MD_TIGER }, |
| 237 |
{ "haval", GCRY_MD_HAVAL }, |
| 238 |
{ "sha3-224", GCRY_MD_SHA3_224 }, |
| 239 |
{ "sha3-256", GCRY_MD_SHA3_256 }, |
| 240 |
{ "sha3-384", GCRY_MD_SHA3_384 }, |
| 241 |
{ "sha3-512", GCRY_MD_SHA3_512 }, |
| 242 |
{ "sm3", GCRY_MD_SM3 }, |
| 243 |
{ "shake128", GCRY_MD_SHAKE128 }, |
| 244 |
{ "shake256", GCRY_MD_SHAKE256 }, |
| 245 |
{ "sha512-224", GCRY_MD_SHA512_224 }, |
| 246 |
{ "sha512-256", GCRY_MD_SHA512_256 }, |
| 247 |
{ NULL, 0 } |
| 248 |
}; |
| 249 |
int algo; |
| 250 |
int i; |
| 251 |
|
| 252 |
for (i=0; hashnames[i].name; i++) |
| 253 |
{ |
| 254 |
if ( strlen (hashnames[i].name) == n |
| 255 |
&& !memcmp (hashnames[i].name, s, n)) |
| 256 |
break; |
| 257 |
} |
| 258 |
if (hashnames[i].name) |
| 259 |
algo = hashnames[i].algo; |
| 260 |
else |
| 261 |
{ |
| 262 |
/* In case of not listed or dynamically allocated hash |
| 263 |
algorithm we fall back to this somewhat slower |
| 264 |
method. Further, it also allows using OIDs as |
| 265 |
algorithm names. */ |
| 266 |
char *tmpname; |
| 267 |
|
| 268 |
tmpname = xtrymalloc (n+1); |
| 269 |
if (!tmpname) |
| 270 |
algo = 0; /* Out of core - silently give up. */ |
| 271 |
else |
| 272 |
{ |
| 273 |
memcpy (tmpname, s, n); |
| 274 |
tmpname[n] = 0; |
| 275 |
algo = _gcry_md_map_name (tmpname); |
| 276 |
xfree (tmpname); |
| 277 |
} |
| 278 |
} |
| 279 |
return algo; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/* Get the "nbits" parameter from an s-expression of the format: |
| 284 |
* |
| 285 |
* (algo |
| 286 |
* (parameter_name_1 ....) |
| 287 |
* .... |
| 288 |
* (parameter_name_n ....)) |
| 289 |
* |
| 290 |
* Example: |
| 291 |
* |
| 292 |
* (rsa |
| 293 |
* (nbits 4:2048)) |
| 294 |
* |
| 295 |
* On success the value for nbits is stored at R_NBITS. If no nbits |
| 296 |
* parameter is found, the function returns success and stores 0 at |
| 297 |
* R_NBITS. For parsing errors the function returns an error code and |
| 298 |
* stores 0 at R_NBITS. |
| 299 |
*/ |
| 300 |
gpg_err_code_t |
| 301 |
_gcry_pk_util_get_nbits (gcry_sexp_t list, unsigned int *r_nbits) |
| 302 |
{ |
| 303 |
char buf[50]; |
| 304 |
const char *s; |
| 305 |
size_t n; |
| 306 |
|
| 307 |
*r_nbits = 0; |
| 308 |
|
| 309 |
list = sexp_find_token (list, "nbits", 0); |
| 310 |
if (!list) |
| 311 |
return 0; /* No NBITS found. */ |
| 312 |
|
| 313 |
s = sexp_nth_data (list, 1, &n); |
| 314 |
if (!s || n >= DIM (buf) - 1 ) |
| 315 |
{ |
| 316 |
/* NBITS given without a cdr. */ |
| 317 |
sexp_release (list); |
| 318 |
return GPG_ERR_INV_OBJ; |
| 319 |
} |
| 320 |
memcpy (buf, s, n); |
| 321 |
buf[n] = 0; |
| 322 |
*r_nbits = (unsigned int)strtoul (buf, NULL, 0); |
| 323 |
sexp_release (list); |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
/* Get the optional "rsa-use-e" parameter from an s-expression of the |
| 329 |
* format: |
| 330 |
* |
| 331 |
* (algo |
| 332 |
* (parameter_name_1 ....) |
| 333 |
* .... |
| 334 |
* (parameter_name_n ....)) |
| 335 |
* |
| 336 |
* Example: |
| 337 |
* |
| 338 |
* (rsa |
| 339 |
* (nbits 4:2048) |
| 340 |
* (rsa-use-e 2:41)) |
| 341 |
* |
| 342 |
* On success the value for nbits is stored at R_E. If no rsa-use-e |
| 343 |
* parameter is found, the function returns success and stores 65537 at |
| 344 |
* R_E. For parsing errors the function returns an error code and |
| 345 |
* stores 0 at R_E. |
| 346 |
*/ |
| 347 |
gpg_err_code_t |
| 348 |
_gcry_pk_util_get_rsa_use_e (gcry_sexp_t list, unsigned long *r_e) |
| 349 |
{ |
| 350 |
char buf[50]; |
| 351 |
const char *s; |
| 352 |
size_t n; |
| 353 |
|
| 354 |
*r_e = 0; |
| 355 |
|
| 356 |
list = sexp_find_token (list, "rsa-use-e", 0); |
| 357 |
if (!list) |
| 358 |
{ |
| 359 |
*r_e = 65537; /* Not given, use the value generated by old versions. */ |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
s = sexp_nth_data (list, 1, &n); |
| 364 |
if (!s || n >= DIM (buf) - 1 ) |
| 365 |
{ |
| 366 |
/* No value or value too large. */ |
| 367 |
sexp_release (list); |
| 368 |
return GPG_ERR_INV_OBJ; |
| 369 |
} |
| 370 |
memcpy (buf, s, n); |
| 371 |
buf[n] = 0; |
| 372 |
*r_e = strtoul (buf, NULL, 0); |
| 373 |
sexp_release (list); |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
/* Parse a "sig-val" s-expression and store the inner parameter list at |
| 379 |
R_PARMS. ALGO_NAMES is used to verify that the algorithm in |
| 380 |
"sig-val" is valid. Returns 0 on success and stores a new list at |
| 381 |
R_PARMS which must be freed by the caller. On error R_PARMS is set |
| 382 |
to NULL and an error code returned. If R_ECCFLAGS is not NULL flag |
| 383 |
values are set into it; as of now they are only used with ecc |
| 384 |
algorithms. */ |
| 385 |
gpg_err_code_t |
| 386 |
_gcry_pk_util_preparse_sigval (gcry_sexp_t s_sig, const char **algo_names, |
| 387 |
gcry_sexp_t *r_parms, int *r_eccflags) |
| 388 |
{ |
| 389 |
gpg_err_code_t rc; |
| 390 |
gcry_sexp_t l1 = NULL; |
| 391 |
gcry_sexp_t l2 = NULL; |
| 392 |
char *name = NULL; |
| 393 |
int i; |
| 394 |
|
| 395 |
*r_parms = NULL; |
| 396 |
if (r_eccflags) |
| 397 |
*r_eccflags = 0; |
| 398 |
|
| 399 |
/* Extract the signature value. */ |
| 400 |
l1 = sexp_find_token (s_sig, "sig-val", 0); |
| 401 |
if (!l1) |
| 402 |
{ |
| 403 |
rc = GPG_ERR_INV_OBJ; /* Does not contain a signature value object. */ |
| 404 |
goto leave; |
| 405 |
} |
| 406 |
|
| 407 |
l2 = sexp_nth (l1, 1); |
| 408 |
if (!l2) |
| 409 |
{ |
| 410 |
rc = GPG_ERR_NO_OBJ; /* No cadr for the sig object. */ |
| 411 |
goto leave; |
| 412 |
} |
| 413 |
name = sexp_nth_string (l2, 0); |
| 414 |
if (!name) |
| 415 |
{ |
| 416 |
rc = GPG_ERR_INV_OBJ; /* Invalid structure of object. */ |
| 417 |
goto leave; |
| 418 |
} |
| 419 |
else if (!strcmp (name, "flags")) |
| 420 |
{ |
| 421 |
/* Skip a "flags" parameter and look again for the algorithm |
| 422 |
name. This is not used but here just for the sake of |
| 423 |
consistent S-expressions we need to handle it. */ |
| 424 |
sexp_release (l2); |
| 425 |
l2 = sexp_nth (l1, 2); |
| 426 |
if (!l2) |
| 427 |
{ |
| 428 |
rc = GPG_ERR_INV_OBJ; |
| 429 |
goto leave; |
| 430 |
} |
| 431 |
xfree (name); |
| 432 |
name = sexp_nth_string (l2, 0); |
| 433 |
if (!name) |
| 434 |
{ |
| 435 |
rc = GPG_ERR_INV_OBJ; /* Invalid structure of object. */ |
| 436 |
goto leave; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
for (i=0; algo_names[i]; i++) |
| 441 |
if (!stricmp (name, algo_names[i])) |
| 442 |
break; |
| 443 |
if (!algo_names[i]) |
| 444 |
{ |
| 445 |
rc = GPG_ERR_CONFLICT; /* "sig-val" uses an unexpected algo. */ |
| 446 |
goto leave; |
| 447 |
} |
| 448 |
if (r_eccflags) |
| 449 |
{ |
| 450 |
if (!strcmp (name, "eddsa")) |
| 451 |
*r_eccflags = PUBKEY_FLAG_EDDSA; |
| 452 |
if (!strcmp (name, "gost")) |
| 453 |
*r_eccflags = PUBKEY_FLAG_GOST; |
| 454 |
if (!strcmp (name, "sm2")) |
| 455 |
*r_eccflags = PUBKEY_FLAG_SM2; |
| 456 |
} |
| 457 |
|
| 458 |
*r_parms = l2; |
| 459 |
l2 = NULL; |
| 460 |
rc = 0; |
| 461 |
|
| 462 |
leave: |
| 463 |
xfree (name); |
| 464 |
sexp_release (l2); |
| 465 |
sexp_release (l1); |
| 466 |
return rc; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
/* Parse a "enc-val" s-expression and store the inner parameter list |
| 471 |
at R_PARMS. ALGO_NAMES is used to verify that the algorithm in |
| 472 |
"enc-val" is valid. Returns 0 on success and stores a new list at |
| 473 |
R_PARMS which must be freed by the caller. On error R_PARMS is set |
| 474 |
to NULL and an error code returned. If R_ECCFLAGS is not NULL flag |
| 475 |
values are set into it; as of now they are only used with ecc |
| 476 |
algorithms. |
| 477 |
|
| 478 |
(enc-val |
| 479 |
[(flags [raw, pkcs1, oaep, no-blinding])] |
| 480 |
[(hash-algo <algo>)] |
| 481 |
[(label <label>)] |
| 482 |
(<algo> |
| 483 |
(<param_name1> <mpi>) |
| 484 |
... |
| 485 |
(<param_namen> <mpi>))) |
| 486 |
|
| 487 |
HASH-ALGO and LABEL are specific to OAEP. CTX will be updated with |
| 488 |
encoding information. */ |
| 489 |
gpg_err_code_t |
| 490 |
_gcry_pk_util_preparse_encval (gcry_sexp_t sexp, const char **algo_names, |
| 491 |
gcry_sexp_t *r_parms, |
| 492 |
struct pk_encoding_ctx *ctx) |
| 493 |
{ |
| 494 |
gcry_err_code_t rc = 0; |
| 495 |
gcry_sexp_t l1 = NULL; |
| 496 |
gcry_sexp_t l2 = NULL; |
| 497 |
char *name = NULL; |
| 498 |
size_t n; |
| 499 |
int parsed_flags = 0; |
| 500 |
int i; |
| 501 |
|
| 502 |
*r_parms = NULL; |
| 503 |
|
| 504 |
/* Check that the first element is valid. */ |
| 505 |
l1 = sexp_find_token (sexp, "enc-val" , 0); |
| 506 |
if (!l1) |
| 507 |
{ |
| 508 |
rc = GPG_ERR_INV_OBJ; /* Does not contain an encrypted value object. */ |
| 509 |
goto leave; |
| 510 |
} |
| 511 |
|
| 512 |
l2 = sexp_nth (l1, 1); |
| 513 |
if (!l2) |
| 514 |
{ |
| 515 |
rc = GPG_ERR_NO_OBJ; /* No cadr for the data object. */ |
| 516 |
goto leave; |
| 517 |
} |
| 518 |
|
| 519 |
/* Extract identifier of sublist. */ |
| 520 |
name = sexp_nth_string (l2, 0); |
| 521 |
if (!name) |
| 522 |
{ |
| 523 |
rc = GPG_ERR_INV_OBJ; /* Invalid structure of object. */ |
| 524 |
goto leave; |
| 525 |
} |
| 526 |
|
| 527 |
if (!strcmp (name, "flags")) |
| 528 |
{ |
| 529 |
const char *s; |
| 530 |
|
| 531 |
/* There is a flags element - process it. */ |
| 532 |
rc = _gcry_pk_util_parse_flaglist (l2, &parsed_flags, &ctx->encoding); |
| 533 |
if (rc) |
| 534 |
goto leave; |
| 535 |
if (ctx->encoding == PUBKEY_ENC_PSS) |
| 536 |
{ |
| 537 |
rc = GPG_ERR_CONFLICT; |
| 538 |
goto leave; |
| 539 |
} |
| 540 |
|
| 541 |
/* Get the OAEP parameters HASH-ALGO and LABEL, if any. */ |
| 542 |
if (ctx->encoding == PUBKEY_ENC_OAEP) |
| 543 |
{ |
| 544 |
/* Get HASH-ALGO. */ |
| 545 |
sexp_release (l2); |
| 546 |
l2 = sexp_find_token (l1, "hash-algo", 0); |
| 547 |
if (l2) |
| 548 |
{ |
| 549 |
s = sexp_nth_data (l2, 1, &n); |
| 550 |
if (!s) |
| 551 |
rc = GPG_ERR_NO_OBJ; |
| 552 |
else |
| 553 |
{ |
| 554 |
ctx->hash_algo = get_hash_algo (s, n); |
| 555 |
if (!ctx->hash_algo) |
| 556 |
rc = GPG_ERR_DIGEST_ALGO; |
| 557 |
} |
| 558 |
if (rc) |
| 559 |
goto leave; |
| 560 |
} |
| 561 |
|
| 562 |
/* Get LABEL. */ |
| 563 |
sexp_release (l2); |
| 564 |
l2 = sexp_find_token (l1, "label", 0); |
| 565 |
if (l2) |
| 566 |
{ |
| 567 |
s = sexp_nth_data (l2, 1, &n); |
| 568 |
if (!s) |
| 569 |
rc = GPG_ERR_NO_OBJ; |
| 570 |
else if (n > 0) |
| 571 |
{ |
| 572 |
ctx->label = xtrymalloc (n); |
| 573 |
if (!ctx->label) |
| 574 |
rc = gpg_err_code_from_syserror (); |
| 575 |
else |
| 576 |
{ |
| 577 |
memcpy (ctx->label, s, n); |
| 578 |
ctx->labellen = n; |
| 579 |
} |
| 580 |
} |
| 581 |
if (rc) |
| 582 |
goto leave; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
/* Get the next which has the actual data - skip HASH-ALGO and LABEL. */ |
| 587 |
for (i = 2; (sexp_release (l2), l2 = sexp_nth (l1, i)); i++) |
| 588 |
{ |
| 589 |
s = sexp_nth_data (l2, 0, &n); |
| 590 |
if (!(n == 9 && !memcmp (s, "hash-algo", 9)) |
| 591 |
&& !(n == 5 && !memcmp (s, "label", 5)) |
| 592 |
&& !(n == 15 && !memcmp (s, "random-override", 15))) |
| 593 |
break; |
| 594 |
} |
| 595 |
if (!l2) |
| 596 |
{ |
| 597 |
rc = GPG_ERR_NO_OBJ; /* No cadr for the data object. */ |
| 598 |
goto leave; |
| 599 |
} |
| 600 |
|
| 601 |
/* Extract sublist identifier. */ |
| 602 |
xfree (name); |
| 603 |
name = sexp_nth_string (l2, 0); |
| 604 |
if (!name) |
| 605 |
{ |
| 606 |
rc = GPG_ERR_INV_OBJ; /* Invalid structure of object. */ |
| 607 |
goto leave; |
| 608 |
} |
| 609 |
} |
| 610 |
else /* No flags - flag as legacy structure. */ |
| 611 |
parsed_flags |= PUBKEY_FLAG_LEGACYRESULT; |
| 612 |
|
| 613 |
for (i=0; algo_names[i]; i++) |
| 614 |
if (!stricmp (name, algo_names[i])) |
| 615 |
break; |
| 616 |
if (!algo_names[i]) |
| 617 |
{ |
| 618 |
rc = GPG_ERR_CONFLICT; /* "enc-val" uses an unexpected algo. */ |
| 619 |
goto leave; |
| 620 |
} |
| 621 |
|
| 622 |
*r_parms = l2; |
| 623 |
l2 = NULL; |
| 624 |
ctx->flags |= parsed_flags; |
| 625 |
rc = 0; |
| 626 |
|
| 627 |
leave: |
| 628 |
xfree (name); |
| 629 |
sexp_release (l2); |
| 630 |
sexp_release (l1); |
| 631 |
return rc; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
/* Initialize an encoding context. */ |
| 636 |
void |
| 637 |
_gcry_pk_util_init_encoding_ctx (struct pk_encoding_ctx *ctx, |
| 638 |
enum pk_operation op, |
| 639 |
unsigned int nbits) |
| 640 |
{ |
| 641 |
ctx->op = op; |
| 642 |
ctx->nbits = nbits; |
| 643 |
ctx->encoding = PUBKEY_ENC_UNKNOWN; |
| 644 |
ctx->flags = 0; |
| 645 |
if (fips_mode ()) |
| 646 |
{ |
| 647 |
ctx->hash_algo = GCRY_MD_SHA256; |
| 648 |
} |
| 649 |
else |
| 650 |
{ |
| 651 |
ctx->hash_algo = GCRY_MD_SHA1; |
| 652 |
} |
| 653 |
ctx->label = NULL; |
| 654 |
ctx->labellen = 0; |
| 655 |
ctx->rnd = NULL; |
| 656 |
ctx->rndlen = 0; |
| 657 |
ctx->saltlen = 20; |
| 658 |
ctx->verify_cmp = NULL; |
| 659 |
ctx->verify_arg = NULL; |
| 660 |
} |
| 661 |
|
| 662 |
/* Free a context initialzied by _gcry_pk_util_init_encoding_ctx. */ |
| 663 |
void |
| 664 |
_gcry_pk_util_free_encoding_ctx (struct pk_encoding_ctx *ctx) |
| 665 |
{ |
| 666 |
xfree (ctx->label); |
| 667 |
xfree (ctx->rnd); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
/* Take the hash value and convert into an MPI, suitable for |
| 672 |
passing to the low level functions. We currently support the |
| 673 |
old style way of passing just a MPI and the modern interface which |
| 674 |
allows passing flags so that we can choose between raw and pkcs1 |
| 675 |
padding - may be more padding options later. |
| 676 |
|
| 677 |
(<mpi>) |
| 678 |
or |
| 679 |
(data |
| 680 |
[(flags [raw, direct, pkcs1, oaep, pss, |
| 681 |
no-blinding, rfc6979, eddsa, prehash])] |
| 682 |
[(hash <algo> <value>)] |
| 683 |
[(value <text>)] |
| 684 |
[(hash-algo <algo>)] |
| 685 |
[(label <label>)] |
| 686 |
[(salt-length <length>)] |
| 687 |
[(random-override <data>)] |
| 688 |
) |
| 689 |
|
| 690 |
Either the VALUE or the HASH element must be present for use |
| 691 |
with signatures. VALUE is used for encryption. |
| 692 |
|
| 693 |
HASH-ALGO is specific to OAEP, PSS and EDDSA. |
| 694 |
|
| 695 |
LABEL is used for OAEP, RAW or RFC6979. |
| 696 |
|
| 697 |
SALT-LENGTH is for PSS, it is limited to 16384 bytes. |
| 698 |
|
| 699 |
RANDOM-OVERRIDE is used to replace random nonces for regression |
| 700 |
testing. */ |
| 701 |
gcry_err_code_t |
| 702 |
_gcry_pk_util_data_to_mpi (gcry_sexp_t input, gcry_mpi_t *ret_mpi, |
| 703 |
struct pk_encoding_ctx *ctx) |
| 704 |
{ |
| 705 |
gcry_err_code_t rc = 0; |
| 706 |
gcry_sexp_t ldata, lhash, lvalue; |
| 707 |
size_t n; |
| 708 |
const char *s; |
| 709 |
int unknown_flag = 0; |
| 710 |
int parsed_flags = 0; |
| 711 |
|
| 712 |
*ret_mpi = NULL; |
| 713 |
ldata = sexp_find_token (input, "data", 0); |
| 714 |
if (!ldata) |
| 715 |
{ /* assume old style */ |
| 716 |
int mpifmt = (ctx->flags & PUBKEY_FLAG_RAW_FLAG) ? |
| 717 |
GCRYMPI_FMT_OPAQUE : GCRYMPI_FMT_STD; |
| 718 |
|
| 719 |
*ret_mpi = sexp_nth_mpi (input, 0, mpifmt); |
| 720 |
return *ret_mpi ? GPG_ERR_NO_ERROR : GPG_ERR_INV_OBJ; |
| 721 |
} |
| 722 |
|
| 723 |
/* See whether there is a flags list. */ |
| 724 |
{ |
| 725 |
gcry_sexp_t lflags = sexp_find_token (ldata, "flags", 0); |
| 726 |
if (lflags) |
| 727 |
{ |
| 728 |
if (_gcry_pk_util_parse_flaglist (lflags, |
| 729 |
&parsed_flags, &ctx->encoding)) |
| 730 |
unknown_flag = 1; |
| 731 |
sexp_release (lflags); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
if (ctx->encoding == PUBKEY_ENC_UNKNOWN) |
| 736 |
ctx->encoding = PUBKEY_ENC_RAW; /* default to raw */ |
| 737 |
|
| 738 |
/* Get HASH or MPI */ |
| 739 |
lhash = sexp_find_token (ldata, "hash", 0); |
| 740 |
lvalue = lhash? NULL : sexp_find_token (ldata, "value", 0); |
| 741 |
|
| 742 |
if (!(!lhash ^ !lvalue)) |
| 743 |
rc = GPG_ERR_INV_OBJ; /* none or both given */ |
| 744 |
else if (unknown_flag) |
| 745 |
rc = GPG_ERR_INV_FLAG; |
| 746 |
else if (ctx->encoding == PUBKEY_ENC_RAW |
| 747 |
&& (ctx->flags & PUBKEY_FLAG_BYTE_STRING)) |
| 748 |
{ |
| 749 |
gcry_sexp_t list; |
| 750 |
void *value; |
| 751 |
size_t valuelen; |
| 752 |
|
| 753 |
if (!lvalue) |
| 754 |
{ |
| 755 |
rc = GPG_ERR_INV_OBJ; |
| 756 |
goto leave; |
| 757 |
} |
| 758 |
|
| 759 |
/* Get optional LABEL. */ |
| 760 |
list = sexp_find_token (ldata, "label", 0); |
| 761 |
if (list) |
| 762 |
{ |
| 763 |
ctx->label = sexp_nth_buffer (list, 1, &ctx->labellen); |
| 764 |
sexp_release (list); |
| 765 |
} |
| 766 |
|
| 767 |
/* Get optional RANDOM-OVERRIDE. */ |
| 768 |
list = sexp_find_token (ldata, "random-override", 0); |
| 769 |
if (list) |
| 770 |
{ |
| 771 |
ctx->rnd = sexp_nth_buffer (list, 1, &ctx->rndlen); |
| 772 |
sexp_release (list); |
| 773 |
} |
| 774 |
|
| 775 |
/* Get VALUE. */ |
| 776 |
value = sexp_nth_buffer (lvalue, 1, &valuelen); |
| 777 |
if (!value) |
| 778 |
{ |
| 779 |
rc = GPG_ERR_INV_OBJ; |
| 780 |
goto leave; |
| 781 |
} |
| 782 |
|
| 783 |
/* Note that mpi_set_opaque takes ownership of VALUE. */ |
| 784 |
*ret_mpi = mpi_set_opaque (NULL, value, valuelen*8); |
| 785 |
} |
| 786 |
else if (ctx->encoding == PUBKEY_ENC_RAW |
| 787 |
&& ((parsed_flags & PUBKEY_FLAG_EDDSA) |
| 788 |
|| (ctx->flags & PUBKEY_FLAG_EDDSA))) |
| 789 |
{ |
| 790 |
/* Prepare for EdDSA. */ |
| 791 |
gcry_sexp_t list; |
| 792 |
void *value; |
| 793 |
size_t valuelen; |
| 794 |
|
| 795 |
if (!lvalue) |
| 796 |
{ |
| 797 |
rc = GPG_ERR_INV_OBJ; |
| 798 |
goto leave; |
| 799 |
} |
| 800 |
/* Hash algo is determined by curve. No hash-algo is OK. */ |
| 801 |
/* Get HASH-ALGO. */ |
| 802 |
list = sexp_find_token (ldata, "hash-algo", 0); |
| 803 |
if (list) |
| 804 |
{ |
| 805 |
s = sexp_nth_data (list, 1, &n); |
| 806 |
if (!s) |
| 807 |
rc = GPG_ERR_NO_OBJ; |
| 808 |
else |
| 809 |
{ |
| 810 |
ctx->hash_algo = get_hash_algo (s, n); |
| 811 |
if (!ctx->hash_algo) |
| 812 |
rc = GPG_ERR_DIGEST_ALGO; |
| 813 |
} |
| 814 |
sexp_release (list); |
| 815 |
} |
| 816 |
if (rc) |
| 817 |
goto leave; |
| 818 |
|
| 819 |
/* Get LABEL. */ |
| 820 |
list = sexp_find_token (ldata, "label", 0); |
| 821 |
if (list) |
| 822 |
{ |
| 823 |
s = sexp_nth_data (list, 1, &n); |
| 824 |
if (!s) |
| 825 |
rc = GPG_ERR_NO_OBJ; |
| 826 |
else if (n > 0) |
| 827 |
{ |
| 828 |
ctx->label = xtrymalloc (n); |
| 829 |
if (!ctx->label) |
| 830 |
rc = gpg_err_code_from_syserror (); |
| 831 |
else |
| 832 |
{ |
| 833 |
memcpy (ctx->label, s, n); |
| 834 |
ctx->labellen = n; |
| 835 |
} |
| 836 |
} |
| 837 |
sexp_release (list); |
| 838 |
if (rc) |
| 839 |
goto leave; |
| 840 |
} |
| 841 |
|
| 842 |
/* Get VALUE. */ |
| 843 |
value = sexp_nth_buffer (lvalue, 1, &valuelen); |
| 844 |
if (!value) |
| 845 |
{ |
| 846 |
/* We assume that a zero length message is meant by |
| 847 |
"(value)". This is commonly used by test vectors. Note |
| 848 |
that S-expression do not allow zero length items. */ |
| 849 |
valuelen = 0; |
| 850 |
value = xtrymalloc (1); |
| 851 |
if (!value) |
| 852 |
rc = gpg_err_code_from_syserror (); |
| 853 |
} |
| 854 |
else if ((valuelen * 8) < valuelen) |
| 855 |
{ |
| 856 |
xfree (value); |
| 857 |
rc = GPG_ERR_TOO_LARGE; |
| 858 |
} |
| 859 |
if (rc) |
| 860 |
goto leave; |
| 861 |
|
| 862 |
/* Note that mpi_set_opaque takes ownership of VALUE. */ |
| 863 |
*ret_mpi = mpi_set_opaque (NULL, value, valuelen*8); |
| 864 |
} |
| 865 |
else if (ctx->encoding == PUBKEY_ENC_RAW |
| 866 |
&& (lhash || (lvalue && (parsed_flags & PUBKEY_FLAG_PREHASH))) |
| 867 |
&& ((parsed_flags & PUBKEY_FLAG_RAW_FLAG) |
| 868 |
|| (parsed_flags & PUBKEY_FLAG_RFC6979))) |
| 869 |
{ |
| 870 |
void * value; |
| 871 |
size_t valuelen; |
| 872 |
gcry_sexp_t list; |
| 873 |
|
| 874 |
/* Raw encoding along with a hash element. This is commonly |
| 875 |
used for DSA. For better backward error compatibility we |
| 876 |
allow this only if either the rfc6979 flag has been given or |
| 877 |
the raw flags was explicitly given. */ |
| 878 |
|
| 879 |
if (lvalue && (parsed_flags & PUBKEY_FLAG_PREHASH)) |
| 880 |
{ |
| 881 |
/* Get HASH-ALGO. */ |
| 882 |
list = sexp_find_token (ldata, "hash-algo", 0); |
| 883 |
if (list) |
| 884 |
{ |
| 885 |
s = sexp_nth_data (list, 1, &n); |
| 886 |
if (!s) |
| 887 |
rc = GPG_ERR_NO_OBJ; |
| 888 |
else |
| 889 |
{ |
| 890 |
ctx->hash_algo = get_hash_algo (s, n); |
| 891 |
if (!ctx->hash_algo) |
| 892 |
rc = GPG_ERR_DIGEST_ALGO; |
| 893 |
} |
| 894 |
sexp_release (list); |
| 895 |
if (rc) |
| 896 |
goto leave; |
| 897 |
} |
| 898 |
|
| 899 |
/* Get optional LABEL. */ |
| 900 |
list = sexp_find_token (ldata, "label", 0); |
| 901 |
if (list) |
| 902 |
{ |
| 903 |
s = sexp_nth_data (list, 1, &n); |
| 904 |
if (!s) |
| 905 |
rc = GPG_ERR_NO_OBJ; |
| 906 |
else if (n > 0) |
| 907 |
{ |
| 908 |
ctx->label = xtrymalloc (n); |
| 909 |
if (!ctx->label) |
| 910 |
rc = gpg_err_code_from_syserror (); |
| 911 |
else |
| 912 |
{ |
| 913 |
memcpy (ctx->label, s, n); |
| 914 |
ctx->labellen = n; |
| 915 |
} |
| 916 |
} |
| 917 |
else |
| 918 |
rc = GPG_ERR_INV_ARG; |
| 919 |
sexp_release (list); |
| 920 |
if (rc) |
| 921 |
goto leave; |
| 922 |
} |
| 923 |
|
| 924 |
if ( !(value=sexp_nth_buffer (lvalue, 1, &valuelen)) || !valuelen ) |
| 925 |
rc = GPG_ERR_INV_OBJ; |
| 926 |
else if ((valuelen * 8) < valuelen) |
| 927 |
{ |
| 928 |
xfree (value); |
| 929 |
rc = GPG_ERR_TOO_LARGE; |
| 930 |
} |
| 931 |
else |
| 932 |
*ret_mpi = mpi_set_opaque (NULL, value, valuelen*8); |
| 933 |
} |
| 934 |
else if (lhash) |
| 935 |
{ |
| 936 |
/* Get optional LABEL. */ |
| 937 |
list = sexp_find_token (ldata, "label", 0); |
| 938 |
if (list) |
| 939 |
{ |
| 940 |
s = sexp_nth_data (list, 1, &n); |
| 941 |
if (!s) |
| 942 |
rc = GPG_ERR_NO_OBJ; |
| 943 |
else if (n > 0) |
| 944 |
{ |
| 945 |
ctx->label = xtrymalloc (n); |
| 946 |
if (!ctx->label) |
| 947 |
rc = gpg_err_code_from_syserror (); |
| 948 |
else |
| 949 |
{ |
| 950 |
memcpy (ctx->label, s, n); |
| 951 |
ctx->labellen = n; |
| 952 |
} |
| 953 |
} |
| 954 |
else |
| 955 |
rc = GPG_ERR_INV_ARG; |
| 956 |
sexp_release (list); |
| 957 |
if (rc) |
| 958 |
goto leave; |
| 959 |
} |
| 960 |
|
| 961 |
if (sexp_length (lhash) != 3) |
| 962 |
rc = GPG_ERR_INV_OBJ; |
| 963 |
else if ( !(s=sexp_nth_data (lhash, 1, &n)) || !n ) |
| 964 |
rc = GPG_ERR_INV_OBJ; |
| 965 |
else |
| 966 |
{ |
| 967 |
ctx->hash_algo = get_hash_algo (s, n); |
| 968 |
|
| 969 |
if (!ctx->hash_algo) |
| 970 |
rc = GPG_ERR_DIGEST_ALGO; |
| 971 |
else if ( !(value=sexp_nth_buffer (lhash, 2, &valuelen)) |
| 972 |
|| !valuelen ) |
| 973 |
rc = GPG_ERR_INV_OBJ; |
| 974 |
else if ((valuelen * 8) < valuelen) |
| 975 |
{ |
| 976 |
xfree (value); |
| 977 |
rc = GPG_ERR_TOO_LARGE; |
| 978 |
} |
| 979 |
else |
| 980 |
*ret_mpi = mpi_set_opaque (NULL, value, valuelen*8); |
| 981 |
} |
| 982 |
} |
| 983 |
else |
| 984 |
rc = GPG_ERR_CONFLICT; |
| 985 |
|
| 986 |
if (rc) |
| 987 |
goto leave; |
| 988 |
} |
| 989 |
else if (ctx->encoding == PUBKEY_ENC_RAW && lvalue) |
| 990 |
{ |
| 991 |
/* RFC6979 may only be used with the a hash value and not the |
| 992 |
MPI based value. */ |
| 993 |
if (parsed_flags & PUBKEY_FLAG_RFC6979) |
| 994 |
{ |
| 995 |
rc = GPG_ERR_CONFLICT; |
| 996 |
goto leave; |
| 997 |
} |
| 998 |
|
| 999 |
/* Get the value */ |
| 1000 |
*ret_mpi = sexp_nth_mpi (lvalue, 1, GCRYMPI_FMT_USG); |
| 1001 |
if (!*ret_mpi) |
| 1002 |
rc = GPG_ERR_INV_OBJ; |
| 1003 |
} |
| 1004 |
else if (ctx->encoding == PUBKEY_ENC_PKCS1 && lvalue |
| 1005 |
&& ctx->op == PUBKEY_OP_ENCRYPT) |
| 1006 |
{ |
| 1007 |
const void * value; |
| 1008 |
size_t valuelen; |
| 1009 |
gcry_sexp_t list; |
| 1010 |
void *random_override = NULL; |
| 1011 |
size_t random_override_len = 0; |
| 1012 |
|
| 1013 |
if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen ) |
| 1014 |
rc = GPG_ERR_INV_OBJ; |
| 1015 |
else |
| 1016 |
{ |
| 1017 |
/* Get optional RANDOM-OVERRIDE. */ |
| 1018 |
list = sexp_find_token (ldata, "random-override", 0); |
| 1019 |
if (list) |
| 1020 |
{ |
| 1021 |
if (fips_mode ()) |
| 1022 |
{ |
| 1023 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_FLAGS)) |
| 1024 |
{ |
| 1025 |
sexp_release (list); |
| 1026 |
rc = GPG_ERR_INV_FLAG; |
| 1027 |
goto leave; |
| 1028 |
} |
| 1029 |
else |
| 1030 |
fips_service_indicator_mark_non_compliant (); |
| 1031 |
} |
| 1032 |
s = sexp_nth_data (list, 1, &n); |
| 1033 |
if (!s) |
| 1034 |
rc = GPG_ERR_NO_OBJ; |
| 1035 |
else if (n > 0) |
| 1036 |
{ |
| 1037 |
random_override = xtrymalloc (n); |
| 1038 |
if (!random_override) |
| 1039 |
rc = gpg_err_code_from_syserror (); |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
memcpy (random_override, s, n); |
| 1043 |
random_override_len = n; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
sexp_release (list); |
| 1047 |
if (rc) |
| 1048 |
goto leave; |
| 1049 |
} |
| 1050 |
|
| 1051 |
rc = _gcry_rsa_pkcs1_encode_for_enc (ret_mpi, ctx->nbits, |
| 1052 |
value, valuelen, |
| 1053 |
random_override, |
| 1054 |
random_override_len); |
| 1055 |
xfree (random_override); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
else if (ctx->encoding == PUBKEY_ENC_PKCS1 |
| 1059 |
&& (lhash || (lvalue && (parsed_flags & PUBKEY_FLAG_PREHASH))) |
| 1060 |
&& (ctx->op == PUBKEY_OP_SIGN || ctx->op == PUBKEY_OP_VERIFY)) |
| 1061 |
{ |
| 1062 |
if (lvalue && (parsed_flags & PUBKEY_FLAG_PREHASH)) |
| 1063 |
{ |
| 1064 |
void * value; |
| 1065 |
size_t valuelen; |
| 1066 |
gcry_sexp_t list; |
| 1067 |
|
| 1068 |
/* Get HASH-ALGO. */ |
| 1069 |
list = sexp_find_token (ldata, "hash-algo", 0); |
| 1070 |
if (list) |
| 1071 |
{ |
| 1072 |
s = sexp_nth_data (list, 1, &n); |
| 1073 |
if (!s) |
| 1074 |
rc = GPG_ERR_NO_OBJ; |
| 1075 |
else |
| 1076 |
{ |
| 1077 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1078 |
if (!ctx->hash_algo) |
| 1079 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1080 |
} |
| 1081 |
sexp_release (list); |
| 1082 |
if (rc) |
| 1083 |
goto leave; |
| 1084 |
} |
| 1085 |
|
| 1086 |
if ( !(value=sexp_nth_buffer (lvalue, 1, &valuelen)) || !valuelen ) |
| 1087 |
rc = GPG_ERR_INV_OBJ; |
| 1088 |
else if ((valuelen * 8) < valuelen) |
| 1089 |
{ |
| 1090 |
xfree (value); |
| 1091 |
rc = GPG_ERR_TOO_LARGE; |
| 1092 |
} |
| 1093 |
else |
| 1094 |
{ |
| 1095 |
void *hash; |
| 1096 |
|
| 1097 |
n = _gcry_md_get_algo_dlen (ctx->hash_algo); |
| 1098 |
hash = xtrymalloc (n); |
| 1099 |
if (!hash) |
| 1100 |
rc = gpg_err_code_from_syserror (); |
| 1101 |
else |
| 1102 |
{ |
| 1103 |
_gcry_md_hash_buffer (ctx->hash_algo, hash, value, valuelen); |
| 1104 |
rc = _gcry_rsa_pkcs1_encode_for_sig (ret_mpi, ctx->nbits, |
| 1105 |
hash, n, ctx->hash_algo); |
| 1106 |
xfree (hash); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
else if (lhash) |
| 1111 |
{ |
| 1112 |
if (sexp_length (lhash) != 3) |
| 1113 |
rc = GPG_ERR_INV_OBJ; |
| 1114 |
else if ( !(s=sexp_nth_data (lhash, 1, &n)) || !n ) |
| 1115 |
rc = GPG_ERR_INV_OBJ; |
| 1116 |
else |
| 1117 |
{ |
| 1118 |
const void * value; |
| 1119 |
size_t valuelen; |
| 1120 |
|
| 1121 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1122 |
|
| 1123 |
if (!ctx->hash_algo) |
| 1124 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1125 |
else if ( !(value=sexp_nth_data (lhash, 2, &valuelen)) |
| 1126 |
|| !valuelen ) |
| 1127 |
rc = GPG_ERR_INV_OBJ; |
| 1128 |
else |
| 1129 |
rc = _gcry_rsa_pkcs1_encode_for_sig (ret_mpi, ctx->nbits, |
| 1130 |
value, valuelen, |
| 1131 |
ctx->hash_algo); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
} |
| 1135 |
else if (ctx->encoding == PUBKEY_ENC_PKCS1_RAW && lvalue |
| 1136 |
&& (ctx->op == PUBKEY_OP_SIGN || ctx->op == PUBKEY_OP_VERIFY)) |
| 1137 |
{ |
| 1138 |
const void * value; |
| 1139 |
size_t valuelen; |
| 1140 |
|
| 1141 |
if (sexp_length (lvalue) != 2) |
| 1142 |
rc = GPG_ERR_INV_OBJ; |
| 1143 |
else if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) |
| 1144 |
|| !valuelen ) |
| 1145 |
rc = GPG_ERR_INV_OBJ; |
| 1146 |
else |
| 1147 |
rc = _gcry_rsa_pkcs1_encode_raw_for_sig (ret_mpi, ctx->nbits, |
| 1148 |
value, valuelen); |
| 1149 |
} |
| 1150 |
else if (ctx->encoding == PUBKEY_ENC_OAEP && lvalue |
| 1151 |
&& ctx->op == PUBKEY_OP_ENCRYPT) |
| 1152 |
{ |
| 1153 |
const void * value; |
| 1154 |
size_t valuelen; |
| 1155 |
|
| 1156 |
if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen ) |
| 1157 |
rc = GPG_ERR_INV_OBJ; |
| 1158 |
else |
| 1159 |
{ |
| 1160 |
gcry_sexp_t list; |
| 1161 |
void *random_override = NULL; |
| 1162 |
size_t random_override_len = 0; |
| 1163 |
|
| 1164 |
/* Get HASH-ALGO. */ |
| 1165 |
list = sexp_find_token (ldata, "hash-algo", 0); |
| 1166 |
if (list) |
| 1167 |
{ |
| 1168 |
s = sexp_nth_data (list, 1, &n); |
| 1169 |
if (!s) |
| 1170 |
rc = GPG_ERR_NO_OBJ; |
| 1171 |
else |
| 1172 |
{ |
| 1173 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1174 |
if (!ctx->hash_algo) |
| 1175 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1176 |
} |
| 1177 |
sexp_release (list); |
| 1178 |
if (rc) |
| 1179 |
goto leave; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/* Get LABEL. */ |
| 1183 |
list = sexp_find_token (ldata, "label", 0); |
| 1184 |
if (list) |
| 1185 |
{ |
| 1186 |
s = sexp_nth_data (list, 1, &n); |
| 1187 |
if (!s) |
| 1188 |
rc = GPG_ERR_NO_OBJ; |
| 1189 |
else if (n > 0) |
| 1190 |
{ |
| 1191 |
ctx->label = xtrymalloc (n); |
| 1192 |
if (!ctx->label) |
| 1193 |
rc = gpg_err_code_from_syserror (); |
| 1194 |
else |
| 1195 |
{ |
| 1196 |
memcpy (ctx->label, s, n); |
| 1197 |
ctx->labellen = n; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
sexp_release (list); |
| 1201 |
if (rc) |
| 1202 |
goto leave; |
| 1203 |
} |
| 1204 |
/* Get optional RANDOM-OVERRIDE. */ |
| 1205 |
list = sexp_find_token (ldata, "random-override", 0); |
| 1206 |
if (list) |
| 1207 |
{ |
| 1208 |
if (fips_mode ()) |
| 1209 |
{ |
| 1210 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_FLAGS)) |
| 1211 |
{ |
| 1212 |
sexp_release (list); |
| 1213 |
rc = GPG_ERR_INV_FLAG; |
| 1214 |
goto leave; |
| 1215 |
} |
| 1216 |
else |
| 1217 |
fips_service_indicator_mark_non_compliant (); |
| 1218 |
} |
| 1219 |
s = sexp_nth_data (list, 1, &n); |
| 1220 |
if (!s) |
| 1221 |
rc = GPG_ERR_NO_OBJ; |
| 1222 |
else if (n > 0) |
| 1223 |
{ |
| 1224 |
random_override = xtrymalloc (n); |
| 1225 |
if (!random_override) |
| 1226 |
rc = gpg_err_code_from_syserror (); |
| 1227 |
else |
| 1228 |
{ |
| 1229 |
memcpy (random_override, s, n); |
| 1230 |
random_override_len = n; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
sexp_release (list); |
| 1234 |
if (rc) |
| 1235 |
goto leave; |
| 1236 |
} |
| 1237 |
|
| 1238 |
rc = _gcry_rsa_oaep_encode (ret_mpi, ctx->nbits, ctx->hash_algo, |
| 1239 |
value, valuelen, |
| 1240 |
ctx->label, ctx->labellen, |
| 1241 |
random_override, random_override_len); |
| 1242 |
|
| 1243 |
xfree (random_override); |
| 1244 |
} |
| 1245 |
} |
| 1246 |
else if (ctx->encoding == PUBKEY_ENC_PSS && ctx->op == PUBKEY_OP_SIGN) |
| 1247 |
{ |
| 1248 |
const void * value; |
| 1249 |
size_t valuelen; |
| 1250 |
gcry_sexp_t list; |
| 1251 |
void *random_override = NULL; |
| 1252 |
|
| 1253 |
if (lvalue) |
| 1254 |
{ |
| 1255 |
/* Get HASH-ALGO. */ |
| 1256 |
list = sexp_find_token (ldata, "hash-algo", 0); |
| 1257 |
if (list) |
| 1258 |
{ |
| 1259 |
s = sexp_nth_data (list, 1, &n); |
| 1260 |
if (!s) |
| 1261 |
rc = GPG_ERR_NO_OBJ; |
| 1262 |
else |
| 1263 |
{ |
| 1264 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1265 |
if (!ctx->hash_algo) |
| 1266 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1267 |
} |
| 1268 |
sexp_release (list); |
| 1269 |
if (rc) |
| 1270 |
goto leave; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if ( !(value=sexp_nth_data (lvalue, 1, &valuelen)) || !valuelen ) |
| 1274 |
rc = GPG_ERR_INV_OBJ; |
| 1275 |
ctx->flags |= PUBKEY_FLAG_PREHASH; |
| 1276 |
} |
| 1277 |
else if (lhash) |
| 1278 |
{ |
| 1279 |
if (sexp_length (lhash) != 3) |
| 1280 |
rc = GPG_ERR_INV_OBJ; |
| 1281 |
else if ( !(s=sexp_nth_data (lhash, 1, &n)) || !n ) |
| 1282 |
rc = GPG_ERR_INV_OBJ; |
| 1283 |
else |
| 1284 |
{ |
| 1285 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1286 |
|
| 1287 |
if (!ctx->hash_algo) |
| 1288 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1289 |
else if ( !(value=sexp_nth_data (lhash, 2, &valuelen)) |
| 1290 |
|| !valuelen ) |
| 1291 |
rc = GPG_ERR_INV_OBJ; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
else |
| 1295 |
rc = GPG_ERR_CONFLICT; |
| 1296 |
|
| 1297 |
if (rc) |
| 1298 |
goto leave; |
| 1299 |
|
| 1300 |
/* Get SALT-LENGTH. */ |
| 1301 |
list = sexp_find_token (ldata, "salt-length", 0); |
| 1302 |
if (list) |
| 1303 |
{ |
| 1304 |
s = sexp_nth_data (list, 1, &n); |
| 1305 |
if (!s) |
| 1306 |
{ |
| 1307 |
rc = GPG_ERR_NO_OBJ; |
| 1308 |
goto leave; |
| 1309 |
} |
| 1310 |
ctx->saltlen = (unsigned int)strtoul (s, NULL, 10); |
| 1311 |
sexp_release (list); |
| 1312 |
} |
| 1313 |
|
| 1314 |
/* Get optional RANDOM-OVERRIDE. */ |
| 1315 |
list = sexp_find_token (ldata, "random-override", 0); |
| 1316 |
if (list) |
| 1317 |
{ |
| 1318 |
if (fips_mode ()) |
| 1319 |
{ |
| 1320 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK_FLAGS)) |
| 1321 |
{ |
| 1322 |
sexp_release (list); |
| 1323 |
rc = GPG_ERR_INV_FLAG; |
| 1324 |
goto leave; |
| 1325 |
} |
| 1326 |
else |
| 1327 |
fips_service_indicator_mark_non_compliant (); |
| 1328 |
} |
| 1329 |
s = sexp_nth_data (list, 1, &n); |
| 1330 |
if (!s) |
| 1331 |
rc = GPG_ERR_NO_OBJ; |
| 1332 |
else if (n == ctx->saltlen) |
| 1333 |
{ |
| 1334 |
random_override = xtrymalloc (n); |
| 1335 |
if (!random_override) |
| 1336 |
rc = gpg_err_code_from_syserror (); |
| 1337 |
else |
| 1338 |
memcpy (random_override, s, n); |
| 1339 |
} |
| 1340 |
else |
| 1341 |
rc = GPG_ERR_INV_ARG; |
| 1342 |
sexp_release (list); |
| 1343 |
if (rc) |
| 1344 |
goto leave; |
| 1345 |
} |
| 1346 |
|
| 1347 |
/* Encode the data. (NBITS-1 is due to 8.1.1, step 1.) */ |
| 1348 |
rc = _gcry_rsa_pss_encode (ret_mpi, ctx->nbits - 1, |
| 1349 |
ctx->hash_algo, ctx->saltlen, |
| 1350 |
!(ctx->flags & PUBKEY_FLAG_PREHASH), |
| 1351 |
value, valuelen, |
| 1352 |
random_override); |
| 1353 |
xfree (random_override); |
| 1354 |
} |
| 1355 |
else if (ctx->encoding == PUBKEY_ENC_PSS && ctx->op == PUBKEY_OP_VERIFY) |
| 1356 |
{ |
| 1357 |
gcry_sexp_t list; |
| 1358 |
|
| 1359 |
if (lvalue) |
| 1360 |
{ |
| 1361 |
/* Get HASH-ALGO. */ |
| 1362 |
list = sexp_find_token (ldata, "hash-algo", 0); |
| 1363 |
if (list) |
| 1364 |
{ |
| 1365 |
s = sexp_nth_data (list, 1, &n); |
| 1366 |
if (!s) |
| 1367 |
rc = GPG_ERR_NO_OBJ; |
| 1368 |
else |
| 1369 |
{ |
| 1370 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1371 |
if (!ctx->hash_algo) |
| 1372 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1373 |
} |
| 1374 |
sexp_release (list); |
| 1375 |
if (rc) |
| 1376 |
goto leave; |
| 1377 |
} |
| 1378 |
|
| 1379 |
*ret_mpi = sexp_nth_mpi (lvalue, 1, GCRYMPI_FMT_OPAQUE); |
| 1380 |
if (!*ret_mpi) |
| 1381 |
rc = GPG_ERR_INV_OBJ; |
| 1382 |
|
| 1383 |
ctx->flags |= PUBKEY_FLAG_PREHASH; |
| 1384 |
} |
| 1385 |
else if (lhash) |
| 1386 |
{ |
| 1387 |
if (sexp_length (lhash) != 3) |
| 1388 |
rc = GPG_ERR_INV_OBJ; |
| 1389 |
else if ( !(s=sexp_nth_data (lhash, 1, &n)) || !n ) |
| 1390 |
rc = GPG_ERR_INV_OBJ; |
| 1391 |
else |
| 1392 |
{ |
| 1393 |
ctx->hash_algo = get_hash_algo (s, n); |
| 1394 |
|
| 1395 |
if (!ctx->hash_algo) |
| 1396 |
rc = GPG_ERR_DIGEST_ALGO; |
| 1397 |
else |
| 1398 |
{ |
| 1399 |
*ret_mpi = sexp_nth_mpi (lhash, 2, GCRYMPI_FMT_OPAQUE); |
| 1400 |
if (!*ret_mpi) |
| 1401 |
rc = GPG_ERR_INV_OBJ; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
} |
| 1405 |
else |
| 1406 |
rc = GPG_ERR_CONFLICT; |
| 1407 |
|
| 1408 |
if (rc) |
| 1409 |
goto leave; |
| 1410 |
|
| 1411 |
/* Get SALT-LENGTH. */ |
| 1412 |
list = sexp_find_token (ldata, "salt-length", 0); |
| 1413 |
if (list) |
| 1414 |
{ |
| 1415 |
s = sexp_nth_data (list, 1, &n); |
| 1416 |
if (!s) |
| 1417 |
{ |
| 1418 |
rc = GPG_ERR_NO_OBJ; |
| 1419 |
goto leave; |
| 1420 |
} |
| 1421 |
ctx->saltlen = (unsigned int)strtoul (s, NULL, 10); |
| 1422 |
if (ctx->saltlen > 16384) |
| 1423 |
rc = GPG_ERR_TOO_LARGE; |
| 1424 |
sexp_release (list); |
| 1425 |
if (rc) |
| 1426 |
goto leave; |
| 1427 |
} |
| 1428 |
|
| 1429 |
ctx->verify_cmp = pss_verify_cmp; |
| 1430 |
ctx->verify_arg = *ret_mpi; |
| 1431 |
} |
| 1432 |
else |
| 1433 |
rc = GPG_ERR_CONFLICT; |
| 1434 |
|
| 1435 |
leave: |
| 1436 |
sexp_release (ldata); |
| 1437 |
sexp_release (lhash); |
| 1438 |
sexp_release (lvalue); |
| 1439 |
|
| 1440 |
if (!rc) |
| 1441 |
ctx->flags |= parsed_flags; |
| 1442 |
else |
| 1443 |
{ |
| 1444 |
xfree (ctx->label); |
| 1445 |
ctx->label = NULL; |
| 1446 |
xfree (ctx->rnd); |
| 1447 |
ctx->rnd = NULL; |
| 1448 |
} |
| 1449 |
|
| 1450 |
return rc; |
| 1451 |
} |