text
stringlengths
0
357
success = 1;
cleanup:
sqlite3_finalize(stmt);
return success;
}
/*******************************************************************************
* Main interface
******************************************************************************/
dc_apeerstate_t* dc_apeerstate_new(dc_context_t* context)
{
dc_apeerstate_t* peerstate = NULL;
if ((peerstate=calloc(1, sizeof(dc_apeerstate_t)))==NULL) {
exit(43); /* cannot allocate little memory, unrecoverable error */
}
peerstate->context = context;
return peerstate;
}
void dc_apeerstate_unref(dc_apeerstate_t* peerstate)
{
dc_apeerstate_empty(peerstate);
free(peerstate);
}
/**
* Render an Autocrypt-Gossip header value. The contained key is either
* public_key or gossip_key if public_key is NULL.
*
* @memberof dc_apeerstate_t
* @param peerstate The peerstate object.
* @return String that can be be used directly in an `Autocrypt-Gossip:` statement,
* `Autocrypt-Gossip:` is _not_ included in the returned string. If there
* is not key for the peer that can be gossiped, NULL is returned.
*/
char* dc_apeerstate_render_gossip_header(const dc_apeerstate_t* peerstate, int min_verified)
{
char* ret = NULL;
dc_aheader_t* autocryptheader = dc_aheader_new();
if (peerstate==NULL || peerstate->addr==NULL) {
goto cleanup;
}
autocryptheader->prefer_encrypt = DC_PE_NOPREFERENCE; /* the spec says, we SHOULD NOT gossip this flag */
autocryptheader->addr = dc_strdup(peerstate->addr);
autocryptheader->public_key = dc_key_ref(dc_apeerstate_peek_key(peerstate, min_verified)); /* may be NULL */
ret = dc_aheader_render(autocryptheader);
cleanup:
dc_aheader_unref(autocryptheader);
return ret;
}
/**
* Return either public_key or gossip_key if public_key is null or not verified.
* The function does not check if the keys are valid but the caller can assume
* the returned key has data.
*
* This function does not do the Autocrypt encryption recommendation; it just
* returns a key that can be used.
*
* @memberof dc_apeerstate_t
* @param peerstate The peerstate object.
* @param min_verified The minimal verification criterion the key should match.
* Typically either DC_NOT_VERIFIED (0) if there is no need for the key being verified
* or DC_BIDIRECT_VERIFIED (2) for bidirectional verification requirement.
* @return public_key or gossip_key, NULL if nothing is available.
* the returned pointer MUST NOT be unref()'d.
*/
dc_key_t* dc_apeerstate_peek_key(const dc_apeerstate_t* peerstate, int min_verified)
{
if ( peerstate==NULL
|| (peerstate->public_key && (peerstate->public_key->binary==NULL || peerstate->public_key->bytes<=0))
|| (peerstate->gossip_key && (peerstate->gossip_key->binary==NULL || peerstate->gossip_key->bytes<=0))
|| (peerstate->verified_key && (peerstate->verified_key->binary==NULL || peerstate->verified_key->bytes<=0))) {
return NULL;
}
if (min_verified)
{
return peerstate->verified_key;
}
if (peerstate->public_key)
{
return peerstate->public_key;
}