]>
Commit | Line | Data |
---|---|---|
5a02d13d RL |
1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include <string.h> | |
11 | ||
12 | #include <openssl/bio.h> | |
13 | #include <openssl/bn.h> | |
14 | #include <openssl/rsa.h> | |
15 | #include <openssl/evp.h> | |
16 | #include <openssl/provider.h> | |
17 | #include <openssl/core_names.h> | |
18 | #include "internal/nelem.h" | |
19 | #include "crypto/evp.h" /* For the internal API */ | |
20 | #include "testutil.h" | |
21 | ||
22 | typedef struct { | |
23 | OPENSSL_CTX *ctx1; | |
24 | OSSL_PROVIDER *prov1; | |
25 | OPENSSL_CTX *ctx2; | |
26 | OSSL_PROVIDER *prov2; | |
27 | } FIXTURE; | |
28 | ||
29 | static void tear_down(FIXTURE *fixture) | |
30 | { | |
31 | if (fixture != NULL) { | |
32 | OSSL_PROVIDER_unload(fixture->prov1); | |
33 | OSSL_PROVIDER_unload(fixture->prov2); | |
34 | OPENSSL_CTX_free(fixture->ctx1); | |
35 | OPENSSL_CTX_free(fixture->ctx2); | |
36 | OPENSSL_free(fixture); | |
37 | } | |
38 | } | |
39 | ||
40 | static FIXTURE *set_up(const char *testcase_name) | |
41 | { | |
42 | FIXTURE *fixture; | |
43 | ||
44 | if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))) | |
45 | || !TEST_ptr(fixture->ctx1 = OPENSSL_CTX_new()) | |
46 | || !TEST_ptr(fixture->prov1 = OSSL_PROVIDER_load(fixture->ctx1, | |
47 | "default")) | |
48 | || !TEST_ptr(fixture->ctx2 = OPENSSL_CTX_new()) | |
49 | || !TEST_ptr(fixture->prov2 = OSSL_PROVIDER_load(fixture->ctx2, | |
50 | "default"))) { | |
51 | tear_down(fixture); | |
52 | return NULL; | |
53 | } | |
54 | return fixture; | |
55 | } | |
56 | ||
57 | static int test_pass_rsa(FIXTURE *fixture) | |
58 | { | |
59 | /* Array indexes */ | |
60 | #define N 0 | |
61 | #define E 1 | |
62 | #define D 2 | |
63 | #define P 3 | |
64 | #define Q 4 | |
65 | #define F3 5 /* Extra factor */ | |
66 | #define DP 6 | |
67 | #define DQ 7 | |
68 | #define E3 8 /* Extra exponent */ | |
69 | #define QINV 9 | |
70 | #define C3 10 /* Extra coefficient */ | |
71 | ||
72 | size_t i; | |
73 | int ret = 0; | |
74 | RSA *rsa = NULL; | |
75 | BIGNUM *bn1 = NULL, *bn2 = NULL, *bn3 = NULL; | |
76 | EVP_PKEY *pk = NULL; | |
77 | EVP_KEYMGMT *km1 = NULL, *km2 = NULL; | |
78 | void *provdata = NULL; | |
79 | /* | |
80 | * 32-bit RSA key, extracted from this command, | |
81 | * executed with OpenSSL 1.0.2: | |
82 | * | |
83 | * openssl genrsa 32 | openssl rsa -text | |
84 | */ | |
85 | static BN_ULONG expected[] = { | |
86 | 0xbc747fc5, /* N */ | |
87 | 0x10001, /* E */ | |
88 | 0x7b133399, /* D */ | |
89 | 0xe963, /* P */ | |
90 | 0xceb7, /* Q */ | |
91 | 0, /* F3 */ | |
92 | 0x8599, /* DP */ | |
93 | 0xbd87, /* DQ */ | |
94 | 0, /* E3 */ | |
95 | 0xcc3b, /* QINV */ | |
96 | 0, /* C3 */ | |
97 | 0 /* Extra, should remain zero */ | |
98 | }; | |
99 | static unsigned long keydata[OSSL_NELEM(expected)] = { 0, }; | |
100 | OSSL_PARAM params[] = { | |
101 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &keydata[N]), | |
102 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &keydata[E]), | |
103 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &keydata[D]), | |
104 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &keydata[P]), | |
105 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &keydata[Q]), | |
106 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &keydata[F3]), | |
107 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &keydata[DP]), | |
108 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &keydata[DQ]), | |
109 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &keydata[E3]), | |
110 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT, &keydata[QINV]), | |
111 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT, &keydata[C3]), | |
112 | OSSL_PARAM_END | |
113 | }; | |
114 | ||
115 | if (!TEST_ptr(rsa = RSA_new())) | |
116 | goto err; | |
117 | ||
118 | if (!TEST_ptr(bn1 = BN_new()) | |
119 | || !TEST_true(BN_set_word(bn1, expected[N])) | |
120 | || !TEST_ptr(bn2 = BN_new()) | |
121 | || !TEST_true(BN_set_word(bn2, expected[E])) | |
122 | || !TEST_ptr(bn3 = BN_new()) | |
123 | || !TEST_true(BN_set_word(bn3, expected[D])) | |
124 | || !TEST_true(RSA_set0_key(rsa, bn1, bn2, bn3))) | |
125 | goto err; | |
126 | ||
127 | if (!TEST_ptr(bn1 = BN_new()) | |
128 | || !TEST_true(BN_set_word(bn1, expected[P])) | |
129 | || !TEST_ptr(bn2 = BN_new()) | |
130 | || !TEST_true(BN_set_word(bn2, expected[Q])) | |
131 | || !TEST_true(RSA_set0_factors(rsa, bn1, bn2))) | |
132 | goto err; | |
133 | ||
134 | if (!TEST_ptr(bn1 = BN_new()) | |
135 | || !TEST_true(BN_set_word(bn1, expected[DP])) | |
136 | || !TEST_ptr(bn2 = BN_new()) | |
137 | || !TEST_true(BN_set_word(bn2, expected[DQ])) | |
138 | || !TEST_ptr(bn3 = BN_new()) | |
139 | || !TEST_true(BN_set_word(bn3, expected[QINV])) | |
140 | || !TEST_true(RSA_set0_crt_params(rsa, bn1, bn2, bn3))) | |
141 | goto err; | |
142 | bn1 = bn2 = bn3 = NULL; | |
143 | ||
144 | if (!TEST_ptr(pk = EVP_PKEY_new()) | |
145 | || !TEST_true(EVP_PKEY_assign_RSA(pk, rsa))) | |
146 | goto err; | |
147 | rsa = NULL; | |
148 | ||
149 | if (!TEST_ptr(km1 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA", NULL)) | |
150 | || !TEST_ptr(km2 = EVP_KEYMGMT_fetch(fixture->ctx2, "RSA", NULL)) | |
151 | || !TEST_ptr_ne(km1, km2)) | |
152 | goto err; | |
153 | ||
154 | if (!TEST_ptr(evp_keymgmt_export_to_provider(pk, km1, 0)) | |
155 | || !TEST_ptr(provdata = evp_keymgmt_export_to_provider(pk, km2, 0))) | |
156 | goto err; | |
157 | ||
158 | if (!TEST_true(evp_keymgmt_exportkey(km2, provdata, params))) | |
159 | goto err; | |
160 | ||
161 | /* | |
162 | * At this point, the hope is that keydata will have all the numbers | |
163 | * from the key. | |
164 | */ | |
165 | ||
166 | for (i = 0; i < OSSL_NELEM(expected); i++) | |
167 | ret += !! TEST_int_eq(expected[i], keydata[i]); | |
168 | ||
169 | ret = (ret == OSSL_NELEM(expected)); | |
170 | ||
171 | err: | |
172 | RSA_free(rsa); | |
173 | BN_free(bn1); | |
174 | BN_free(bn2); | |
175 | BN_free(bn3); | |
176 | EVP_PKEY_free(pk); | |
177 | EVP_KEYMGMT_free(km1); | |
178 | EVP_KEYMGMT_free(km2); | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
183 | static int (*tests[])(FIXTURE *) = { | |
184 | test_pass_rsa | |
185 | }; | |
186 | ||
187 | static int test_pass_key(int n) | |
188 | { | |
189 | SETUP_TEST_FIXTURE(FIXTURE, set_up); | |
190 | EXECUTE_TEST(tests[n], tear_down); | |
191 | return result; | |
192 | } | |
193 | ||
194 | int setup_tests(void) | |
195 | { | |
196 | ADD_ALL_TESTS(test_pass_key, 1); | |
197 | return 1; | |
198 | } |