aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/reclaim_credential.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2020-08-19 23:53:02 +0200
committerMartin Schanzenbach <mschanzenbach@posteo.de>2020-08-20 17:59:02 +0200
commita57d476abbe857365aff157f389cc1188b5dd090 (patch)
treee8f7163ef7e6f5426748fed8d2eaa5183038a5d6 /src/reclaim/reclaim_credential.c
parente75869506cc08e08056168383bd4ab02e1f007de (diff)
downloadgnunet-a57d476abbe857365aff157f389cc1188b5dd090.tar.gz
gnunet-a57d476abbe857365aff157f389cc1188b5dd090.zip
reclaim: Attestations now called credentials. Credentials are presented to third parties as presentations.
Diffstat (limited to 'src/reclaim/reclaim_credential.c')
-rw-r--r--src/reclaim/reclaim_credential.c501
1 files changed, 484 insertions, 17 deletions
diff --git a/src/reclaim/reclaim_credential.c b/src/reclaim/reclaim_credential.c
index 10b17ae96..5c8974400 100644
--- a/src/reclaim/reclaim_credential.c
+++ b/src/reclaim/reclaim_credential.c
@@ -165,9 +165,9 @@ GNUNET_RECLAIM_credential_number_to_typename (uint32_t type)
165 */ 165 */
166int 166int
167GNUNET_RECLAIM_credential_string_to_value (uint32_t type, 167GNUNET_RECLAIM_credential_string_to_value (uint32_t type,
168 const char *s, 168 const char *s,
169 void **data, 169 void **data,
170 size_t *data_size) 170 size_t *data_size)
171{ 171{
172 unsigned int i; 172 unsigned int i;
173 struct Plugin *plugin; 173 struct Plugin *plugin;
@@ -197,8 +197,8 @@ GNUNET_RECLAIM_credential_string_to_value (uint32_t type,
197 */ 197 */
198char * 198char *
199GNUNET_RECLAIM_credential_value_to_string (uint32_t type, 199GNUNET_RECLAIM_credential_value_to_string (uint32_t type,
200 const void *data, 200 const void *data,
201 size_t data_size) 201 size_t data_size)
202{ 202{
203 unsigned int i; 203 unsigned int i;
204 struct Plugin *plugin; 204 struct Plugin *plugin;
@@ -229,9 +229,9 @@ GNUNET_RECLAIM_credential_value_to_string (uint32_t type,
229 */ 229 */
230struct GNUNET_RECLAIM_Credential * 230struct GNUNET_RECLAIM_Credential *
231GNUNET_RECLAIM_credential_new (const char *attr_name, 231GNUNET_RECLAIM_credential_new (const char *attr_name,
232 uint32_t type, 232 uint32_t type,
233 const void *data, 233 const void *data,
234 size_t data_size) 234 size_t data_size)
235{ 235{
236 struct GNUNET_RECLAIM_Credential *attr; 236 struct GNUNET_RECLAIM_Credential *attr;
237 char *write_ptr; 237 char *write_ptr;
@@ -335,7 +335,7 @@ GNUNET_RECLAIM_credential_list_deserialize (const char *data, size_t data_size)
335 ale = GNUNET_new (struct GNUNET_RECLAIM_CredentialListEntry); 335 ale = GNUNET_new (struct GNUNET_RECLAIM_CredentialListEntry);
336 ale->credential = 336 ale->credential =
337 GNUNET_RECLAIM_credential_deserialize (read_ptr, 337 GNUNET_RECLAIM_credential_deserialize (read_ptr,
338 data_size - (read_ptr - data)); 338 data_size - (read_ptr - data));
339 if (NULL == ale->credential) 339 if (NULL == ale->credential)
340 { 340 {
341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -371,9 +371,9 @@ GNUNET_RECLAIM_credential_list_dup (
371 GNUNET_assert (NULL != ale->credential); 371 GNUNET_assert (NULL != ale->credential);
372 result_ale->credential = 372 result_ale->credential =
373 GNUNET_RECLAIM_credential_new (ale->credential->name, 373 GNUNET_RECLAIM_credential_new (ale->credential->name,
374 ale->credential->type, 374 ale->credential->type,
375 ale->credential->data, 375 ale->credential->data,
376 ale->credential->data_size); 376 ale->credential->data_size);
377 result_ale->credential->id = ale->credential->id; 377 result_ale->credential->id = ale->credential->id;
378 GNUNET_CONTAINER_DLL_insert (result->list_head, 378 GNUNET_CONTAINER_DLL_insert (result->list_head,
379 result->list_tail, 379 result->list_tail,
@@ -490,7 +490,7 @@ GNUNET_RECLAIM_credential_deserialize (const char *data, size_t data_size)
490 return NULL; 490 return NULL;
491 } 491 }
492 credential = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Credential) 492 credential = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Credential)
493 + data_len + name_len + 1); 493 + data_len + name_len + 1);
494 credential->type = ntohs (atts->credential_type); 494 credential->type = ntohs (atts->credential_type);
495 credential->flag = ntohl (atts->credential_flag); 495 credential->flag = ntohl (atts->credential_flag);
496 credential->id = atts->credential_id; 496 credential->id = atts->credential_id;
@@ -511,7 +511,7 @@ GNUNET_RECLAIM_credential_deserialize (const char *data, size_t data_size)
511 511
512struct GNUNET_RECLAIM_AttributeList* 512struct GNUNET_RECLAIM_AttributeList*
513GNUNET_RECLAIM_credential_get_attributes (const struct 513GNUNET_RECLAIM_credential_get_attributes (const struct
514 GNUNET_RECLAIM_Credential *credential) 514 GNUNET_RECLAIM_Credential *credential)
515{ 515{
516 unsigned int i; 516 unsigned int i;
517 struct Plugin *plugin; 517 struct Plugin *plugin;
@@ -531,7 +531,7 @@ GNUNET_RECLAIM_credential_get_attributes (const struct
531 531
532char* 532char*
533GNUNET_RECLAIM_credential_get_issuer (const struct 533GNUNET_RECLAIM_credential_get_issuer (const struct
534 GNUNET_RECLAIM_Credential *credential) 534 GNUNET_RECLAIM_Credential *credential)
535{ 535{
536 unsigned int i; 536 unsigned int i;
537 struct Plugin *plugin; 537 struct Plugin *plugin;
@@ -551,8 +551,8 @@ GNUNET_RECLAIM_credential_get_issuer (const struct
551 551
552int 552int
553GNUNET_RECLAIM_credential_get_expiration (const struct 553GNUNET_RECLAIM_credential_get_expiration (const struct
554 GNUNET_RECLAIM_Credential *credential, 554 GNUNET_RECLAIM_Credential *credential,
555 struct GNUNET_TIME_Absolute*exp) 555 struct GNUNET_TIME_Absolute*exp)
556{ 556{
557 unsigned int i; 557 unsigned int i;
558 struct Plugin *plugin; 558 struct Plugin *plugin;
@@ -568,3 +568,470 @@ GNUNET_RECLAIM_credential_get_expiration (const struct
568 } 568 }
569 return GNUNET_SYSERR; 569 return GNUNET_SYSERR;
570} 570}
571
572
573/**
574 * Convert an presentation type name to the corresponding number
575 *
576 * @param typename name to convert
577 * @return corresponding number, UINT32_MAX on error
578 */
579uint32_t
580GNUNET_RECLAIM_presentation_typename_to_number (const char *typename)
581{
582 unsigned int i;
583 struct Plugin *plugin;
584 uint32_t ret;
585 init ();
586 for (i = 0; i < num_plugins; i++)
587 {
588 plugin = credential_plugins[i];
589 if (UINT32_MAX !=
590 (ret = plugin->api->typename_to_number_p (plugin->api->cls,
591 typename)))
592 return ret;
593 }
594 return UINT32_MAX;
595}
596
597
598/**
599 * Convert an presentation type number to the corresponding presentation type string
600 *
601 * @param type number of a type
602 * @return corresponding typestring, NULL on error
603 */
604const char *
605GNUNET_RECLAIM_presentation_number_to_typename (uint32_t type)
606{
607 unsigned int i;
608 struct Plugin *plugin;
609 const char *ret;
610
611 init ();
612 for (i = 0; i < num_plugins; i++)
613 {
614 plugin = credential_plugins[i];
615 if (NULL !=
616 (ret = plugin->api->number_to_typename_p (plugin->api->cls, type)))
617 return ret;
618 }
619 return NULL;
620}
621
622
623/**
624 * Convert human-readable version of a 'claim' of an presentation to the binary
625 * representation
626 *
627 * @param type type of the claim
628 * @param s human-readable string
629 * @param data set to value in binary encoding (will be allocated)
630 * @param data_size set to number of bytes in @a data
631 * @return #GNUNET_OK on success
632 */
633int
634GNUNET_RECLAIM_presentation_string_to_value (uint32_t type,
635 const char *s,
636 void **data,
637 size_t *data_size)
638{
639 unsigned int i;
640 struct Plugin *plugin;
641
642 init ();
643 for (i = 0; i < num_plugins; i++)
644 {
645 plugin = credential_plugins[i];
646 if (GNUNET_OK == plugin->api->string_to_value_p (plugin->api->cls,
647 type,
648 s,
649 data,
650 data_size))
651 return GNUNET_OK;
652 }
653 return GNUNET_SYSERR;
654}
655
656
657/**
658 * Convert the 'claim' of an presentation to a string
659 *
660 * @param type the type of presentation
661 * @param data claim in binary encoding
662 * @param data_size number of bytes in @a data
663 * @return NULL on error, otherwise human-readable representation of the claim
664 */
665char *
666GNUNET_RECLAIM_presentation_value_to_string (uint32_t type,
667 const void *data,
668 size_t data_size)
669{
670 unsigned int i;
671 struct Plugin *plugin;
672 char *ret;
673
674 init ();
675 for (i = 0; i < num_plugins; i++)
676 {
677 plugin = credential_plugins[i];
678 if (NULL != (ret = plugin->api->value_to_string_p (plugin->api->cls,
679 type,
680 data,
681 data_size)))
682 return ret;
683 }
684 return NULL;
685}
686
687
688struct GNUNET_RECLAIM_Presentation *
689GNUNET_RECLAIM_presentation_new (uint32_t type,
690 const void *data,
691 size_t data_size)
692{
693 struct GNUNET_RECLAIM_Presentation *attr;
694 char *write_ptr;
695
696 attr = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Presentation)
697 + data_size);
698 attr->type = type;
699 attr->data_size = data_size;
700 write_ptr = (char *) &attr[1];
701 GNUNET_memcpy (write_ptr, data, data_size);
702 attr->data = write_ptr;
703 return attr;
704}
705
706
707/**
708 * Get required size for serialization buffer
709 *
710 * @param attrs the attribute list to serialize
711 * @return the required buffer size
712 */
713size_t
714GNUNET_RECLAIM_presentation_list_serialize_get_size (
715 const struct GNUNET_RECLAIM_PresentationList *presentations)
716{
717 struct GNUNET_RECLAIM_PresentationListEntry *le;
718 size_t len = 0;
719
720 for (le = presentations->list_head; NULL != le; le = le->next)
721 {
722 GNUNET_assert (NULL != le->presentation);
723 len += GNUNET_RECLAIM_presentation_serialize_get_size (le->presentation);
724 len += sizeof(struct GNUNET_RECLAIM_PresentationListEntry);
725 }
726 return len;
727}
728
729
730/**
731 * Serialize an attribute list
732 *
733 * @param attrs the attribute list to serialize
734 * @param result the serialized attribute
735 * @return length of serialized data
736 */
737size_t
738GNUNET_RECLAIM_presentation_list_serialize (
739 const struct GNUNET_RECLAIM_PresentationList *presentations,
740 char *result)
741{
742 struct GNUNET_RECLAIM_PresentationListEntry *le;
743 size_t len;
744 size_t total_len;
745 char *write_ptr;
746 write_ptr = result;
747 total_len = 0;
748 for (le = presentations->list_head; NULL != le; le = le->next)
749 {
750 GNUNET_assert (NULL != le->presentation);
751 len = GNUNET_RECLAIM_presentation_serialize (le->presentation, write_ptr);
752 total_len += len;
753 write_ptr += len;
754 }
755 return total_len;
756}
757
758
759/**
760 * Deserialize an presentation list
761 *
762 * @param data the serialized attribute list
763 * @param data_size the length of the serialized data
764 * @return a GNUNET_IDENTITY_PROVIDER_AttributeList, must be free'd by caller
765 */
766struct GNUNET_RECLAIM_PresentationList *
767GNUNET_RECLAIM_presentation_list_deserialize (const char *data, size_t
768 data_size)
769{
770 struct GNUNET_RECLAIM_PresentationList *al;
771 struct GNUNET_RECLAIM_PresentationListEntry *ale;
772 size_t att_len;
773 const char *read_ptr;
774
775 al = GNUNET_new (struct GNUNET_RECLAIM_PresentationList);
776
777 if ((data_size < sizeof(struct Presentation)
778 + sizeof(struct GNUNET_RECLAIM_PresentationListEntry)))
779 return al;
780
781 read_ptr = data;
782 while (((data + data_size) - read_ptr) >= sizeof(struct Presentation))
783 {
784 ale = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry);
785 ale->presentation =
786 GNUNET_RECLAIM_presentation_deserialize (read_ptr,
787 data_size - (read_ptr - data));
788 if (NULL == ale->presentation)
789 {
790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
791 "Failed to deserialize malformed presentation.\n");
792 GNUNET_free (ale);
793 return al;
794 }
795 GNUNET_CONTAINER_DLL_insert (al->list_head, al->list_tail, ale);
796 att_len = GNUNET_RECLAIM_presentation_serialize_get_size (
797 ale->presentation);
798 read_ptr += att_len;
799 }
800 return al;
801}
802
803
804/**
805 * Make a (deep) copy of the presentation list
806 * @param attrs claim list to copy
807 * @return copied claim list
808 */
809struct GNUNET_RECLAIM_PresentationList *
810GNUNET_RECLAIM_presentation_list_dup (
811 const struct GNUNET_RECLAIM_PresentationList *al)
812{
813 struct GNUNET_RECLAIM_PresentationListEntry *ale;
814 struct GNUNET_RECLAIM_PresentationListEntry *result_ale;
815 struct GNUNET_RECLAIM_PresentationList *result;
816
817 result = GNUNET_new (struct GNUNET_RECLAIM_PresentationList);
818 for (ale = al->list_head; NULL != ale; ale = ale->next)
819 {
820 result_ale = GNUNET_new (struct GNUNET_RECLAIM_PresentationListEntry);
821 GNUNET_assert (NULL != ale->presentation);
822 result_ale->presentation =
823 GNUNET_RECLAIM_presentation_new (ale->presentation->type,
824 ale->presentation->data,
825 ale->presentation->data_size);
826 result_ale->presentation->credential_id = ale->presentation->credential_id;
827 GNUNET_CONTAINER_DLL_insert (result->list_head,
828 result->list_tail,
829 result_ale);
830 }
831 return result;
832}
833
834
835/**
836 * Destroy presentation list
837 *
838 * @param attrs list to destroy
839 */
840void
841GNUNET_RECLAIM_presentation_list_destroy (
842 struct GNUNET_RECLAIM_PresentationList *al)
843{
844 struct GNUNET_RECLAIM_PresentationListEntry *ale;
845 struct GNUNET_RECLAIM_PresentationListEntry *tmp_ale;
846
847 for (ale = al->list_head; NULL != ale;)
848 {
849 if (NULL != ale->presentation)
850 GNUNET_free (ale->presentation);
851 tmp_ale = ale;
852 ale = ale->next;
853 GNUNET_free (tmp_ale);
854 }
855 GNUNET_free (al);
856}
857
858
859/**
860 * Get required size for serialization buffer
861 *
862 * @param attr the presentation to serialize
863 * @return the required buffer size
864 */
865size_t
866GNUNET_RECLAIM_presentation_serialize_get_size (
867 const struct GNUNET_RECLAIM_Presentation *presentation)
868{
869 return sizeof(struct Presentation) + presentation->data_size;
870}
871
872
873/**
874 * Serialize an presentation
875 *
876 * @param attr the presentation to serialize
877 * @param result the serialized presentation
878 * @return length of serialized data
879 */
880size_t
881GNUNET_RECLAIM_presentation_serialize (
882 const struct GNUNET_RECLAIM_Presentation *presentation,
883 char *result)
884{
885 struct Presentation *atts;
886 char *write_ptr;
887
888 atts = (struct Presentation *) result;
889 atts->presentation_type = htons (presentation->type);
890 atts->credential_id = presentation->credential_id;
891 write_ptr = (char *) &atts[1];
892 GNUNET_memcpy (write_ptr, presentation->data, presentation->data_size);
893 atts->data_size = htons (presentation->data_size);
894
895 return sizeof(struct Presentation) + presentation->data_size;
896}
897
898
899/**
900 * Deserialize an presentation
901 *
902 * @param data the serialized presentation
903 * @param data_size the length of the serialized data
904 *
905 * @return a GNUNET_IDENTITY_PROVIDER_Attribute, must be free'd by caller
906 */
907struct GNUNET_RECLAIM_Presentation *
908GNUNET_RECLAIM_presentation_deserialize (const char *data, size_t data_size)
909{
910 struct GNUNET_RECLAIM_Presentation *presentation;
911 struct Presentation *atts;
912 size_t data_len;
913 char *write_ptr;
914
915 if (data_size < sizeof(struct Presentation))
916 return NULL;
917
918 atts = (struct Presentation *) data;
919 data_len = ntohs (atts->data_size);
920 if (data_size < sizeof(struct Presentation) + data_len)
921 {
922 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
923 "Buffer too small to deserialize\n");
924 return NULL;
925 }
926 presentation = GNUNET_malloc (sizeof(struct GNUNET_RECLAIM_Presentation)
927 + data_len);
928 presentation->type = ntohs (atts->presentation_type);
929 presentation->credential_id = atts->credential_id;
930 presentation->data_size = data_len;
931
932 write_ptr = (char *) &presentation[1];
933 GNUNET_memcpy (write_ptr, &atts[1], data_len);
934 presentation->data = write_ptr;
935 return presentation;
936}
937
938
939struct GNUNET_RECLAIM_AttributeList*
940GNUNET_RECLAIM_presentation_get_attributes (const struct
941 GNUNET_RECLAIM_Presentation *
942 presentation)
943{
944 unsigned int i;
945 struct Plugin *plugin;
946 struct GNUNET_RECLAIM_AttributeList *ret;
947 init ();
948 for (i = 0; i < num_plugins; i++)
949 {
950 plugin = credential_plugins[i];
951 if (NULL !=
952 (ret = plugin->api->get_attributes_p (plugin->api->cls,
953 presentation)))
954 return ret;
955 }
956 return NULL;
957}
958
959
960char*
961GNUNET_RECLAIM_presentation_get_issuer (const struct
962 GNUNET_RECLAIM_Presentation *
963 presentation)
964{
965 unsigned int i;
966 struct Plugin *plugin;
967 char *ret;
968 init ();
969 for (i = 0; i < num_plugins; i++)
970 {
971 plugin = credential_plugins[i];
972 if (NULL !=
973 (ret = plugin->api->get_issuer_p (plugin->api->cls,
974 presentation)))
975 return ret;
976 }
977 return NULL;
978}
979
980
981int
982GNUNET_RECLAIM_presentation_get_expiration (const struct
983 GNUNET_RECLAIM_Presentation *
984 presentation,
985 struct GNUNET_TIME_Absolute*exp)
986{
987 unsigned int i;
988 struct Plugin *plugin;
989 init ();
990 for (i = 0; i < num_plugins; i++)
991 {
992 plugin = credential_plugins[i];
993 if (GNUNET_OK != plugin->api->get_expiration_p (plugin->api->cls,
994 presentation,
995 exp))
996 continue;
997 return GNUNET_OK;
998 }
999 return GNUNET_SYSERR;
1000}
1001
1002/**
1003 * Create a presentation from a credential and a lift of (selected)
1004 * attributes in the credential.
1005 * FIXME not yet implemented
1006 *
1007 * @param cred the credential to use
1008 * @param attrs the attributes to present from the credential
1009 * @return the credential presentation presenting the attributes according
1010 * to the presentation mechanism of the credential
1011 * or NULL on error.
1012 */
1013int
1014GNUNET_RECLAIM_credential_get_presentation (
1015 const struct GNUNET_RECLAIM_Credential *cred,
1016 const struct GNUNET_RECLAIM_AttributeList *attrs,
1017 struct GNUNET_RECLAIM_Presentation **pres)
1018{
1019 unsigned int i;
1020 struct Plugin *plugin;
1021 init ();
1022 for (i = 0; i < num_plugins; i++)
1023 {
1024 plugin = credential_plugins[i];
1025 if (GNUNET_OK != plugin->api->create_presentation (plugin->api->cls,
1026 cred,
1027 attrs,
1028 pres))
1029 continue;
1030 (*pres)->credential_id = cred->id;
1031 return GNUNET_OK;
1032 }
1033 return GNUNET_SYSERR;
1034}
1035
1036
1037