|
|
|
@ -1,12 +1,16 @@
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
#include "rsa_signature_2017.h"
|
|
|
|
|
|
|
|
|
|
#include "model/ap/activity.h"
|
|
|
|
|
#include "model/crypto/keys.h"
|
|
|
|
|
|
|
|
|
|
#include "rdf/serial.h"
|
|
|
|
|
#include "rdf/memory_store.h"
|
|
|
|
|
#include "rdf/normalize.h"
|
|
|
|
|
|
|
|
|
|
#include "collections/iface.h"
|
|
|
|
|
#include "sha256/sha256.h"
|
|
|
|
|
|
|
|
|
|
#include "collections/collection.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
@ -36,16 +40,18 @@ static struct rdf_serialize_field activity_ref_types[] = {
|
|
|
|
|
struct rdf_serialize_field ap_activity_rdf[] = {
|
|
|
|
|
{ "", offsetof( struct ap_activity, id ), &rdf_id_field },
|
|
|
|
|
{ "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>", offsetof( struct ap_activity, type ), &rdf_enum_field, &types },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#actor>", offsetof( struct ap_activity, actor ), &rdf_string_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#to>", offsetof( struct ap_activity, to ), &rdf_array_of_field, &rdf_string_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#cc>", offsetof( struct ap_activity, cc ), &rdf_array_of_field, &rdf_string_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#bcc>", offsetof( struct ap_activity, bcc ), &rdf_array_of_field, &rdf_string_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#actor>", offsetof( struct ap_activity, actor ), &rdf_string_ref_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#to>", offsetof( struct ap_activity, to ), &rdf_array_of_field, &rdf_string_ref_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#cc>", offsetof( struct ap_activity, cc ), &rdf_array_of_field, &rdf_string_ref_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#bcc>", offsetof( struct ap_activity, bcc ), &rdf_array_of_field, &rdf_string_ref_field },
|
|
|
|
|
{ "<https://www.w3.org/ns/activitystreams#object>", offsetof( struct ap_activity, object.tag ), &rdf_tagged_union_field, activity_ref_types },
|
|
|
|
|
{ NULL, sizeof(struct ap_activity) },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct rdf_serialize_field ap_activity_signature_rdf[] = {
|
|
|
|
|
{ "", 0, &rdf_id_blank },
|
|
|
|
|
{ "", 0, &rdf_id_blank },
|
|
|
|
|
{ "<http://purl.org/dc/terms/creator>", offsetof( struct ap_signature, creator ), &rdf_string_ref_field },
|
|
|
|
|
{ "<http://purl.org/dc/terms/created>", offsetof( struct ap_signature, created ), &rdf_date_field },
|
|
|
|
|
{ NULL },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -62,7 +68,7 @@ char* serialize_normalized_object( struct rdf_serialize_field* layout, void* obj
|
|
|
|
|
rdf_serialize_object( &rs, layout, object );
|
|
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
|
struct collection_iface i;
|
|
|
|
|
struct collection i;
|
|
|
|
|
rdf_memory_store_as_collection( ms, &i );
|
|
|
|
|
rdf_normalize(i);
|
|
|
|
|
|
|
|
|
@ -84,29 +90,53 @@ cleanup:
|
|
|
|
|
return outbuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ap_activity_create_rsa_signature_2017( struct ap_activity* act )
|
|
|
|
|
static bool calculate_hash_for_object( struct rdf_serialize_field* layout, void* object, char* hash )
|
|
|
|
|
{
|
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
|
|
// Serialize to normalized RDF
|
|
|
|
|
char* buffer = serialize_normalized_object( layout, object, &size );
|
|
|
|
|
if( !buffer ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash with SHA-256
|
|
|
|
|
sha256_easy_hash( buffer, size, &hash[0] );
|
|
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
|
printf( "RDF: \n" );
|
|
|
|
|
fwrite( buffer, size, 1, stdout );
|
|
|
|
|
printf( "\nHash: " );
|
|
|
|
|
for( int i = 0; i < 32; ++i ) {
|
|
|
|
|
printf( "%02X", (int)((unsigned char*)hash)[i] );
|
|
|
|
|
}
|
|
|
|
|
printf( "\n" );
|
|
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ap_activity_create_rsa_signature_2017( struct ap_activity* act, struct crypto_keys* keys )
|
|
|
|
|
{
|
|
|
|
|
char* activity_buf = NULL;
|
|
|
|
|
char* signature_buf = NULL;
|
|
|
|
|
printf( "ap_activity_create_rsa_signature_2017\n" );
|
|
|
|
|
|
|
|
|
|
size_t activity_buf_size = 0;
|
|
|
|
|
activity_buf = serialize_normalized_object( ap_activity_rdf, act, &activity_buf_size );
|
|
|
|
|
if( !activity_buf ) { return /* false */; }
|
|
|
|
|
// Setup signature
|
|
|
|
|
asprintf( &act->signature.creator, "%s#main-key", act->actor );
|
|
|
|
|
act->signature.created = time(NULL);
|
|
|
|
|
|
|
|
|
|
size_t signature_buf_size = 0;
|
|
|
|
|
signature_buf = serialize_normalized_object( ap_activity_signature_rdf, &act->signature, &signature_buf_size );
|
|
|
|
|
if( !signature_buf ) { return /* false */; }
|
|
|
|
|
char raw_hash[64];
|
|
|
|
|
if( !calculate_hash_for_object( ap_activity_rdf, act, &raw_hash[0] ) ) { return NULL; }
|
|
|
|
|
if( !calculate_hash_for_object( ap_activity_signature_rdf, &act->signature, &raw_hash[32] ) ) { return NULL; }
|
|
|
|
|
|
|
|
|
|
printf( "\nRDF (size=%d):\n", activity_buf_size );
|
|
|
|
|
fwrite( activity_buf, 1, activity_buf_size, stdout );
|
|
|
|
|
printf( "\n" );
|
|
|
|
|
fwrite( signature_buf, 1, signature_buf_size, stdout );
|
|
|
|
|
printf( "\n" );
|
|
|
|
|
char hash[32];
|
|
|
|
|
sha256_easy_hash( raw_hash, 64, &hash[0] );
|
|
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
free(activity_buf);
|
|
|
|
|
free(signature_buf);
|
|
|
|
|
return;
|
|
|
|
|
char* sign = crypto_keys_sign( keys, &hash[0], 32 );
|
|
|
|
|
act->signature.value = sign;
|
|
|
|
|
act->signature.type = apst_rsa_signature_2017;
|
|
|
|
|
printf( "act->signature = %s\n", sign );
|
|
|
|
|
return (act->has_signature = !!act->signature.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|