VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTCrPkix-1.cpp@ 77923

Last change on this file since 77923 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.2 KB
Line 
1/* $Id: tstRTCrPkix-1.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT testcase - Crypto - Public-Key Infrastructure \#1.
4 */
5
6/*
7 * Copyright (C) 2018-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/crypto/pkix.h>
32
33#include <iprt/err.h>
34#include <iprt/string.h>
35#include <iprt/test.h>
36#include <iprt/crypto/key.h>
37#include <iprt/crypto/digest.h>
38
39
40/*********************************************************************************************************************************
41* Global Variables *
42*********************************************************************************************************************************/
43static RTTEST g_hTest;
44
45/**
46 * Key pairs to use when testing.
47 */
48static const struct { unsigned cBits; const char *pszPrivateKey, *pszPublicKey, *pszPassword; } g_aKeyPairs[] =
49{
50 {
51 4096,
52 "-----BEGIN RSA PRIVATE KEY-----\n"
53 "MIIJKQIBAAKCAgEA1SOurMTVz033GGi+5VrMb0SnU7Dj49ZQCKSuxaSFK4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bWpRVNpXcCAwEAAQ==\n"
116 "-----END PUBLIC KEY-----\n",
117 NULL
118 },
119 {
120 2048,
121 "-----BEGIN RSA PRIVATE KEY-----\n"
122 "MIIEogIBAAKCAQEA06LAmfLBnRldEQF6E9CcMisCiaaDco0fYJvu60jkSBiA29k2\n"
123 "Ru7LzTF6ctNXkC25P4RC25RjOYJbC0iS5YIR7VYFP6R505zDWs8vONeFchdQpfau\n"
124 "TVjpgipIFovNGEUOGgXKD60n8txceuSygA3fg80movXmI7O+QLyrUkvFx2onDdVM\n"
125 "Vlt8uhBwv8h62mJArienFDbNyQcmj47Y5pxkBRrcA8qnti+I3I3yA3kslq2O0QtN\n"
126 "LHA7ttFYjieCcVv7pm/5g4kI2XyPv56RSem/RNsEv/qoK+g/h+b2C0sVO7eUyM6n\n"
127 "x9VT8w+ODunnYWs1HiAGAhzj7NhsnJp0gm88KwIDAQABAoIBAEvePnlx4yK0Yv6j\n"
128 "ruXHlRcPABvki57XJHZ3sBC80sldr2Qg3CpVlM38fM8JIIzZN12jxmv9KA0HxCep\n"
129 "Xq/UDyUr/zmvdtT7j7TQLTeNW5No9EpqwlWMGDnHeoxKlb2rk8CUbrlr87RGdwi/\n"
130 "T5ZEYupW8xDcYiJOX1fJywj3jPFNX70Iofirz+twKJuq/pT/It1b3VKVBZb5qSW/\n"
131 "kfMMnJ1kELEAk7ue1sXm5QzF0/CizHNalEGJjuKauH21iCy1BGuJ00F31iploB4f\n"
132 "lqzXpNbDGyFWfQo6bZwduyrdgBe2dFt4mg5htknJPo4oSl+oLi4HewhwO3jpt06z\n"
133 "KRoT8XECgYEA7vVX6QwGbfnK/+CePiTBrD3FOgzfDagn5jSrvH0Km/YDVIa/6T7k\n"
134 "9M2qw5MP7D9gWPDkS7L8hL/YxCSP0mYf4ABp89/n++V6ON7tEjyA3SixXpCqLYUd\n"
135 "nSYl/ygJblEujFvhVtZaKyGpTMQXyJpCbV3ZdAar8Mg2p36MusitsscCgYEA4rqU\n"
136 "oTurBh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JArienFDbNyQcmj47Y5pxk\n"
153 "BRrcA8qnti+I3I3yA3kslq2O0QtNLHA7ttFYjieCcVv7pm/5g4kI2XyPv56RSem/\n"
154 "RNsEv/qoK+g/h+b2C0sVO7eUyM6nx9VT8w+ODunnYWs1HiAGAhzj7NhsnJp0gm88\n"
155 "KwIDAQAB\n"
156 "-----END PUBLIC KEY-----\n",
157 NULL
158 },
159 {
160 1024,
161 "-----BEGIN RSA PRIVATE KEY-----\n"
162 "MIICXAIBAAKBgQC2wFEkDX17SxuhH4jrSl/+lSEEXI2YGzXbDnsroXMjAa6pGj9f\n"
163 "7+VOGvnBTJnT2FubDSvpaXMIEO0PTjMpS2fKKdn1jljAj3vfF9HpyyKOBgLwY1Pl\n"
164 "fwj3bNPUomGZ+sgigNYWJ4+lXlSxJ7UlTQuQd7PiRsgCEIRny+5thH/rSwIDAQAB\n"
165 "AoGAEzUTUh642YSDWuPdmB0xCajS14qCt0Hk3ykeeO93Em7S1KMVlhe4mgTryw0p\n"
166 "/cH3nsw7mUSj+m0M/VbSubxbJA7VMVoaM3gnnHAttQVrGHxKMfA2Yupp0gLB9SFa\n"
167 "W0oLO2NNz9IElQfPYWsir2VSqMbgil9srHxNMRMjcTv0O4ECQQDe8vstmZ3b2q5u\n"
168 "L+Fd5pGF+rK919Bh59Nuvv3xPsJVoVjcfRJKGLKVMe+AK9YicM2jqqgV9UQ7gSZK\n"
169 "z5jxS1YDAkEA0dfOsmFFGrAu4vAJf/YxJm/G7DyinM4Ffq1fVxCIZGOJxU5+EtH3\n"
170 "YTRA0U6kM77O9i4Ms2LM9agSz76hdPjXGQJARVxowo4JK44EOGmS/qit23XcR+2t\n"
171 "edgq0kh/Lp+szAEvaSFMIFtAq+PmNATvULWxdFqygmpUuQJ8DEg7t84NSwJAfMS7\n"
172 "UpbBVvAAwNCGZX5FlRwLA/W9nkxlOf/t2z+qST5h8V4NWjVbyIEgNRN0UIwYVInm\n"
173 "5VZOlZX8sWcgawN2KQJBAMvkCsY6sVjlK2FXA9f3FVHs6DT4g2TRLvCkwZAjbibY\n"
174 "qy2W1RrPdtPOKXfr251hAlimxwcGXwTsRm07qirlQjE=\n"
175 "-----END RSA PRIVATE KEY-----\n",
176 "-----BEGIN PUBLIC KEY-----\n"
177 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2wFEkDX17SxuhH4jrSl/+lSEE\n"
178 "XI2YGzXbDnsroXMjAa6pGj9f7+VOGvnBTJnT2FubDSvpaXMIEO0PTjMpS2fKKdn1\n"
179 "jljAj3vfF9HpyyKOBgLwY1Plfwj3bNPUomGZ+sgigNYWJ4+lXlSxJ7UlTQuQd7Pi\n"
180 "RsgCEIRny+5thH/rSwIDAQAB\n"
181 "-----END PUBLIC KEY-----\n",
182 NULL
183 },
184 {
185 512,
186 "-----BEGIN RSA PRIVATE KEY-----\n"
187 "MIIBOwIBAAJBAMgbhgcN8LxMNpEZgOC3hgI61pAwSxn4X8rSBHyTt7pfqbU0g2Tk\n"
188 "PsNT7J6YS2xN+MwKiYNDeCTjRRbt67o1ZscCAwEAAQJBAKyXOKEq/+CYZ1P8yDCJ\n"
189 "eZbAwsD4Nj4+//gB7ga4rXWbeDbkEFtLsN7wHIl1RQobfddStC5edTTbVJMk/NmX\n"
190 "ESkCIQDpouOkB/cJvxfqeHqXuk4IS2s/hESEjX8dxFPsa3iNVQIhANtDCGPHhSvf\n"
191 "za9hH/Wqxzbf2IrAPn/aJVNmphSi6wOrAiBj77IR2vpXp+7R86D0v9NbBu+kJq6s\n"
192 "SF4kXHNNgJb7VQIhAKfuFTTmkRZjWNNj3eh4Hg/nLaBHURb26vOPgM/5X2n1AiAo\n"
193 "b9m3zOpoO/0MAGCQ6qDHeebjvd65LSKgsmuDOSiOLw==\n"
194 "-----END RSA PRIVATE KEY-----\n",
195 "-----BEGIN PUBLIC KEY-----\n"
196 "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMgbhgcN8LxMNpEZgOC3hgI61pAwSxn4\n"
197 "X8rSBHyTt7pfqbU0g2TkPsNT7J6YS2xN+MwKiYNDeCTjRRbt67o1ZscCAwEAAQ==\n"
198 "-----END PUBLIC KEY-----\n",
199 NULL
200 },
201
202 /*
203 * Keys with passwords.
204 */
205 {
206 2048,
207 "-----BEGIN RSA PRIVATE KEY-----\n"
208 "Proc-Type: 4,ENCRYPTED\n"
209 "DEK-Info: AES-128-CBC,86B32E02F476832DE26291AEF884BFB2\n"
210 "\n"
211 "3vqVAOubNaajTSUj/t0ueXRG11kVOCbQkj6AoB4bO+xYUabtcisM4I34It6GN1ZJ\n"
212 "yXv2DcCE3At31LvvqS8bYGvRhY+oPpCUkC4DX+RX9Tkw5ivl1F9pv/rL3nv2F3LX\n"
213 "KxMUcygwJOG2ItPu+vLI0HDYGn9reR+6boriwQfU6S8An4C6LrIZK0hUN0Bpr6W+\n"
214 "JyTX9B3Tgy/BldW6yziRzYUZHnnKEKKacvHP5l0n/6nn6iFSJSFmnzvsedwOvUI0\n"
215 "eHQ1LvbfQnd5yIalQ5S8UkgpKb5S4s2U0AthAC67m+Nc0E8NcbCMY1JT4FlsWVLD\n"
216 "GqWmjKhwEBgoPRROEiq39KgPnoxnCEIOiQ6l8kZ0uvqlCHhWM4b1UVqb6hyrmY32\n"
217 "SEBiwRqFewVYzPFI1+vT3CH/BJcXCBISNj2c4OZDqhmgncGWpLwqU1GIlLp82o3l\n"
218 "t58WfNuqUM7bc/T6cIKAI2JoR2R96Zo0cgL+419msVUdZXhM/10K3W+wbHUVuSqh\n"
219 "iDOCJhXWIhu47kjbCOh7OvpOtOPayWBLQiGh1Q4+WQU6t6Vdr/i71dKP0/P/QHwk\n"
220 "ELNaWv/RLbE6PqKuXcjtoIqzynTvS/6C7PLEKEX3PB6kZNV+m7C0Dxu4BFj04vtx\n"
221 "5CL71sGaB1ETYUdMRSvCa+f/1zwUXngmozUL+D4PkCz/vT5FYKElWt7RBMt8N+rC\n"
222 "Iga+YqqvnuSPrxGXLCGZBuI2V+0BwG1p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Els04TSGE\n"
239 "sJ0Himl934+ryfNXYIRWkq91i5+rENyZ475XBMjg8fblhvHy7vy4GfUo0PKVXxWS\n"
240 "nPqOPSLEP3r2vsCX5l+KRBnGi4TeGWDTB8R6oA6HKY5ybtzUr1MHKwa7K7YJu7M9\n"
241 "DW7n2JPLRajUMioO9wbYK70qlbxjeOu0V62D68fWoa3alSWMlMBv9KZW9g2oJHQy\n"
242 "mUO2OdJFdyaah3z6vTKtzxmZ+NB4iwIjD6Go1CMj+FOjjjJb3EgUOIZAsRz/+9MF\n"
243 "S3cRfh/8u9cZQ20Woh5vmw1anXxbwk6Z8uIFYrdgcY5G7ak0/3VukbP7VzvG+voY\n"
244 "AwIDAQAB\n"
245 "-----END PUBLIC KEY-----\n",
246 "password"
247 }
248};
249
250
251
252
253static void test1()
254{
255 RTTestSub(g_hTest, "Basics");
256 int rc;
257 RTCRKEY hPublicKey = NIL_RTCRKEY;
258 RTCRKEY hPrivateKey = NIL_RTCRKEY;
259
260 for (unsigned i = 0; i < RT_ELEMENTS(g_aKeyPairs); i++)
261 {
262 RTCrKeyRelease(hPublicKey);
263 hPublicKey = NIL_RTCRKEY;
264 RTCrKeyRelease(hPrivateKey);
265 hPrivateKey = NIL_RTCRKEY;
266
267 /*
268 * Load the key pair.
269 */
270 rc = RTCrKeyCreateFromBuffer(&hPublicKey, 0, g_aKeyPairs[i].pszPublicKey, strlen(g_aKeyPairs[i].pszPublicKey),
271 NULL /*pszPassword*/, NULL /*pErrInfo*/, NULL /*pszErrorTag*/);
272 if (RT_FAILURE(rc))
273 RTTestIFailed("Error %Rrc decoding public key #%u (%u bits)", rc, i, g_aKeyPairs[i].cBits);
274
275 rc = RTCrKeyCreateFromBuffer(&hPrivateKey, 0, g_aKeyPairs[i].pszPrivateKey, strlen(g_aKeyPairs[i].pszPrivateKey),
276 g_aKeyPairs[i].pszPassword, NULL /*pErrInfo*/, NULL /*pszErrorTag*/);
277 if (RT_FAILURE(rc))
278 RTTestIFailed("Error %Rrc decoding private key #%u (%u bits)", rc, i, g_aKeyPairs[i].cBits);
279
280 if (hPrivateKey == NIL_RTCRKEY || hPublicKey == NIL_RTCRKEY)
281 continue;
282
283 /*
284 * If we've got a password encrypted key, try some incorrect password.
285 */
286 if (g_aKeyPairs[i].pszPassword)
287 {
288 static const char * const s_apszBadPassword[] =
289 {
290 "bad-password", "", "<>", "really really long long long bad bad bad bad bad password password password password",
291 "a", "ab", "abc", "abcd", "abcde", "fdcba"
292 };
293 for (unsigned iPasswd = 0; iPasswd < RT_ELEMENTS(s_apszBadPassword); iPasswd++)
294 {
295 RTCRKEY hKey = NIL_RTCRKEY;
296 rc = RTCrKeyCreateFromBuffer(&hKey, 0, g_aKeyPairs[i].pszPrivateKey, strlen(g_aKeyPairs[i].pszPrivateKey),
297 s_apszBadPassword[iPasswd], NULL /*pErrInfo*/, NULL /*pszErrorTag*/);
298 if (rc != VERR_CR_KEY_DECRYPTION_FAILED)
299 RTTestIFailed("Unexpected bad password response %Rrc decoding private key #%u (%u bits) using '%s' as password",
300 rc, i, g_aKeyPairs[i].cBits, s_apszBadPassword[iPasswd]);
301 }
302 }
303
304 /*
305 * Create corresponding signing and verifying decoder instances.
306 */
307 static struct { uint32_t cBits; const char *pszObjId; } const s_aSignatures[] =
308 {
309 { 128, RTCR_PKCS1_MD2_WITH_RSA_OID },
310 //{ 128, RTCR_PKCS1_MD4_WITH_RSA_OID },
311 { 128, RTCR_PKCS1_MD5_WITH_RSA_OID },
312 { 160, RTCR_PKCS1_SHA1_WITH_RSA_OID },
313 { 256, RTCR_PKCS1_SHA256_WITH_RSA_OID },
314 { 224, RTCR_PKCS1_SHA224_WITH_RSA_OID },
315 { 384, RTCR_PKCS1_SHA384_WITH_RSA_OID },
316 { 512, RTCR_PKCS1_SHA512_WITH_RSA_OID },
317 };
318 RTCRPKIXSIGNATURE hSign = NIL_RTCRPKIXSIGNATURE;
319 RTCRPKIXSIGNATURE hVerify = NIL_RTCRPKIXSIGNATURE;
320 for (unsigned iSig = 0; iSig < RT_ELEMENTS(s_aSignatures); iSig++)
321 {
322 RTCrPkixSignatureRelease(hSign);
323 hSign = NIL_RTCRPKIXSIGNATURE;
324 RTCrPkixSignatureRelease(hVerify);
325 hVerify = NIL_RTCRPKIXSIGNATURE;
326
327 rc = RTCrPkixSignatureCreateByObjIdString(&hSign, s_aSignatures[iSig].pszObjId, hPrivateKey, NULL, true /*fSigning*/);
328 if (RT_FAILURE(rc))
329 RTTestIFailed("RTCrPkixSignatureCreateByObjIdString failed with %Rrc on %u bits private key and %u bits MD (%s)",
330 rc, g_aKeyPairs[i].cBits, s_aSignatures[iSig].cBits, s_aSignatures[iSig].pszObjId);
331
332 rc = RTCrPkixSignatureCreateByObjIdString(&hVerify, s_aSignatures[iSig].pszObjId, hPublicKey, NULL, false /*fSigning*/);
333 if (RT_FAILURE(rc))
334 RTTestIFailed("RTCrPkixSignatureCreateByObjIdString failed with %Rrc on %u bits public key and %u bits MD (%s)",
335 rc, g_aKeyPairs[i].cBits, s_aSignatures[iSig].cBits, s_aSignatures[iSig].pszObjId);
336
337 if (RT_FAILURE(rc) || hSign == NIL_RTCRPKIXSIGNATURE || hVerify == NIL_RTCRPKIXSIGNATURE)
338 continue;
339
340 /*
341 * Try a few different boilplate things.
342 */
343 static struct { void const *pv; size_t cb; } const s_aTexts[] =
344 {
345 { RT_STR_TUPLE("") },
346 { RT_STR_TUPLE("IPRT") },
347 { RT_STR_TUPLE("abcdef") },
348 };
349
350 for (unsigned iText = 0; iText < RT_ELEMENTS(s_aTexts); iText++)
351 {
352 uint8_t abSignature[4096];
353 size_t cbSignature = sizeof(abSignature);
354
355 RTCRDIGEST hDigest = NIL_RTCRDIGEST;
356 rc = RTCrDigestCreateByObjIdString(&hDigest, s_aSignatures[iSig].pszObjId);
357 if (RT_SUCCESS(rc))
358 {
359 RTTESTI_CHECK_RC(RTCrDigestUpdate(hDigest, s_aTexts[iText].pv, s_aTexts[iText].cb), VINF_SUCCESS);
360
361 rc = RTCrPkixSignatureSign(hSign, hDigest, abSignature, &cbSignature);
362 if (RT_SUCCESS(rc))
363 {
364 rc = RTCrPkixSignatureVerify(hVerify, hDigest, abSignature, cbSignature);
365 if (RT_FAILURE(rc))
366 RTTestIFailed("RTCrPkixSignatureVerify failed with %Rrc for %u bits MD with %u bits key (%s); signature length %u",
367 rc, s_aSignatures[iSig].cBits, g_aKeyPairs[i].cBits, s_aSignatures[iSig].pszObjId, cbSignature);
368
369 }
370 else if (rc != VERR_CR_PKIX_HASH_TOO_LONG_FOR_KEY)
371 RTTestIFailed("RTCrPkixSignatureSign failed with %Rrc for %u bits MD with %u bits key (%s)",
372 rc, s_aSignatures[iSig].cBits, g_aKeyPairs[i].cBits, s_aSignatures[iSig].pszObjId);
373 RTCrDigestRelease(hDigest);
374 }
375 else
376 RTTestIFailed("RTCrDigestCreateByObjIdString failed with %Rrc for %s (%u bits)",
377 rc, s_aSignatures[iSig].pszObjId, s_aSignatures[iSig].cBits);
378 }
379 }
380
381 RTCrPkixSignatureRelease(hSign);
382 hSign = NIL_RTCRPKIXSIGNATURE;
383 RTCrPkixSignatureRelease(hVerify);
384 hVerify = NIL_RTCRPKIXSIGNATURE;
385 }
386
387 RTCrKeyRelease(hPublicKey);
388 hPublicKey = NIL_RTCRKEY;
389 RTCrKeyRelease(hPrivateKey);
390 hPrivateKey = NIL_RTCRKEY;
391}
392
393
394
395
396int main()
397{
398 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTCrPkix-1", &g_hTest);
399 if (rcExit != RTEXITCODE_SUCCESS)
400 return rcExit;
401 RTTestBanner(g_hTest);
402
403 test1();
404
405 return RTTestSummaryAndDestroy(g_hTest);
406}
407
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette