main.c (3464B)
1 #include <stdlib.h> 2 #include <string.h> 3 #include <fcntl.h> 4 #include <errno.h> 5 6 #include <basedir.h> 7 #include <cwalk.h> 8 9 #include <lq/base.h> 10 #include <lq/crypto.h> 11 #include <lq/config.h> 12 #include <lq/io.h> 13 #include <lq/err.h> 14 #include <lq/cert.h> 15 #include <lq/msg.h> 16 #include <lq/envelope.h> 17 18 #define INIT_LQ 0x01 19 #define INIT_CRYPTO 0x02 20 #define INIT_ALICE 0x04 21 #define INIT_BOB 0x08 22 23 static int init_state; 24 static xdgHandle xdg; 25 static LQPrivKey *pk_alice; 26 static LQPubKey *pubk_alice; 27 static LQPrivKey *pk_bob; 28 static LQPubKey *pubk_bob; 29 char passphrase_alice[] = "1234"; 30 char passphrase_bob[] = "5678"; 31 32 33 int lq_ui_init() { 34 int r; 35 char *path[8]; 36 char outpath[LQ_PATH_MAX]; 37 38 xdgInitHandle(&xdg); 39 lq_init(); 40 init_state |= INIT_LQ; 41 42 // Set up storage path. 43 path[0] = (char*)xdgCacheHome(&xdg); 44 path[1] = "libqaeda"; 45 path[2] = NULL; 46 cwk_path_join_multiple((const char**)path, outpath, LQ_PATH_MAX); 47 ensuredir(outpath); 48 49 // Set up configuration. 50 r = lq_config_set(LQ_CFG_DIR_BASE, outpath); 51 if (r) { 52 return ERR_FAIL; 53 } 54 r = lq_config_set(LQ_CFG_DIR_DATA, outpath); 55 if (r) { 56 return ERR_FAIL; 57 } 58 59 // Initialize crypto subsystem. 60 r = lq_crypto_init(outpath); 61 if (r) { 62 return ERR_FAIL; 63 } 64 65 return ERR_OK; 66 } 67 68 void lq_ui_free() { 69 if (init_state & INIT_BOB) { 70 lq_publickey_free(pubk_bob); 71 lq_privatekey_free(pk_bob); 72 } 73 if (init_state & INIT_ALICE) { 74 lq_publickey_free(pubk_alice); 75 lq_privatekey_free(pk_alice); 76 } 77 if (init_state & INIT_CRYPTO) { 78 lq_crypto_free(); 79 } 80 if (init_state & INIT_LQ) { 81 xdgWipeHandle(&xdg); 82 lq_finish(); 83 } 84 } 85 86 87 int main(int argc, char **argv) { 88 int f; 89 int r; 90 LQCert *cert; 91 LQMsg *req; 92 LQMsg *res; 93 LQEnvelope *env; 94 char out[LQ_BLOCKSIZE]; 95 size_t out_len; 96 97 r = lq_ui_init(); 98 if (r) { 99 return 1; 100 } 101 102 pk_alice = lq_privatekey_load(passphrase_alice, strlen(passphrase_alice), NULL); 103 if (pk_alice == NULL) { 104 lq_ui_free(); 105 return 1; 106 } 107 pubk_alice = lq_publickey_from_privatekey(pk_alice); 108 if (pubk_alice == NULL) { 109 lq_ui_free(); 110 return 1; 111 } 112 pk_bob = lq_privatekey_load(passphrase_bob, strlen(passphrase_bob), NULL); 113 if (pk_bob == NULL) { 114 lq_ui_free(); 115 return 1; 116 } 117 pubk_bob = lq_publickey_from_privatekey(pk_bob); 118 if (pubk_bob == NULL) { 119 lq_ui_free(); 120 return 1; 121 } 122 123 req = lq_msg_new("foo", 4); 124 if (req == NULL) { 125 lq_ui_free(); 126 return 1; 127 } 128 129 cert = lq_certificate_new(NULL); 130 r = lq_certificate_request(cert, req, pk_alice); 131 if (r != ERR_OK) { 132 lq_certificate_free(cert); 133 lq_ui_free(); 134 return 1; 135 } 136 137 res = lq_msg_new("barbaz", 7); 138 if (res == NULL) { 139 lq_certificate_free(cert); 140 lq_ui_free(); 141 return 1; 142 } 143 lq_msg_literal(res); 144 r = lq_certificate_respond(cert, res, pk_bob); 145 if (r != ERR_OK) { 146 lq_certificate_free(cert); 147 lq_ui_free(); 148 return 1; 149 } 150 151 r = lq_certificate_verify(cert); 152 if (r != ERR_OK) { 153 lq_certificate_free(cert); 154 lq_ui_free(); 155 return 1; 156 } 157 158 env = lq_envelope_new(cert, 42); 159 out_len = LQ_BLOCKSIZE; 160 r = lq_envelope_serialize(env, NULL, out, &out_len); 161 if (r != ERR_OK) { 162 lq_envelope_free(env); 163 lq_ui_free(); 164 return 1; 165 } 166 lq_envelope_free(env); 167 168 f = lq_open("./out.dat", O_WRONLY | O_CREAT, S_IRWXU); 169 if (f < 0) { 170 lq_ui_free(); 171 return errno; 172 } 173 174 r = lq_write(f, out, out_len); 175 if (r != out_len) { 176 lq_close(f); 177 lq_ui_free(); 178 return 1; 179 } 180 lq_close(f); 181 182 r = lq_envelope_deserialize(&env, NULL, out, out_len); 183 if (r != ERR_OK) { 184 lq_ui_free(); 185 return 1; 186 } 187 lq_envelope_free(env); 188 lq_ui_free(); 189 }