Commit 99121ea1 authored by stettberger's avatar stettberger

Verify of signed messages

parent 7b3c5fb5
......@@ -29,6 +29,7 @@
#include <signal.h>
#include <errno.h>
#include <getopt.h>
#include <dirent.h>
/* {{{ Structs, Globals Declarations */
#define VERSION "0.1 beta"
......@@ -338,7 +339,7 @@ void proc_channels_input(Channel *c, char *buf) {
cuser=silc_client_on_channel(c->silc, silc_client->conn->local_entry);
char *str=silc_client_chumode_char(cuser->mode);
print_out(c->silc->channel_name, "%s<%s%s> %s",
silc_client->sign?"[SIGNED] ":"",
silc_client->sign?"[S] ":"",
str?str:" ",
silc_client->conn->local_entry->nickname,
msg);
......@@ -514,6 +515,92 @@ char *silc_client_umode(SilcUInt32 mode) {
return stat;
}
/* }}} */
/* {{{ silc_verify_payload */
char *silc_verify_message(SilcClientEntry sender, SilcMessagePayload message) {
char *retval=malloc(5);
if(!retval){
perror("si: Couldn't allocate memory");
return NULL;
}
sprintf(retval, "[?] ");
SilcMessageSignedPayload sig = silc_message_get_signature(message);
if(!sig) return retval;
unsigned char *pk_data;
SilcUInt32 pk_datalen;
int i;
char *fpKey;
SilcPublicKey pk = silc_message_signed_get_public_key(sig, &pk_data, &pk_datalen);
if (!pk) {
if(sender->fingerprint){
fpKey=silc_fingerprint(sender->fingerprint, sender->fingerprint_len);
}
else
return retval;
}
else
fpKey = silc_hash_fingerprint(NULL, pk_data, pk_datalen);
char *fp=malloc(strlen(fpKey)+1);
if (!fp) {
perror("si: Couldn't allocate memory");
return retval;
}
for (i=0; i<strlen(fpKey); i++){
if (fpKey[i]==' ')
fp[i]='_';
else
fp[i]=fpKey[i];
}
fp[i]=0;
free(fpKey);
DIR *dir;
char *path=malloc(strlen((char *)getenv("HOME"))+40);
if (!path) {
perror("si: Couldn't allocate memory");
return retval;
}
sprintf(path, "%s/.silc/clientkeys", (char *)getenv("HOME"));
create_dirtree(path);
dir=opendir(path);
struct dirent *entry;
while((entry=readdir(dir))) {
if(!strncmp(entry->d_name, fp, strlen(fp))){
path=realloc(path, strlen((char *)getenv("HOME"))+50+strlen(entry->d_name));
if (!path) {
perror("si: Couldn't allocate memory");
return retval;
}
sprintf(path, "%s/.silc/clientkeys/%s", (char *)getenv("HOME"), entry->d_name);
free(fp);
SilcPublicKey cached_pk=NULL;
/* try to load the file */
if (!silc_pkcs_load_public_key(path, &cached_pk, SILC_PKCS_FILE_PEM) &&
!silc_pkcs_load_public_key(path, &cached_pk, SILC_PKCS_FILE_BIN))
if(!pk)
return retval;
if(cached_pk){
if(pk)
silc_pkcs_public_key_free(pk);
pk = cached_pk;
}
/* the public key is now in pk, our "level of trust" in ret */
if ((pk) && silc_message_signed_verify(sig, message, pk,
silc_client->client->sha1hash)!= SILC_AUTH_OK)
sprintf(retval, "[F] ");
else
sprintf(retval, "[S] ");
if (pk)
silc_pkcs_public_key_free(pk);
return retval;
}
}
free(fp);
return retval;
}
/* }}} */
/* {{{ callback_get_client_entry */
void callback_get_client_entry (SilcClient client,
SilcClientConnection conn,
......@@ -765,11 +852,15 @@ silc_channel_message(SilcClient client, SilcClientConnection conn,
SilcChannelUser cuser;
cuser=silc_client_on_channel(channel, sender);
char *str=silc_client_chumode_char(cuser->mode);
char *sigstat=NULL;
if(flags & SILC_MESSAGE_FLAG_SIGNED){
sigstat=silc_verify_message(sender,payload);
}
if (flags & SILC_MESSAGE_FLAG_ACTION)
print_out(channel->channel_name, "%s* %s %s", (flags & SILC_MESSAGE_FLAG_SIGNED)?"[SIGNED] ":"",sender->nickname, (char *)content);
print_out(channel->channel_name, "%s* %s %s", sigstat?sigstat:"",sender->nickname, (char *)content);
else
print_out(channel->channel_name, "%s<%s%s> %s", (flags & SILC_MESSAGE_FLAG_SIGNED)?"[SIGNED] ":"", str?str:" ", sender->nickname, (char *)content);
print_out(channel->channel_name, "%s<%s%s> %s", sigstat?sigstat:"", str?str:" ", sender->nickname, (char *)content);
free(str);
}
/* }}} */
......@@ -1022,37 +1113,47 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
SilcPublicKey key;
void *entry;
char *filename, *pos;
int i;
type=va_arg(va, int);
entry=va_arg(va, void *);
key=va_arg(va, SilcPublicKey);
unsigned char *pk;
SilcUInt32 pk_len;
pk = silc_pkcs_public_key_encode(key, &pk_len);
char *fpKey = silc_hash_fingerprint(0, pk, pk_len);
char *fp=malloc(strlen(fpKey)+1);
if (!fp) {
perror("si: Couldn't allocate memory");
return;
}
for (i=0; i<strlen(fpKey); i++){
if (fpKey[i]==' ')
fp[i]='_';
else
fp[i]=fpKey[i];
}
fp[i]=0;
free(fpKey);
if(type== SILC_ID_SERVER) {
server=(SilcServerEntry)entry;
/* $HOME/.silc/clientkeys/name:server */
filename=malloc(strlen(getenv("HOME"))+strlen(server->server_name)+30);
if(!filename){
perror("si: Couldn't allocate memory");
return;
}
sprintf(filename, "%s/.silc/incoming_keys/%s:server", getenv("HOME"), server->server_name);
}
else if(type== SILC_ID_CHANNEL) {
channel=(SilcChannelEntry)entry;
filename=malloc(strlen(getenv("HOME"))+strlen(channel->channel_name)+30);
/* $HOME/.silc/clientkeys/fp(name):server */
filename=malloc(strlen(getenv("HOME"))+strlen(server->server_name)+30+strlen(fp));
if(!filename){
perror("si: Couldn't allocate memory");
return;
}
sprintf(filename, "%s/.silc/incoming_keys/%s:channel", getenv("HOME"), channel->channel_name);
sprintf(filename, "%s/.silc/incoming_keys/%s(%s):server", getenv("HOME"), fp, server->server_name);
}
else if(type== SILC_ID_CLIENT) {
sender=(SilcClientEntry)entry;
filename=malloc(strlen(getenv("HOME"))+strlen(sender->nickname)+30);
filename=malloc(strlen(getenv("HOME"))+strlen(sender->nickname)+30+strlen(fp));
if(!filename){
perror("si: Couldn't allocate memory");
return;
}
sprintf(filename, "%s/.silc/incoming_keys/%s", getenv("HOME"), sender->nickname);
sprintf(filename, "%s/.silc/incoming_keys/%s(%s)", getenv("HOME"), fp, sender->nickname);
}
pos=strrchr(filename, '/');
pos[0]=0;
......@@ -1078,9 +1179,10 @@ silc_connected(SilcClient si, SilcClientConnection conn,
SilcChannelEntry channel;
Silc client = si->application;
if (status == SILC_CLIENT_CONN_ERROR) {
if (status >= SILC_CLIENT_CONN_ERROR) {
fprintf(stderr, "si: Could not connect to server\n");
silc_client_close_connection(si, conn);
exit(-1);
return;
}
......@@ -1360,6 +1462,7 @@ silc_private_message(SilcClient client, SilcClientConnection conn,
Query *q;
char *path=NULL;
char *sigstat=NULL;
for (q=queries; q; q=q->next)
if (SILC_ID_CLIENT_COMPARE(sender->id, q->silc->id)){
/* We have an open query */
......@@ -1371,7 +1474,10 @@ silc_private_message(SilcClient client, SilcClientConnection conn,
sprintf(path, "query/%s", sender->nickname);
return;
}
print_out(path?path:"", "%s<%s> %s",(flags & SILC_MESSAGE_FLAG_SIGNED)?"[SIGNED] ":"", sender->nickname, (char *)msg);
if(flags & SILC_MESSAGE_FLAG_SIGNED)
sigstat=silc_verify_message(sender, payload);
print_out(path?path:"", "%s<%s> %s",sigstat?sigstat:"", sender->nickname, (char *)msg);
free(path);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment