annotate src/lib-dcrypt/test-crypto.c @ 20646:90d375d8878e

dcrypt: Fix encrypted test keys
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Mon, 15 Aug 2016 13:30:44 +0300
parents 9a2ac2a69648
children 536d185e3794
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
1 #include "lib.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
2 #include "buffer.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
3 #include "str.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
4 #include "dcrypt.h"
20596
9a2ac2a69648 lib-dcrypt: add dcrypt-iostream.h
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20581
diff changeset
5 #include "dcrypt-iostream.h"
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
6 #include "ostream.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
7 #include "ostream-encrypt.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
8 #include "istream.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
9 #include "iostream-temp.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
10 #include "randgen.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
11 #include "test-common.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
12 #include "hex-binary.h"
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
13 #include <fcntl.h>
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
14 #include <sys/stat.h>
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
15 #include <stdio.h>
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
16
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
17
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
18 static
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
19 void test_cipher_test_vectors(void)
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
20 {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
21 static struct {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
22 const char *key;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
23 const char *iv;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
24 const char *pt;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
25 const char *ct;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
26 } vectors[] =
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
27 {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
28 { "2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090a0b0c0d0e0f", "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d" },
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
29 { "2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b2" }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
30 };
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
31
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
32
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
33 test_begin("test_cipher_test_vectors");
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
34
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
35 buffer_t *key,*iv,*pt,*ct,*res_enc,*res_dec;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
36
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
37 key = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
38 iv = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
39 pt = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
40 ct = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
41
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
42 res_enc = buffer_create_dynamic(pool_datastack_create(), 32);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
43 res_dec = buffer_create_dynamic(pool_datastack_create(), 32);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
44
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
45 for(size_t i = 0; i < N_ELEMENTS(vectors); i++) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
46 struct dcrypt_context_symmetric *ctx;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
47
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
48 buffer_set_used_size(key, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
49 buffer_set_used_size(iv, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
50 buffer_set_used_size(pt, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
51 buffer_set_used_size(ct, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
52 buffer_set_used_size(res_enc, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
53 buffer_set_used_size(res_dec, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
54
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
55 hex_to_binary(vectors[i].key, key);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
56 hex_to_binary(vectors[i].iv, iv);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
57 hex_to_binary(vectors[i].pt, pt);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
58 hex_to_binary(vectors[i].ct, ct);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
59
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
60 if (!dcrypt_ctx_sym_create("AES-128-CBC", DCRYPT_MODE_ENCRYPT, &ctx, NULL)) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
61 test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
62 continue;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
63 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
64
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
65 dcrypt_ctx_sym_set_padding(ctx, FALSE);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
66
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
67 dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
68 dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
69
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
70 test_assert_idx(dcrypt_ctx_sym_init(ctx, NULL), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
71
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
72 test_assert_idx(dcrypt_ctx_sym_update(ctx, pt->data, pt->used, res_enc, NULL), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
73 test_assert_idx(dcrypt_ctx_sym_final(ctx, res_enc, NULL), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
74
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
75 test_assert_idx(buffer_cmp(ct, res_enc), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
76
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
77 dcrypt_ctx_sym_destroy(&ctx);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
78
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
79 if (!dcrypt_ctx_sym_create("AES-128-CBC", DCRYPT_MODE_DECRYPT, &ctx, NULL)) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
80 test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
81 continue;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
82 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
83
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
84 dcrypt_ctx_sym_set_padding(ctx, FALSE);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
85
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
86 dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
87 dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
88
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
89 test_assert_idx(dcrypt_ctx_sym_init(ctx, NULL), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
90 test_assert_idx(dcrypt_ctx_sym_update(ctx, res_enc->data, res_enc->used, res_dec, NULL), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
91 test_assert_idx(dcrypt_ctx_sym_final(ctx, res_dec, NULL), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
92
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
93 test_assert_idx(buffer_cmp(pt, res_dec), i);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
94
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
95 dcrypt_ctx_sym_destroy(&ctx);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
96 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
97
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
98 test_end();
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
99 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
100
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
101 static
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
102 void test_cipher_aead_test_vectors(void)
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
103 {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
104 struct dcrypt_context_symmetric *ctx;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
105 const char *error = NULL;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
106
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
107 test_begin("test_cipher_aead_test_vectors");
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
108
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
109 if (!dcrypt_ctx_sym_create("aes-128-gcm", DCRYPT_MODE_ENCRYPT, &ctx, &error)) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
110 test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
111 return;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
112 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
113
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
114 buffer_t *key, *iv, *aad, *pt, *ct, *tag, *tag_res, *res;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
115
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
116 key = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
117 iv = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
118 aad = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
119 pt = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
120 ct = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
121 tag = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
122 res = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
123 tag_res = buffer_create_dynamic(pool_datastack_create(), 16);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
124
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
125 hex_to_binary("feffe9928665731c6d6a8f9467308308", key);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
126 hex_to_binary("cafebabefacedbaddecaf888", iv);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
127 hex_to_binary("d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255", pt);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
128 hex_to_binary("42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985", ct);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
129 hex_to_binary("4d5c2af327cd64a62cf35abd2ba6fab4", tag);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
130
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
131 dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
132 dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
133 dcrypt_ctx_sym_set_aad(ctx, aad->data, aad->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
134 test_assert(dcrypt_ctx_sym_init(ctx, &error));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
135 test_assert(dcrypt_ctx_sym_update(ctx, pt->data, pt->used, res, &error));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
136 test_assert(dcrypt_ctx_sym_final(ctx, res, &error));
20402
b5bc1e6b0574 lib-dcrypt: Check for all the return values in unit tests
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20398
diff changeset
137 test_assert(dcrypt_ctx_sym_get_tag(ctx, tag_res));
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
138
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
139 test_assert(buffer_cmp(ct, res) == TRUE);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
140 test_assert(buffer_cmp(tag, tag_res) == TRUE);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
141
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
142 dcrypt_ctx_sym_destroy(&ctx);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
143
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
144 if (!dcrypt_ctx_sym_create("aes-128-gcm", DCRYPT_MODE_DECRYPT, &ctx, &error)) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
145 test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
146 } else {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
147
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
148 buffer_set_used_size(res, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
149
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
150 dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
151 dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
152 dcrypt_ctx_sym_set_aad(ctx, aad->data, aad->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
153 dcrypt_ctx_sym_set_tag(ctx, tag->data, tag->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
154 test_assert(dcrypt_ctx_sym_init(ctx, &error));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
155 test_assert(dcrypt_ctx_sym_update(ctx, ct->data, ct->used, res, &error));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
156 test_assert(dcrypt_ctx_sym_final(ctx, res, &error));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
157
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
158 test_assert(buffer_cmp(pt, res) == TRUE);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
159
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
160 dcrypt_ctx_sym_destroy(&ctx);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
161 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
162
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
163 test_end();
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
164 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
165
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
166 static
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
167 void test_hmac_test_vectors(void)
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
168 {
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
169 test_begin("test_hmac_test_vectors");
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
170
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
171 buffer_t *pt, *ct, *key, *res;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
172 pt = buffer_create_dynamic(pool_datastack_create(), 50);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
173 key = buffer_create_dynamic(pool_datastack_create(), 20);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
174 ct = buffer_create_dynamic(pool_datastack_create(), 32);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
175 res = buffer_create_dynamic(pool_datastack_create(), 32);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
176
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
177 hex_to_binary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", key);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
178 hex_to_binary("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", pt);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
179 hex_to_binary("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", res);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
180
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
181 struct dcrypt_context_hmac *hctx;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
182 if (!dcrypt_ctx_hmac_create("sha256", &hctx, NULL)) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
183 test_assert_failed("dcrypt_ctx_hmac_create", __FILE__, __LINE__-1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
184 } else {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
185 dcrypt_ctx_hmac_set_key(hctx, key->data, key->used);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
186 test_assert(dcrypt_ctx_hmac_init(hctx, NULL));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
187 test_assert(dcrypt_ctx_hmac_update(hctx, pt->data, pt->used, NULL));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
188 test_assert(dcrypt_ctx_hmac_final(hctx, ct, NULL));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
189 test_assert(buffer_cmp(ct, res));
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
190 dcrypt_ctx_hmac_destroy(&hctx);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
191 }
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
192
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
193 test_end();
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
194 }
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
195
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
196 static
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
197 void test_load_v1_keys(void)
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
198 {
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
199 test_begin("test_load_v1_keys");
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
200
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
201 const char *error = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
202 const char *data1 = "1\t716\t1\t0567e6bf9579813ae967314423b0fceb14bda24749303923de9a9bb9370e0026f995901a57e63113eeb2baf0c940e978d00686cbb52bd5014bc318563375876255\t0300E46DA2125427BE968EB3B649910CDC4C405E5FFDE18D433A97CABFEE28CEEFAE9EE356C792004FFB80981D67E741B8CC036A34235A8D2E1F98D1658CFC963D07EB\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
203
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
204 enum dcrypt_key_format format;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
205 enum dcrypt_key_version version;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
206 enum dcrypt_key_kind kind;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
207 enum dcrypt_key_encryption_type encryption_type;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
208 const char *encryption_key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
209 const char *key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
210
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
211 bool ret = dcrypt_key_string_get_info(data1, &format, &version,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
212 &kind, &encryption_type, &encryption_key_hash,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
213 &key_hash, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
214
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
215 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
216 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
217 test_assert(format == DCRYPT_FORMAT_DOVECOT);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
218 test_assert(version == DCRYPT_KEY_VERSION_1);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
219 test_assert(kind == DCRYPT_KEY_KIND_PRIVATE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
220 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_KEY);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
221 test_assert(strcmp(encryption_key_hash, "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0") == 0);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
222 test_assert(strcmp(key_hash, "7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f") == 0);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
223
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
224 const char* data2 = "1\t716\t0301EB00973C4EFC8FCECA4EA33E941F50B561199A5159BCB6C2EED9DD1D62D65E38A254979D89E28F0C28883E71EE2AD264CD16B863FA094A8F6F69A56B62E8918040\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
225
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
226 error = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
227 encryption_key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
228 key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
229
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
230 ret = dcrypt_key_string_get_info(data2, &format, &version,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
231 &kind, &encryption_type, &encryption_key_hash,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
232 &key_hash, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
233
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
234 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
235 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
236 test_assert(format == DCRYPT_FORMAT_DOVECOT);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
237 test_assert(version == DCRYPT_KEY_VERSION_1);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
238 test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
239 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
240 test_assert(encryption_key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
241 test_assert(strcmp(key_hash, "7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f") == 0);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
242
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
243 /* This is the key that should be able to decrypt key1 */
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
244 const char *data3 = "1\t716\t0\t048FD04FD3612B22D32790C592CF21CEF417EFD2EA34AE5F688FA5B51BED29E05A308B68DA78E16E90B47A11E133BD9A208A2894FD01B0BEE865CE339EA3FB17AC\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
245
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
246 error = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
247 encryption_key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
248 key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
249
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
250 ret = dcrypt_key_string_get_info(data3, &format, &version,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
251 &kind, &encryption_type, &encryption_key_hash,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
252 &key_hash, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
253 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
254 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
255 test_assert(format == DCRYPT_FORMAT_DOVECOT);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
256 test_assert(version == DCRYPT_KEY_VERSION_1);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
257 test_assert(kind == DCRYPT_KEY_KIND_PRIVATE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
258 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
259 test_assert(encryption_key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
260 test_assert(strcmp(key_hash, "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0") == 0);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
261
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
262 /* key3's key_hash should and does match key1's encryption_key_hash */
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
263 struct dcrypt_private_key *pkey = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
264 struct dcrypt_private_key *pkey2 = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
265 pkey = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
266 error = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
267
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
268 ret = dcrypt_key_load_private(&pkey2, data3, NULL, NULL, &error);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
269 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
270 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
271
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
272 ret = dcrypt_key_load_private(&pkey, data1, NULL, pkey2, &error);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
273 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
274 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
275
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
276 dcrypt_key_unref_private(&pkey2);
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
277 dcrypt_key_unref_private(&pkey);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
278
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
279 test_end();
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
280 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
281
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
282 static
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
283 void test_load_v1_key(void)
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
284 {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
285 test_begin("test_load_v1_key");
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
286
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
287 buffer_t *key_1 = buffer_create_dynamic(pool_datastack_create(), 128);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
288
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
289 struct dcrypt_private_key *pkey = NULL, *pkey2 = NULL;
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
290 const char *error = NULL;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
291
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
292 test_assert(dcrypt_key_load_private(&pkey, "1\t716\t0\t048FD04FD3612B22D32790C592CF21CEF417EFD2EA34AE5F688FA5B51BED29E05A308B68DA78E16E90B47A11E133BD9A208A2894FD01B0BEE865CE339EA3FB17AC\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0", NULL, NULL, &error));
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
293 if (pkey != NULL) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
294 buffer_set_used_size(key_1, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
295 /* check that key_id matches */
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
296 struct dcrypt_public_key *pubkey = NULL;
20398
cf3bb56f9ce9 lib-dcrypt: dcrypt_key_convert_private_to_public() can no longer fail.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20376
diff changeset
297 dcrypt_key_convert_private_to_public(pkey, &pubkey);
20402
b5bc1e6b0574 lib-dcrypt: Check for all the return values in unit tests
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20398
diff changeset
298 test_assert(dcrypt_key_store_public(pubkey, DCRYPT_FORMAT_DOVECOT, key_1, NULL));
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
299 buffer_set_used_size(key_1, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
300 dcrypt_key_id_public(pubkey, "sha256", key_1, &error);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
301 test_assert(strcmp("792caad4d38c9eb2134a0cbc844eae386116de096a0ccafc98479825fc99b6a1", binary_to_hex(key_1->data, key_1->used)) == 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
302
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
303 dcrypt_key_unref_public(&pubkey);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
304 pkey2 = NULL;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
305
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
306 test_assert(dcrypt_key_load_private(&pkey2, "1\t716\t1\t0567e6bf9579813ae967314423b0fceb14bda24749303923de9a9bb9370e0026f995901a57e63113eeb2baf0c940e978d00686cbb52bd5014bc318563375876255\t0300E46DA2125427BE968EB3B649910CDC4C405E5FFDE18D433A97CABFEE28CEEFAE9EE356C792004FFB80981D67E741B8CC036A34235A8D2E1F98D1658CFC963D07EB\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f", NULL, pkey, &error));
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
307 if (pkey2 != NULL) {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
308 buffer_set_used_size(key_1, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
309 /* check that key_id matches */
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
310 struct dcrypt_public_key *pubkey = NULL;
20398
cf3bb56f9ce9 lib-dcrypt: dcrypt_key_convert_private_to_public() can no longer fail.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20376
diff changeset
311 dcrypt_key_convert_private_to_public(pkey2, &pubkey);
20402
b5bc1e6b0574 lib-dcrypt: Check for all the return values in unit tests
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20398
diff changeset
312 test_assert(dcrypt_key_store_public(pubkey, DCRYPT_FORMAT_DOVECOT, key_1, NULL));
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
313 buffer_set_used_size(key_1, 0);
20402
b5bc1e6b0574 lib-dcrypt: Check for all the return values in unit tests
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20398
diff changeset
314 test_assert(dcrypt_key_id_public_old(pubkey, key_1, &error));
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
315 test_assert(strcmp("7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f", binary_to_hex(key_1->data, key_1->used)) == 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
316
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
317 dcrypt_key_unref_public(&pubkey);
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
318 dcrypt_key_unref_private(&pkey2);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
319 }
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
320 dcrypt_key_unref_private(&pkey);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
321 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
322
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
323 test_end();
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
324 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
325
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
326 static
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
327 void test_load_v1_public_key(void)
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
328 {
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
329 test_begin("test_load_v1_public_key");
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
330
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
331 const char* data1 = "1\t716\t030131D8A5FD5167947A0AE9CB112ADED6526654635AA5887051EE2364414B60FF32EBA8FA0BBE9485DBDE8794BBBCB44BBFC0D662A4287A848BA570D4E5E45A11FE0F\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
332
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
333 const char* error = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
334 const char* key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
335 const char* encryption_key_hash = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
336
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
337 enum dcrypt_key_format format;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
338 enum dcrypt_key_version version;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
339 enum dcrypt_key_kind kind;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
340 enum dcrypt_key_encryption_type encryption_type;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
341
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
342 bool ret = dcrypt_key_string_get_info(data1, &format, &version,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
343 &kind, &encryption_type, &encryption_key_hash,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
344 &key_hash, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
345
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
346 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
347 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
348 test_assert(format == DCRYPT_FORMAT_DOVECOT);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
349 test_assert(version == DCRYPT_KEY_VERSION_1);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
350 test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
351 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
20417
4da83dd8b95c lib-dcrypt: Make static analyzer happier
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20416
diff changeset
352 test_assert(key_hash != NULL && strcmp(key_hash, "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0") == 0);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
353 test_assert(encryption_key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
354
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
355 struct dcrypt_public_key *pub_key = NULL;
20574
79fa3bdcfc95 lib-dcrypt: drop format from key_load_public
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20573
diff changeset
356 ret = dcrypt_key_load_public(&pub_key, data1, &error);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
357 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
358 test_assert(error == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
359
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
360 test_assert(dcrypt_key_type_public(pub_key) == DCRYPT_KEY_EC);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
361
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
362 dcrypt_key_unref_public(&pub_key);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
363 test_assert(pub_key == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
364
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
365 test_end();
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
366 }
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
367
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
368 static
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
369 void test_load_v2_key(void)
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
370 {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
371 const char *keys[] = {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
372 "-----BEGIN PRIVATE KEY-----\n" \
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
373 "MGcCAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcETTBLAgEBBCC25AkD65uhlZXCAdwN\n" \
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
374 "yLJV2ui8A/CUyqyEMrezvwgMO6EkAyIAAybRUR3MsH0+0PQcDwkrXOJ9aePwzTQV\n" \
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
375 "DN51+n1JCxbI\n" \
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
376 "-----END PRIVATE KEY-----\n",
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
377 "2:1.2.840.10045.3.1.7:0:0000002100b6e40903eb9ba19595c201dc0dc8b255dae8bc03f094caac8432b7b3bf080c3b:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd",
20646
90d375d8878e dcrypt: Fix encrypted test keys
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20596
diff changeset
378 "2:1.2.840.10045.3.1.7:2:aes-256-ctr:483bd74fd3d91763:sha256:2048:d44ae35d3af7a2febcb15cde0c3693e7ed98595665ed655a97fa918d346d5c661a6e2339f4:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd",
90d375d8878e dcrypt: Fix encrypted test keys
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20596
diff changeset
379 "2:1.2.840.10045.3.1.7:1:aes-256-ctr:2574c10be28a4c09:sha256:2048:a750ec9dea91999f108f943485a20f273f40f75c37fc9bcccdedda514c8243e550d69ce1bd:02237a199d7d945aa6492275a02881071eceec5749caf2485da8c64fb601229098:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd"
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
380 };
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
381
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
382 test_begin("test_load_v2_key");
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
383 const char *error = NULL;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
384 buffer_t *tmp = buffer_create_dynamic(default_pool, 256);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
385
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
386 struct dcrypt_private_key *priv,*priv2;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
387
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
388 test_assert_idx(dcrypt_key_load_private(&priv2, keys[0], NULL, NULL, &error), 0);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
389 test_assert_idx(dcrypt_key_store_private(priv2, DCRYPT_FORMAT_PEM, NULL, tmp, NULL, NULL, &error), 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
390 test_assert_idx(strcmp(str_c(tmp), keys[0])==0, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
391 buffer_set_used_size(tmp, 0);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
392
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
393 test_assert_idx(dcrypt_key_load_private(&priv, keys[1], NULL, NULL, &error), 1);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
394 test_assert_idx(dcrypt_key_store_private(priv, DCRYPT_FORMAT_DOVECOT, NULL, tmp, NULL, NULL, &error), 1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
395 test_assert_idx(strcmp(str_c(tmp), keys[1])==0, 1);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
396 buffer_set_used_size(tmp, 0);
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
397 dcrypt_key_unref_private(&priv);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
398
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
399 test_assert_idx(dcrypt_key_load_private(&priv, keys[2], "This Is Sparta", NULL, &error), 2);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
400 test_assert_idx(dcrypt_key_store_private(priv, DCRYPT_FORMAT_DOVECOT, "aes-256-ctr", tmp, "This Is Sparta", NULL, &error), 2);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
401 buffer_set_used_size(tmp, 0);
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
402 dcrypt_key_unref_private(&priv);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
403
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
404 struct dcrypt_public_key *pub = NULL;
20398
cf3bb56f9ce9 lib-dcrypt: dcrypt_key_convert_private_to_public() can no longer fail.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 20376
diff changeset
405 dcrypt_key_convert_private_to_public(priv2, &pub);
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
406 test_assert_idx(dcrypt_key_load_private(&priv, keys[3], NULL, priv2, &error), 3);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
407 test_assert_idx(dcrypt_key_store_private(priv, DCRYPT_FORMAT_DOVECOT, "ecdh-aes-256-ctr", tmp, NULL, pub, &error), 3);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
408 buffer_set_used_size(tmp, 0);
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
409 dcrypt_key_unref_private(&priv2);
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
410 dcrypt_key_unref_private(&priv);
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
411 dcrypt_key_unref_public(&pub);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
412
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
413 buffer_free(&tmp);
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
414
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
415 if (error != NULL) error = NULL;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
416
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
417 test_end();
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
418 }
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
419
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
420 static
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
421 void test_load_v2_public_key(void)
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
422 {
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
423 struct dcrypt_public_key *pub = NULL;
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
424 const char *error;
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
425
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
426 test_begin("test_load_v2_public_key");
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
427 const char *key = "2:3058301006072a8648ce3d020106052b810400230344000301c50954e734dd8b410a607764a7057065a45510da52f2c6e28e0cb353b9c389fa8cb786943ae991fce9befed78fb162fbbc615415f06af06c8cc80c37f4e94ff6c7:185a7212542782e239111f9c19d126ad55b18ddaf4883d66afe8d9627c3607d8";
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
428
20574
79fa3bdcfc95 lib-dcrypt: drop format from key_load_public
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20573
diff changeset
429 test_assert(dcrypt_key_load_public(&pub, key, &error));
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
430
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
431 buffer_t *tmp = buffer_create_dynamic(default_pool, 256);
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
432
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
433 if (pub != NULL) {
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
434 test_assert(dcrypt_key_store_public(pub, DCRYPT_FORMAT_DOVECOT, tmp, &error));
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
435 test_assert(strcmp(key, str_c(tmp))==0);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
436 buffer_free(&tmp);
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
437 dcrypt_key_unref_public(&pub);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
438 }
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
439
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
440 test_end();
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
441 }
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
442
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
443 static
20429
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
444 void test_get_info_v2_key(void) {
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
445 test_begin("test_get_info_v2_key");
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
446
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
447 const char *key = "2:305e301006072a8648ce3d020106052b81040026034a000203fcc90034fa03d6fb79a0fc8b3b43c3398f68e76029307360cdcb9e27bb7e84b3c19dfb7244763bc4d442d216f09b7b7945ed9d182f3156550e9ee30b237a0217dbf79d28975f31:86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc";
20429
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
448 enum dcrypt_key_format format;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
449 enum dcrypt_key_version version = DCRYPT_KEY_VERSION_NA;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
450 enum dcrypt_key_kind kind;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
451 enum dcrypt_key_encryption_type encryption_type;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
452 const char *encryption_key_hash = NULL;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
453 const char *key_hash = NULL;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
454 const char *error = NULL;
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
455
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
456 test_assert(dcrypt_key_string_get_info(key, &format, &version,
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
457 &kind, &encryption_type, &encryption_key_hash,
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
458 &key_hash, &error));
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
459 test_assert(error == NULL);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
460 test_assert(format == DCRYPT_FORMAT_DOVECOT);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
461 test_assert(version == DCRYPT_KEY_VERSION_2);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
462
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
463 test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
464 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
465 test_assert(encryption_key_hash == NULL);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
466 test_assert(key_hash != NULL && strcmp(key_hash,
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
467 "86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc") == 0);
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
468
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
469 test_end();
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
470 }
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
471
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
472 static
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
473 void test_gen_and_get_info_rsa_pem(void)
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
474 {
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
475 test_begin("test_gen_and_get_info_rsa_pem");
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
476
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
477 const char *error = NULL;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
478 bool ret = FALSE;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
479 struct dcrypt_keypair pair;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
480 string_t* buf = str_new(default_pool, 4096);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
481
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
482 ret = dcrypt_keypair_generate(&pair, DCRYPT_KEY_RSA, 1024, NULL, NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
483 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
484
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
485 /* test public key */
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
486 enum dcrypt_key_format format;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
487 enum dcrypt_key_version version;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
488 enum dcrypt_key_kind kind;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
489 enum dcrypt_key_encryption_type encryption_type;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
490 const char *encryption_key_hash;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
491 const char *key_hash;
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
492
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
493 ret = dcrypt_key_store_public(pair.pub, DCRYPT_FORMAT_PEM, buf,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
494 &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
495 test_assert(ret == TRUE);
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
496
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
497 ret = dcrypt_key_string_get_info(str_c(buf), &format, &version,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
498 &kind, &encryption_type, &encryption_key_hash,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
499 &key_hash, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
500 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
501 test_assert(format == DCRYPT_FORMAT_PEM);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
502 test_assert(version == DCRYPT_KEY_VERSION_NA);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
503
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
504 test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
505 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
506 test_assert(encryption_key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
507 test_assert(key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
508
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
509 /* test private key */
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
510 buffer_set_used_size(buf, 0);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
511 ret = dcrypt_key_store_private(pair.priv, DCRYPT_FORMAT_PEM, NULL,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
512 buf, NULL, NULL, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
513
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
514 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
515
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
516 ret = dcrypt_key_string_get_info(str_c(buf), &format, &version,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
517 &kind, &encryption_type, &encryption_key_hash,
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
518 &key_hash, &error);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
519
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
520 test_assert(ret == TRUE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
521 test_assert(format == DCRYPT_FORMAT_PEM);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
522 test_assert(version == DCRYPT_KEY_VERSION_NA);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
523
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
524 test_assert(kind == DCRYPT_KEY_KIND_PRIVATE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
525
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
526 test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
527 test_assert(encryption_key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
528 test_assert(key_hash == NULL);
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
529
20566
6ebb03fc9a25 dcrypt: Update API for testing
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20498
diff changeset
530 dcrypt_keypair_unref(&pair);
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
531 buffer_free(&buf);
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
532
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
533 test_end();
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
534 }
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
535
20498
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
536 static
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
537 void test_get_info_rsa_private_key(void)
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
538 {
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
539 test_begin("test_get_info_rsa_private_key");
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
540
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
541 const char *key = "-----BEGIN RSA PRIVATE KEY-----\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
542 "MIICXQIBAAKBgQC89q02I9NezBLQ+otn5XLYE7S+GsKUz59ogr45DA/6MI9jey0W\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
543 "56SeWQ1FJD1vDhAx/TRBMfOmhcIPsBjc5sakYOawPdoiqLjOIlO+iHwnbbmLuMsq\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
544 "ue09vgvZsKjuTr2F5DOFQY43Bq/Nd+4bjHJItdOM58+xwA2I/8vDbtI8jwIDAQAB\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
545 "AoGBAJCUrTMfdjqyKjN7f+6ewKBTc5eBIiB6O53ba3B6qj7jqNKVDIrZ8jq2KFEe\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
546 "yWKPgBS/h5vafHKNJU6bjmp2qMUJPB7PTA876eDo0cq9PplUqihiTlXJFwNQYtF+\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
547 "o27To5t25+5qdSAj657+lQfFT9Xn9fzYHDmotURxH10FgFkBAkEA+7Ny6lBTeb3W\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
548 "LnP0UPfPzQLilEr8u81PLWe69RGtsEaMQHGpHOl4e+bvvVYbG1cgxwxI1m01uR9r\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
549 "qpD3qLUdrQJBAMAw6UvN8R+opYTZzwqK7Nliil2QZMPmXM04SV1iFq26NM60w2Fm\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
550 "HqOOh0EbpSWsFtIgxJFWoZOtrguxqCJuUqsCQF3EoXf3StHczhDqM8eCOpD2lTCH\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
551 "qxXPy8JvlW+9EUbNUWykq0rRE4idJQ0VKe4KjHR6+Buh/dSkhvi5Hvpj1tUCQHRv\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
552 "LWeXZLVhXqWVrzEb6VHpuRnmGKX2MdLCfu/sNQEbBlMUgCnJzFYaSybOsMaZ81lq\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
553 "MKw8Z7coSYEcKFhzrfECQQD7l+4Bhy8Zuz6VoGGIZwIhxkJrImBFmaUwx8N6jg20\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
554 "sgDRYwCoGkGd7B8uIHZLJoWzSSutHiu5i5PYUy5VT1yT\n"
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
555 "-----END RSA PRIVATE KEY-----\n";
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
556
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
557 const char *error = NULL;
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
558
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
559 test_assert(!dcrypt_key_string_get_info(key, NULL, NULL,
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
560 NULL, NULL, NULL, NULL, &error));
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
561 test_assert(error != NULL && strstr(error, "pkey") != NULL);
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
562
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
563 test_end();
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
564 }
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
565
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
566 static
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
567 void test_get_info_invalid_keys(void) {
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
568 test_begin("test_get_info_invalid_keys");
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
569
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
570 const char *key = "1:716:030131D8A5FD5167947A0AE9CB112ADED6526654635AA5887051EE2364414B60FF32EBA8FA0BBE9485DBDE8794BBBCB44BBFC0D662A4287A848BA570D4E5E45A11FE0F:d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
571 const char *error = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
572
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
573 test_assert(dcrypt_key_string_get_info(key, NULL, NULL,
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
574 NULL, NULL, NULL, NULL, &error) == FALSE);
20581
e8a810c9c96c lib-dcrypt: don't give strstr NULL in test-crypto
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20576
diff changeset
575 test_assert(error != NULL && strstr(error, "tab") != NULL);
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
576
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
577 key = "2\t305e301006072a8648ce3d020106052b81040026034a000203fcc90034fa03d6fb79a0fc8b3b43c3398f68e76029307360cdcb9e27bb7e84b3c19dfb7244763bc4d442d216f09b7b7945ed9d182f3156550e9ee30b237a0217dbf79d28975f31\t86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc";
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
578 error = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
579
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
580 test_assert(dcrypt_key_string_get_info(key, NULL, NULL,
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
581 NULL, NULL, NULL, NULL, &error) == FALSE);
20581
e8a810c9c96c lib-dcrypt: don't give strstr NULL in test-crypto
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20576
diff changeset
582 test_assert(error != NULL && strstr(error, "colon") != NULL);
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
583
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
584 key = "2";
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
585 error = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
586
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
587 test_assert(dcrypt_key_string_get_info(key, NULL, NULL,
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
588 NULL, NULL, NULL, NULL, &error) == FALSE);
20581
e8a810c9c96c lib-dcrypt: don't give strstr NULL in test-crypto
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20576
diff changeset
589 test_assert(error != NULL && strstr(error, "Unknown") != NULL);
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
590
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
591 test_end();
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
592 }
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
593
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
594 static
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
595 void test_load_invalid_keys(void) {
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
596 test_begin("test_load_invalid_keys");
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
597
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
598 const char *error = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
599 const char *key = "1:716:0301EB00973C4EFC8FCECA4EA33E941F50B561199A5159BCB6C2EED9DD1D62D65E38A254979D89E28F0C28883E71EE2AD264CD16B863FA094A8F6F69A56B62E8918040:7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
600 struct dcrypt_public_key *pub_key = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
601
20574
79fa3bdcfc95 lib-dcrypt: drop format from key_load_public
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20573
diff changeset
602 bool ret = dcrypt_key_load_public(&pub_key, key, &error);
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
603 test_assert(ret == FALSE);
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
604 test_assert(error != NULL);
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
605
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
606 error = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
607 key = "2:305e301006072a8648ce3d020106052b81040026034a000203fcc90034fa03d6fb79a0fc8b3b43c3398f68e76029307360cdcb9e27bb7e84b3c19dfb7244763bc4d442d216f09b7b7945ed9d182f3156550e9ee30b237a0217dbf79d28975f31:86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc";
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
608 struct dcrypt_private_key *priv_key = NULL;
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
609
20573
071903e5586b lib-dcrypt: drop format from key_load_private
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20570
diff changeset
610 ret = dcrypt_key_load_private(&priv_key, key, NULL, NULL, &error);
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
611 test_assert(ret == FALSE);
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
612 test_assert(error != NULL);
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
613
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
614 test_end();
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
615 }
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
616
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
617 int main(void) {
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
618 random_init();
20576
6e584af6f758 lib-dcrypt: Skip checks if no backend found
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20574
diff changeset
619 if (!dcrypt_initialize(NULL, NULL, NULL)) {
6e584af6f758 lib-dcrypt: Skip checks if no backend found
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20574
diff changeset
620 i_error("No functional dcrypt backend found - skipping tests");
6e584af6f758 lib-dcrypt: Skip checks if no backend found
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20574
diff changeset
621 return 0;
6e584af6f758 lib-dcrypt: Skip checks if no backend found
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20574
diff changeset
622 }
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
623
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
624 static void (*test_functions[])(void) = {
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
625 test_cipher_test_vectors,
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
626 test_cipher_aead_test_vectors,
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
627 test_hmac_test_vectors,
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
628 test_load_v1_keys,
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
629 test_load_v1_key,
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
630 test_load_v1_public_key,
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
631 test_load_v2_key,
20326
71285f4d97dd dcrypt: Add test for public key loading
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20215
diff changeset
632 test_load_v2_public_key,
20429
ba3484993516 lib-dcrypt: correctly set version 2 on key info
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20417
diff changeset
633 test_get_info_v2_key,
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
634 test_gen_and_get_info_rsa_pem,
20498
43c279a779f5 lib-dcrypt: tell in error_r that RSA key has to be converted to pkey
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20429
diff changeset
635 test_get_info_rsa_private_key,
20570
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
636 test_get_info_invalid_keys,
fe9af2fb0dae lib-dcrypt: change v2 key field separator to ':'
Martti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
parents: 20566
diff changeset
637 test_load_invalid_keys,
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
638 NULL
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
639 };
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
640
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
641 int ret = test_run(test_functions);
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
642
20416
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
643 dcrypt_deinitialize();
fd85f5b85819 dcrypt: Add tests for v1 and v2 public keys and RSA
Aki Tuomi <aki.tuomi@dovecot.fi>
parents: 20402
diff changeset
644 random_deinit();
20215
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
645
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
646 return ret;
389c5094c2a8 lib-dcrypt: Initial implementation
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
647 }