src/map_asn1.c
author Krista Grothoff <krista@pep-project.org>
Sat, 24 Sep 2016 18:09:18 +0200
branchENGINE-27
changeset 1185 4b01328f3cf2
parent 667 a3615d44326a
child 940 5f0a9d0f0ecd
permissions -rw-r--r--
ENGINE-27: update_identity no longer pays attention to the input identity's fpr, but pulls one from elect_pubkey.
     1 #include "pEp_internal.h"
     2 #include "map_asn1.h"
     3 
     4 Identity_t *Identity_from_Struct(
     5         const pEp_identity *ident,
     6         Identity_t *result
     7     )
     8 {
     9     assert(ident);
    10     if (!ident)
    11         return NULL;
    12 
    13     if (!result)
    14         result = (Identity_t *) calloc(1, sizeof(Identity_t));
    15     assert(result);
    16     if (!result)
    17         return NULL;
    18 
    19     if (ident->address) {
    20         result->address = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    21                 ident->address, -1);
    22         if (ident->address && !result->address)
    23             goto enomem;
    24     }
    25 
    26     if (ident->fpr) {
    27         if (OCTET_STRING_fromString(&result->fpr, ident->fpr))
    28             goto enomem;
    29     }
    30 
    31     if (ident->user_id) {
    32         result->user_id = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    33                 ident->user_id, -1);
    34         if (ident->user_id && !result->user_id)
    35             goto enomem;
    36     }
    37 
    38     if (ident->username) {
    39         result->username = OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    40                 ident->username, -1);
    41         if (ident->username && !result->username)
    42             goto enomem;
    43     }
    44 
    45     if (ident->comm_type != PEP_ct_unknown) {
    46         result->comm_type = malloc(sizeof(long));
    47         assert(result->comm_type);
    48         if (!result->comm_type)
    49             goto enomem;
    50         *result->comm_type = ident->comm_type;
    51     }
    52 
    53     if (ident->lang[0]) {
    54         result->lang = OCTET_STRING_new_fromBuf(&asn_DEF_ISO639_1,
    55                 ident->lang, 2);
    56         if (!result->lang)
    57             goto enomem;
    58     }
    59 
    60     return result;
    61 
    62 enomem:
    63     ASN_STRUCT_FREE(asn_DEF_Identity, result);
    64     return NULL;
    65 }
    66 
    67 pEp_identity *Identity_to_Struct(Identity_t *ident, pEp_identity *result)
    68 {
    69     assert(ident);
    70     if (!ident)
    71         return NULL;
    72     
    73     if (!result)
    74         result = new_identity(NULL, NULL, NULL, NULL);
    75     if (!result)
    76         return NULL;
    77 
    78     if (ident->address) {
    79         result->address = strndup((char *) ident->address->buf,
    80                 ident->address->size);
    81         assert(result->address);
    82         if (!result->address)
    83             goto enomem;
    84     }
    85 
    86     result->fpr = strndup((char *) ident->fpr.buf, ident->fpr.size);
    87     assert(result->fpr);
    88     if (!result->fpr)
    89         goto enomem;
    90 
    91     if (ident->user_id) {
    92         result->user_id = strndup((char *) ident->user_id->buf,
    93                 ident->user_id->size);
    94         assert(result->user_id);
    95         if (!result->user_id)
    96             goto enomem;
    97     }
    98 
    99     if (ident->username) {
   100         result->username = strndup((char *) ident->username->buf,
   101                 ident->username->size);
   102         assert(result->username);
   103         if (!result->username)
   104             goto enomem;
   105     }
   106 
   107     if (ident->comm_type)
   108         result->comm_type = (PEP_comm_type) *ident->comm_type;
   109 
   110     if (ident->lang) {
   111         result->lang[0] = ident->lang->buf[0];
   112         result->lang[1] = ident->lang->buf[1];
   113     }
   114 
   115     return result;
   116 
   117 enomem:
   118     free_identity(result);
   119     return NULL;
   120 }
   121 
   122 KeyList_t *KeyList_from_stringlist(
   123         const stringlist_t *list,
   124         KeyList_t *result
   125     )
   126 {
   127     assert(list);
   128     if (!list)
   129         return NULL;
   130 
   131     if (!result)
   132         result = (KeyList_t *) calloc(1, sizeof(KeyList_t));
   133     assert(result);
   134     if (!result)
   135         return NULL;
   136 
   137     for (const stringlist_t *l = list; l && l->value; l=l->next) {
   138         Hash_t *key = OCTET_STRING_new_fromBuf(&asn_DEF_Hash, l->value, -1);
   139         if (!key)
   140             goto enomem;
   141 
   142         if (ASN_SEQUENCE_ADD(&result->list, key)) {
   143             ASN_STRUCT_FREE(asn_DEF_Hash, key);
   144             goto enomem;
   145         }
   146     }
   147 
   148     return result;
   149 
   150 enomem:
   151     ASN_STRUCT_FREE(asn_DEF_KeyList, result);
   152     return NULL;
   153 }
   154 
   155 stringlist_t *KeyList_to_stringlist(KeyList_t *list, stringlist_t *result)
   156 {
   157     assert(list);
   158     if (!list)
   159         return NULL;
   160 
   161     if (!result)
   162         result = new_stringlist(NULL);
   163     if (!result)
   164         return NULL;
   165 
   166     stringlist_t *r = result;
   167     for (int i=0; i<list->list.count; i++) {
   168         char *str = strndup((char *) list->list.array[i]->buf,
   169                 list->list.array[i]->size);
   170         assert(str);
   171         if (!str)
   172             goto enomem;
   173         r = stringlist_add(r, str);
   174         free(str);
   175         if (!r)
   176             goto enomem;
   177     }
   178 
   179     return result;
   180 
   181 enomem:
   182     free_stringlist(result);
   183     return NULL;
   184 }
   185