Line data Source code
1 : /*
2 : SSSD
3 :
4 : Certificates - Utilities tests
5 :
6 : Authors:
7 : Sumit Bose <sbose@redhat.com>
8 :
9 : Copyright (C) 2015 Red Hat
10 :
11 : This program is free software; you can redistribute it and/or modify
12 : it under the terms of the GNU General Public License as published by
13 : the Free Software Foundation; either version 3 of the License, or
14 : (at your option) any later version.
15 :
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 :
21 : You should have received a copy of the GNU General Public License
22 : along with this program. If not, see <http://www.gnu.org/licenses/>.
23 : */
24 : #include "config.h"
25 :
26 : #include <popt.h>
27 : #ifdef HAVE_LIBCRYPTO
28 : #include <openssl/objects.h>
29 : #include <openssl/crypto.h>
30 : #endif
31 :
32 : #include "util/cert.h"
33 : #include "tests/cmocka/common_mock.h"
34 : #include "util/crypto/nss/nss_util.h"
35 : #include "util/crypto/sss_crypto.h"
36 :
37 :
38 : /* TODO: create a certificate for this test */
39 : const uint8_t test_cert_der[] = {
40 : 0x30, 0x82, 0x04, 0x09, 0x30, 0x82, 0x02, 0xf1, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x09,
41 : 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,
42 : 0x34, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x49, 0x50, 0x41, 0x2e,
43 : 0x44, 0x45, 0x56, 0x45, 0x4c, 0x31, 0x1e, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x15,
44 : 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68,
45 : 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35, 0x30, 0x34, 0x32, 0x38, 0x31,
46 : 0x30, 0x32, 0x31, 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x31, 0x37, 0x30, 0x34, 0x32, 0x38, 0x31, 0x30,
47 : 0x32, 0x31, 0x31, 0x31, 0x5a, 0x30, 0x32, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a,
48 : 0x0c, 0x09, 0x49, 0x50, 0x41, 0x2e, 0x44, 0x45, 0x56, 0x45, 0x4c, 0x31, 0x1c, 0x30, 0x1a, 0x06,
49 : 0x03, 0x55, 0x04, 0x03, 0x0c, 0x13, 0x69, 0x70, 0x61, 0x2d, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x2e,
50 : 0x69, 0x70, 0x61, 0x2e, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06,
51 : 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f,
52 : 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb2, 0x32, 0x92, 0xab, 0x47, 0xb8,
53 : 0x0c, 0x13, 0x54, 0x4a, 0x1f, 0x1e, 0x29, 0x06, 0xff, 0xd0, 0x50, 0xcb, 0xf7, 0x5f, 0x79, 0x91,
54 : 0x65, 0xb1, 0x39, 0x01, 0x83, 0x6a, 0xad, 0x9e, 0x77, 0x3b, 0xf3, 0x0d, 0xd7, 0xb9, 0xf6, 0xdc,
55 : 0x9e, 0x4a, 0x49, 0xa7, 0xd0, 0x66, 0x72, 0xcc, 0xbf, 0x77, 0xd6, 0xde, 0xa9, 0xfe, 0x67, 0x96,
56 : 0xcc, 0x49, 0xf1, 0x37, 0x23, 0x2e, 0xc4, 0x50, 0xf4, 0xeb, 0xba,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, 0x24, 0x6e, 0x4f,
68 : 0xbd, 0xec, 0x22, 0x8a, 0x07, 0x11, 0x3d, 0xf9, 0xd3, 0x15, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3,
69 : 0x82, 0x01, 0x26, 0x30, 0x82, 0x01, 0x22, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18,
70 : 0x30, 0x16, 0x80, 0x14, 0xf2, 0x9d, 0x42, 0x4e, 0x0f, 0xc4, 0x48, 0x25, 0x58, 0x2f, 0x1c, 0xce,
71 : 0x0f, 0xa1, 0x3f, 0x22, 0xc8, 0x55, 0xc8, 0x91, 0x30, 0x3b, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
72 : 0x05, 0x07, 0x01, 0x01, 0x04, 0x2f, 0x30, 0x2d, 0x30, 0x2b, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
73 : 0x05, 0x07, 0x30, 0x01, 0x86, 0x1f, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x69, 0x70, 0x61,
74 : 0x2d, 0x63, 0x61, 0x2e, 0x69, 0x70, 0x61, 0x2e, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x2f, 0x63, 0x61,
75 : 0x2f, 0x6f, 0x63, 0x73, 0x70, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04,
76 : 0x04, 0x03, 0x02, 0x04, 0xf0, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14,
77 : 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
78 : 0x05, 0x07, 0x03, 0x02, 0x30, 0x74, 0x06, 0x03, 0x55, 0x1d, 0x1f, 0x04, 0x6d, 0x30, 0x6b, 0x30,
79 : 0x69, 0xa0, 0x31, 0xa0, 0x2f, 0x86, 0x2d, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x69, 0x70,
80 : 0x61, 0x2d, 0x63, 0x61, 0x2e, 0x69, 0x70, 0x61, 0x2e, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x2f, 0x69,
81 : 0x70, 0x61, 0x2f, 0x63, 0x72, 0x6c, 0x2f, 0x4d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x43, 0x52, 0x4c,
82 : 0x2e, 0x62, 0x69, 0x6e, 0xa2, 0x34, 0xa4, 0x32, 0x30, 0x30, 0x31, 0x0e, 0x30, 0x0c, 0x06, 0x03,
83 : 0x55, 0x04, 0x0a, 0x0c, 0x05, 0x69, 0x70, 0x61, 0x63, 0x61, 0x31, 0x1e, 0x30, 0x1c, 0x06, 0x03,
84 : 0x55, 0x04, 0x03, 0x0c, 0x15, 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65,
85 : 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d,
86 : 0x0e, 0x04, 0x16, 0x04, 0x14, 0x2d, 0x2b, 0x3f, 0xcb, 0xf5, 0xb2, 0xff, 0x32, 0x2c, 0xa8, 0xc2,
87 : 0x1c, 0xdd, 0xbd, 0x8c, 0x80, 0x1e, 0xdd, 0x31, 0x82, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
88 : 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x9a, 0x47, 0x2e,
89 : 0x50, 0xa7, 0x4d, 0x1d, 0x53, 0x0f, 0xc9, 0x71, 0x42, 0x0c, 0xe5, 0xda, 0x7d, 0x49, 0x64, 0xe7,
90 : 0xab, 0xc8, 0xdf, 0xdf, 0x02, 0xc1, 0x87, 0xd1, 0x5b, 0xde, 0xda, 0x6f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, 0xb6, 0x6a, 0x26,
102 : 0x63, 0xb1, 0xd3, 0xbf, 0x55, 0x83, 0x82, 0x9b, 0x36, 0x6c, 0x33, 0x64, 0x0f, 0x50, 0xc0, 0x55,
103 : 0x94, 0x13, 0xc3, 0x85, 0xf4, 0xd5, 0x71, 0x65, 0xd0, 0xc0, 0xdd, 0xfc, 0xe6, 0xec, 0x9c, 0x5b,
104 : 0xf0, 0x11, 0xb5, 0x2c, 0xf3, 0x48, 0xc1, 0x36, 0x8c, 0xa2, 0x96, 0x48, 0x84};
105 :
106 : #define TEST_CERT_PEM "-----BEGIN CERTIFICATE-----\n" \
107 : "MIIECTCCAvGgAwIBAgIBCTANBgkqhkiG9w0BAQsFADA0MRIwEAYDVQQKDAlJUEEu\n" \
108 : "REVWRUwxHjAcBgNVBAMMFUNlcnRpZmljYXRlIEF1dGhvcml0eTAeFw0xNTA0Mjgx\n" \
109 : "MDIxMTFaFw0xNzA0MjgxMDIxMTFaMDIxEjAQBgNVBAoMCUlQQS5ERVZFTDEcMBoG\n" \
110 : "A1UEAwwTaXBhLWRldmVsLmlwYS5kZXZlbDCCASIwDQYJKoZIhvcNAQEBBQADggEP\n" \
111 : "ADCCAQoCggEBALIykqtHuAwTVEofHikG/9BQy/dfeZFlsTkBg2qtnnc78w3Xufbc\n" \
112 : "nkpJp9Bmcsy/d9beqf5nlsxJ8TcjLsRQ9Ou6YtQjTfM3OILuOz8s0ICbF6qb66bd\n" \
113 : "9hX/BrLO/9+KnpWFSR+E/YEmzgYyDTbKfBWBaGuPPrOi/K6vwkRYFZVA/FYZkYDt\n" \
114 : "QhFmBO884HYzS4P6frRH3PvtRqWNCmaHpe97dGKsvnM2ybT+IMSB8/54GajQr3+B\n" \
115 : "ciRh2XaT4wvSTxkXM1fUgrDxqAP2AZmpuIyDyboZh+rWOwbrTPfx5SipELZG3uHh\n" \
116 : "P8HMcr4qQ8b20LWgxCRuT73sIooHET350xUCAwEAAaOCASYwggEiMB8GA1UdIwQY\n" \
117 : "MBaAFPKdQk4PxEglWC8czg+hPyLIVciRMDsGCCsGAQUFBwEBBC8wLTArBggrBgEF\n" \
118 : "BQcwAYYfaHR0cDovL2lwYS1jYS5pcGEuZGV2ZWwvY2Evb2NzcDAOBgNVHQ8BAf8E\n" \
119 : "BAMCBPAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMHQGA1UdHwRtMGsw\n" \
120 : "aaAxoC+GLWh0dHA6Ly9pcGEtY2EuaXBhLmRldmVsL2lwYS9jcmwvTWFzdGVyQ1JM\n" \
121 : "LmJpbqI0pDIwMDEOMAwGA1UECgwFaXBhY2ExHjAcBgNVBAMMFUNlcnRpZmljYXRl\n" \
122 : "IEF1dGhvcml0eTAdBgNVHQ4EFgQULSs/y/Wy/zIsqMIc3b2MgB7dMYIwDQYJKoZI\n" \
123 : "hvcNAQELBQADggEBAJpHLlCnTR1TD8lxQgzl2n1JZOeryN/fAsGH0Vve2m8r5PC+\n" \
124 : "ugnfAoULiuabBn1pOGxy/0x7Kg0/Iy8WRv8Fk7DqJCjXEqFXuFkZJfNDCtP9DzeN\n" \
125 : "uMoV50iKoMfHS38BPFjXN+X/fSsBrA2fUWrlQCTmXlUN97gvQqxt5Slrxgukvxm9\n" \
126 : "OSfu/sWz22LUvtJHupYwWv1iALgnXS86lAuVNYVALLxn34r58XsZlj5CSBMjBJWp\n" \
127 : "axEzgUdag3L2IPqOQXuPd0d8x11G9E/9gQquOSe2aiZjsdO/VYOCmzZsM2QPUMBV\n" \
128 : "lBPDhfTVcWXQwN385uycW/ARtSzzSME2jKKWSIQ=\n" \
129 : "-----END CERTIFICATE-----\n"
130 :
131 : #define TEST_CERT_DERB64 \
132 : "MIIECTCCAvGgAwIBAgIBCTANBgkqhkiG9w0BAQsFADA0MRIwEAYDVQQKDAlJUEEu" \
133 : "REVWRUwxHjAcBgNVBAMMFUNlcnRpZmljYXRlIEF1dGhvcml0eTAeFw0xNTA0Mjgx" \
134 : "MDIxMTFaFw0xN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ULiuabBn1pOGxy/0x7Kg0/Iy8WRv8Fk7DqJCjXEqFXuFkZJfNDCtP9DzeN" \
150 : "uMoV50iKoMfHS38BPFjXN+X/fSsBrA2fUWrlQCTmXlUN97gvQqxt5Slrxgukvxm9" \
151 : "OSfu/sWz22LUvtJHupYwWv1iALgnXS86lAuVNYVALLxn34r58XsZlj5CSBMjBJWp" \
152 : "axEzgUdag3L2IPqOQXuPd0d8x11G9E/9gQquOSe2aiZjsdO/VYOCmzZsM2QPUMBV" \
153 : "lBPDhfTVcWXQwN385uycW/ARtSzzSME2jKKWSIQ="
154 :
155 : struct test_state {
156 : void *dummy;
157 : };
158 :
159 7 : static int setup(void **state)
160 : {
161 7 : struct test_state *ts = NULL;
162 :
163 7 : assert_true(leak_check_setup());
164 :
165 7 : ts = talloc(global_talloc_context, struct test_state);
166 7 : assert_non_null(ts);
167 :
168 7 : check_leaks_push(ts);
169 7 : *state = (void *)ts;
170 7 : return 0;
171 : }
172 :
173 7 : static int teardown(void **state)
174 : {
175 7 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
176 :
177 7 : assert_non_null(ts);
178 :
179 7 : assert_true(check_leaks_pop(ts));
180 7 : talloc_free(ts);
181 7 : assert_true(leak_check_teardown());
182 7 : return 0;
183 : }
184 :
185 1 : void test_sss_cert_der_to_pem(void **state)
186 : {
187 : int ret;
188 : char *pem_str;
189 : size_t pem_size;
190 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
191 1 : assert_non_null(ts);
192 :
193 1 : ret = sss_cert_der_to_pem(NULL, NULL, 0, NULL, NULL);
194 1 : assert_int_equal(ret, EINVAL);
195 :
196 1 : ret = sss_cert_der_to_pem(ts, test_cert_der, sizeof(test_cert_der),
197 : &pem_str, &pem_size);
198 1 : assert_int_equal(ret, EOK);
199 1 : assert_int_equal(sizeof(TEST_CERT_PEM) - 1, pem_size);
200 1 : assert_string_equal(pem_str, TEST_CERT_PEM);
201 :
202 1 : talloc_free(pem_str);
203 1 : }
204 :
205 1 : void test_sss_cert_pem_to_der(void **state)
206 : {
207 : int ret;
208 : uint8_t *der;
209 : size_t der_size;
210 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
211 1 : assert_non_null(ts);
212 :
213 1 : ret = sss_cert_pem_to_der(NULL, NULL, NULL, NULL);
214 1 : assert_int_equal(ret, EINVAL);
215 :
216 1 : ret = sss_cert_pem_to_der(ts, TEST_CERT_PEM, &der, &der_size);
217 1 : assert_int_equal(ret, EOK);
218 1 : assert_int_equal(sizeof(test_cert_der), der_size);
219 1 : assert_memory_equal(der, test_cert_der, der_size);
220 :
221 1 : talloc_free(der);
222 1 : }
223 :
224 1 : void test_sss_cert_derb64_to_pem(void **state)
225 : {
226 : int ret;
227 : char *pem_str;
228 : size_t pem_size;
229 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
230 1 : assert_non_null(ts);
231 :
232 1 : ret = sss_cert_derb64_to_pem(NULL, NULL, NULL, NULL);
233 1 : assert_int_equal(ret, EINVAL);
234 :
235 1 : ret = sss_cert_derb64_to_pem(ts, TEST_CERT_DERB64, &pem_str, &pem_size);
236 1 : assert_int_equal(ret, EOK);
237 1 : assert_int_equal(sizeof(TEST_CERT_PEM) - 1, pem_size);
238 1 : assert_string_equal(pem_str, TEST_CERT_PEM);
239 :
240 1 : talloc_free(pem_str);
241 1 : }
242 :
243 1 : void test_sss_cert_pem_to_derb64(void **state)
244 : {
245 : int ret;
246 : char *derb64;
247 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
248 1 : assert_non_null(ts);
249 :
250 1 : ret = sss_cert_pem_to_derb64(NULL, NULL, NULL);
251 1 : assert_int_equal(ret, EINVAL);
252 :
253 1 : ret = sss_cert_pem_to_derb64(ts, TEST_CERT_PEM, &derb64);
254 1 : assert_int_equal(ret, EOK);
255 1 : assert_string_equal(derb64, TEST_CERT_DERB64);
256 :
257 1 : talloc_free(derb64);
258 1 : }
259 :
260 1 : void test_bin_to_ldap_filter_value(void **state)
261 : {
262 : int ret;
263 : size_t c;
264 : char *str;
265 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
266 1 : assert_non_null(ts);
267 :
268 : struct test_data {
269 : uint8_t blob[5];
270 : const char *str;
271 1 : } test_data[] = {
272 : {{0x01, 0x02, 0x03, 0x04, 0x05}, "\\01\\02\\03\\04\\05"},
273 : {{0x00, 0x00, 0x00, 0x00, 0x00}, "\\00\\00\\00\\00\\00"},
274 : {{0xff, 0xff, 0xff, 0xff, 0xff}, "\\ff\\ff\\ff\\ff\\ff"},
275 : {{0xca, 0xfe, 0xc0, 0xff, 0xee}, "\\ca\\fe\\c0\\ff\\ee"},
276 : {{0}, NULL}
277 : };
278 :
279 1 : ret = bin_to_ldap_filter_value(ts, NULL, 0, NULL);
280 1 : assert_int_equal(ret, EINVAL);
281 :
282 5 : for (c = 0; test_data[c].str != NULL; c++) {
283 4 : ret = bin_to_ldap_filter_value(ts, test_data[c].blob, 5, &str);
284 4 : assert_int_equal(ret, EOK);
285 4 : assert_string_equal(str, test_data[c].str);
286 :
287 4 : talloc_free(str);
288 : }
289 :
290 1 : }
291 :
292 1 : void test_sss_cert_derb64_to_ldap_filter(void **state)
293 : {
294 : int ret;
295 : char *filter;
296 :
297 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
298 1 : assert_non_null(ts);
299 :
300 1 : ret = sss_cert_derb64_to_ldap_filter(ts, NULL, NULL, NULL);
301 1 : assert_int_equal(ret, EINVAL);
302 :
303 1 : ret = sss_cert_derb64_to_ldap_filter(ts, "AAECAwQFBgcICQ==", "attrName",
304 : &filter);
305 1 : assert_int_equal(ret, EOK);
306 1 : assert_string_equal(filter,
307 : "(attrName=\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09)");
308 :
309 1 : talloc_free(filter);
310 1 : }
311 :
312 :
313 : #define SSH_TEST_CERT \
314 : "MIIECTCCAvGgAwIBAgIBCDANBgkqhkiG9w0BAQsFADA0MRIwEAYDVQQKDAlJUEEu" \
315 : "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 : "hvcNAQELBQADggEBADH7Nj00qqGhGJeXJQAsepqSskz/wooqXh8vgVyb8SS4N0/c" \
331 : "0aQtVmY81xamlXE12ZFpwDX43d+EufBkwCUKFX/+8JFDd2doAyeJxv1xM22kKRpc" \
332 : "AqITPgMsa9ToGMWxjbVpc/X/5YfZixWPF0/eZUTotBj9oaR039UrhGfyN7OguF/G" \
333 : "rzmxtB5y4ZrMpcD/Oe90mkd9HY7sA/fB8OWOUgeRfQoh97HNS0UiDWsPtfxmjQG5" \
334 : "zotpoBIZmdH+ipYsu58HohHVlM9Wi5H4QmiiXl+Soldkq7eXYlafcmT7wv8+cKwz" \
335 : "Nz0Tm3+eYpFqRo3skr6QzXi525Jkg3r6r+kkhxU="
336 :
337 : #define SSH_PUB_KEY "AAAAB3NzaC1yc2EAAAADAQABAAABAQC11KuelZWPmdGliy6RQIxX2xJTwVxkLG2W/OSd3Bsj42jBwvcEvsIwelXuB5kMjy8fIWyDXQ5m2sDB2EL00nC2H7Ki7Y9C6sNtLFFJnnZPhLOTofVR6J3fDGr2MduQ7trvk9IkoDSA4pF4LosAprQI/FQuPXm24CE3IyRszJooriZFd6Pd2L72BdFYyxg0G0FjsuyMc2CXjVSZ2ViqOuwh3aJOqjq5va8ULfNa88Ff5hi2wjFG6/jeDPFEbXexJs2mGUJjZv/7NqMBawmDMaS4lET4/lEWVbge9udOPyMe19BwmwPwFduidu3z2OZ3PAzMH4VAwm06jx/yRI3d5r2P"
338 :
339 1 : void test_cert_to_ssh_key(void **state)
340 : {
341 : int ret;
342 : uint8_t *key;
343 : size_t key_size;
344 : uint8_t *exp_key;
345 : size_t exp_key_size;
346 : uint8_t *der;
347 : size_t der_size;
348 :
349 1 : struct test_state *ts = talloc_get_type_abort(*state, struct test_state);
350 1 : assert_non_null(ts);
351 :
352 1 : der = sss_base64_decode(ts, SSH_TEST_CERT, &der_size);
353 1 : assert_non_null(der);
354 :
355 1 : exp_key = sss_base64_decode(ts, SSH_PUB_KEY, &exp_key_size);
356 1 : assert_non_null(exp_key);
357 :
358 1 : ret = cert_to_ssh_key(ts, "sql:" ABS_SRC_DIR "/src/tests/cmocka/p11_nssdb",
359 : der, der_size, &key, &key_size);
360 1 : assert_int_equal(ret, EOK);
361 1 : assert_int_equal(key_size, exp_key_size);
362 1 : assert_memory_equal(key, exp_key, exp_key_size);
363 :
364 1 : talloc_free(der);
365 1 : talloc_free(key);
366 1 : talloc_free(exp_key);
367 1 : }
368 :
369 1 : int main(int argc, const char *argv[])
370 : {
371 : poptContext pc;
372 : int opt;
373 : int ret;
374 6 : struct poptOption long_options[] = {
375 : POPT_AUTOHELP
376 5 : SSSD_DEBUG_OPTS
377 : POPT_TABLEEND
378 : };
379 :
380 1 : const struct CMUnitTest tests[] = {
381 : cmocka_unit_test_setup_teardown(test_sss_cert_der_to_pem,
382 : setup, teardown),
383 : cmocka_unit_test_setup_teardown(test_sss_cert_pem_to_der,
384 : setup, teardown),
385 : cmocka_unit_test_setup_teardown(test_sss_cert_derb64_to_pem,
386 : setup, teardown),
387 : cmocka_unit_test_setup_teardown(test_sss_cert_pem_to_derb64,
388 : setup, teardown),
389 : cmocka_unit_test_setup_teardown(test_bin_to_ldap_filter_value,
390 : setup, teardown),
391 : cmocka_unit_test_setup_teardown(test_sss_cert_derb64_to_ldap_filter,
392 : setup, teardown),
393 : cmocka_unit_test_setup_teardown(test_cert_to_ssh_key,
394 : setup, teardown),
395 : };
396 :
397 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
398 1 : debug_level = SSSDBG_INVALID;
399 :
400 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
401 1 : while((opt = poptGetNextOpt(pc)) != -1) {
402 : switch(opt) {
403 : default:
404 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
405 : poptBadOption(pc, 0), poptStrerror(opt));
406 0 : poptPrintUsage(pc, stderr, 0);
407 0 : return 1;
408 : }
409 : }
410 1 : poptFreeContext(pc);
411 :
412 1 : DEBUG_CLI_INIT(debug_level);
413 :
414 1 : ret = cmocka_run_group_tests(tests, NULL, NULL);
415 :
416 : #ifdef HAVE_LIBCRYPTO
417 : CRYPTO_cleanup_all_ex_data(); /* to make valgrind happy */
418 : #endif
419 :
420 : #ifdef HAVE_NSS
421 : /* Cleanup NSS and NSPR to make valgrind happy. */
422 1 : nspr_nss_cleanup();
423 : #endif
424 :
425 :
426 1 : return ret;
427 : }
|