|
26 | 26 |
|
27 | 27 | #ifdef WP_HAVE_RSA |
28 | 28 |
|
| 29 | +#ifndef ARRAY_SIZE |
| 30 | +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 31 | +#endif |
| 32 | + |
29 | 33 | static const unsigned char rsa_key_der_256[] = |
30 | 34 | { |
31 | 35 | 0x30, 0x81, 0xC1, 0x02, 0x01, 0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, |
@@ -1025,4 +1029,154 @@ int test_rsa_load_cert(void* data) |
1025 | 1029 | OSSL_STORE_close(ctx); |
1026 | 1030 | return err; |
1027 | 1031 | } |
| 1032 | + |
| 1033 | +int test_rsa_fromdata(void* data) |
| 1034 | +{ |
| 1035 | + (void)data; |
| 1036 | + int err = 0; |
| 1037 | + EVP_PKEY_CTX *ctx_wolf = NULL; |
| 1038 | + EVP_PKEY_CTX *ctx_ossl = NULL; |
| 1039 | + |
| 1040 | + PRINT_MSG("Testing EVP_PKEY_fromdata"); |
| 1041 | + |
| 1042 | + ctx_wolf = EVP_PKEY_CTX_new_from_name(wpLibCtx, "RSA", NULL); |
| 1043 | + ctx_ossl = EVP_PKEY_CTX_new_from_name(osslLibCtx, "RSA", NULL); |
| 1044 | + if (ctx_wolf == NULL || ctx_ossl == NULL) { |
| 1045 | + err = 1; |
| 1046 | + } |
| 1047 | + |
| 1048 | + if (err == 0) { |
| 1049 | + /* EVP_PKEY_fromdata_init returns 1 on success */ |
| 1050 | + err |= EVP_PKEY_fromdata_init(ctx_wolf) != 1; |
| 1051 | + err |= EVP_PKEY_fromdata_init(ctx_ossl) != 1; |
| 1052 | + } |
| 1053 | + |
| 1054 | + if (err == 0) { |
| 1055 | + EVP_PKEY *pkey_wolf = NULL; |
| 1056 | + EVP_PKEY *pkey_ossl = NULL; |
| 1057 | + |
| 1058 | + /* Permutations of the selection field to test */ |
| 1059 | + static const int selections[] = { |
| 1060 | + EVP_PKEY_KEYPAIR, |
| 1061 | + EVP_PKEY_PUBLIC_KEY, |
| 1062 | + EVP_PKEY_PRIVATE_KEY, |
| 1063 | + }; |
| 1064 | + |
| 1065 | + /* Parameter data fields */ |
| 1066 | + unsigned long rsa_n = 0xbc747fc5; |
| 1067 | + unsigned long rsa_e = 0x10001; |
| 1068 | + unsigned long rsa_d = 0x7b133399; |
| 1069 | + const char *foo = "some string"; |
| 1070 | + size_t foo_l = strlen(foo); |
| 1071 | + const char bar[] = "some other string"; |
| 1072 | + |
| 1073 | + /* Permutations of the params field to test */ |
| 1074 | + OSSL_PARAM params_none[] = { |
| 1075 | + OSSL_PARAM_END |
| 1076 | + }; |
| 1077 | + OSSL_PARAM params_n[] = { |
| 1078 | + OSSL_PARAM_ulong("n", &rsa_n), |
| 1079 | + OSSL_PARAM_END |
| 1080 | + }; |
| 1081 | + OSSL_PARAM params_e[] = { |
| 1082 | + OSSL_PARAM_ulong("e", &rsa_e), |
| 1083 | + OSSL_PARAM_END |
| 1084 | + }; |
| 1085 | + OSSL_PARAM params_d[] = { |
| 1086 | + OSSL_PARAM_ulong("d", &rsa_d), |
| 1087 | + OSSL_PARAM_END |
| 1088 | + }; |
| 1089 | + OSSL_PARAM params_ne[] = { |
| 1090 | + OSSL_PARAM_ulong("n", &rsa_n), |
| 1091 | + OSSL_PARAM_ulong("e", &rsa_e), |
| 1092 | + OSSL_PARAM_END |
| 1093 | + }; |
| 1094 | + OSSL_PARAM params_nd[] = { |
| 1095 | + OSSL_PARAM_ulong("n", &rsa_n), |
| 1096 | + OSSL_PARAM_ulong("d", &rsa_d), |
| 1097 | + OSSL_PARAM_END |
| 1098 | + }; |
| 1099 | + OSSL_PARAM params_ed[] = { |
| 1100 | + OSSL_PARAM_ulong("e", &rsa_e), |
| 1101 | + OSSL_PARAM_ulong("d", &rsa_d), |
| 1102 | + OSSL_PARAM_END |
| 1103 | + }; |
| 1104 | + OSSL_PARAM params_ned[] = { |
| 1105 | + OSSL_PARAM_ulong("n", &rsa_n), |
| 1106 | + OSSL_PARAM_ulong("e", &rsa_e), |
| 1107 | + OSSL_PARAM_ulong("d", &rsa_d), |
| 1108 | + OSSL_PARAM_END |
| 1109 | + }; |
| 1110 | + OSSL_PARAM params_extra_ulong[] = { |
| 1111 | + OSSL_PARAM_ulong("n", &rsa_n), |
| 1112 | + OSSL_PARAM_ulong("e", &rsa_e), |
| 1113 | + OSSL_PARAM_ulong("d", &rsa_d), |
| 1114 | + OSSL_PARAM_ulong("asdf", &rsa_d), |
| 1115 | + OSSL_PARAM_END |
| 1116 | + }; |
| 1117 | + OSSL_PARAM params_extra_str[] = { |
| 1118 | + OSSL_PARAM_ulong("n", &rsa_n), |
| 1119 | + OSSL_PARAM_ulong("e", &rsa_e), |
| 1120 | + OSSL_PARAM_ulong("d", &rsa_d), |
| 1121 | + { "foo", OSSL_PARAM_UTF8_PTR, &foo, foo_l, 0 }, |
| 1122 | + { "bar", OSSL_PARAM_UTF8_STRING, (void *)&bar, sizeof(bar) - 1, 0 }, |
| 1123 | + OSSL_PARAM_END |
| 1124 | + }; |
| 1125 | + OSSL_PARAM* params_table[] = { |
| 1126 | + params_none, |
| 1127 | + params_n, |
| 1128 | + params_e, |
| 1129 | + params_d, |
| 1130 | + params_ne, |
| 1131 | + params_nd, |
| 1132 | + params_ed, |
| 1133 | + params_ned, |
| 1134 | + params_extra_ulong, |
| 1135 | + params_extra_str, |
| 1136 | + }; |
| 1137 | + |
| 1138 | + for (unsigned i = 0; i < ARRAY_SIZE(selections); i++) { |
| 1139 | + for (unsigned j = 0; j < ARRAY_SIZE(params_table); j++) { |
| 1140 | + int status_wolf = EVP_PKEY_fromdata(ctx_wolf, &pkey_wolf, |
| 1141 | + selections[i], ¶ms_table[j][0]); |
| 1142 | + int status_ossl = EVP_PKEY_fromdata(ctx_ossl, &pkey_ossl, |
| 1143 | + selections[i], ¶ms_table[j][0]); |
| 1144 | + |
| 1145 | + if (status_wolf != status_ossl) { |
| 1146 | + PRINT_MSG("EVP_PKEY_fromdata (wolf=%d) and (ossl=%d) status " |
| 1147 | + "mismatch for selection %d (0x%08X) and params %d", |
| 1148 | + status_wolf, status_ossl, i, selections[i], j); |
| 1149 | + err = 1; |
| 1150 | + } |
| 1151 | + else if (status_wolf == 1) { |
| 1152 | + PRINT_MSG("EVP_PKEY_fromdata (wolf) succeeded for " |
| 1153 | + "selection %d (0x%08X) and params %d", |
| 1154 | + i, selections[i], j); |
| 1155 | + |
| 1156 | + if (EVP_PKEY_cmp(pkey_wolf, pkey_ossl) != 1) { |
| 1157 | + PRINT_MSG("EVP_PKEY_cmp failed for selection %d " |
| 1158 | + "(0x%08X)", i, selections[i]); |
| 1159 | + err = 1; |
| 1160 | + } |
| 1161 | + if (EVP_PKEY_cmp_parameters(pkey_wolf, pkey_ossl) != 1) { |
| 1162 | + PRINT_MSG("EVP_PKEY_cmp_parameters failed for " |
| 1163 | + "selection %d (0x%08X)", i, selections[i]); |
| 1164 | + err = 1; |
| 1165 | + } |
| 1166 | + } |
| 1167 | + |
| 1168 | + EVP_PKEY_free(pkey_wolf); |
| 1169 | + EVP_PKEY_free(pkey_ossl); |
| 1170 | + pkey_wolf = NULL; |
| 1171 | + pkey_ossl = NULL; |
| 1172 | + } |
| 1173 | + } |
| 1174 | + } |
| 1175 | + |
| 1176 | + EVP_PKEY_CTX_free(ctx_wolf); |
| 1177 | + EVP_PKEY_CTX_free(ctx_ossl); |
| 1178 | + |
| 1179 | + return err; |
| 1180 | +} |
| 1181 | + |
1028 | 1182 | #endif /* WP_HAVE_RSA */ |
0 commit comments