VirtualBox

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

Last change on this file since 74414 was 73807, checked in by vboxsync, 6 years ago

tstRTCrPkix-1: Forgot to update the digest with the text we wanted to sign (been signing empty messages till now). bugref:9152

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