text
stringlengths
0
357
free(key);
}
int dc_key_set_from_binary(dc_key_t* key, const void* data, int bytes, int type)
{
dc_key_empty(key);
if (key==NULL || data==NULL || bytes <= 0) {
return 0;
}
key->binary = malloc(bytes);
if (key->binary==NULL) {
exit(40);
}
memcpy(key->binary, data, bytes);
key->bytes = bytes;
key->type = type;
return 1;
}
int dc_key_set_from_key(dc_key_t* key, const dc_key_t* o)
{
dc_key_empty(key);
if (key==NULL || o==NULL) {
return 0;
}
return dc_key_set_from_binary(key, o->binary, o->bytes, o->type);
}
int dc_key_set_from_stmt(dc_key_t* key, sqlite3_stmt* stmt, int index, int type)
{
dc_key_empty(key);
if (key==NULL || stmt==NULL) {
return 0;
}
return dc_key_set_from_binary(key, (unsigned char*)sqlite3_column_blob(stmt, index), sqlite3_column_bytes(stmt, index), type);
}
int dc_key_set_from_base64(dc_key_t* key, const char* base64, int type)
{
size_t indx = 0, result_len = 0;
char* result = NULL;
dc_key_empty(key);
if (key==NULL || base64==NULL) {
return 0;
}
if (mailmime_base64_body_parse(base64, strlen(base64), &indx, &result/*must be freed using mmap_string_unref()*/, &result_len)!=MAILIMF_NO_ERROR
|| result==NULL || result_len==0) {
return 0; /* bad key */
}
dc_key_set_from_binary(key, result, result_len, type);
mmap_string_unref(result);
return 1;
}
int dc_key_set_from_file(dc_key_t* key, const char* pathNfilename, dc_context_t* context)
{
char* buf = NULL;
const char* headerline = NULL; // just pointer inside buf, must not be freed
const char* base64 = NULL; // - " -
size_t buf_bytes = 0;
int type = -1;
int success = 0;
dc_key_empty(key);
if (key==NULL || pathNfilename==NULL) {
goto cleanup;
}
if (!dc_read_file(context, pathNfilename, (void**)&buf, &buf_bytes)
|| buf_bytes < 50) {
goto cleanup; /* error is already loged */
}
if (!dc_split_armored_data(buf, &headerline, NULL, NULL, &base64)
|| headerline==NULL || base64==NULL) {
goto cleanup;
}
if (strcmp(headerline, "-----BEGIN PGP PUBLIC KEY BLOCK-----")==0) {
type = DC_KEY_PUBLIC;
}
else if (strcmp(headerline, "-----BEGIN PGP PRIVATE KEY BLOCK-----")==0) {
type = DC_KEY_PRIVATE;
}
else {
dc_log_warning(context, 0, "Header missing for key \"%s\".", pathNfilename);
goto cleanup;
}