text
stringlengths
0
357
* May be created by dc_render_setup_code() on another device or by
* a completely different app as Thunderbird/Enigmail or K-9.
* @return The decrypted private key as armored-ascii-data or NULL on errors.
* Must be dc_key_unref()'d.
*/
char* dc_decrypt_setup_file(dc_context_t* context, const char* passphrase, const char* filecontent)
{
char* fc_buf = NULL;
const char* fc_headerline = NULL;
const char* fc_base64 = NULL;
char* binary = NULL;
size_t binary_bytes = 0;
size_t indx = 0;
void* plain = NULL;
size_t plain_bytes = 0;
char* payload = NULL;
/* extract base64 from filecontent */
fc_buf = dc_strdup(filecontent);
if (!dc_split_armored_data(fc_buf, &fc_headerline, NULL, NULL, &fc_base64)
|| fc_headerline==NULL || strcmp(fc_headerline, "-----BEGIN PGP MESSAGE-----")!=0 || fc_base64==NULL) {
goto cleanup;
}
/* convert base64 to binary */
if (mailmime_base64_body_parse(fc_base64, strlen(fc_base64), &indx, &binary/*must be freed using mmap_string_unref()*/, &binary_bytes)!=MAILIMF_NO_ERROR
|| binary==NULL || binary_bytes==0) {
goto cleanup;
}
/* decrypt symmetrically */
if (!dc_pgp_symm_decrypt(context, passphrase, binary, binary_bytes, &plain, &plain_bytes)) {
goto cleanup;
}
payload = strndup((const char*)plain, plain_bytes);
cleanup:
free(plain);
free(fc_buf);
if (binary) { mmap_string_unref(binary); }
return payload;
}
/**
* Create random setup code.
*
* The created "Autocrypt Level 1" setup code has the form `1234-1234-1234-1234-1234-1234-1234-1234-1234`.
* Linebreaks and spaces are not added to the setup code, but the `-` are.
* The setup code is typically given to dc_render_setup_file().
*
* A higher-level function to initiate the key transfer is dc_initiate_key_transfer().
*
* @private @memberof dc_context_t
* @param context The context as created by dc_context_new().
* @return Setup code, must be free()'d after usage. NULL on errors.
*/
char* dc_create_setup_code(dc_context_t* context)
{
#define CODE_ELEMS 9
uint16_t random_val = 0;
int i = 0;
dc_strbuilder_t ret;
dc_strbuilder_init(&ret, 0);
for (i = 0; i < CODE_ELEMS; i++)
{
do
{
if (!RAND_bytes((unsigned char*)&random_val, sizeof(uint16_t))) {
dc_log_warning(context, 0, "Falling back to pseudo-number generation for the setup code.");
RAND_pseudo_bytes((unsigned char*)&random_val, sizeof(uint16_t));
}
}
while (random_val > 60000); /* make sure the modulo below does not reduce entropy (range is 0..65535, a module 10000 would make appearing values <=535 one time more often than other values) */
random_val = random_val % 10000; /* force all blocks into the range 0..9999 */
dc_strbuilder_catf(&ret, "%s%04i", i?"-":"", (int)random_val);
}
return ret.buf;
}
/* Function remove all special characters from the given code and brings it to the 9x4 form */
char* dc_normalize_setup_code(dc_context_t* context, const char* in)
{
if (in==NULL) {
return NULL;
}
dc_strbuilder_t out;
dc_strbuilder_init(&out, 0);
int outlen = 0;
const char* p1 = in;
while (*p1) {
if (*p1 >= '0' && *p1 <= '9') {
dc_strbuilder_catf(&out, "%c", *p1);