1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
#include "system.h"
#include <gcrypt.h>
#include <rpm/rpmcrypto.h>
#include "rpmpgp_internal.h"
#include "debug.h"
static int hashalgo2gcryalgo(int hashalgo)
{
switch (hashalgo) {
case RPM_HASH_MD5:
return GCRY_MD_MD5;
case RPM_HASH_SHA1:
return GCRY_MD_SHA1;
case RPM_HASH_SHA224:
return GCRY_MD_SHA224;
case RPM_HASH_SHA256:
return GCRY_MD_SHA256;
case RPM_HASH_SHA384:
return GCRY_MD_SHA384;
case RPM_HASH_SHA512:
return GCRY_MD_SHA512;
default:
return 0;
}
}
/****************************** RSA **************************************/
struct pgpDigSigRSA_s {
gcry_mpi_t s;
};
struct pgpDigKeyRSA_s {
gcry_mpi_t n;
gcry_mpi_t e;
};
static int pgpSetSigMpiRSA(pgpDigAlg pgpsig, int num, const uint8_t *p)
{
struct pgpDigSigRSA_s *sig = pgpsig->data;
int mlen = pgpMpiLen(p);
int rc = 1;
if (!sig)
sig = pgpsig->data = xcalloc(1, sizeof(*sig));
switch (num) {
case 0:
if (!gcry_mpi_scan(&sig->s, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
}
return rc;
}
static int pgpSetKeyMpiRSA(pgpDigAlg pgpkey, int num, const uint8_t *p)
{
struct pgpDigKeyRSA_s *key = pgpkey->data;
int mlen = pgpMpiLen(p);
int rc = 1;
if (!key)
key = pgpkey->data = xcalloc(1, sizeof(*key));
switch (num) {
case 0:
if (!gcry_mpi_scan(&key->n, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
case 1:
if (!gcry_mpi_scan(&key->e, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
}
return rc;
}
static int pgpVerifySigRSA(pgpDigAlg pgpkey, pgpDigAlg pgpsig, uint8_t *hash, size_t hashlen, int hash_algo)
{
struct pgpDigKeyRSA_s *key = pgpkey->data;
struct pgpDigSigRSA_s *sig = pgpsig->data;
gcry_sexp_t sexp_sig = NULL, sexp_data = NULL, sexp_pkey = NULL;
const char *hash_algo_name;
int rc = 1;
if (!sig || !key)
return rc;
hash_algo_name = gcry_md_algo_name(hashalgo2gcryalgo(hash_algo));
gcry_sexp_build(&sexp_sig, NULL, "(sig-val (rsa (s %M)))", sig->s);
gcry_sexp_build(&sexp_data, NULL, "(data (flags pkcs1) (hash %s %b))", hash_algo_name, (int)hashlen, (const char *)hash);
gcry_sexp_build(&sexp_pkey, NULL, "(public-key (rsa (n %M) (e %M)))", key->n, key->e);
if (sexp_sig && sexp_data && sexp_pkey)
rc = gcry_pk_verify(sexp_sig, sexp_data, sexp_pkey) == 0 ? 0 : 1;
gcry_sexp_release(sexp_sig);
gcry_sexp_release(sexp_data);
gcry_sexp_release(sexp_pkey);
return rc;
}
static void pgpFreeSigRSA(pgpDigAlg pgpsig)
{
struct pgpDigSigRSA_s *sig = pgpsig->data;
if (sig) {
gcry_mpi_release(sig->s);
pgpsig->data = _free(sig);
}
}
static void pgpFreeKeyRSA(pgpDigAlg pgpkey)
{
struct pgpDigKeyRSA_s *key = pgpkey->data;
if (key) {
gcry_mpi_release(key->n);
gcry_mpi_release(key->e);
pgpkey->data = _free(key);
}
}
/****************************** DSA **************************************/
struct pgpDigSigDSA_s {
gcry_mpi_t r;
gcry_mpi_t s;
};
struct pgpDigKeyDSA_s {
gcry_mpi_t p;
gcry_mpi_t q;
gcry_mpi_t g;
gcry_mpi_t y;
};
static int pgpSetSigMpiDSA(pgpDigAlg pgpsig, int num, const uint8_t *p)
{
struct pgpDigSigDSA_s *sig = pgpsig->data;
int mlen = pgpMpiLen(p);
int rc = 1;
if (!sig)
sig = pgpsig->data = xcalloc(1, sizeof(*sig));
switch (num) {
case 0:
if (!gcry_mpi_scan(&sig->r, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
case 1:
if (!gcry_mpi_scan(&sig->s, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
}
return rc;
}
static int pgpSetKeyMpiDSA(pgpDigAlg pgpkey, int num, const uint8_t *p)
{
struct pgpDigKeyDSA_s *key = pgpkey->data;
int mlen = pgpMpiLen(p);
int rc = 1;
if (!key)
key = pgpkey->data = xcalloc(1, sizeof(*key));
switch (num) {
case 0:
if (!gcry_mpi_scan(&key->p, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
case 1:
if (!gcry_mpi_scan(&key->q, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
case 2:
if (!gcry_mpi_scan(&key->g, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
case 3:
if (!gcry_mpi_scan(&key->y, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
}
return rc;
}
static int pgpVerifySigDSA(pgpDigAlg pgpkey, pgpDigAlg pgpsig, uint8_t *hash, size_t hashlen, int hash_algo)
{
struct pgpDigKeyDSA_s *key = pgpkey->data;
struct pgpDigSigDSA_s *sig = pgpsig->data;
gcry_sexp_t sexp_sig = NULL, sexp_data = NULL, sexp_pkey = NULL;
int rc = 1;
size_t qlen;
if (!sig || !key)
return rc;
qlen = (mpi_get_nbits(key->q) + 7) / 8;
if (qlen < 20)
qlen = 20; /* sanity */
if (hashlen > qlen)
hashlen = qlen; /* dsa2: truncate hash to qlen */
gcry_sexp_build(&sexp_sig, NULL, "(sig-val (dsa (r %M) (s %M)))", sig->r, sig->s);
gcry_sexp_build(&sexp_data, NULL, "(data (flags raw) (value %b))", (int)hashlen, (const char *)hash);
gcry_sexp_build(&sexp_pkey, NULL, "(public-key (dsa (p %M) (q %M) (g %M) (y %M)))", key->p, key->q, key->g, key->y);
if (sexp_sig && sexp_data && sexp_pkey)
rc = gcry_pk_verify(sexp_sig, sexp_data, sexp_pkey) == 0 ? 0 : 1;
gcry_sexp_release(sexp_sig);
gcry_sexp_release(sexp_data);
gcry_sexp_release(sexp_pkey);
return rc;
}
static void pgpFreeSigDSA(pgpDigAlg pgpsig)
{
struct pgpDigSigDSA_s *sig = pgpsig->data;
if (sig) {
gcry_mpi_release(sig->r);
gcry_mpi_release(sig->s);
pgpsig->data = _free(sig);
}
}
static void pgpFreeKeyDSA(pgpDigAlg pgpkey)
{
struct pgpDigKeyDSA_s *key = pgpkey->data;
if (key) {
gcry_mpi_release(key->p);
gcry_mpi_release(key->q);
gcry_mpi_release(key->g);
gcry_mpi_release(key->y);
pgpkey->data = _free(key);
}
}
/****************************** EDDSA **************************************/
struct pgpDigSigEDDSA_s {
gcry_mpi_t r;
gcry_mpi_t s;
};
struct pgpDigKeyEDDSA_s {
gcry_mpi_t q;
};
static int pgpSetSigMpiEDDSA(pgpDigAlg pgpsig, int num, const uint8_t *p)
{
struct pgpDigSigEDDSA_s *sig = pgpsig->data;
int mlen = pgpMpiLen(p);
int rc = 1;
if (!sig)
sig = pgpsig->data = xcalloc(1, sizeof(*sig));
switch (num) {
case 0:
if (!gcry_mpi_scan(&sig->r, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
case 1:
if (!gcry_mpi_scan(&sig->s, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
}
return rc;
}
static int pgpSetKeyMpiEDDSA(pgpDigAlg pgpkey, int num, const uint8_t *p)
{
struct pgpDigKeyEDDSA_s *key = pgpkey->data;
int mlen = pgpMpiLen(p);
int rc = 1;
if (!key)
key = pgpkey->data = xcalloc(1, sizeof(*key));
switch (num) {
case 0:
if (!gcry_mpi_scan(&key->q, GCRYMPI_FMT_PGP, p, mlen, NULL))
rc = 0;
break;
}
return rc;
}
static int
ed25519_zero_extend(gcry_mpi_t x, unsigned char *buf, int bufl)
{
int n = (gcry_mpi_get_nbits(x) + 7) / 8;
if (n == 0 || n > bufl)
return 1;
n = bufl - n;
if (n)
memset(buf, 0, n);
gcry_mpi_print(GCRYMPI_FMT_USG, buf + n, bufl - n, NULL, x);
return 0;
}
static int pgpVerifySigEDDSA(pgpDigAlg pgpkey, pgpDigAlg pgpsig, uint8_t *hash, size_t hashlen, int hash_algo)
{
struct pgpDigKeyEDDSA_s *key = pgpkey->data;
struct pgpDigSigEDDSA_s *sig = pgpsig->data;
gcry_sexp_t sexp_sig = NULL, sexp_data = NULL, sexp_pkey = NULL;
int rc = 1;
unsigned char buf_r[32], buf_s[32];
if (!sig || !key)
return rc;
if (pgpkey->curve != PGPCURVE_ED25519)
return rc;
if (ed25519_zero_extend(sig->r, buf_r, 32) || ed25519_zero_extend(sig->s, buf_s, 32))
return rc;
gcry_sexp_build(&sexp_sig, NULL, "(sig-val (eddsa (r %b) (s %b)))", 32, (const char *)buf_r, 32, (const char *)buf_s, 32);
gcry_sexp_build(&sexp_data, NULL, "(data (flags eddsa) (hash-algo sha512) (value %b))", (int)hashlen, (const char *)hash);
gcry_sexp_build(&sexp_pkey, NULL, "(public-key (ecc (curve \"Ed25519\") (flags eddsa) (q %M)))", key->q);
if (sexp_sig && sexp_data && sexp_pkey)
rc = gcry_pk_verify(sexp_sig, sexp_data, sexp_pkey) == 0 ? 0 : 1;
gcry_sexp_release(sexp_sig);
gcry_sexp_release(sexp_data);
gcry_sexp_release(sexp_pkey);
return rc;
}
static void pgpFreeSigEDDSA(pgpDigAlg pgpsig)
{
struct pgpDigSigEDDSA_s *sig = pgpsig->data;
if (sig) {
gcry_mpi_release(sig->r);
gcry_mpi_release(sig->s);
pgpsig->data = _free(sig);
}
}
static void pgpFreeKeyEDDSA(pgpDigAlg pgpkey)
{
struct pgpDigKeyEDDSA_s *key = pgpkey->data;
if (key) {
gcry_mpi_release(key->q);
pgpkey->data = _free(key);
}
}
/****************************** NULL **************************************/
static int pgpSetMpiNULL(pgpDigAlg pgpkey, int num, const uint8_t *p)
{
return 1;
}
static int pgpVerifyNULL(pgpDigAlg pgpkey, pgpDigAlg pgpsig,
uint8_t *hash, size_t hashlen, int hash_algo)
{
return 1;
}
static int pgpSupportedCurve(int curve)
{
if (curve == PGPCURVE_ED25519) {
static int supported_ed25519;
if (!supported_ed25519) {
gcry_sexp_t sexp = NULL;
unsigned int nbits;
gcry_sexp_build(&sexp, NULL, "(public-key (ecc (curve \"Ed25519\")))");
nbits = gcry_pk_get_nbits(sexp);
gcry_sexp_release(sexp);
supported_ed25519 = nbits > 0 ? 1 : -1;
}
return supported_ed25519 > 0;
}
return 0;
}
pgpDigAlg pgpPubkeyNew(int algo, int curve)
{
pgpDigAlg ka = xcalloc(1, sizeof(*ka));;
switch (algo) {
case PGPPUBKEYALGO_RSA:
ka->setmpi = pgpSetKeyMpiRSA;
ka->free = pgpFreeKeyRSA;
ka->mpis = 2;
break;
case PGPPUBKEYALGO_DSA:
ka->setmpi = pgpSetKeyMpiDSA;
ka->free = pgpFreeKeyDSA;
ka->mpis = 4;
break;
case PGPPUBKEYALGO_EDDSA:
if (!pgpSupportedCurve(curve)) {
ka->setmpi = pgpSetMpiNULL;
ka->mpis = -1;
break;
}
ka->setmpi = pgpSetKeyMpiEDDSA;
ka->free = pgpFreeKeyEDDSA;
ka->mpis = 1;
ka->curve = curve;
break;
default:
ka->setmpi = pgpSetMpiNULL;
ka->mpis = -1;
break;
}
ka->verify = pgpVerifyNULL; /* keys can't be verified */
return ka;
}
pgpDigAlg pgpSignatureNew(int algo)
{
pgpDigAlg sa = xcalloc(1, sizeof(*sa));
switch (algo) {
case PGPPUBKEYALGO_RSA:
sa->setmpi = pgpSetSigMpiRSA;
sa->free = pgpFreeSigRSA;
sa->verify = pgpVerifySigRSA;
sa->mpis = 1;
break;
case PGPPUBKEYALGO_DSA:
sa->setmpi = pgpSetSigMpiDSA;
sa->free = pgpFreeSigDSA;
sa->verify = pgpVerifySigDSA;
sa->mpis = 2;
break;
case PGPPUBKEYALGO_EDDSA:
sa->setmpi = pgpSetSigMpiEDDSA;
sa->free = pgpFreeSigEDDSA;
sa->verify = pgpVerifySigEDDSA;
sa->mpis = 2;
break;
default:
sa->setmpi = pgpSetMpiNULL;
sa->verify = pgpVerifyNULL;
sa->mpis = -1;
break;
}
return sa;
}
|