libqaeda

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

test_cert.c (5602B)


      1 #include <check.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "lq/msg.h"
      6 #include "lq/cert.h"
      7 #include "lq/mem.h"
      8 #include "lq/crypto.h"
      9 #include "lq/config.h"
     10 #include "lq/base.h"
     11 #include "lq/io.h"
     12 
     13 const char *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
     14 const char *data_two = "Que trata de la condición y ejercicio del famoso hidalgo D. Quijote de la Mancha En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha mucho tiempo que vivía un hidalgo de los de lanza en astillero, adarga antigua, rocín flaco y galgo corredor.";
     15 
     16 //// sha256sum "foo" 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
     17 //static const char privkeydata[32] = {
     18 //	0x2c, 0x26, 0xb4, 0x6b, 0x68, 0xff, 0xc6, 0x8f,
     19 //	0xf9, 0x9b, 0x45, 0x3c, 0x1d, 0x30, 0x41, 0x34,
     20 //	0x13, 0x42, 0x2d, 0x70, 0x64, 0x83, 0xbf, 0xa0,
     21 //	0xf9, 0x8a, 0x5e, 0x88, 0x62, 0x66, 0xe7, 0xae,
     22 //};
     23 
     24 // sha256sum "bar" fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9
     25 static const char passphrase[32] = {
     26 	0xfc, 0xde, 0x2b, 0x2e, 0xdb, 0xa5, 0x6b, 0xf4,
     27 	0x08, 0x60, 0x1f, 0xb7, 0x21, 0xfe, 0x9b, 0x5c,
     28 	0x33, 0x8d, 0x10, 0xee, 0x42, 0x9e, 0xa0, 0x4f,
     29 	0xae, 0x55, 0x11, 0xb6, 0x8f, 0xbf, 0x8f, 0xb9,
     30 };
     31 
     32 
     33 START_TEST(check_cert_symmetric_nomsg) {
     34 	int r;
     35 	size_t c;
     36 	LQCert *cert;
     37 	char buf[4096];
     38 
     39 	cert = lq_certificate_new(NULL);
     40 	c = 4096;
     41 	r = lq_certificate_serialize(cert, buf, &c, NULL);
     42 	ck_assert_int_eq(r, 0);
     43 	lq_certificate_free(cert);
     44 
     45 	r = lq_certificate_deserialize(&cert, buf, c, NULL);
     46 	ck_assert_int_eq(r, 0);
     47 	lq_certificate_free(cert);
     48 }
     49 END_TEST
     50 
     51 START_TEST(check_cert_symmetric_req_nosig) {
     52 	int r;
     53 	size_t c;
     54 	LQCert *cert;
     55 	LQMsg *req;
     56 	char buf[4096];
     57 
     58 	req = lq_msg_new(data, strlen(data) + 1);
     59 	cert = lq_certificate_new(NULL);
     60 	r = lq_certificate_request(cert, req, NULL);
     61 	c = 4096;
     62 	r = lq_certificate_serialize(cert, buf, &c, NULL);
     63 	ck_assert_int_eq(r, 0);
     64 	lq_certificate_free(cert);
     65 
     66 	r = lq_certificate_deserialize(&cert, buf, c, NULL);
     67 	ck_assert_int_eq(r, 0);
     68 	lq_certificate_free(cert);
     69 }
     70 END_TEST
     71 
     72 START_TEST(check_cert_symmetric_req_sig) {
     73 	int r;
     74 	size_t c;
     75 	LQCert *cert;
     76 	LQMsg *req;
     77 	LQPrivKey *pk;
     78 	char buf[4096];
     79 
     80 	pk = lq_privatekey_new(passphrase, 32);
     81 	req = lq_msg_new(data, strlen(data) + 1);
     82 	cert = lq_certificate_new(NULL);
     83 	lq_privatekey_unlock(pk, passphrase, 32);
     84 	r = lq_certificate_request(cert, req, pk);
     85 	ck_assert_int_eq(r, 0);
     86 
     87 	c = 4096;
     88 	r = lq_certificate_serialize(cert, buf, &c, NULL);
     89 	ck_assert_int_eq(r, 0);
     90 	lq_certificate_free(cert);
     91 
     92 	r = lq_certificate_deserialize(&cert, buf, c, NULL);
     93 	ck_assert_int_eq(r, 0);
     94 	lq_certificate_free(cert);
     95 	lq_privatekey_free(pk);
     96 }
     97 END_TEST
     98 
     99 START_TEST(check_cert_symmetric_rsp_onesig) {
    100 	int r;
    101 	size_t c;
    102 	LQCert *cert;
    103 	LQMsg *req;
    104 	LQMsg *res;
    105 	LQPrivKey *pk;
    106 	char buf[4096];
    107 
    108 	pk = lq_privatekey_new(passphrase, 32);
    109 	req = lq_msg_new(data, strlen(data) + 1);
    110 	res = lq_msg_new(data_two, strlen(data_two) + 1);
    111 	cert = lq_certificate_new(NULL);
    112 	lq_privatekey_unlock(pk, passphrase, 32);
    113 	r = lq_certificate_request(cert, req, pk);
    114 	ck_assert_int_eq(r, 0);
    115 
    116 	c = 4096;
    117 	r = lq_certificate_serialize(cert, buf, &c, NULL);
    118 	ck_assert_int_eq(r, 0);
    119 	lq_certificate_free(cert);
    120 
    121 	r = lq_certificate_deserialize(&cert, buf, c, NULL);
    122 	ck_assert_int_eq(r, 0);
    123 	r = lq_certificate_respond(cert, res, pk);
    124 	ck_assert_int_eq(r, 0);
    125 	lq_certificate_free(cert);
    126 	lq_privatekey_free(pk);
    127 }
    128 END_TEST
    129 
    130 START_TEST(check_cert_symmetric_rsp_bothsig) {
    131 	int r;
    132 	size_t c;
    133 	LQCert *cert;
    134 	LQMsg *req;
    135 	LQMsg *res;
    136 	LQPrivKey *pk;
    137 	char buf[4096];
    138 
    139 	pk = lq_privatekey_new(passphrase, 32);
    140 	req = lq_msg_new(data, strlen(data) + 1);
    141 	ck_assert_ptr_nonnull(req);
    142 	cert = lq_certificate_new(NULL);
    143 	ck_assert_ptr_nonnull(cert);
    144 	lq_privatekey_unlock(pk, passphrase, 32);
    145 	r = lq_certificate_request(cert, req, NULL);
    146 	r = lq_certificate_sign(cert, pk);
    147 	ck_assert_int_eq(r, 0);
    148 
    149 	res = lq_msg_new(data_two, strlen(data_two) + 1);
    150 	ck_assert_ptr_nonnull(res);
    151 	r = lq_certificate_respond(cert, res, NULL);
    152 	ck_assert_int_eq(r, 0);
    153 	r = lq_certificate_sign(cert, pk);
    154 	ck_assert_int_eq(r, 0);
    155 
    156 	c = 4096;
    157 	r = lq_certificate_serialize(cert, buf, &c, NULL);
    158 	ck_assert_int_eq(r, 0);
    159 	lq_certificate_free(cert);
    160 
    161 	r = lq_certificate_deserialize(&cert, buf, c, NULL);
    162 	ck_assert_int_eq(r, 0);
    163 	lq_certificate_free(cert);
    164 }
    165 END_TEST
    166 
    167 Suite * common_suite(void) {
    168 	Suite *s;
    169 	TCase *tc;
    170 
    171 	s = suite_create("cert");
    172 	tc = tcase_create("serialize");
    173 //	tcase_add_test(tc, check_cert_symmetric_nomsg);
    174 //	tcase_add_test(tc, check_cert_symmetric_req_nosig);
    175 	tcase_add_test(tc, check_cert_symmetric_req_sig);
    176 //	tcase_add_test(tc, check_cert_symmetric_rsp_onesig);
    177 //	tcase_add_test(tc, check_cert_symmetric_rsp_bothsig);
    178 	suite_add_tcase(s, tc);
    179 
    180 	return s;
    181 }
    182 
    183 int main(void) {
    184 	int r;
    185 	int n_fail;
    186 	char path[LQ_PATH_MAX];
    187 
    188 	Suite *s;
    189 	SRunner *sr;
    190 
    191 	r = lq_init();
    192 	if (r) {
    193 		return 1;
    194 	}
    195 
    196 	lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
    197 	r = lq_crypto_init(mktempdir(path));
    198 	if (r) {
    199 		return 1;
    200 	}
    201 
    202 	s = common_suite();	
    203 	sr = srunner_create(s);
    204 
    205 	srunner_run_all(sr, CK_VERBOSE);
    206 	n_fail = srunner_ntests_failed(sr);
    207 	srunner_free(sr);
    208 
    209 	return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    210 }