diff options
Diffstat (limited to 'src/credential/credential_serialization.c')
-rw-r--r-- | src/credential/credential_serialization.c | 85 |
1 files changed, 36 insertions, 49 deletions
diff --git a/src/credential/credential_serialization.c b/src/credential/credential_serialization.c index 95b29a49c..4e461c654 100644 --- a/src/credential/credential_serialization.c +++ b/src/credential/credential_serialization.c | |||
@@ -31,7 +31,6 @@ | |||
31 | #include "gnunet_credential_service.h" | 31 | #include "gnunet_credential_service.h" |
32 | #include "gnunet_signatures.h" | 32 | #include "gnunet_signatures.h" |
33 | #include "credential.h" | 33 | #include "credential.h" |
34 | #include "delegate.h" | ||
35 | 34 | ||
36 | /** | 35 | /** |
37 | * Calculate how many bytes we will need to serialize | 36 | * Calculate how many bytes we will need to serialize |
@@ -480,7 +479,7 @@ GNUNET_CREDENTIAL_credential_deserialize (const char*data, | |||
480 | //TODO own file for delegate de/serialization | 479 | //TODO own file for delegate de/serialization |
481 | 480 | ||
482 | int | 481 | int |
483 | GNUNET_CREDENTIAL_delegate_serialize (struct GNUNET_CREDENTIAL_Delegate *cred, | 482 | GNUNET_CREDENTIAL_delegate_serialize (struct GNUNET_CREDENTIAL_Delegate *dele, |
484 | char **data) | 483 | char **data) |
485 | { | 484 | { |
486 | size_t size; | 485 | size_t size; |
@@ -488,46 +487,47 @@ GNUNET_CREDENTIAL_delegate_serialize (struct GNUNET_CREDENTIAL_Delegate *cred, | |||
488 | int attr_len; | 487 | int attr_len; |
489 | 488 | ||
490 | // +1 for \0 | 489 | // +1 for \0 |
491 | if (0 == cred->subject_attribute_len){ | 490 | if (0 == dele->subject_attribute_len){ |
492 | attr_len = cred->issuer_attribute_len + 1; | 491 | attr_len = dele->issuer_attribute_len + 1; |
493 | } else { | 492 | } else { |
494 | attr_len = cred->issuer_attribute_len + cred->subject_attribute_len + 1; | 493 | attr_len = dele->issuer_attribute_len + dele->subject_attribute_len + 2; |
495 | } | 494 | } |
496 | size = sizeof (struct DelegateEntry) + attr_len; | 495 | size = sizeof (struct DelegateEntry) + attr_len; |
497 | 496 | ||
498 | char tmp_str[attr_len]; | 497 | char tmp_str[attr_len]; |
499 | GNUNET_memcpy(tmp_str, cred->issuer_attribute, cred->issuer_attribute_len); | 498 | GNUNET_memcpy(tmp_str, dele->issuer_attribute, dele->issuer_attribute_len); |
500 | if (0 != cred->subject_attribute_len){ | 499 | if (0 != dele->subject_attribute_len){ |
501 | GNUNET_memcpy(tmp_str + cred->issuer_attribute_len, cred->subject_attribute, cred->subject_attribute_len); | 500 | tmp_str[dele->issuer_attribute_len] = '\0'; |
501 | GNUNET_memcpy(tmp_str + dele->issuer_attribute_len + 1, dele->subject_attribute, dele->subject_attribute_len); | ||
502 | } | 502 | } |
503 | tmp_str[attr_len - 1] = '\0'; | 503 | tmp_str[attr_len - 1] = '\0'; |
504 | 504 | ||
505 | *data = GNUNET_malloc (size); | 505 | *data = GNUNET_malloc (size); |
506 | cdata = (struct DelegateEntry*)*data; | 506 | cdata = (struct DelegateEntry*)*data; |
507 | cdata->subject_key = cred->subject_key; | 507 | cdata->subject_key = dele->subject_key; |
508 | cdata->issuer_key = cred->issuer_key; | 508 | cdata->issuer_key = dele->issuer_key; |
509 | cdata->expiration = GNUNET_htonll (cred->expiration.abs_value_us); | 509 | cdata->expiration = GNUNET_htonll (dele->expiration.abs_value_us); |
510 | cdata->signature = cred->signature; | 510 | cdata->signature = dele->signature; |
511 | cdata->issuer_attribute_len = htonl (cred->issuer_attribute_len + 1); | 511 | cdata->issuer_attribute_len = htonl (dele->issuer_attribute_len + 1); |
512 | if (0 == cred->subject_attribute_len){ | 512 | if (0 == dele->subject_attribute_len){ |
513 | cdata->subject_attribute_len = htonl (0); | 513 | cdata->subject_attribute_len = htonl (0); |
514 | } else { | 514 | } else { |
515 | cdata->subject_attribute_len = htonl (cred->subject_attribute_len + 1); | 515 | cdata->subject_attribute_len = htonl (dele->subject_attribute_len + 1); |
516 | } | 516 | } |
517 | cdata->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL); | 517 | cdata->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DELEGATE); |
518 | cdata->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_EcdsaSignature)); | 518 | cdata->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_EcdsaSignature)); |
519 | 519 | ||
520 | GNUNET_memcpy (&cdata[1], | 520 | GNUNET_memcpy (&cdata[1], |
521 | tmp_str, | 521 | tmp_str, |
522 | attr_len); | 522 | attr_len); |
523 | 523 | ||
524 | if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL, | 524 | if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_DELEGATE, |
525 | &cdata->purpose, | 525 | &cdata->purpose, |
526 | &cdata->signature, | 526 | &cdata->signature, |
527 | &cdata->issuer_key)) | 527 | &cdata->issuer_key)) |
528 | { | 528 | { |
529 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Serialize: Invalid delegate\n"); | 529 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Serialize: Invalid delegate\n"); |
530 | //return NULL; | 530 | return 0; |
531 | } | 531 | } |
532 | return size; | 532 | return size; |
533 | } | 533 | } |
@@ -536,62 +536,49 @@ struct GNUNET_CREDENTIAL_Delegate* | |||
536 | GNUNET_CREDENTIAL_delegate_deserialize (const char* data, | 536 | GNUNET_CREDENTIAL_delegate_deserialize (const char* data, |
537 | size_t data_size) | 537 | size_t data_size) |
538 | { | 538 | { |
539 | struct GNUNET_CREDENTIAL_Delegate *cred; | 539 | struct GNUNET_CREDENTIAL_Delegate *dele; |
540 | struct DelegateEntry *cdata; | 540 | struct DelegateEntry *cdata; |
541 | char *attr_combo_str; | 541 | char *attr_combo_str; |
542 | 542 | ||
543 | if (data_size < sizeof (struct DelegateEntry)) | 543 | if (data_size < sizeof (struct DelegateEntry)) |
544 | return NULL; | 544 | return NULL; |
545 | cdata = (struct DelegateEntry*)data; | 545 | cdata = (struct DelegateEntry*)data; |
546 | if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL, | 546 | if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_DELEGATE, |
547 | &cdata->purpose, | 547 | &cdata->purpose, |
548 | &cdata->signature, | 548 | &cdata->signature, |
549 | &cdata->issuer_key)) | 549 | &cdata->issuer_key)) |
550 | { | 550 | { |
551 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Deserialize: Invalid delegate\n"); | 551 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Deserialize: Invalid delegate\n"); |
552 | //return NULL; | 552 | return NULL; |
553 | } | 553 | } |
554 | attr_combo_str = (char*)&cdata[1]; | 554 | attr_combo_str = (char*)&cdata[1]; |
555 | int iss_len = ntohl(cdata->issuer_attribute_len); | 555 | int iss_len = ntohl(cdata->issuer_attribute_len); |
556 | int sub_len = ntohl(cdata->subject_attribute_len); | 556 | int sub_len = ntohl(cdata->subject_attribute_len); |
557 | int attr_combo_len = iss_len + sub_len; | 557 | int attr_combo_len = iss_len + sub_len; |
558 | |||
559 | cred = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Delegate) + attr_combo_len); | ||
560 | 558 | ||
561 | cred->issuer_key = cdata->issuer_key; | 559 | dele = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Delegate) + attr_combo_len); |
562 | cred->subject_key = cdata->subject_key; | 560 | |
563 | GNUNET_memcpy (&cred[1], | 561 | dele->issuer_key = cdata->issuer_key; |
562 | dele->subject_key = cdata->subject_key; | ||
563 | GNUNET_memcpy (&dele[1], | ||
564 | attr_combo_str, | 564 | attr_combo_str, |
565 | attr_combo_len); | 565 | attr_combo_len); |
566 | cred->signature = cdata->signature; | 566 | dele->signature = cdata->signature; |
567 | 567 | ||
568 | // Parse the combo attribute string, split into issuer and subject | 568 | // Set the pointers for the attributes |
569 | dele->issuer_attribute = (char*)&dele[1]; | ||
570 | dele->issuer_attribute_len = iss_len; | ||
571 | dele->subject_attribute_len = sub_len; | ||
569 | if(0 == sub_len){ | 572 | if(0 == sub_len){ |
570 | cred->issuer_attribute = attr_combo_str; | 573 | dele->subject_attribute = NULL; |
571 | cred->issuer_attribute_len = attr_combo_len; | ||
572 | cred->subject_attribute = '\0'; | ||
573 | cred->subject_attribute_len = 0; | ||
574 | } else { | 574 | } else { |
575 | // -1: array index starts from 0 | 575 | dele->subject_attribute = (char*)&dele[1] + iss_len; |
576 | char *tmp_str = GNUNET_malloc(iss_len); | 576 | |
577 | GNUNET_memcpy(tmp_str, attr_combo_str, iss_len - 1); | ||
578 | tmp_str[iss_len] = '\0'; | ||
579 | cred->issuer_attribute = strdup(tmp_str); | ||
580 | cred->issuer_attribute_len = iss_len; | ||
581 | GNUNET_free(tmp_str); | ||
582 | |||
583 | // -1: both times, starting from 0 | ||
584 | tmp_str = GNUNET_malloc(sub_len); | ||
585 | GNUNET_memcpy(tmp_str, attr_combo_str + iss_len - 1, sub_len - 1); | ||
586 | tmp_str[sub_len] = '\0'; | ||
587 | cred->subject_attribute = strdup(tmp_str); | ||
588 | cred->subject_attribute_len = sub_len; | ||
589 | GNUNET_free(tmp_str); | ||
590 | } | 577 | } |
591 | 578 | ||
592 | cred->expiration.abs_value_us = GNUNET_ntohll (cdata->expiration); | 579 | dele->expiration.abs_value_us = GNUNET_ntohll (cdata->expiration); |
593 | 580 | ||
594 | return cred; | 581 | return dele; |
595 | } | 582 | } |
596 | 583 | ||
597 | /* end of credential_serialization.c */ | 584 | /* end of credential_serialization.c */ |