VirtualBox

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

Last change on this file since 100506 was 100421, checked in by vboxsync, 18 months ago

IPRT/PKCS8: add key format for PKCS #8 bugref:10299

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/* $Id: tstRTCrPkix-1.cpp 100421 2023-07-06 19:24:56Z vboxsync $ */
2/** @file
3 * IPRT testcase - Crypto - Public-Key Infrastructure \#1.
4 */
5
6/*
7 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/crypto/pkix.h>
42
43#include <iprt/err.h>
44#include <iprt/string.h>
45#include <iprt/test.h>
46#include <iprt/crypto/key.h>
47#include <iprt/crypto/digest.h>
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53static RTTEST g_hTest;
54
55/**
56 * Key pairs to use when testing.
57 */
58static const struct { unsigned cBits; const char *pszPrivateKey, *pszPublicKey, *pszPassword; } g_aKeyPairs[] =
59{
60 {
61 4096,
62 "-----BEGIN RSA PRIVATE KEY-----\n"
63 "MIIJKQIBAAKCAgEA1SOurMTVz033GGi+5VrMb0SnU7Dj49ZQCKSuxaSFK4tvbZXQ\n"
64 "BRSgwC1PcQVyt3GdoC71i3O4f9TxaA870icCIY7cqf4LKL9uB5Vga2SNMfx3+Kqc\n"
65 "JVt9LFsghXfLocdfV1k+xeDVGcSP7uUvnXoIZyeS8puqoRYNiua1UT+ddXwihTId\n"
66 "+6O9Q8IxcCPWkqW89LYBQVFqqMYoWzNcbEctY6WpPzZk3er+AvMekBD409LbtT7j\n"
67 "TrzIGd6eQ0aF2MyVA6lOwe3u99Ubo/FTpule/FQ5LXaEmlHPfDbIw+LRArdYgjoQ\n"
68 "U9l4SFajm0VbIKd2LFn5SRXHTbtAoKX2zpaoi8GF3u8VR/EmmTPYFHr2gUoLuyeT\n"
69 "aY56OG/5ns7N/NRzOX1d1lNRFcQYNCXPEtqaUfUfMJU4Jqp1LOEcd1xMkOUh8lc7\n"
70 "DyvUfhry+SAcxB5SxcyjdWEXpj4G12/N3f6vsRoZNTFt5j0hsbiOAOFykgN0a2OF\n"
71 "77bsd975e1mxkqXJ9A0sbB8EXsD2PSrUZ7Pt+T9CiQGOjqVUg2Vr1jevcQRHe5ed\n"
72 "/R+B2jp6MjYjbr7cKqcXaRxEprGl+U5kIygql93DTgQaXwX/ZjXmwjXvQ0W4Oxxe\n"
73 "xqyW6YvDBYeNKxstuM5qfgzYf7FD/8lZYkyMAXELgpCqC92xlTbWpRVNpXcCAwEA\n"
74 "AQKCAgAlkBpSvIXp+RWZKayrAyuQWIscxsoC91w3ib57epk1qWdD6uk0XARQmius\n"
75 "AYfMKKvc9Sm1H/neHYtGCZlDWjiX7XOaSflxfvtHPt41Tw1LR/Fk07ydINiYnp7G\n"
76 "puwuYNK+tC3J9evYlLnBIocXu9ALTgAp3aFermJInoxJ+2omsG/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GOjqVUg2Vr1jevcQRHe5ed/R+B2jp6MjYjbr7cKqcXaRxEprGl+U5kIygq\n"
124 "l93DTgQaXwX/ZjXmwjXvQ0W4OxxexqyW6YvDBYeNKxstuM5qfgzYf7FD/8lZYkyM\n"
125 "AXELgpCqC92xlTbWpRVNpXcCAwEAAQ==\n"
126 "-----END PUBLIC KEY-----\n",
127 NULL
128 },
129 {
130 2048,
131 "-----BEGIN RSA PRIVATE KEY-----\n"
132 "MIIEogIBAAKCAQEA06LAmfLBnRldEQF6E9CcMisCiaaDco0fYJvu60jkSBiA29k2\n"
133 "Ru7LzTF6ctNXkC25P4RC25RjOYJbC0iS5YIR7VYFP6R505zDWs8vONeFchdQpfau\n"
134 "TVjpgipIFovNGEUOGgXKD60n8txceuSygA3fg80movXmI7O+QLyrUkvFx2onDdVM\n"
135 "Vlt8uhBwv8h62mJArienFDbNyQcmj47Y5pxkBRrcA8qnti+I3I3yA3kslq2O0QtN\n"
136 "LHA7ttFYjieCcVv7pm/5g4kI2XyPv56RSem/RNsEv/qoK+g/h+b2C0sVO7eUyM6n\n"
137 "x9VT8w+ODunnYWs1HiAGAhzj7NhsnJp0gm88KwIDAQABAoIBAEvePnlx4yK0Yv6j\n"
138 "ruXHlRcPABvki57XJHZ3sBC80sldr2Qg3CpVlM38fM8JIIzZN12jxmv9KA0HxCep\n"
139 "Xq/UDyUr/zmvdtT7j7TQLTeNW5No9EpqwlWMGDnHeoxKlb2rk8CUbrlr87RGdwi/\n"
140 "T5ZEYupW8xDcYiJOX1fJywj3jPFNX70Iofirz+twKJuq/pT/It1b3VKVBZb5qSW/\n"
141 "kfMMnJ1kELEAk7ue1sXm5QzF0/CizHNalEGJjuKauH21iCy1BGuJ00F31iploB4f\n"
142 "lqzXpNbDGyFWfQo6bZwduyrdgBe2dFt4mg5htknJPo4oSl+oLi4HewhwO3jpt06z\n"
143 "KRoT8XECgYEA7vVX6QwGbfnK/+CePiTBrD3FOgzfDagn5jSrvH0Km/YDVIa/6T7k\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BiA29k2Ru7LzTF6ctNXkC25P4RC25RjOYJbC0iS5YIR\n"
161 "7VYFP6R505zDWs8vONeFchdQpfauTVjpgipIFovNGEUOGgXKD60n8txceuSygA3f\n"
162 "g80movXmI7O+QLyrUkvFx2onDdVMVlt8uhBwv8h62mJArienFDbNyQcmj47Y5pxk\n"
163 "BRrcA8qnti+I3I3yA3kslq2O0QtNLHA7ttFYjieCcVv7pm/5g4kI2XyPv56RSem/\n"
164 "RNsEv/qoK+g/h+b2C0sVO7eUyM6nx9VT8w+ODunnYWs1HiAGAhzj7NhsnJp0gm88\n"
165 "KwIDAQAB\n"
166 "-----END PUBLIC KEY-----\n",
167 NULL
168 },
169 {
170 1024,
171 "-----BEGIN RSA PRIVATE KEY-----\n"
172 "MIICXAIBAAKBgQC2wFEkDX17SxuhH4jrSl/+lSEEXI2YGzXbDnsroXMjAa6pGj9f\n"
173 "7+VOGvnBTJnT2FubDSvpaXMIEO0PTjMpS2fKKdn1jljAj3vfF9HpyyKOBgLwY1Pl\n"
174 "fwj3bNPUomGZ+sgigNYWJ4+lXlSxJ7UlTQuQd7PiRsgCEIRny+5thH/rSwIDAQAB\n"
175 "AoGAEzUTUh642YSDWuPdmB0xCajS14qCt0Hk3ykeeO93Em7S1KMVlhe4mgTryw0p\n"
176 "/cH3nsw7mUSj+m0M/VbSubxbJA7VMVoaM3gnnHAttQVrGHxKMfA2Yupp0gLB9SFa\n"
177 "W0oLO2NNz9IElQfPYWsir2VSqMbgil9srHxNMRMjcTv0O4ECQQDe8vstmZ3b2q5u\n"
178 "L+Fd5pGF+rK919Bh59Nuvv3xPsJVoVjcfRJKGLKVMe+AK9YicM2jqqgV9UQ7gSZK\n"
179 "z5jxS1YDAkEA0dfOsmFFGrAu4vAJf/YxJm/G7DyinM4Ffq1fVxCIZGOJxU5+EtH3\n"
180 "YTRA0U6kM77O9i4Ms2LM9agSz76hdPjXGQJARVxowo4JK44EOGmS/qit23XcR+2t\n"
181 "edgq0kh/Lp+szAEvaSFMIFtAq+PmNATvULWxdFqygmpUuQJ8DEg7t84NSwJAfMS7\n"
182 "UpbBVvAAwNCGZX5FlRwLA/W9nkxlOf/t2z+qST5h8V4NWjVbyIEgNRN0UIwYVInm\n"
183 "5VZOlZX8sWcgawN2KQJBAMvkCsY6sVjlK2FXA9f3FVHs6DT4g2TRLvCkwZAjbibY\n"
184 "qy2W1RrPdtPOKXfr251hAlimxwcGXwTsRm07qirlQjE=\n"
185 "-----END RSA PRIVATE KEY-----\n",
186 "-----BEGIN PUBLIC KEY-----\n"
187 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2wFEkDX17SxuhH4jrSl/+lSEE\n"
188 "XI2YGzXbDnsroXMjAa6pGj9f7+VOGvnBTJnT2FubDSvpaXMIEO0PTjMpS2fKKdn1\n"
189 "jljAj3vfF9HpyyKOBgLwY1Plfwj3bNPUomGZ+sgigNYWJ4+lXlSxJ7UlTQuQd7Pi\n"
190 "RsgCEIRny+5thH/rSwIDAQAB\n"
191 "-----END PUBLIC KEY-----\n",
192 NULL
193 },
194 {
195 512,
196 "-----BEGIN RSA PRIVATE KEY-----\n"
197 "MIIBOwIBAAJBAMgbhgcN8LxMNpEZgOC3hgI61pAwSxn4X8rSBHyTt7pfqbU0g2Tk\n"
198 "PsNT7J6YS2xN+MwKiYNDeCTjRRbt67o1ZscCAwEAAQJBAKyXOKEq/+CYZ1P8yDCJ\n"
199 "eZbAwsD4Nj4+//gB7ga4rXWbeDbkEFtLsN7wHIl1RQobfddStC5edTTbVJMk/NmX\n"
200 "ESkCIQDpouOkB/cJvxfqeHqXuk4IS2s/hESEjX8dxFPsa3iNVQIhANtDCGPHhSvf\n"
201 "za9hH/Wqxzbf2IrAPn/aJVNmphSi6wOrAiBj77IR2vpXp+7R86D0v9NbBu+kJq6s\n"
202 "SF4kXHNNgJb7VQIhAKfuFTTmkRZjWNNj3eh4Hg/nLaBHURb26vOPgM/5X2n1AiAo\n"
203 "b9m3zOpoO/0MAGCQ6qDHeebjvd65LSKgsmuDOSiOLw==\n"
204 "-----END RSA PRIVATE KEY-----\n",
205 "-----BEGIN PUBLIC KEY-----\n"
206 "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMgbhgcN8LxMNpEZgOC3hgI61pAwSxn4\n"
207 "X8rSBHyTt7pfqbU0g2TkPsNT7J6YS2xN+MwKiYNDeCTjRRbt67o1ZscCAwEAAQ==\n"
208 "-----END PUBLIC KEY-----\n",
209 NULL
210 },
211
212 /*
213 * Keys with passwords.
214 */
215 {
216 2048,
217 "-----BEGIN RSA PRIVATE KEY-----\n"
218 "Proc-Type: 4,ENCRYPTED\n"
219 "DEK-Info: AES-128-CBC,86B32E02F476832DE26291AEF884BFB2\n"
220 "\n"
221 "3vqVAOubNaajTSUj/t0ueXRG11kVOCbQkj6AoB4bO+xYUabtcisM4I34It6GN1ZJ\n"
222 "yXv2DcCE3At31LvvqS8bYGvRhY+oPpCUkC4DX+RX9Tkw5ivl1F9pv/rL3nv2F3LX\n"
223 "KxMUcygwJOG2ItPu+vLI0HDYGn9reR+6boriwQfU6S8An4C6LrIZK0hUN0Bpr6W+\n"
224 "JyTX9B3Tgy/BldW6yziRzYUZHnnKEKKacvHP5l0n/6nn6iFSJSFmnzvsedwOvUI0\n"
225 "eHQ1LvbfQnd5yIalQ5S8UkgpKb5S4s2U0AthAC67m+Nc0E8NcbCMY1JT4FlsWVLD\n"
226 "GqWmjKhwEBgoPRROEiq39KgPnoxnCEIOiQ6l8kZ0uvqlCHhWM4b1UVqb6hyrmY32\n"
227 "SEBiwRqFewVYzPFI1+vT3CH/BJcXCBISNj2c4OZDqhmgncGWpLwqU1GIlLp82o3l\n"
228 "t58WfNuqUM7bc/T6cIKAI2JoR2R96Zo0cgL+419msVUdZXhM/10K3W+wbHUVuSqh\n"
229 "iDOCJhXWIhu47kjbCOh7OvpOtOPayWBLQiGh1Q4+WQU6t6Vdr/i71dKP0/P/QHwk\n"
230 "ELNaWv/RLbE6PqKuXcjtoIqz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+Sl17qDrw9H/Y7k1j90zWFn8kykpwSRt0bV\n"
246 "-----END RSA PRIVATE KEY-----\n",
247 "-----BEGIN PUBLIC KEY-----\n"
248 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvvqfSDO1HN3Els04TSGE\n"
249 "sJ0Himl934+ryfNXYIRWkq91i5+rENyZ475XBMjg8fblhvHy7vy4GfUo0PKVXxWS\n"
250 "nPqOPSLEP3r2vsCX5l+KRBnGi4TeGWDTB8R6oA6HKY5ybtzUr1MHKwa7K7YJu7M9\n"
251 "DW7n2JPLRajUMioO9wbYK70qlbxjeOu0V62D68fWoa3alSWMlMBv9KZW9g2oJHQy\n"
252 "mUO2OdJFdyaah3z6vTKtzxmZ+NB4iwIjD6Go1CMj+FOjjjJb3EgUOIZAsRz/+9MF\n"
253 "S3cRfh/8u9cZQ20Woh5vmw1anXxbwk6Z8uIFYrdgcY5G7ak0/3VukbP7VzvG+voY\n"
254 "AwIDAQAB\n"
255 "-----END PUBLIC KEY-----\n",
256 "password"
257 },
258
259 /*
260 * PKCS8 Test Keys
261 */
262 {
263 1024,
264 "-----BEGIN PRIVATE KEY-----\n"
265 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAK4uHX+XRbLQ7dGm\n"
266 "sE1IqNDi4Obf7WS2TwfklmterJvCMrN3DxDAFq9et5j8kFRtI0Lgbc6sVAxlSkaw\n"
267 "+0LltbkC8JX0cjPSIlozzcZn+9dQ+m5rVLDl3AaV3kBLrYpnNggdTRiHuVbNPqZq\n"
268 "0CNDMxCqHpqRjtIOuoKukcOZasD5AgMBAAECgYA4IlKNaTIkM+NBGshcz9rgHw4+\n"
269 "OdKnD34e3BOCHOvh8s8mOWuYiV+GOy9OVa8qFlYz2mJpJe6cZBRw/d6sK53Jrzc1\n"
270 "ULULW9YNqgkhdhTm0z8QolYjBU+qp9pAXhh29tCdMxgCWAsiVR9jsnFtPQX4QEmM\n"
271 "9t+65ghTFQWtQXMqpQJBANly600i4GYoxvzvp67RvUkmnG47LvwuVRMwUAmAX6QP\n"
272 "Ww5q6aJd9HnHttLsNHxgX49aVxgpFu2uJI2SwSV3qwMCQQDND2kty83UXW5RahIt\n"
273 "BXAY8W60Itw6+bPLg3P4IixDCoHphnLqkz5ZT2NxxPsAPGeaFZDVyNs3Hgasnd8V\n"
274 "V8VTAkEAi4KWgrvQmtqoqFkeDSRVvBwAmxxvja4wOQpzH1V0hy6u7fYcBWcgVg2T\n"
275 "N4oCNpYiWTfNzxt1sXJb01UHhIFdfwJAO8ZiQpdGSMFzhwgEhFsxchPu0VPYHtjr\n"
276 "MEgBZjOP83r8o7YtiXOimSYrNt7UzBzPlnry3V7PiCGYkHj0rqQHQQJBANi5N5X4\n"
277 "g7dNDsE5i1B0JsQ4ru8qE60ZtoOOCwNjwiI/IIsMVW2KqhTBynEYLnWolkRRogEF\n"
278 "ACoRRxUBhj9EefI=\n"
279 "-----END PRIVATE KEY-----\n",
280 "-----BEGIN PUBLIC KEY-----\n"
281 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuLh1/l0Wy0O3RprBNSKjQ4uDm\n"
282 "3+1ktk8H5JZrXqybwjKzdw8QwBavXreY/JBUbSNC4G3OrFQMZUpGsPtC5bW5AvCV\n"
283 "9HIz0iJaM83GZ/vXUPpua1Sw5dwGld5AS62KZzYIHU0Yh7lWzT6matAjQzMQqh6a\n"
284 "kY7SDrqCrpHDmWrA+QIDAQAB\n"
285 "-----END PUBLIC KEY-----\n",
286 NULL
287 }
288};
289
290
291
292
293static void test1()
294{
295 RTTestSub(g_hTest, "Basics");
296 int rc;
297 RTCRKEY hPublicKey = NIL_RTCRKEY;
298 RTCRKEY hPrivateKey = NIL_RTCRKEY;
299
300 for (unsigned i = 0; i < RT_ELEMENTS(g_aKeyPairs); i++)
301 {
302 RTCrKeyRelease(hPublicKey);
303 hPublicKey = NIL_RTCRKEY;
304 RTCrKeyRelease(hPrivateKey);
305 hPrivateKey = NIL_RTCRKEY;
306
307 /*
308 * Load the key pair.
309 */
310 rc = RTCrKeyCreateFromBuffer(&hPublicKey, 0, g_aKeyPairs[i].pszPublicKey, strlen(g_aKeyPairs[i].pszPublicKey),
311 NULL /*pszPassword*/, NULL /*pErrInfo*/, NULL /*pszErrorTag*/);
312 if (RT_FAILURE(rc))
313 RTTestIFailed("Error %Rrc decoding public key #%u (%u bits)", rc, i, g_aKeyPairs[i].cBits);
314
315 rc = RTCrKeyCreateFromBuffer(&hPrivateKey, 0, g_aKeyPairs[i].pszPrivateKey, strlen(g_aKeyPairs[i].pszPrivateKey),
316 g_aKeyPairs[i].pszPassword, NULL /*pErrInfo*/, NULL /*pszErrorTag*/);
317 if (RT_FAILURE(rc))
318 RTTestIFailed("Error %Rrc decoding private key #%u (%u bits)", rc, i, g_aKeyPairs[i].cBits);
319
320 if (hPrivateKey == NIL_RTCRKEY || hPublicKey == NIL_RTCRKEY)
321 continue;
322
323 /*
324 * If we've got a password encrypted key, try some incorrect password.
325 */
326 if (g_aKeyPairs[i].pszPassword)
327 {
328 static const char * const s_apszBadPassword[] =
329 {
330 "bad-password", "", "<>", "really really long long long bad bad bad bad bad password password password password",
331 "a", "ab", "abc", "abcd", "abcde", "fdcba"
332 };
333 for (unsigned iPasswd = 0; iPasswd < RT_ELEMENTS(s_apszBadPassword); iPasswd++)
334 {
335 RTCRKEY hKey = NIL_RTCRKEY;
336 rc = RTCrKeyCreateFromBuffer(&hKey, 0, g_aKeyPairs[i].pszPrivateKey, strlen(g_aKeyPairs[i].pszPrivateKey),
337 s_apszBadPassword[iPasswd], NULL /*pErrInfo*/, NULL /*pszErrorTag*/);
338 if (rc != VERR_CR_KEY_DECRYPTION_FAILED)
339 RTTestIFailed("Unexpected bad password response %Rrc decoding private key #%u (%u bits) using '%s' as password",
340 rc, i, g_aKeyPairs[i].cBits, s_apszBadPassword[iPasswd]);
341 }
342 }
343
344 /*
345 * Create corresponding signing and verifying decoder instances.
346 */
347 static struct { uint32_t cBits; const char *pszObjId; } const s_aSignatures[] =
348 {
349 { 128, RTCR_PKCS1_MD2_WITH_RSA_OID },
350 //{ 128, RTCR_PKCS1_MD4_WITH_RSA_OID },
351 { 128, RTCR_PKCS1_MD5_WITH_RSA_OID },
352 { 160, RTCR_PKCS1_SHA1_WITH_RSA_OID },
353 { 256, RTCR_PKCS1_SHA256_WITH_RSA_OID },
354 { 224, RTCR_PKCS1_SHA224_WITH_RSA_OID },
355 { 384, RTCR_PKCS1_SHA384_WITH_RSA_OID },
356 { 512, RTCR_PKCS1_SHA512_WITH_RSA_OID },
357 };
358 RTCRPKIXSIGNATURE hSign = NIL_RTCRPKIXSIGNATURE;
359 RTCRPKIXSIGNATURE hVerify = NIL_RTCRPKIXSIGNATURE;
360 for (unsigned iSig = 0; iSig < RT_ELEMENTS(s_aSignatures); iSig++)
361 {
362 RTCrPkixSignatureRelease(hSign);
363 hSign = NIL_RTCRPKIXSIGNATURE;
364 RTCrPkixSignatureRelease(hVerify);
365 hVerify = NIL_RTCRPKIXSIGNATURE;
366
367 rc = RTCrPkixSignatureCreateByObjIdString(&hSign, s_aSignatures[iSig].pszObjId, hPrivateKey, NULL, true /*fSigning*/);
368 if (RT_FAILURE(rc))
369 RTTestIFailed("RTCrPkixSignatureCreateByObjIdString failed with %Rrc on %u bits private key and %u bits MD (%s)",
370 rc, g_aKeyPairs[i].cBits, s_aSignatures[iSig].cBits, s_aSignatures[iSig].pszObjId);
371
372 rc = RTCrPkixSignatureCreateByObjIdString(&hVerify, s_aSignatures[iSig].pszObjId, hPublicKey, NULL, false /*fSigning*/);
373 if (RT_FAILURE(rc))
374 RTTestIFailed("RTCrPkixSignatureCreateByObjIdString failed with %Rrc on %u bits public key and %u bits MD (%s)",
375 rc, g_aKeyPairs[i].cBits, s_aSignatures[iSig].cBits, s_aSignatures[iSig].pszObjId);
376
377 if (RT_FAILURE(rc) || hSign == NIL_RTCRPKIXSIGNATURE || hVerify == NIL_RTCRPKIXSIGNATURE)
378 continue;
379
380 /*
381 * Try a few different boilplate things.
382 */
383 static struct { void const *pv; size_t cb; } const s_aTexts[] =
384 {
385 { RT_STR_TUPLE("") },
386 { RT_STR_TUPLE("IPRT") },
387 { RT_STR_TUPLE("abcdef") },
388 };
389
390 for (unsigned iText = 0; iText < RT_ELEMENTS(s_aTexts); iText++)
391 {
392 uint8_t abSignature[4096];
393 size_t cbSignature = sizeof(abSignature);
394
395 RTCRDIGEST hDigest = NIL_RTCRDIGEST;
396 rc = RTCrDigestCreateByObjIdString(&hDigest, s_aSignatures[iSig].pszObjId);
397 if (RT_SUCCESS(rc))
398 {
399 RTTESTI_CHECK_RC(RTCrDigestUpdate(hDigest, s_aTexts[iText].pv, s_aTexts[iText].cb), VINF_SUCCESS);
400
401 rc = RTCrPkixSignatureSign(hSign, hDigest, abSignature, &cbSignature);
402 if (RT_SUCCESS(rc))
403 {
404 rc = RTCrPkixSignatureVerify(hVerify, hDigest, abSignature, cbSignature);
405 if (RT_FAILURE(rc))
406 RTTestIFailed("RTCrPkixSignatureVerify failed with %Rrc for %u bits MD with %u bits key (%s); signature length %u",
407 rc, s_aSignatures[iSig].cBits, g_aKeyPairs[i].cBits, s_aSignatures[iSig].pszObjId, cbSignature);
408
409 }
410 else if (rc != VERR_CR_PKIX_HASH_TOO_LONG_FOR_KEY)
411 RTTestIFailed("RTCrPkixSignatureSign failed with %Rrc for %u bits MD with %u bits key (%s)",
412 rc, s_aSignatures[iSig].cBits, g_aKeyPairs[i].cBits, s_aSignatures[iSig].pszObjId);
413 RTCrDigestRelease(hDigest);
414 }
415 else
416 RTTestIFailed("RTCrDigestCreateByObjIdString failed with %Rrc for %s (%u bits)",
417 rc, s_aSignatures[iSig].pszObjId, s_aSignatures[iSig].cBits);
418 }
419 }
420
421 RTCrPkixSignatureRelease(hSign);
422 hSign = NIL_RTCRPKIXSIGNATURE;
423 RTCrPkixSignatureRelease(hVerify);
424 hVerify = NIL_RTCRPKIXSIGNATURE;
425 }
426
427 RTCrKeyRelease(hPublicKey);
428 hPublicKey = NIL_RTCRKEY;
429 RTCrKeyRelease(hPrivateKey);
430 hPrivateKey = NIL_RTCRKEY;
431}
432
433
434
435
436int main()
437{
438 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTCrPkix-1", &g_hTest);
439 if (rcExit != RTEXITCODE_SUCCESS)
440 return rcExit;
441 RTTestBanner(g_hTest);
442
443 test1();
444
445 return RTTestSummaryAndDestroy(g_hTest);
446}
447
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