diff options
Diffstat (limited to 'src/credential/credential_serialization.c')
-rw-r--r-- | src/credential/credential_serialization.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/credential/credential_serialization.c b/src/credential/credential_serialization.c index 40fa112dd..95b29a49c 100644 --- a/src/credential/credential_serialization.c +++ b/src/credential/credential_serialization.c | |||
@@ -31,6 +31,7 @@ | |||
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" | ||
34 | 35 | ||
35 | /** | 36 | /** |
36 | * Calculate how many bytes we will need to serialize | 37 | * Calculate how many bytes we will need to serialize |
@@ -402,6 +403,7 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len, | |||
402 | c_count, | 403 | c_count, |
403 | cd); | 404 | cd); |
404 | } | 405 | } |
406 | |||
405 | int | 407 | int |
406 | GNUNET_CREDENTIAL_credential_serialize (struct | 408 | GNUNET_CREDENTIAL_credential_serialize (struct |
407 | GNUNET_CREDENTIAL_Credential *cred, | 409 | GNUNET_CREDENTIAL_Credential *cred, |
@@ -475,5 +477,121 @@ GNUNET_CREDENTIAL_credential_deserialize (const char*data, | |||
475 | return cred; | 477 | return cred; |
476 | } | 478 | } |
477 | 479 | ||
480 | //TODO own file for delegate de/serialization | ||
481 | |||
482 | int | ||
483 | GNUNET_CREDENTIAL_delegate_serialize (struct GNUNET_CREDENTIAL_Delegate *cred, | ||
484 | char **data) | ||
485 | { | ||
486 | size_t size; | ||
487 | struct DelegateEntry *cdata; | ||
488 | int attr_len; | ||
489 | |||
490 | // +1 for \0 | ||
491 | if (0 == cred->subject_attribute_len){ | ||
492 | attr_len = cred->issuer_attribute_len + 1; | ||
493 | } else { | ||
494 | attr_len = cred->issuer_attribute_len + cred->subject_attribute_len + 1; | ||
495 | } | ||
496 | size = sizeof (struct DelegateEntry) + attr_len; | ||
497 | |||
498 | char tmp_str[attr_len]; | ||
499 | GNUNET_memcpy(tmp_str, cred->issuer_attribute, cred->issuer_attribute_len); | ||
500 | if (0 != cred->subject_attribute_len){ | ||
501 | GNUNET_memcpy(tmp_str + cred->issuer_attribute_len, cred->subject_attribute, cred->subject_attribute_len); | ||
502 | } | ||
503 | tmp_str[attr_len - 1] = '\0'; | ||
504 | |||
505 | *data = GNUNET_malloc (size); | ||
506 | cdata = (struct DelegateEntry*)*data; | ||
507 | cdata->subject_key = cred->subject_key; | ||
508 | cdata->issuer_key = cred->issuer_key; | ||
509 | cdata->expiration = GNUNET_htonll (cred->expiration.abs_value_us); | ||
510 | cdata->signature = cred->signature; | ||
511 | cdata->issuer_attribute_len = htonl (cred->issuer_attribute_len + 1); | ||
512 | if (0 == cred->subject_attribute_len){ | ||
513 | cdata->subject_attribute_len = htonl (0); | ||
514 | } else { | ||
515 | cdata->subject_attribute_len = htonl (cred->subject_attribute_len + 1); | ||
516 | } | ||
517 | cdata->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL); | ||
518 | cdata->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_EcdsaSignature)); | ||
519 | |||
520 | GNUNET_memcpy (&cdata[1], | ||
521 | tmp_str, | ||
522 | attr_len); | ||
523 | |||
524 | if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL, | ||
525 | &cdata->purpose, | ||
526 | &cdata->signature, | ||
527 | &cdata->issuer_key)) | ||
528 | { | ||
529 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Serialize: Invalid delegate\n"); | ||
530 | //return NULL; | ||
531 | } | ||
532 | return size; | ||
533 | } | ||
534 | |||
535 | struct GNUNET_CREDENTIAL_Delegate* | ||
536 | GNUNET_CREDENTIAL_delegate_deserialize (const char* data, | ||
537 | size_t data_size) | ||
538 | { | ||
539 | struct GNUNET_CREDENTIAL_Delegate *cred; | ||
540 | struct DelegateEntry *cdata; | ||
541 | char *attr_combo_str; | ||
542 | |||
543 | if (data_size < sizeof (struct DelegateEntry)) | ||
544 | return NULL; | ||
545 | cdata = (struct DelegateEntry*)data; | ||
546 | if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL, | ||
547 | &cdata->purpose, | ||
548 | &cdata->signature, | ||
549 | &cdata->issuer_key)) | ||
550 | { | ||
551 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Deserialize: Invalid delegate\n"); | ||
552 | //return NULL; | ||
553 | } | ||
554 | attr_combo_str = (char*)&cdata[1]; | ||
555 | int iss_len = ntohl(cdata->issuer_attribute_len); | ||
556 | int sub_len = ntohl(cdata->subject_attribute_len); | ||
557 | int attr_combo_len = iss_len + sub_len; | ||
558 | |||
559 | cred = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Delegate) + attr_combo_len); | ||
560 | |||
561 | cred->issuer_key = cdata->issuer_key; | ||
562 | cred->subject_key = cdata->subject_key; | ||
563 | GNUNET_memcpy (&cred[1], | ||
564 | attr_combo_str, | ||
565 | attr_combo_len); | ||
566 | cred->signature = cdata->signature; | ||
567 | |||
568 | // Parse the combo attribute string, split into issuer and subject | ||
569 | if(0 == sub_len){ | ||
570 | cred->issuer_attribute = attr_combo_str; | ||
571 | cred->issuer_attribute_len = attr_combo_len; | ||
572 | cred->subject_attribute = '\0'; | ||
573 | cred->subject_attribute_len = 0; | ||
574 | } else { | ||
575 | // -1: array index starts from 0 | ||
576 | char *tmp_str = GNUNET_malloc(iss_len); | ||
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 | } | ||
591 | |||
592 | cred->expiration.abs_value_us = GNUNET_ntohll (cdata->expiration); | ||
593 | |||
594 | return cred; | ||
595 | } | ||
478 | 596 | ||
479 | /* end of credential_serialization.c */ | 597 | /* end of credential_serialization.c */ |