24 #include "mrmailbox_internal.h" 25 #include "mraheader.h" 26 #include "mrapeerstate.h" 27 #include "mrmimeparser.h" 33 void mraheader_empty(mraheader_t* ths)
39 ths->m_prefer_encrypt = 0;
44 if( ths->m_public_key->m_binary ) {
45 mrkey_unref(ths->m_public_key);
46 ths->m_public_key = mrkey_new();
59 char* mraheader_render(
const mraheader_t* ths)
62 char* keybase64_wrapped = NULL;
64 mrstrbuilder_init(&ret);
66 if( ths==NULL || ths->m_addr==NULL || ths->m_public_key->m_binary==NULL || ths->m_public_key->m_type!=MR_PUBLIC ) {
70 mrstrbuilder_cat(&ret,
"addr=");
71 mrstrbuilder_cat(&ret, ths->m_addr);
72 mrstrbuilder_cat(&ret,
"; ");
74 if( ths->m_prefer_encrypt==MRA_PE_MUTUAL ) {
75 mrstrbuilder_cat(&ret,
"prefer-encrypt=mutual; ");
78 mrstrbuilder_cat(&ret,
"keydata= ");
82 if( (keybase64_wrapped = mrkey_render_base64(ths->m_public_key, 78,
" ", 0)) == NULL ) {
86 mrstrbuilder_cat(&ret, keybase64_wrapped);
91 if( !success ) { free(ret.m_buf); ret.m_buf = NULL; }
92 free(keybase64_wrapped);
102 static int add_attribute(mraheader_t* ths,
const char* name,
const char* value )
105 if( strcasecmp(name,
"addr")==0 )
108 || strlen(value) < 3 || strchr(value,
'@')==NULL || strchr(value,
'.')==NULL
112 ths->m_addr = mr_normalize_addr(value);
116 else if( strcasecmp(name,
"type")==0 )
118 if( value == NULL ) {
121 if( strcasecmp(value,
"1")==0 || strcasecmp(value,
"0" )==0 || strcasecmp(value,
"p" )==0 ) {
127 else if( strcasecmp(name,
"prefer-encrypt")==0 )
129 if( value && strcasecmp(value,
"mutual")==0 ) {
130 ths->m_prefer_encrypt = MRA_PE_MUTUAL;
135 else if( strcasecmp(name,
"keydata")==0 )
138 || ths->m_public_key->m_binary || ths->m_public_key->m_bytes ) {
141 return mrkey_set_from_base64(ths->m_public_key, value, MR_PUBLIC);
143 else if( name[0]==
'_' )
157 int mraheader_set_from_string(mraheader_t* ths,
const char* header_str__)
164 #define AHEADER_WS "\t\r\n " 165 char *header_str = NULL;
166 char *p, *beg_attr_name, *after_attr_name, *beg_attr_value;
169 mraheader_empty(ths);
170 ths->m_prefer_encrypt = MRA_PE_NOPREFERENCE;
172 if( ths == NULL || header_str__ == NULL ) {
176 header_str = safe_strdup(header_str__);
180 p += strspn(p, AHEADER_WS
"=;");
182 beg_attr_value = NULL;
183 p += strcspn(p, AHEADER_WS
"=;");
184 if( p != beg_attr_name )
188 p += strspn(p, AHEADER_WS);
191 p += strspn(p, AHEADER_WS
"=");
195 p += strcspn(p,
";");
200 mr_trim(beg_attr_value);
204 p += strspn(p, AHEADER_WS
";");
206 *after_attr_name =
'\0';
207 if( !add_attribute(ths, beg_attr_name, beg_attr_value) ) {
214 if( ths->m_addr && ths->m_public_key->m_binary ) {
220 if( !success ) { mraheader_empty(ths); }
233 mraheader_t* mraheader_new()
235 mraheader_t* ths = NULL;
237 if( (ths=calloc(1,
sizeof(mraheader_t)))==NULL ) {
241 ths->m_public_key = mrkey_new();
250 void mraheader_unref(mraheader_t* ths)
257 mrkey_unref(ths->m_public_key);
265 mraheader_t* mraheader_new_from_imffields(
const char* wanted_from,
const struct mailimf_fields* header)
268 mraheader_t* fine_header = NULL;
270 if( wanted_from == NULL || header == NULL ) {
274 for( cur = clist_begin(header->fld_list); cur!=NULL ; cur=clist_next(cur) )
276 struct mailimf_field* field = (
struct mailimf_field*)clist_content(cur);
277 if( field && field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD )
279 struct mailimf_optional_field* optional_field = field->fld_data.fld_optional_field;
280 if( optional_field && optional_field->fld_name && strcasecmp(optional_field->fld_name,
"Autocrypt")==0 )
283 mraheader_t* test = mraheader_new();
284 if( !mraheader_set_from_string(test, optional_field->fld_value)
285 || strcasecmp(test->m_addr, wanted_from)!=0 ) {
286 mraheader_unref(test);
290 if( fine_header == NULL ) {
294 mraheader_unref(fine_header);
295 mraheader_unref(test);