Increase version to 0.10.2
[libisds.git] / client / verifyreceivedhash.c
blob7b5c5dc8727a6c742d0ba7a31e85813a96f678d7
1 #include "../config.h"
2 #define _XOPEN_SOURCE XOPEN_SOURCE_LEVEL_FOR_STRDUP
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <locale.h>
6 #include <time.h>
7 #include <string.h>
8 #include <isds.h>
9 #include "common.h"
12 int main(void) {
13 struct isds_ctx *ctx = NULL;
14 isds_error err;
15 char *last_message_id = NULL;
17 setlocale(LC_ALL, "");
19 err = isds_init();
20 if (err) {
21 printf("isds_init() failed: %s\n", isds_strerror(err));
22 exit(EXIT_FAILURE);
25 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
27 ctx = isds_ctx_create();
28 if (!ctx) {
29 printf("isds_ctx_create() failed");
32 err = isds_set_timeout(ctx, 10000);
33 if (err) {
34 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
37 err = isds_login(ctx, url, username(), password(), NULL, NULL);
38 if (err) {
39 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
40 isds_long_message(ctx));
41 } else {
42 printf("Logged in :)\n");
47 /* Get list of received messages */
49 struct tm from_time_tm = {
50 .tm_year = 2000 - 1900,
51 .tm_mon = 1 - 1,
52 .tm_mday = 1,
53 .tm_hour = 1,
54 .tm_min = 2,
55 .tm_sec = 3,
56 .tm_isdst = -1
58 time_t from_time_t = mktime(&from_time_tm);
59 struct timeval from_time = {
60 .tv_sec = from_time_t,
61 .tv_usec = 4000
63 unsigned long int number = 0;
64 struct isds_list *messages = NULL, *item;
65 struct isds_message *last_message = NULL;
67 /* TODO: Try different criteria */
68 printf("Getting list of received messages\n");
69 err = isds_get_list_of_received_messages(ctx, &from_time, NULL, NULL,
70 MESSAGESTATE_ANY, 0, &number, &messages);
71 if (err)
72 printf("isds_get_list_of_received_messages() failed: %s: %s\n",
73 isds_strerror(err), isds_long_message(ctx));
74 else {
75 printf("isds_get_list_of_received_messages() succeeded: "
76 "number of messages = %lu:\n", number);
77 for(item = messages; item; item = item->next) {
78 last_message = (struct isds_message *) (item->data);
83 if (last_message) {
84 /*Save last message for latter refference */
85 if (last_message->envelope && last_message->envelope->dmID) {
86 last_message_id = strdup(last_message->envelope->dmID);
90 isds_list_free(&messages);
94 if (last_message_id) {
95 /* Download last message */
96 struct isds_message *message = NULL;
98 printf("Getting last received message with ID: %s\n", last_message_id);
99 err = isds_get_received_message(ctx, last_message_id, &message);
100 if (err)
101 printf("isds_get_received_message() failed: %s: %s\n",
102 isds_strerror(err), isds_long_message(ctx));
103 else {
104 printf("isds_get_received_message() succeeded:\n");
105 print_message(message);
109 /* Verify message hash */
110 printf("Verifying last received message hash against server\n");
111 err = isds_verify_message_hash(ctx, message);
112 if (!err) {
113 printf("isds_verify_message_hash() succeeded: "
114 "message is genuine\n");
115 printf("Computed hash: ");
116 print_hash(message->envelope->hash);
117 } else if (err == IE_NOTEQUAL) {
118 printf("isds_verify_message_hash() failed: message is a fake\n");
119 printf("Computed hash: ");
120 print_hash(message->envelope->hash);
121 printf("This should not happen\n");
122 } else {
123 printf("isds_verify_message_hash() failed: %s: %s\n",
124 isds_strerror(err), isds_long_message(ctx));
128 /* Download last signed message */
129 printf("Getting last signed received message with ID: %s\n",
130 last_message_id);
131 err = isds_get_signed_received_message(ctx, last_message_id, &message);
132 if (err)
133 printf("isds_get_signed_received_message() failed: %s: %s\n",
134 isds_strerror(err), isds_long_message(ctx));
135 else {
136 printf("isds_get_signed_received_message() succeeded:\n");
137 printf("\tMessage should have the same content ;)\n");
140 /* Verify signed message hash */
141 printf("Verifying last signed received message hash against server\n");
142 err = isds_verify_message_hash(ctx, message);
143 if (!err) {
144 printf("isds_verify_message_hash() succeeded: "
145 "message is genuine\n");
146 printf("Computed hash: ");
147 print_hash(message->envelope->hash);
148 } else if (err == IE_NOTEQUAL) {
149 printf("isds_verify_message_hash() failed: message is a fake\n");
150 printf("Computed hash: ");
151 print_hash(message->envelope->hash);
152 printf("This should not happen\n");
153 } else {
154 printf("isds_verify_message_hash() failed: %s: %s\n",
155 isds_strerror(err), isds_long_message(ctx));
159 isds_message_free(&message);
160 free(last_message_id);
165 err = isds_logout(ctx);
166 if (err) {
167 printf("isds_logout() failed: %s\n", isds_strerror(err));
171 err = isds_ctx_free(&ctx);
172 if (err) {
173 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
177 err = isds_cleanup();
178 if (err) {
179 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
182 exit (EXIT_SUCCESS);