diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-03-23 16:39:05 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-03-23 16:39:05 +0000 |
commit | d3bc6a83ba461e0d0dcc2c415c536f0c6116261b (patch) | |
tree | 1646ccf9489682f8531fa53ab960138d44a10b7c /src/fs/fs_search.c | |
parent | af9ebccd1d30144d10af1adf0e9475278a9b0441 (diff) | |
download | gnunet-d3bc6a83ba461e0d0dcc2c415c536f0c6116261b.tar.gz gnunet-d3bc6a83ba461e0d0dcc2c415c536f0c6116261b.zip |
-eliminating duplicate code
Diffstat (limited to 'src/fs/fs_search.c')
-rw-r--r-- | src/fs/fs_search.c | 106 |
1 files changed, 60 insertions, 46 deletions
diff --git a/src/fs/fs_search.c b/src/fs/fs_search.c index 5b09c8a05..94a77e136 100644 --- a/src/fs/fs_search.c +++ b/src/fs/fs_search.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2001-2006, 2008-2011 Christian Grothoff (and other contributing authors) | 3 | (C) 2001-2006, 2008-2012 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -513,28 +513,32 @@ process_sks_result (struct GNUNET_FS_SearchContext *sc, const char *id_update, | |||
513 | 513 | ||
514 | 514 | ||
515 | /** | 515 | /** |
516 | * Process a keyword-search result. | 516 | * Decrypt a block using a 'keyword' as the passphrase. Given the |
517 | * KSK public key derived from the keyword, this function looks up | ||
518 | * the original keyword in the search context and decrypts the | ||
519 | * given ciphertext block. | ||
517 | * | 520 | * |
518 | * @param sc our search context | 521 | * @param sc search context with the keywords |
519 | * @param kb the kblock | 522 | * @param public_key public key to use to lookup the keyword |
520 | * @param size size of kb | 523 | * @param edata encrypted data |
524 | * @param edata_size number of bytes in 'edata' (and 'data') | ||
525 | * @param data where to store the plaintext | ||
526 | * @return keyword index on success, GNUNET_SYSERR on error (no such | ||
527 | * keyword, internal error) | ||
521 | */ | 528 | */ |
522 | static void | 529 | static int |
523 | process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb, | 530 | decrypt_block_with_keyword (const struct GNUNET_FS_SearchContext *sc, |
524 | size_t size) | 531 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key, |
525 | { | 532 | const void *edata, |
526 | unsigned int i; | 533 | size_t edata_size, |
527 | size_t j; | 534 | char *data) |
535 | { | ||
528 | GNUNET_HashCode q; | 536 | GNUNET_HashCode q; |
529 | char pt[size - sizeof (struct KBlock)]; | ||
530 | struct GNUNET_CRYPTO_AesSessionKey skey; | 537 | struct GNUNET_CRYPTO_AesSessionKey skey; |
531 | struct GNUNET_CRYPTO_AesInitializationVector iv; | 538 | struct GNUNET_CRYPTO_AesInitializationVector iv; |
532 | const char *eos; | 539 | int i; |
533 | struct GNUNET_CONTAINER_MetaData *meta; | ||
534 | struct GNUNET_FS_Uri *uri; | ||
535 | char *emsg; | ||
536 | 540 | ||
537 | GNUNET_CRYPTO_hash (&kb->keyspace, | 541 | GNUNET_CRYPTO_hash (public_key, |
538 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | 542 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), |
539 | &q); | 543 | &q); |
540 | /* find key */ | 544 | /* find key */ |
@@ -545,17 +549,46 @@ process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb, | |||
545 | { | 549 | { |
546 | /* oops, does not match any of our keywords!? */ | 550 | /* oops, does not match any of our keywords!? */ |
547 | GNUNET_break (0); | 551 | GNUNET_break (0); |
548 | return; | 552 | return GNUNET_SYSERR; |
549 | } | 553 | } |
550 | /* decrypt */ | 554 | /* decrypt */ |
551 | GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv); | 555 | GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv); |
552 | if (-1 == | 556 | if (-1 == |
553 | GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey, | 557 | GNUNET_CRYPTO_aes_decrypt (edata, edata_size, &skey, |
554 | &iv, pt)) | 558 | &iv, data)) |
555 | { | 559 | { |
556 | GNUNET_break (0); | 560 | GNUNET_break (0); |
557 | return; | 561 | return GNUNET_SYSERR; |
558 | } | 562 | } |
563 | return i; | ||
564 | } | ||
565 | |||
566 | |||
567 | /** | ||
568 | * Process a keyword-search result. | ||
569 | * | ||
570 | * @param sc our search context | ||
571 | * @param kb the kblock | ||
572 | * @param size size of kb | ||
573 | */ | ||
574 | static void | ||
575 | process_kblock (struct GNUNET_FS_SearchContext *sc, const struct KBlock *kb, | ||
576 | size_t size) | ||
577 | { | ||
578 | size_t j; | ||
579 | char pt[size - sizeof (struct KBlock)]; | ||
580 | const char *eos; | ||
581 | struct GNUNET_CONTAINER_MetaData *meta; | ||
582 | struct GNUNET_FS_Uri *uri; | ||
583 | char *emsg; | ||
584 | int i; | ||
585 | |||
586 | if (-1 == (i = decrypt_block_with_keyword (sc, | ||
587 | &kb->keyspace, | ||
588 | &kb[1], | ||
589 | size - sizeof (struct KBlock), | ||
590 | pt))) | ||
591 | return; | ||
559 | /* parse */ | 592 | /* parse */ |
560 | eos = memchr (pt, 0, sizeof (pt)); | 593 | eos = memchr (pt, 0, sizeof (pt)); |
561 | if (NULL == eos) | 594 | if (NULL == eos) |
@@ -601,39 +634,20 @@ static void | |||
601 | process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb, | 634 | process_nblock (struct GNUNET_FS_SearchContext *sc, const struct NBlock *nb, |
602 | size_t size) | 635 | size_t size) |
603 | { | 636 | { |
604 | unsigned int i; | ||
605 | size_t j; | 637 | size_t j; |
606 | GNUNET_HashCode q; | ||
607 | char pt[size - sizeof (struct NBlock)]; | 638 | char pt[size - sizeof (struct NBlock)]; |
608 | struct GNUNET_CRYPTO_AesSessionKey skey; | ||
609 | struct GNUNET_CRYPTO_AesInitializationVector iv; | ||
610 | const char *eos; | 639 | const char *eos; |
611 | struct GNUNET_CONTAINER_MetaData *meta; | 640 | struct GNUNET_CONTAINER_MetaData *meta; |
612 | struct GNUNET_FS_Uri *uri; | 641 | struct GNUNET_FS_Uri *uri; |
613 | char *uris; | 642 | char *uris; |
643 | int i; | ||
614 | 644 | ||
615 | GNUNET_CRYPTO_hash (&nb->keyspace, | 645 | if (-1 == (i = decrypt_block_with_keyword (sc, |
616 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | 646 | &nb->keyspace, |
617 | &q); | 647 | &nb[1], |
618 | /* find key */ | 648 | size - sizeof (struct NBlock), |
619 | for (i = 0; i < sc->uri->data.ksk.keywordCount; i++) | 649 | pt))) |
620 | if (0 == memcmp (&q, &sc->requests[i].query, sizeof (GNUNET_HashCode))) | ||
621 | break; | ||
622 | if (i == sc->uri->data.ksk.keywordCount) | ||
623 | { | ||
624 | /* oops, does not match any of our keywords!? */ | ||
625 | GNUNET_break (0); | ||
626 | return; | 650 | return; |
627 | } | ||
628 | /* decrypt */ | ||
629 | GNUNET_CRYPTO_hash_to_aes_key (&sc->requests[i].key, &skey, &iv); | ||
630 | if (-1 == | ||
631 | GNUNET_CRYPTO_aes_decrypt (&nb[1], size - sizeof (struct NBlock), &skey, | ||
632 | &iv, pt)) | ||
633 | { | ||
634 | GNUNET_break (0); | ||
635 | return; | ||
636 | } | ||
637 | /* parse */ | 651 | /* parse */ |
638 | eos = memchr (pt, 0, sizeof (pt)); | 652 | eos = memchr (pt, 0, sizeof (pt)); |
639 | if (NULL == eos) | 653 | if (NULL == eos) |