|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
#include "http_sign.h"
|
|
|
|
|
|
|
|
|
|
#include "sha256/sha256.h"
|
|
|
|
|
#include "model/account.h"
|
|
|
|
|
#include "model/crypto/keys.h"
|
|
|
|
|
#include "model/crypto/base64.h"
|
|
|
|
|
#include "model/ap/inbox_envelope.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
@ -54,6 +57,212 @@ bool http_signature_make( const char* inbox, struct crypto_keys* keys, struct ht
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
static void print_downcase( FILE* f, const char* str )
|
|
|
|
|
{
|
|
|
|
|
for(;*str;++str) {
|
|
|
|
|
switch( *str ) {
|
|
|
|
|
case 'A' ... 'Z':
|
|
|
|
|
fputc( *str - 'A' + 'a', f );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fputc( *str, f );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_header_line( FILE* hl, struct ap_envelope* env, const char* header_name )
|
|
|
|
|
{
|
|
|
|
|
fprintf( hl, "%s: ", header_name );
|
|
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
|
for( int i = 0; i < env->headers.count; ++i ) {
|
|
|
|
|
if( 0 == strcasecmp(header_name,env->headers.items[i].key ) ) {
|
|
|
|
|
if( !first ) {
|
|
|
|
|
fprintf( hl, "," );
|
|
|
|
|
}
|
|
|
|
|
fprintf( hl, "%s", env->headers.items[i].value );
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf( hl, "\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool validate_body_digest( struct ap_envelope* env )
|
|
|
|
|
{
|
|
|
|
|
// Per: https://datatracker.ietf.org/doc/html/rfc3230
|
|
|
|
|
|
|
|
|
|
char* digest_header = NULL;
|
|
|
|
|
char* content_length_header = NULL;
|
|
|
|
|
char* hash = NULL;
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
// Get the Digest header
|
|
|
|
|
for( int i = 0; i < env->headers.count; ++i ) {
|
|
|
|
|
char* key = env->headers.items[i].key;
|
|
|
|
|
char* value = env->headers.items[i].value;
|
|
|
|
|
if( 0 == strcasecmp( "Digest", key ) ) {
|
|
|
|
|
free(digest_header);
|
|
|
|
|
digest_header = strdup(value);
|
|
|
|
|
} else if( 0 == strcasecmp( "Content-Length", key ) ) {
|
|
|
|
|
free(content_length_header);
|
|
|
|
|
content_length_header = strdup(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( !digest_header ) { goto failed; }
|
|
|
|
|
if( !content_length_header ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
// Validate the body length against the Content-Length header
|
|
|
|
|
int content_length = -1;
|
|
|
|
|
sscanf( content_length_header, "%d", &content_length );
|
|
|
|
|
if( strlen(env->body) != content_length ) {
|
|
|
|
|
printf( "Content-Length mismatch\n" );
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* digest = NULL;
|
|
|
|
|
char* algo = strtok_r(digest_header, "=", &digest );
|
|
|
|
|
|
|
|
|
|
// Only implementing SHA-256
|
|
|
|
|
if( 0 != strcmp( algo, "SHA-256") ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
// Calculate sha-256 hash
|
|
|
|
|
char raw_hash[32];
|
|
|
|
|
sha256_easy_hash( env->body, content_length, raw_hash );
|
|
|
|
|
hash = base64_strict_encode(raw_hash,32);
|
|
|
|
|
|
|
|
|
|
result = ( 0 == strcmp(digest,hash) );
|
|
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
free(hash);
|
|
|
|
|
free(digest_header);
|
|
|
|
|
return result;
|
|
|
|
|
failed:
|
|
|
|
|
result = false;
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_signature_validate( struct ap_envelope* env )
|
|
|
|
|
{
|
|
|
|
|
char* signature_header = NULL;
|
|
|
|
|
char* date_header = NULL;
|
|
|
|
|
char* algorithm = NULL;
|
|
|
|
|
char* key_id = NULL;
|
|
|
|
|
char* headers = NULL;
|
|
|
|
|
char* signature = NULL;
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
// Get the Signature header
|
|
|
|
|
for( int i = 0; i < env->headers.count; ++i ) {
|
|
|
|
|
char* key = env->headers.items[i].key;
|
|
|
|
|
char* value = env->headers.items[i].value;
|
|
|
|
|
if( 0 == strcasecmp( "Signature", key ) ) {
|
|
|
|
|
free(signature_header);
|
|
|
|
|
signature_header = strdup(value);
|
|
|
|
|
} else if( 0 == strcasecmp( "Date", key ) ) {
|
|
|
|
|
free(date_header);
|
|
|
|
|
date_header = strdup(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( !signature_header ) { return false; }
|
|
|
|
|
printf( "Found Signature: %s\n", signature_header );
|
|
|
|
|
|
|
|
|
|
// Validate time
|
|
|
|
|
if( date_header ) {
|
|
|
|
|
printf( "TODO: validate received within window\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Break apart the header
|
|
|
|
|
char* rem = NULL;
|
|
|
|
|
for( char* part = strtok_r(signature_header,",",&rem); part; part = strtok_r(NULL,",",&rem) ) {
|
|
|
|
|
// Split apart the key="value" pair
|
|
|
|
|
char* key;
|
|
|
|
|
char* value;
|
|
|
|
|
key = strtok_r( part, "=", &value );
|
|
|
|
|
|
|
|
|
|
// Strip the "" off
|
|
|
|
|
if( value[0] != '\"' ) { goto failed; }
|
|
|
|
|
value = &value[1];
|
|
|
|
|
int len = strlen(value);
|
|
|
|
|
if( value[len-1] != '\"' ) { goto failed; }
|
|
|
|
|
value[len-1] = '\0';
|
|
|
|
|
|
|
|
|
|
// Handle parts
|
|
|
|
|
if( 0 == strcmp(key,"keyId") ) {
|
|
|
|
|
key_id = value;
|
|
|
|
|
} else if( 0 == strcmp(key,"algorithm") ) {
|
|
|
|
|
algorithm = value;
|
|
|
|
|
} else if( 0 == strcmp(key,"headers") ) {
|
|
|
|
|
headers = value;
|
|
|
|
|
} else if( 0 == strcmp(key,"signature") ) {
|
|
|
|
|
signature = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf( "\t%s = %s\n", key, value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure we got all the required information
|
|
|
|
|
if( !signature ) { goto failed; }
|
|
|
|
|
if( !headers ) { goto failed; }
|
|
|
|
|
if( !key_id ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
// Get actor URI
|
|
|
|
|
char* key_name = NULL;
|
|
|
|
|
char* actor_uri = strtok_r( key_id, "#", &key_name );
|
|
|
|
|
if( !actor_uri ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
// Get the account
|
|
|
|
|
struct account* actor = account_fetch_from_uri(actor_uri);
|
|
|
|
|
if( !actor ) { printf( "failed to load account for %s\n", actor_uri ); goto failed; }
|
|
|
|
|
|
|
|
|
|
// Get the public key
|
|
|
|
|
struct crypto_keys* keys = account_get_public_key( actor, key_name );
|
|
|
|
|
if( !keys ) { goto failed; }
|
|
|
|
|
|
|
|
|
|
// Create the hash line
|
|
|
|
|
char* hash_line = NULL;
|
|
|
|
|
size_t hash_line_size = 0;
|
|
|
|
|
FILE* hl = open_memstream( &hash_line, &hash_line_size );
|
|
|
|
|
|
|
|
|
|
for( char* part = strtok_r( headers, " ", &rem ); part; part = strtok_r(NULL," ",&rem) ) {
|
|
|
|
|
printf( "header: %s\n", part );
|
|
|
|
|
if( 0 == strcmp(part,"(request-target)") ) {
|
|
|
|
|
fprintf( hl, "(request-target): post /inbox\n" );
|
|
|
|
|
} else if( 0 == strcmp(part,"digest") ) {
|
|
|
|
|
if( !validate_body_digest( env ) ) {
|
|
|
|
|
printf( "Digest validation failed\n" );
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
print_header_line( hl, env, part );
|
|
|
|
|
} else {
|
|
|
|
|
print_header_line( hl, env, part );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(hl);
|
|
|
|
|
|
|
|
|
|
hash_line = realloc(hash_line, hash_line_size+1 );
|
|
|
|
|
hash_line[hash_line_size] = '\0';
|
|
|
|
|
printf( "\nhash_line:\n%s\n", hash_line );
|
|
|
|
|
|
|
|
|
|
char raw_hash[32];
|
|
|
|
|
sha256_easy_hash( hash_line, hash_line_size, raw_hash );
|
|
|
|
|
|
|
|
|
|
char* signature_bin = NULL;
|
|
|
|
|
size_t signature_len = 0;
|
|
|
|
|
if( !base64_decode( signature, (void**)&signature_bin, &signature_len ) ) {
|
|
|
|
|
printf( "Failed to decode base64 signature\n" );
|
|
|
|
|
goto failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf( "TODO: validate HTTP signature - so far, so good\n" );
|
|
|
|
|
cleanup:
|
|
|
|
|
free(signature_header);
|
|
|
|
|
return result;
|
|
|
|
|
failed:
|
|
|
|
|
result = false;
|
|
|
|
|
goto cleanup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_signature_free( struct http_signature* sign )
|
|
|
|
|
{
|
|
|
|
|