libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

main.c (2622B)


      1 #include <stdlib.h>
      2 #include <string.h>
      3 
      4 #include <basedir.h>
      5 #include <cwalk.h>
      6 
      7 #include <lq/base.h>
      8 #include <lq/crypto.h>
      9 #include <lq/config.h>
     10 #include <lq/io.h>
     11 #include <lq/err.h>
     12 #include <lq/cert.h>
     13 #include <lq/msg.h>
     14 
     15 #define INIT_LQ 0x01
     16 #define INIT_CRYPTO 0x02
     17 #define INIT_ALICE 0x04
     18 #define INIT_BOB 0x08
     19 
     20 static int init_state;
     21 static xdgHandle xdg;
     22 static LQPrivKey *pk_alice;
     23 static LQPubKey *pubk_alice;
     24 static LQPrivKey *pk_bob;
     25 static LQPubKey *pubk_bob;
     26 char passphrase_alice[] = "1234";
     27 char passphrase_bob[] = "5678";
     28 
     29 
     30 int lq_ui_init() {
     31 	int r;
     32 	char *path[8];
     33 	char outpath[LQ_PATH_MAX];
     34 
     35 	xdgInitHandle(&xdg);
     36 	lq_init();
     37 	init_state |= INIT_LQ;
     38 
     39 	// Set up storage path.
     40 	path[0] = (char*)xdgCacheHome(&xdg);
     41 	path[1] = "libqaeda";
     42 	path[2] = NULL;
     43 	cwk_path_join_multiple((const char**)path, outpath, LQ_PATH_MAX);
     44 	ensuredir(outpath);
     45 
     46 	// Set up configuration.
     47 	r = lq_config_set(LQ_CFG_DIR_BASE, outpath);
     48 	if (r) {
     49 		return ERR_FAIL;
     50 	}
     51 	r = lq_config_set(LQ_CFG_DIR_DATA, outpath);
     52 	if (r) {
     53 		return ERR_FAIL;
     54 	}
     55 
     56 	// Initialize crypto subsystem.
     57 	r = lq_crypto_init(outpath);
     58 	if (r) {
     59 		return ERR_FAIL;
     60 	}
     61 
     62 	return ERR_OK;
     63 }
     64 
     65 void lq_ui_free() {
     66 	if (init_state & INIT_BOB) {
     67 		lq_publickey_free(pubk_bob);
     68 		lq_privatekey_free(pk_bob);
     69 	}
     70 	if (init_state & INIT_ALICE) {
     71 		lq_publickey_free(pubk_alice);
     72 		lq_privatekey_free(pk_alice);
     73 	}
     74 	if (init_state & INIT_CRYPTO) {
     75 		lq_crypto_free();
     76 	}
     77 	if (init_state & INIT_LQ) {
     78 		xdgWipeHandle(&xdg);
     79 		lq_finish();
     80 	}
     81 }
     82 
     83 
     84 int main(int argc, char **argv) {
     85 	int r;
     86 	LQCert *cert;
     87 	LQMsg *req;
     88 	LQMsg *res;
     89 
     90 	r = lq_ui_init();
     91 	if (r) {
     92 		return 1;
     93 	}
     94 
     95 	pk_alice = lq_privatekey_load(passphrase_alice, strlen(passphrase_alice), NULL);
     96 	if (pk_alice == NULL) {
     97 		lq_ui_free();
     98 		return 1;
     99 	}
    100 	pubk_alice = lq_publickey_from_privatekey(pk_alice);
    101 	if (pubk_alice == NULL) {
    102 		lq_ui_free();
    103 		return 1;
    104 	}
    105 	pk_bob = lq_privatekey_load(passphrase_bob, strlen(passphrase_bob), NULL);
    106 	if (pk_bob == NULL) {
    107 		lq_ui_free();
    108 		return 1;
    109 	}
    110 	pubk_bob = lq_publickey_from_privatekey(pk_bob);
    111 	if (pubk_bob == NULL) {
    112 		lq_ui_free();
    113 		return 1;
    114 	}
    115 
    116 	req = lq_msg_new("foo", 4);
    117 	if (req == NULL) {
    118 		lq_ui_free();
    119 		return 1;
    120 	}
    121 	cert = lq_certificate_new(NULL);
    122 	r = lq_certificate_request(cert, req, pk_alice);
    123 	if (r != ERR_OK) {
    124 		lq_ui_free();
    125 		return 1;
    126 	}
    127 
    128 	res = lq_msg_new("foo", 4);
    129 	if (res == NULL) {
    130 		lq_ui_free();
    131 		return 1;
    132 	}
    133 	r = lq_certificate_respond(cert, res, pk_bob);
    134 	if (r != ERR_OK) {
    135 		lq_ui_free();
    136 		return 1;
    137 	}
    138 
    139 	r = lq_certificate_verify(cert, pubk_alice, NULL);
    140 
    141 	lq_certificate_free(cert);
    142 
    143 	lq_ui_free();
    144 }