evp_test.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  1. /*
  2. * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/err.h>
  16. #include <openssl/x509v3.h>
  17. #include <openssl/pkcs12.h>
  18. #include <openssl/kdf.h>
  19. #include "internal/numbers.h"
  20. #include "testutil.h"
  21. #include "evp_test.h"
  22. typedef struct evp_test_method_st EVP_TEST_METHOD;
  23. /*
  24. * Structure holding test information
  25. */
  26. typedef struct evp_test_st {
  27. STANZA s; /* Common test stanza */
  28. char *name;
  29. int skip; /* Current test should be skipped */
  30. const EVP_TEST_METHOD *meth; /* method for this test */
  31. const char *err, *aux_err; /* Error string for test */
  32. char *expected_err; /* Expected error value of test */
  33. char *func; /* Expected error function string */
  34. char *reason; /* Expected error reason string */
  35. void *data; /* test specific data */
  36. } EVP_TEST;
  37. /*
  38. * Test method structure
  39. */
  40. struct evp_test_method_st {
  41. /* Name of test as it appears in file */
  42. const char *name;
  43. /* Initialise test for "alg" */
  44. int (*init) (EVP_TEST * t, const char *alg);
  45. /* Clean up method */
  46. void (*cleanup) (EVP_TEST * t);
  47. /* Test specific name value pair processing */
  48. int (*parse) (EVP_TEST * t, const char *name, const char *value);
  49. /* Run the test itself */
  50. int (*run_test) (EVP_TEST * t);
  51. };
  52. /*
  53. * Linked list of named keys.
  54. */
  55. typedef struct key_list_st {
  56. char *name;
  57. EVP_PKEY *key;
  58. struct key_list_st *next;
  59. } KEY_LIST;
  60. /*
  61. * List of public and private keys
  62. */
  63. static KEY_LIST *private_keys;
  64. static KEY_LIST *public_keys;
  65. static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst);
  66. static int parse_bin(const char *value, unsigned char **buf, size_t *buflen);
  67. /*
  68. * Compare two memory regions for equality, returning zero if they differ.
  69. * However, if there is expected to be an error and the actual error
  70. * matches then the memory is expected to be different so handle this
  71. * case without producing unnecessary test framework output.
  72. */
  73. static int memory_err_compare(EVP_TEST *t, const char *err,
  74. const void *expected, size_t expected_len,
  75. const void *got, size_t got_len)
  76. {
  77. int r;
  78. if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0)
  79. r = !TEST_mem_ne(expected, expected_len, got, got_len);
  80. else
  81. r = TEST_mem_eq(expected, expected_len, got, got_len);
  82. if (!r)
  83. t->err = err;
  84. return r;
  85. }
  86. /*
  87. * Structure used to hold a list of blocks of memory to test
  88. * calls to "update" like functions.
  89. */
  90. struct evp_test_buffer_st {
  91. unsigned char *buf;
  92. size_t buflen;
  93. size_t count;
  94. int count_set;
  95. };
  96. static void evp_test_buffer_free(EVP_TEST_BUFFER *db)
  97. {
  98. if (db != NULL) {
  99. OPENSSL_free(db->buf);
  100. OPENSSL_free(db);
  101. }
  102. }
  103. /*
  104. * append buffer to a list
  105. */
  106. static int evp_test_buffer_append(const char *value,
  107. STACK_OF(EVP_TEST_BUFFER) **sk)
  108. {
  109. EVP_TEST_BUFFER *db = NULL;
  110. if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db))))
  111. goto err;
  112. if (!parse_bin(value, &db->buf, &db->buflen))
  113. goto err;
  114. db->count = 1;
  115. db->count_set = 0;
  116. if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null()))
  117. goto err;
  118. if (!sk_EVP_TEST_BUFFER_push(*sk, db))
  119. goto err;
  120. return 1;
  121. err:
  122. evp_test_buffer_free(db);
  123. return 0;
  124. }
  125. /*
  126. * replace last buffer in list with copies of itself
  127. */
  128. static int evp_test_buffer_ncopy(const char *value,
  129. STACK_OF(EVP_TEST_BUFFER) *sk)
  130. {
  131. EVP_TEST_BUFFER *db;
  132. unsigned char *tbuf, *p;
  133. size_t tbuflen;
  134. int ncopy = atoi(value);
  135. int i;
  136. if (ncopy <= 0)
  137. return 0;
  138. if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
  139. return 0;
  140. db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
  141. tbuflen = db->buflen * ncopy;
  142. if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen)))
  143. return 0;
  144. for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen)
  145. memcpy(p, db->buf, db->buflen);
  146. OPENSSL_free(db->buf);
  147. db->buf = tbuf;
  148. db->buflen = tbuflen;
  149. return 1;
  150. }
  151. /*
  152. * set repeat count for last buffer in list
  153. */
  154. static int evp_test_buffer_set_count(const char *value,
  155. STACK_OF(EVP_TEST_BUFFER) *sk)
  156. {
  157. EVP_TEST_BUFFER *db;
  158. int count = atoi(value);
  159. if (count <= 0)
  160. return 0;
  161. if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0)
  162. return 0;
  163. db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1);
  164. if (db->count_set != 0)
  165. return 0;
  166. db->count = (size_t)count;
  167. db->count_set = 1;
  168. return 1;
  169. }
  170. /*
  171. * call "fn" with each element of the list in turn
  172. */
  173. static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk,
  174. int (*fn)(void *ctx,
  175. const unsigned char *buf,
  176. size_t buflen),
  177. void *ctx)
  178. {
  179. int i;
  180. for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) {
  181. EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i);
  182. size_t j;
  183. for (j = 0; j < tb->count; j++) {
  184. if (fn(ctx, tb->buf, tb->buflen) <= 0)
  185. return 0;
  186. }
  187. }
  188. return 1;
  189. }
  190. /*
  191. * Unescape some sequences in string literals (only \n for now).
  192. * Return an allocated buffer, set |out_len|. If |input_len|
  193. * is zero, get an empty buffer but set length to zero.
  194. */
  195. static unsigned char* unescape(const char *input, size_t input_len,
  196. size_t *out_len)
  197. {
  198. unsigned char *ret, *p;
  199. size_t i;
  200. if (input_len == 0) {
  201. *out_len = 0;
  202. return OPENSSL_zalloc(1);
  203. }
  204. /* Escaping is non-expanding; over-allocate original size for simplicity. */
  205. if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len)))
  206. return NULL;
  207. for (i = 0; i < input_len; i++) {
  208. if (*input == '\\') {
  209. if (i == input_len - 1 || *++input != 'n') {
  210. TEST_error("Bad escape sequence in file");
  211. goto err;
  212. }
  213. *p++ = '\n';
  214. i++;
  215. input++;
  216. } else {
  217. *p++ = *input++;
  218. }
  219. }
  220. *out_len = p - ret;
  221. return ret;
  222. err:
  223. OPENSSL_free(ret);
  224. return NULL;
  225. }
  226. /*
  227. * For a hex string "value" convert to a binary allocated buffer.
  228. * Return 1 on success or 0 on failure.
  229. */
  230. static int parse_bin(const char *value, unsigned char **buf, size_t *buflen)
  231. {
  232. long len;
  233. /* Check for NULL literal */
  234. if (strcmp(value, "NULL") == 0) {
  235. *buf = NULL;
  236. *buflen = 0;
  237. return 1;
  238. }
  239. /* Check for empty value */
  240. if (*value == '\0') {
  241. /*
  242. * Don't return NULL for zero length buffer. This is needed for
  243. * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
  244. * buffer even if the key length is 0, in order to detect key reset.
  245. */
  246. *buf = OPENSSL_malloc(1);
  247. if (*buf == NULL)
  248. return 0;
  249. **buf = 0;
  250. *buflen = 0;
  251. return 1;
  252. }
  253. /* Check for string literal */
  254. if (value[0] == '"') {
  255. size_t vlen = strlen(++value);
  256. if (vlen == 0 || value[vlen - 1] != '"')
  257. return 0;
  258. vlen--;
  259. *buf = unescape(value, vlen, buflen);
  260. return *buf == NULL ? 0 : 1;
  261. }
  262. /* Otherwise assume as hex literal and convert it to binary buffer */
  263. if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
  264. TEST_info("Can't convert %s", value);
  265. TEST_openssl_errors();
  266. return -1;
  267. }
  268. /* Size of input buffer means we'll never overflow */
  269. *buflen = len;
  270. return 1;
  271. }
  272. /**
  273. *** MESSAGE DIGEST TESTS
  274. **/
  275. typedef struct digest_data_st {
  276. /* Digest this test is for */
  277. const EVP_MD *digest;
  278. /* Input to digest */
  279. STACK_OF(EVP_TEST_BUFFER) *input;
  280. /* Expected output */
  281. unsigned char *output;
  282. size_t output_len;
  283. } DIGEST_DATA;
  284. static int digest_test_init(EVP_TEST *t, const char *alg)
  285. {
  286. DIGEST_DATA *mdat;
  287. const EVP_MD *digest;
  288. if ((digest = EVP_get_digestbyname(alg)) == NULL) {
  289. /* If alg has an OID assume disabled algorithm */
  290. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  291. t->skip = 1;
  292. return 1;
  293. }
  294. return 0;
  295. }
  296. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  297. return 0;
  298. t->data = mdat;
  299. mdat->digest = digest;
  300. return 1;
  301. }
  302. static void digest_test_cleanup(EVP_TEST *t)
  303. {
  304. DIGEST_DATA *mdat = t->data;
  305. sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free);
  306. OPENSSL_free(mdat->output);
  307. }
  308. static int digest_test_parse(EVP_TEST *t,
  309. const char *keyword, const char *value)
  310. {
  311. DIGEST_DATA *mdata = t->data;
  312. if (strcmp(keyword, "Input") == 0)
  313. return evp_test_buffer_append(value, &mdata->input);
  314. if (strcmp(keyword, "Output") == 0)
  315. return parse_bin(value, &mdata->output, &mdata->output_len);
  316. if (strcmp(keyword, "Count") == 0)
  317. return evp_test_buffer_set_count(value, mdata->input);
  318. if (strcmp(keyword, "Ncopy") == 0)
  319. return evp_test_buffer_ncopy(value, mdata->input);
  320. return 0;
  321. }
  322. static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen)
  323. {
  324. return EVP_DigestUpdate(ctx, buf, buflen);
  325. }
  326. static int digest_test_run(EVP_TEST *t)
  327. {
  328. DIGEST_DATA *expected = t->data;
  329. EVP_MD_CTX *mctx;
  330. unsigned char *got = NULL;
  331. unsigned int got_len;
  332. t->err = "TEST_FAILURE";
  333. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  334. goto err;
  335. got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ?
  336. expected->output_len : EVP_MAX_MD_SIZE);
  337. if (!TEST_ptr(got))
  338. goto err;
  339. if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) {
  340. t->err = "DIGESTINIT_ERROR";
  341. goto err;
  342. }
  343. if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) {
  344. t->err = "DIGESTUPDATE_ERROR";
  345. goto err;
  346. }
  347. if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) {
  348. EVP_MD_CTX *mctx_cpy;
  349. char dont[] = "touch";
  350. if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) {
  351. goto err;
  352. }
  353. if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) {
  354. EVP_MD_CTX_free(mctx_cpy);
  355. goto err;
  356. }
  357. if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) {
  358. EVP_MD_CTX_free(mctx_cpy);
  359. t->err = "DIGESTFINALXOF_ERROR";
  360. goto err;
  361. }
  362. if (!TEST_str_eq(dont, "touch")) {
  363. EVP_MD_CTX_free(mctx_cpy);
  364. t->err = "DIGESTFINALXOF_ERROR";
  365. goto err;
  366. }
  367. EVP_MD_CTX_free(mctx_cpy);
  368. got_len = expected->output_len;
  369. if (!EVP_DigestFinalXOF(mctx, got, got_len)) {
  370. t->err = "DIGESTFINALXOF_ERROR";
  371. goto err;
  372. }
  373. } else {
  374. if (!EVP_DigestFinal(mctx, got, &got_len)) {
  375. t->err = "DIGESTFINAL_ERROR";
  376. goto err;
  377. }
  378. }
  379. if (!TEST_int_eq(expected->output_len, got_len)) {
  380. t->err = "DIGEST_LENGTH_MISMATCH";
  381. goto err;
  382. }
  383. if (!memory_err_compare(t, "DIGEST_MISMATCH",
  384. expected->output, expected->output_len,
  385. got, got_len))
  386. goto err;
  387. t->err = NULL;
  388. err:
  389. OPENSSL_free(got);
  390. EVP_MD_CTX_free(mctx);
  391. return 1;
  392. }
  393. static const EVP_TEST_METHOD digest_test_method = {
  394. "Digest",
  395. digest_test_init,
  396. digest_test_cleanup,
  397. digest_test_parse,
  398. digest_test_run
  399. };
  400. /**
  401. *** CIPHER TESTS
  402. **/
  403. typedef struct cipher_data_st {
  404. const EVP_CIPHER *cipher;
  405. int enc;
  406. /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
  407. int aead;
  408. unsigned char *key;
  409. size_t key_len;
  410. unsigned char *iv;
  411. size_t iv_len;
  412. unsigned char *plaintext;
  413. size_t plaintext_len;
  414. unsigned char *ciphertext;
  415. size_t ciphertext_len;
  416. /* GCM, CCM and OCB only */
  417. unsigned char *aad;
  418. size_t aad_len;
  419. unsigned char *tag;
  420. size_t tag_len;
  421. int tag_late;
  422. } CIPHER_DATA;
  423. static int cipher_test_init(EVP_TEST *t, const char *alg)
  424. {
  425. const EVP_CIPHER *cipher;
  426. CIPHER_DATA *cdat;
  427. int m;
  428. if ((cipher = EVP_get_cipherbyname(alg)) == NULL) {
  429. /* If alg has an OID assume disabled algorithm */
  430. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  431. t->skip = 1;
  432. return 1;
  433. }
  434. return 0;
  435. }
  436. cdat = OPENSSL_zalloc(sizeof(*cdat));
  437. cdat->cipher = cipher;
  438. cdat->enc = -1;
  439. m = EVP_CIPHER_mode(cipher);
  440. if (m == EVP_CIPH_GCM_MODE
  441. || m == EVP_CIPH_OCB_MODE
  442. || m == EVP_CIPH_CCM_MODE)
  443. cdat->aead = m;
  444. else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
  445. cdat->aead = -1;
  446. else
  447. cdat->aead = 0;
  448. t->data = cdat;
  449. return 1;
  450. }
  451. static void cipher_test_cleanup(EVP_TEST *t)
  452. {
  453. CIPHER_DATA *cdat = t->data;
  454. OPENSSL_free(cdat->key);
  455. OPENSSL_free(cdat->iv);
  456. OPENSSL_free(cdat->ciphertext);
  457. OPENSSL_free(cdat->plaintext);
  458. OPENSSL_free(cdat->aad);
  459. OPENSSL_free(cdat->tag);
  460. }
  461. static int cipher_test_parse(EVP_TEST *t, const char *keyword,
  462. const char *value)
  463. {
  464. CIPHER_DATA *cdat = t->data;
  465. if (strcmp(keyword, "Key") == 0)
  466. return parse_bin(value, &cdat->key, &cdat->key_len);
  467. if (strcmp(keyword, "IV") == 0)
  468. return parse_bin(value, &cdat->iv, &cdat->iv_len);
  469. if (strcmp(keyword, "Plaintext") == 0)
  470. return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len);
  471. if (strcmp(keyword, "Ciphertext") == 0)
  472. return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
  473. if (cdat->aead) {
  474. if (strcmp(keyword, "AAD") == 0)
  475. return parse_bin(value, &cdat->aad, &cdat->aad_len);
  476. if (strcmp(keyword, "Tag") == 0)
  477. return parse_bin(value, &cdat->tag, &cdat->tag_len);
  478. if (strcmp(keyword, "SetTagLate") == 0) {
  479. if (strcmp(value, "TRUE") == 0)
  480. cdat->tag_late = 1;
  481. else if (strcmp(value, "FALSE") == 0)
  482. cdat->tag_late = 0;
  483. else
  484. return -1;
  485. return 1;
  486. }
  487. }
  488. if (strcmp(keyword, "Operation") == 0) {
  489. if (strcmp(value, "ENCRYPT") == 0)
  490. cdat->enc = 1;
  491. else if (strcmp(value, "DECRYPT") == 0)
  492. cdat->enc = 0;
  493. else
  494. return -1;
  495. return 1;
  496. }
  497. return 0;
  498. }
  499. static int cipher_test_enc(EVP_TEST *t, int enc,
  500. size_t out_misalign, size_t inp_misalign, int frag)
  501. {
  502. CIPHER_DATA *expected = t->data;
  503. unsigned char *in, *expected_out, *tmp = NULL;
  504. size_t in_len, out_len, donelen = 0;
  505. int ok = 0, tmplen, chunklen, tmpflen;
  506. EVP_CIPHER_CTX *ctx = NULL;
  507. t->err = "TEST_FAILURE";
  508. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  509. goto err;
  510. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  511. if (enc) {
  512. in = expected->plaintext;
  513. in_len = expected->plaintext_len;
  514. expected_out = expected->ciphertext;
  515. out_len = expected->ciphertext_len;
  516. } else {
  517. in = expected->ciphertext;
  518. in_len = expected->ciphertext_len;
  519. expected_out = expected->plaintext;
  520. out_len = expected->plaintext_len;
  521. }
  522. if (inp_misalign == (size_t)-1) {
  523. /*
  524. * Exercise in-place encryption
  525. */
  526. tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
  527. if (!tmp)
  528. goto err;
  529. in = memcpy(tmp + out_misalign, in, in_len);
  530. } else {
  531. inp_misalign += 16 - ((out_misalign + in_len) & 15);
  532. /*
  533. * 'tmp' will store both output and copy of input. We make the copy
  534. * of input to specifically aligned part of 'tmp'. So we just
  535. * figured out how much padding would ensure the required alignment,
  536. * now we allocate extended buffer and finally copy the input just
  537. * past inp_misalign in expression below. Output will be written
  538. * past out_misalign...
  539. */
  540. tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
  541. inp_misalign + in_len);
  542. if (!tmp)
  543. goto err;
  544. in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
  545. inp_misalign, in, in_len);
  546. }
  547. if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) {
  548. t->err = "CIPHERINIT_ERROR";
  549. goto err;
  550. }
  551. if (expected->iv) {
  552. if (expected->aead) {
  553. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  554. expected->iv_len, 0)) {
  555. t->err = "INVALID_IV_LENGTH";
  556. goto err;
  557. }
  558. } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
  559. t->err = "INVALID_IV_LENGTH";
  560. goto err;
  561. }
  562. }
  563. if (expected->aead) {
  564. unsigned char *tag;
  565. /*
  566. * If encrypting or OCB just set tag length initially, otherwise
  567. * set tag length and value.
  568. */
  569. if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) {
  570. t->err = "TAG_LENGTH_SET_ERROR";
  571. tag = NULL;
  572. } else {
  573. t->err = "TAG_SET_ERROR";
  574. tag = expected->tag;
  575. }
  576. if (tag || expected->aead != EVP_CIPH_GCM_MODE) {
  577. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  578. expected->tag_len, tag))
  579. goto err;
  580. }
  581. }
  582. if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) {
  583. t->err = "INVALID_KEY_LENGTH";
  584. goto err;
  585. }
  586. if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) {
  587. t->err = "KEY_SET_ERROR";
  588. goto err;
  589. }
  590. if (expected->aead == EVP_CIPH_CCM_MODE) {
  591. if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
  592. t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
  593. goto err;
  594. }
  595. }
  596. if (expected->aad) {
  597. t->err = "AAD_SET_ERROR";
  598. if (!frag) {
  599. if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad,
  600. expected->aad_len))
  601. goto err;
  602. } else {
  603. /*
  604. * Supply the AAD in chunks less than the block size where possible
  605. */
  606. if (expected->aad_len > 0) {
  607. if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, 1))
  608. goto err;
  609. donelen++;
  610. }
  611. if (expected->aad_len > 2) {
  612. if (!EVP_CipherUpdate(ctx, NULL, &chunklen,
  613. expected->aad + donelen,
  614. expected->aad_len - 2))
  615. goto err;
  616. donelen += expected->aad_len - 2;
  617. }
  618. if (expected->aad_len > 1
  619. && !EVP_CipherUpdate(ctx, NULL, &chunklen,
  620. expected->aad + donelen, 1))
  621. goto err;
  622. }
  623. }
  624. if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) {
  625. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  626. expected->tag_len, expected->tag)) {
  627. t->err = "TAG_SET_ERROR";
  628. goto err;
  629. }
  630. }
  631. EVP_CIPHER_CTX_set_padding(ctx, 0);
  632. t->err = "CIPHERUPDATE_ERROR";
  633. tmplen = 0;
  634. if (!frag) {
  635. /* We supply the data all in one go */
  636. if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
  637. goto err;
  638. } else {
  639. /* Supply the data in chunks less than the block size where possible */
  640. if (in_len > 0) {
  641. if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
  642. goto err;
  643. tmplen += chunklen;
  644. in++;
  645. in_len--;
  646. }
  647. if (in_len > 1) {
  648. if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
  649. in, in_len - 1))
  650. goto err;
  651. tmplen += chunklen;
  652. in += in_len - 1;
  653. in_len = 1;
  654. }
  655. if (in_len > 0 ) {
  656. if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
  657. in, 1))
  658. goto err;
  659. tmplen += chunklen;
  660. }
  661. }
  662. if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
  663. t->err = "CIPHERFINAL_ERROR";
  664. goto err;
  665. }
  666. if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len,
  667. tmp + out_misalign, tmplen + tmpflen))
  668. goto err;
  669. if (enc && expected->aead) {
  670. unsigned char rtag[16];
  671. if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) {
  672. t->err = "TAG_LENGTH_INTERNAL_ERROR";
  673. goto err;
  674. }
  675. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  676. expected->tag_len, rtag)) {
  677. t->err = "TAG_RETRIEVE_ERROR";
  678. goto err;
  679. }
  680. if (!memory_err_compare(t, "TAG_VALUE_MISMATCH",
  681. expected->tag, expected->tag_len,
  682. rtag, expected->tag_len))
  683. goto err;
  684. }
  685. t->err = NULL;
  686. ok = 1;
  687. err:
  688. OPENSSL_free(tmp);
  689. EVP_CIPHER_CTX_free(ctx);
  690. return ok;
  691. }
  692. static int cipher_test_run(EVP_TEST *t)
  693. {
  694. CIPHER_DATA *cdat = t->data;
  695. int rv, frag = 0;
  696. size_t out_misalign, inp_misalign;
  697. if (!cdat->key) {
  698. t->err = "NO_KEY";
  699. return 0;
  700. }
  701. if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
  702. /* IV is optional and usually omitted in wrap mode */
  703. if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
  704. t->err = "NO_IV";
  705. return 0;
  706. }
  707. }
  708. if (cdat->aead && !cdat->tag) {
  709. t->err = "NO_TAG";
  710. return 0;
  711. }
  712. for (out_misalign = 0; out_misalign <= 1;) {
  713. static char aux_err[64];
  714. t->aux_err = aux_err;
  715. for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
  716. if (inp_misalign == (size_t)-1) {
  717. /* kludge: inp_misalign == -1 means "exercise in-place" */
  718. BIO_snprintf(aux_err, sizeof(aux_err),
  719. "%s in-place, %sfragmented",
  720. out_misalign ? "misaligned" : "aligned",
  721. frag ? "" : "not ");
  722. } else {
  723. BIO_snprintf(aux_err, sizeof(aux_err),
  724. "%s output and %s input, %sfragmented",
  725. out_misalign ? "misaligned" : "aligned",
  726. inp_misalign ? "misaligned" : "aligned",
  727. frag ? "" : "not ");
  728. }
  729. if (cdat->enc) {
  730. rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
  731. /* Not fatal errors: return */
  732. if (rv != 1) {
  733. if (rv < 0)
  734. return 0;
  735. return 1;
  736. }
  737. }
  738. if (cdat->enc != 1) {
  739. rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
  740. /* Not fatal errors: return */
  741. if (rv != 1) {
  742. if (rv < 0)
  743. return 0;
  744. return 1;
  745. }
  746. }
  747. }
  748. if (out_misalign == 1 && frag == 0) {
  749. /*
  750. * XTS, CCM and Wrap modes have special requirements about input
  751. * lengths so we don't fragment for those
  752. */
  753. if (cdat->aead == EVP_CIPH_CCM_MODE
  754. || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
  755. || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
  756. break;
  757. out_misalign = 0;
  758. frag++;
  759. } else {
  760. out_misalign++;
  761. }
  762. }
  763. t->aux_err = NULL;
  764. return 1;
  765. }
  766. static const EVP_TEST_METHOD cipher_test_method = {
  767. "Cipher",
  768. cipher_test_init,
  769. cipher_test_cleanup,
  770. cipher_test_parse,
  771. cipher_test_run
  772. };
  773. /**
  774. *** MAC TESTS
  775. **/
  776. typedef struct mac_data_st {
  777. /* MAC type */
  778. int type;
  779. /* Algorithm string for this MAC */
  780. char *alg;
  781. /* MAC key */
  782. unsigned char *key;
  783. size_t key_len;
  784. /* Input to MAC */
  785. unsigned char *input;
  786. size_t input_len;
  787. /* Expected output */
  788. unsigned char *output;
  789. size_t output_len;
  790. /* Collection of controls */
  791. STACK_OF(OPENSSL_STRING) *controls;
  792. } MAC_DATA;
  793. static int mac_test_init(EVP_TEST *t, const char *alg)
  794. {
  795. int type;
  796. MAC_DATA *mdat;
  797. if (strcmp(alg, "HMAC") == 0) {
  798. type = EVP_PKEY_HMAC;
  799. } else if (strcmp(alg, "CMAC") == 0) {
  800. #ifndef OPENSSL_NO_CMAC
  801. type = EVP_PKEY_CMAC;
  802. #else
  803. t->skip = 1;
  804. return 1;
  805. #endif
  806. } else if (strcmp(alg, "Poly1305") == 0) {
  807. #ifndef OPENSSL_NO_POLY1305
  808. type = EVP_PKEY_POLY1305;
  809. #else
  810. t->skip = 1;
  811. return 1;
  812. #endif
  813. } else if (strcmp(alg, "SipHash") == 0) {
  814. #ifndef OPENSSL_NO_SIPHASH
  815. type = EVP_PKEY_SIPHASH;
  816. #else
  817. t->skip = 1;
  818. return 1;
  819. #endif
  820. } else
  821. return 0;
  822. mdat = OPENSSL_zalloc(sizeof(*mdat));
  823. mdat->type = type;
  824. mdat->controls = sk_OPENSSL_STRING_new_null();
  825. t->data = mdat;
  826. return 1;
  827. }
  828. /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
  829. static void openssl_free(char *m)
  830. {
  831. OPENSSL_free(m);
  832. }
  833. static void mac_test_cleanup(EVP_TEST *t)
  834. {
  835. MAC_DATA *mdat = t->data;
  836. sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free);
  837. OPENSSL_free(mdat->alg);
  838. OPENSSL_free(mdat->key);
  839. OPENSSL_free(mdat->input);
  840. OPENSSL_free(mdat->output);
  841. }
  842. static int mac_test_parse(EVP_TEST *t,
  843. const char *keyword, const char *value)
  844. {
  845. MAC_DATA *mdata = t->data;
  846. if (strcmp(keyword, "Key") == 0)
  847. return parse_bin(value, &mdata->key, &mdata->key_len);
  848. if (strcmp(keyword, "Algorithm") == 0) {
  849. mdata->alg = OPENSSL_strdup(value);
  850. if (!mdata->alg)
  851. return -1;
  852. return 1;
  853. }
  854. if (strcmp(keyword, "Input") == 0)
  855. return parse_bin(value, &mdata->input, &mdata->input_len);
  856. if (strcmp(keyword, "Output") == 0)
  857. return parse_bin(value, &mdata->output, &mdata->output_len);
  858. if (strcmp(keyword, "Ctrl") == 0)
  859. return sk_OPENSSL_STRING_push(mdata->controls,
  860. OPENSSL_strdup(value)) != 0;
  861. return 0;
  862. }
  863. static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx,
  864. const char *value)
  865. {
  866. int rv;
  867. char *p, *tmpval;
  868. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  869. return 0;
  870. p = strchr(tmpval, ':');
  871. if (p != NULL)
  872. *p++ = '\0';
  873. rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
  874. if (rv == -2)
  875. t->err = "PKEY_CTRL_INVALID";
  876. else if (rv <= 0)
  877. t->err = "PKEY_CTRL_ERROR";
  878. else
  879. rv = 1;
  880. OPENSSL_free(tmpval);
  881. return rv > 0;
  882. }
  883. static int mac_test_run(EVP_TEST *t)
  884. {
  885. MAC_DATA *expected = t->data;
  886. EVP_MD_CTX *mctx = NULL;
  887. EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
  888. EVP_PKEY *key = NULL;
  889. const EVP_MD *md = NULL;
  890. unsigned char *got = NULL;
  891. size_t got_len;
  892. int i;
  893. #ifdef OPENSSL_NO_DES
  894. if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) {
  895. /* Skip DES */
  896. t->err = NULL;
  897. goto err;
  898. }
  899. #endif
  900. if (expected->type == EVP_PKEY_CMAC)
  901. key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len,
  902. EVP_get_cipherbyname(expected->alg));
  903. else
  904. key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key,
  905. expected->key_len);
  906. if (key == NULL) {
  907. t->err = "MAC_KEY_CREATE_ERROR";
  908. goto err;
  909. }
  910. if (expected->type == EVP_PKEY_HMAC) {
  911. if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) {
  912. t->err = "MAC_ALGORITHM_SET_ERROR";
  913. goto err;
  914. }
  915. }
  916. if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
  917. t->err = "INTERNAL_ERROR";
  918. goto err;
  919. }
  920. if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
  921. t->err = "DIGESTSIGNINIT_ERROR";
  922. goto err;
  923. }
  924. for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++)
  925. if (!mac_test_ctrl_pkey(t, pctx,
  926. sk_OPENSSL_STRING_value(expected->controls,
  927. i))) {
  928. t->err = "EVPPKEYCTXCTRL_ERROR";
  929. goto err;
  930. }
  931. if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) {
  932. t->err = "DIGESTSIGNUPDATE_ERROR";
  933. goto err;
  934. }
  935. if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) {
  936. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  937. goto err;
  938. }
  939. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  940. t->err = "TEST_FAILURE";
  941. goto err;
  942. }
  943. if (!EVP_DigestSignFinal(mctx, got, &got_len)
  944. || !memory_err_compare(t, "TEST_MAC_ERR",
  945. expected->output, expected->output_len,
  946. got, got_len)) {
  947. t->err = "TEST_MAC_ERR";
  948. goto err;
  949. }
  950. t->err = NULL;
  951. err:
  952. EVP_MD_CTX_free(mctx);
  953. OPENSSL_free(got);
  954. EVP_PKEY_CTX_free(genctx);
  955. EVP_PKEY_free(key);
  956. return 1;
  957. }
  958. static const EVP_TEST_METHOD mac_test_method = {
  959. "MAC",
  960. mac_test_init,
  961. mac_test_cleanup,
  962. mac_test_parse,
  963. mac_test_run
  964. };
  965. /**
  966. *** PUBLIC KEY TESTS
  967. *** These are all very similar and share much common code.
  968. **/
  969. typedef struct pkey_data_st {
  970. /* Context for this operation */
  971. EVP_PKEY_CTX *ctx;
  972. /* Key operation to perform */
  973. int (*keyop) (EVP_PKEY_CTX *ctx,
  974. unsigned char *sig, size_t *siglen,
  975. const unsigned char *tbs, size_t tbslen);
  976. /* Input to MAC */
  977. unsigned char *input;
  978. size_t input_len;
  979. /* Expected output */
  980. unsigned char *output;
  981. size_t output_len;
  982. } PKEY_DATA;
  983. /*
  984. * Perform public key operation setup: lookup key, allocated ctx and call
  985. * the appropriate initialisation function
  986. */
  987. static int pkey_test_init(EVP_TEST *t, const char *name,
  988. int use_public,
  989. int (*keyopinit) (EVP_PKEY_CTX *ctx),
  990. int (*keyop)(EVP_PKEY_CTX *ctx,
  991. unsigned char *sig, size_t *siglen,
  992. const unsigned char *tbs,
  993. size_t tbslen))
  994. {
  995. PKEY_DATA *kdata;
  996. EVP_PKEY *pkey = NULL;
  997. int rv = 0;
  998. if (use_public)
  999. rv = find_key(&pkey, name, public_keys);
  1000. if (rv == 0)
  1001. rv = find_key(&pkey, name, private_keys);
  1002. if (rv == 0 || pkey == NULL) {
  1003. t->skip = 1;
  1004. return 1;
  1005. }
  1006. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) {
  1007. EVP_PKEY_free(pkey);
  1008. return 0;
  1009. }
  1010. kdata->keyop = keyop;
  1011. if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
  1012. EVP_PKEY_free(pkey);
  1013. OPENSSL_free(kdata);
  1014. return 0;
  1015. }
  1016. if (keyopinit(kdata->ctx) <= 0)
  1017. t->err = "KEYOP_INIT_ERROR";
  1018. t->data = kdata;
  1019. return 1;
  1020. }
  1021. static void pkey_test_cleanup(EVP_TEST *t)
  1022. {
  1023. PKEY_DATA *kdata = t->data;
  1024. OPENSSL_free(kdata->input);
  1025. OPENSSL_free(kdata->output);
  1026. EVP_PKEY_CTX_free(kdata->ctx);
  1027. }
  1028. static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
  1029. const char *value)
  1030. {
  1031. int rv;
  1032. char *p, *tmpval;
  1033. if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
  1034. return 0;
  1035. p = strchr(tmpval, ':');
  1036. if (p != NULL)
  1037. *p++ = '\0';
  1038. rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
  1039. if (rv == -2) {
  1040. t->err = "PKEY_CTRL_INVALID";
  1041. rv = 1;
  1042. } else if (p != NULL && rv <= 0) {
  1043. /* If p has an OID and lookup fails assume disabled algorithm */
  1044. int nid = OBJ_sn2nid(p);
  1045. if (nid == NID_undef)
  1046. nid = OBJ_ln2nid(p);
  1047. if (nid != NID_undef
  1048. && EVP_get_digestbynid(nid) == NULL
  1049. && EVP_get_cipherbynid(nid) == NULL) {
  1050. t->skip = 1;
  1051. rv = 1;
  1052. } else {
  1053. t->err = "PKEY_CTRL_ERROR";
  1054. rv = 1;
  1055. }
  1056. }
  1057. OPENSSL_free(tmpval);
  1058. return rv > 0;
  1059. }
  1060. static int pkey_test_parse(EVP_TEST *t,
  1061. const char *keyword, const char *value)
  1062. {
  1063. PKEY_DATA *kdata = t->data;
  1064. if (strcmp(keyword, "Input") == 0)
  1065. return parse_bin(value, &kdata->input, &kdata->input_len);
  1066. if (strcmp(keyword, "Output") == 0)
  1067. return parse_bin(value, &kdata->output, &kdata->output_len);
  1068. if (strcmp(keyword, "Ctrl") == 0)
  1069. return pkey_test_ctrl(t, kdata->ctx, value);
  1070. return 0;
  1071. }
  1072. static int pkey_test_run(EVP_TEST *t)
  1073. {
  1074. PKEY_DATA *expected = t->data;
  1075. unsigned char *got = NULL;
  1076. size_t got_len;
  1077. if (expected->keyop(expected->ctx, NULL, &got_len,
  1078. expected->input, expected->input_len) <= 0
  1079. || !TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1080. t->err = "KEYOP_LENGTH_ERROR";
  1081. goto err;
  1082. }
  1083. if (expected->keyop(expected->ctx, got, &got_len,
  1084. expected->input, expected->input_len) <= 0) {
  1085. t->err = "KEYOP_ERROR";
  1086. goto err;
  1087. }
  1088. if (!memory_err_compare(t, "KEYOP_MISMATCH",
  1089. expected->output, expected->output_len,
  1090. got, got_len))
  1091. goto err;
  1092. t->err = NULL;
  1093. err:
  1094. OPENSSL_free(got);
  1095. return 1;
  1096. }
  1097. static int sign_test_init(EVP_TEST *t, const char *name)
  1098. {
  1099. return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
  1100. }
  1101. static const EVP_TEST_METHOD psign_test_method = {
  1102. "Sign",
  1103. sign_test_init,
  1104. pkey_test_cleanup,
  1105. pkey_test_parse,
  1106. pkey_test_run
  1107. };
  1108. static int verify_recover_test_init(EVP_TEST *t, const char *name)
  1109. {
  1110. return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
  1111. EVP_PKEY_verify_recover);
  1112. }
  1113. static const EVP_TEST_METHOD pverify_recover_test_method = {
  1114. "VerifyRecover",
  1115. verify_recover_test_init,
  1116. pkey_test_cleanup,
  1117. pkey_test_parse,
  1118. pkey_test_run
  1119. };
  1120. static int decrypt_test_init(EVP_TEST *t, const char *name)
  1121. {
  1122. return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
  1123. EVP_PKEY_decrypt);
  1124. }
  1125. static const EVP_TEST_METHOD pdecrypt_test_method = {
  1126. "Decrypt",
  1127. decrypt_test_init,
  1128. pkey_test_cleanup,
  1129. pkey_test_parse,
  1130. pkey_test_run
  1131. };
  1132. static int verify_test_init(EVP_TEST *t, const char *name)
  1133. {
  1134. return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
  1135. }
  1136. static int verify_test_run(EVP_TEST *t)
  1137. {
  1138. PKEY_DATA *kdata = t->data;
  1139. if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
  1140. kdata->input, kdata->input_len) <= 0)
  1141. t->err = "VERIFY_ERROR";
  1142. return 1;
  1143. }
  1144. static const EVP_TEST_METHOD pverify_test_method = {
  1145. "Verify",
  1146. verify_test_init,
  1147. pkey_test_cleanup,
  1148. pkey_test_parse,
  1149. verify_test_run
  1150. };
  1151. static int pderive_test_init(EVP_TEST *t, const char *name)
  1152. {
  1153. return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
  1154. }
  1155. static int pderive_test_parse(EVP_TEST *t,
  1156. const char *keyword, const char *value)
  1157. {
  1158. PKEY_DATA *kdata = t->data;
  1159. if (strcmp(keyword, "PeerKey") == 0) {
  1160. EVP_PKEY *peer;
  1161. if (find_key(&peer, value, public_keys) == 0)
  1162. return -1;
  1163. if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
  1164. return -1;
  1165. return 1;
  1166. }
  1167. if (strcmp(keyword, "SharedSecret") == 0)
  1168. return parse_bin(value, &kdata->output, &kdata->output_len);
  1169. if (strcmp(keyword, "Ctrl") == 0)
  1170. return pkey_test_ctrl(t, kdata->ctx, value);
  1171. return 0;
  1172. }
  1173. static int pderive_test_run(EVP_TEST *t)
  1174. {
  1175. PKEY_DATA *expected = t->data;
  1176. unsigned char *got = NULL;
  1177. size_t got_len;
  1178. if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) {
  1179. t->err = "DERIVE_ERROR";
  1180. goto err;
  1181. }
  1182. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1183. t->err = "DERIVE_ERROR";
  1184. goto err;
  1185. }
  1186. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  1187. t->err = "DERIVE_ERROR";
  1188. goto err;
  1189. }
  1190. if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH",
  1191. expected->output, expected->output_len,
  1192. got, got_len))
  1193. goto err;
  1194. t->err = NULL;
  1195. err:
  1196. OPENSSL_free(got);
  1197. return 1;
  1198. }
  1199. static const EVP_TEST_METHOD pderive_test_method = {
  1200. "Derive",
  1201. pderive_test_init,
  1202. pkey_test_cleanup,
  1203. pderive_test_parse,
  1204. pderive_test_run
  1205. };
  1206. /**
  1207. *** PBE TESTS
  1208. **/
  1209. typedef enum pbe_type_enum {
  1210. PBE_TYPE_INVALID = 0,
  1211. PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12
  1212. } PBE_TYPE;
  1213. typedef struct pbe_data_st {
  1214. PBE_TYPE pbe_type;
  1215. /* scrypt parameters */
  1216. uint64_t N, r, p, maxmem;
  1217. /* PKCS#12 parameters */
  1218. int id, iter;
  1219. const EVP_MD *md;
  1220. /* password */
  1221. unsigned char *pass;
  1222. size_t pass_len;
  1223. /* salt */
  1224. unsigned char *salt;
  1225. size_t salt_len;
  1226. /* Expected output */
  1227. unsigned char *key;
  1228. size_t key_len;
  1229. } PBE_DATA;
  1230. #ifndef OPENSSL_NO_SCRYPT
  1231. /*
  1232. * Parse unsigned decimal 64 bit integer value
  1233. */
  1234. static int parse_uint64(const char *value, uint64_t *pr)
  1235. {
  1236. const char *p = value;
  1237. if (!TEST_true(*p)) {
  1238. TEST_info("Invalid empty integer value");
  1239. return -1;
  1240. }
  1241. for (*pr = 0; *p; ) {
  1242. if (*pr > UINT64_MAX / 10) {
  1243. TEST_error("Integer overflow in string %s", value);
  1244. return -1;
  1245. }
  1246. *pr *= 10;
  1247. if (!TEST_true(isdigit((unsigned char)*p))) {
  1248. TEST_error("Invalid character in string %s", value);
  1249. return -1;
  1250. }
  1251. *pr += *p - '0';
  1252. p++;
  1253. }
  1254. return 1;
  1255. }
  1256. static int scrypt_test_parse(EVP_TEST *t,
  1257. const char *keyword, const char *value)
  1258. {
  1259. PBE_DATA *pdata = t->data;
  1260. if (strcmp(keyword, "N") == 0)
  1261. return parse_uint64(value, &pdata->N);
  1262. if (strcmp(keyword, "p") == 0)
  1263. return parse_uint64(value, &pdata->p);
  1264. if (strcmp(keyword, "r") == 0)
  1265. return parse_uint64(value, &pdata->r);
  1266. if (strcmp(keyword, "maxmem") == 0)
  1267. return parse_uint64(value, &pdata->maxmem);
  1268. return 0;
  1269. }
  1270. #endif
  1271. static int pbkdf2_test_parse(EVP_TEST *t,
  1272. const char *keyword, const char *value)
  1273. {
  1274. PBE_DATA *pdata = t->data;
  1275. if (strcmp(keyword, "iter") == 0) {
  1276. pdata->iter = atoi(value);
  1277. if (pdata->iter <= 0)
  1278. return -1;
  1279. return 1;
  1280. }
  1281. if (strcmp(keyword, "MD") == 0) {
  1282. pdata->md = EVP_get_digestbyname(value);
  1283. if (pdata->md == NULL)
  1284. return -1;
  1285. return 1;
  1286. }
  1287. return 0;
  1288. }
  1289. static int pkcs12_test_parse(EVP_TEST *t,
  1290. const char *keyword, const char *value)
  1291. {
  1292. PBE_DATA *pdata = t->data;
  1293. if (strcmp(keyword, "id") == 0) {
  1294. pdata->id = atoi(value);
  1295. if (pdata->id <= 0)
  1296. return -1;
  1297. return 1;
  1298. }
  1299. return pbkdf2_test_parse(t, keyword, value);
  1300. }
  1301. static int pbe_test_init(EVP_TEST *t, const char *alg)
  1302. {
  1303. PBE_DATA *pdat;
  1304. PBE_TYPE pbe_type = PBE_TYPE_INVALID;
  1305. if (strcmp(alg, "scrypt") == 0) {
  1306. #ifndef OPENSSL_NO_SCRYPT
  1307. pbe_type = PBE_TYPE_SCRYPT;
  1308. #else
  1309. t->skip = 1;
  1310. return 1;
  1311. #endif
  1312. } else if (strcmp(alg, "pbkdf2") == 0) {
  1313. pbe_type = PBE_TYPE_PBKDF2;
  1314. } else if (strcmp(alg, "pkcs12") == 0) {
  1315. pbe_type = PBE_TYPE_PKCS12;
  1316. } else {
  1317. TEST_error("Unknown pbe algorithm %s", alg);
  1318. }
  1319. pdat = OPENSSL_zalloc(sizeof(*pdat));
  1320. pdat->pbe_type = pbe_type;
  1321. t->data = pdat;
  1322. return 1;
  1323. }
  1324. static void pbe_test_cleanup(EVP_TEST *t)
  1325. {
  1326. PBE_DATA *pdat = t->data;
  1327. OPENSSL_free(pdat->pass);
  1328. OPENSSL_free(pdat->salt);
  1329. OPENSSL_free(pdat->key);
  1330. }
  1331. static int pbe_test_parse(EVP_TEST *t,
  1332. const char *keyword, const char *value)
  1333. {
  1334. PBE_DATA *pdata = t->data;
  1335. if (strcmp(keyword, "Password") == 0)
  1336. return parse_bin(value, &pdata->pass, &pdata->pass_len);
  1337. if (strcmp(keyword, "Salt") == 0)
  1338. return parse_bin(value, &pdata->salt, &pdata->salt_len);
  1339. if (strcmp(keyword, "Key") == 0)
  1340. return parse_bin(value, &pdata->key, &pdata->key_len);
  1341. if (pdata->pbe_type == PBE_TYPE_PBKDF2)
  1342. return pbkdf2_test_parse(t, keyword, value);
  1343. else if (pdata->pbe_type == PBE_TYPE_PKCS12)
  1344. return pkcs12_test_parse(t, keyword, value);
  1345. #ifndef OPENSSL_NO_SCRYPT
  1346. else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
  1347. return scrypt_test_parse(t, keyword, value);
  1348. #endif
  1349. return 0;
  1350. }
  1351. static int pbe_test_run(EVP_TEST *t)
  1352. {
  1353. PBE_DATA *expected = t->data;
  1354. unsigned char *key;
  1355. if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) {
  1356. t->err = "INTERNAL_ERROR";
  1357. goto err;
  1358. }
  1359. if (expected->pbe_type == PBE_TYPE_PBKDF2) {
  1360. if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len,
  1361. expected->salt, expected->salt_len,
  1362. expected->iter, expected->md,
  1363. expected->key_len, key) == 0) {
  1364. t->err = "PBKDF2_ERROR";
  1365. goto err;
  1366. }
  1367. #ifndef OPENSSL_NO_SCRYPT
  1368. } else if (expected->pbe_type == PBE_TYPE_SCRYPT) {
  1369. if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len,
  1370. expected->salt, expected->salt_len, expected->N,
  1371. expected->r, expected->p, expected->maxmem,
  1372. key, expected->key_len) == 0) {
  1373. t->err = "SCRYPT_ERROR";
  1374. goto err;
  1375. }
  1376. #endif
  1377. } else if (expected->pbe_type == PBE_TYPE_PKCS12) {
  1378. if (PKCS12_key_gen_uni(expected->pass, expected->pass_len,
  1379. expected->salt, expected->salt_len,
  1380. expected->id, expected->iter, expected->key_len,
  1381. key, expected->md) == 0) {
  1382. t->err = "PKCS12_ERROR";
  1383. goto err;
  1384. }
  1385. }
  1386. if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len,
  1387. key, expected->key_len))
  1388. goto err;
  1389. t->err = NULL;
  1390. err:
  1391. OPENSSL_free(key);
  1392. return 1;
  1393. }
  1394. static const EVP_TEST_METHOD pbe_test_method = {
  1395. "PBE",
  1396. pbe_test_init,
  1397. pbe_test_cleanup,
  1398. pbe_test_parse,
  1399. pbe_test_run
  1400. };
  1401. /**
  1402. *** BASE64 TESTS
  1403. **/
  1404. typedef enum {
  1405. BASE64_CANONICAL_ENCODING = 0,
  1406. BASE64_VALID_ENCODING = 1,
  1407. BASE64_INVALID_ENCODING = 2
  1408. } base64_encoding_type;
  1409. typedef struct encode_data_st {
  1410. /* Input to encoding */
  1411. unsigned char *input;
  1412. size_t input_len;
  1413. /* Expected output */
  1414. unsigned char *output;
  1415. size_t output_len;
  1416. base64_encoding_type encoding;
  1417. } ENCODE_DATA;
  1418. static int encode_test_init(EVP_TEST *t, const char *encoding)
  1419. {
  1420. ENCODE_DATA *edata;
  1421. if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))
  1422. return 0;
  1423. if (strcmp(encoding, "canonical") == 0) {
  1424. edata->encoding = BASE64_CANONICAL_ENCODING;
  1425. } else if (strcmp(encoding, "valid") == 0) {
  1426. edata->encoding = BASE64_VALID_ENCODING;
  1427. } else if (strcmp(encoding, "invalid") == 0) {
  1428. edata->encoding = BASE64_INVALID_ENCODING;
  1429. if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))
  1430. goto err;
  1431. } else {
  1432. TEST_error("Bad encoding: %s."
  1433. " Should be one of {canonical, valid, invalid}",
  1434. encoding);
  1435. goto err;
  1436. }
  1437. t->data = edata;
  1438. return 1;
  1439. err:
  1440. OPENSSL_free(edata);
  1441. return 0;
  1442. }
  1443. static void encode_test_cleanup(EVP_TEST *t)
  1444. {
  1445. ENCODE_DATA *edata = t->data;
  1446. OPENSSL_free(edata->input);
  1447. OPENSSL_free(edata->output);
  1448. memset(edata, 0, sizeof(*edata));
  1449. }
  1450. static int encode_test_parse(EVP_TEST *t,
  1451. const char *keyword, const char *value)
  1452. {
  1453. ENCODE_DATA *edata = t->data;
  1454. if (strcmp(keyword, "Input") == 0)
  1455. return parse_bin(value, &edata->input, &edata->input_len);
  1456. if (strcmp(keyword, "Output") == 0)
  1457. return parse_bin(value, &edata->output, &edata->output_len);
  1458. return 0;
  1459. }
  1460. static int encode_test_run(EVP_TEST *t)
  1461. {
  1462. ENCODE_DATA *expected = t->data;
  1463. unsigned char *encode_out = NULL, *decode_out = NULL;
  1464. int output_len, chunk_len;
  1465. EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL;
  1466. if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
  1467. t->err = "INTERNAL_ERROR";
  1468. goto err;
  1469. }
  1470. if (expected->encoding == BASE64_CANONICAL_ENCODING) {
  1471. if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
  1472. || !TEST_ptr(encode_out =
  1473. OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len))))
  1474. goto err;
  1475. EVP_EncodeInit(encode_ctx);
  1476. if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
  1477. expected->input, expected->input_len)))
  1478. goto err;
  1479. output_len = chunk_len;
  1480. EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
  1481. output_len += chunk_len;
  1482. if (!memory_err_compare(t, "BAD_ENCODING",
  1483. expected->output, expected->output_len,
  1484. encode_out, output_len))
  1485. goto err;
  1486. }
  1487. if (!TEST_ptr(decode_out =
  1488. OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len))))
  1489. goto err;
  1490. EVP_DecodeInit(decode_ctx);
  1491. if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output,
  1492. expected->output_len) < 0) {
  1493. t->err = "DECODE_ERROR";
  1494. goto err;
  1495. }
  1496. output_len = chunk_len;
  1497. if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
  1498. t->err = "DECODE_ERROR";
  1499. goto err;
  1500. }
  1501. output_len += chunk_len;
  1502. if (expected->encoding != BASE64_INVALID_ENCODING
  1503. && !memory_err_compare(t, "BAD_DECODING",
  1504. expected->input, expected->input_len,
  1505. decode_out, output_len)) {
  1506. t->err = "BAD_DECODING";
  1507. goto err;
  1508. }
  1509. t->err = NULL;
  1510. err:
  1511. OPENSSL_free(encode_out);
  1512. OPENSSL_free(decode_out);
  1513. EVP_ENCODE_CTX_free(decode_ctx);
  1514. EVP_ENCODE_CTX_free(encode_ctx);
  1515. return 1;
  1516. }
  1517. static const EVP_TEST_METHOD encode_test_method = {
  1518. "Encoding",
  1519. encode_test_init,
  1520. encode_test_cleanup,
  1521. encode_test_parse,
  1522. encode_test_run,
  1523. };
  1524. /**
  1525. *** KDF TESTS
  1526. **/
  1527. typedef struct kdf_data_st {
  1528. /* Context for this operation */
  1529. EVP_PKEY_CTX *ctx;
  1530. /* Expected output */
  1531. unsigned char *output;
  1532. size_t output_len;
  1533. } KDF_DATA;
  1534. /*
  1535. * Perform public key operation setup: lookup key, allocated ctx and call
  1536. * the appropriate initialisation function
  1537. */
  1538. static int kdf_test_init(EVP_TEST *t, const char *name)
  1539. {
  1540. KDF_DATA *kdata;
  1541. int kdf_nid = OBJ_sn2nid(name);
  1542. #ifdef OPENSSL_NO_SCRYPT
  1543. if (strcmp(name, "scrypt") == 0) {
  1544. t->skip = 1;
  1545. return 1;
  1546. }
  1547. #endif
  1548. if (kdf_nid == NID_undef)
  1549. kdf_nid = OBJ_ln2nid(name);
  1550. if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata))))
  1551. return 0;
  1552. kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL);
  1553. if (kdata->ctx == NULL) {
  1554. OPENSSL_free(kdata);
  1555. return 0;
  1556. }
  1557. if (EVP_PKEY_derive_init(kdata->ctx) <= 0) {
  1558. EVP_PKEY_CTX_free(kdata->ctx);
  1559. OPENSSL_free(kdata);
  1560. return 0;
  1561. }
  1562. t->data = kdata;
  1563. return 1;
  1564. }
  1565. static void kdf_test_cleanup(EVP_TEST *t)
  1566. {
  1567. KDF_DATA *kdata = t->data;
  1568. OPENSSL_free(kdata->output);
  1569. EVP_PKEY_CTX_free(kdata->ctx);
  1570. }
  1571. static int kdf_test_parse(EVP_TEST *t,
  1572. const char *keyword, const char *value)
  1573. {
  1574. KDF_DATA *kdata = t->data;
  1575. if (strcmp(keyword, "Output") == 0)
  1576. return parse_bin(value, &kdata->output, &kdata->output_len);
  1577. if (strncmp(keyword, "Ctrl", 4) == 0)
  1578. return pkey_test_ctrl(t, kdata->ctx, value);
  1579. return 0;
  1580. }
  1581. static int kdf_test_run(EVP_TEST *t)
  1582. {
  1583. KDF_DATA *expected = t->data;
  1584. unsigned char *got = NULL;
  1585. size_t got_len = expected->output_len;
  1586. if (!TEST_ptr(got = OPENSSL_malloc(got_len == 0 ? 1 : got_len))) {
  1587. t->err = "INTERNAL_ERROR";
  1588. goto err;
  1589. }
  1590. if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) {
  1591. t->err = "KDF_DERIVE_ERROR";
  1592. goto err;
  1593. }
  1594. if (!memory_err_compare(t, "KDF_MISMATCH",
  1595. expected->output, expected->output_len,
  1596. got, got_len))
  1597. goto err;
  1598. t->err = NULL;
  1599. err:
  1600. OPENSSL_free(got);
  1601. return 1;
  1602. }
  1603. static const EVP_TEST_METHOD kdf_test_method = {
  1604. "KDF",
  1605. kdf_test_init,
  1606. kdf_test_cleanup,
  1607. kdf_test_parse,
  1608. kdf_test_run
  1609. };
  1610. /**
  1611. *** KEYPAIR TESTS
  1612. **/
  1613. typedef struct keypair_test_data_st {
  1614. EVP_PKEY *privk;
  1615. EVP_PKEY *pubk;
  1616. } KEYPAIR_TEST_DATA;
  1617. static int keypair_test_init(EVP_TEST *t, const char *pair)
  1618. {
  1619. KEYPAIR_TEST_DATA *data;
  1620. int rv = 0;
  1621. EVP_PKEY *pk = NULL, *pubk = NULL;
  1622. char *pub, *priv = NULL;
  1623. /* Split private and public names. */
  1624. if (!TEST_ptr(priv = OPENSSL_strdup(pair))
  1625. || !TEST_ptr(pub = strchr(priv, ':'))) {
  1626. t->err = "PARSING_ERROR";
  1627. goto end;
  1628. }
  1629. *pub++ = '\0';
  1630. if (!TEST_true(find_key(&pk, priv, private_keys))) {
  1631. TEST_info("Can't find private key: %s", priv);
  1632. t->err = "MISSING_PRIVATE_KEY";
  1633. goto end;
  1634. }
  1635. if (!TEST_true(find_key(&pubk, pub, public_keys))) {
  1636. TEST_info("Can't find public key: %s", pub);
  1637. t->err = "MISSING_PUBLIC_KEY";
  1638. goto end;
  1639. }
  1640. if (pk == NULL && pubk == NULL) {
  1641. /* Both keys are listed but unsupported: skip this test */
  1642. t->skip = 1;
  1643. rv = 1;
  1644. goto end;
  1645. }
  1646. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  1647. goto end;
  1648. data->privk = pk;
  1649. data->pubk = pubk;
  1650. t->data = data;
  1651. rv = 1;
  1652. t->err = NULL;
  1653. end:
  1654. OPENSSL_free(priv);
  1655. return rv;
  1656. }
  1657. static void keypair_test_cleanup(EVP_TEST *t)
  1658. {
  1659. OPENSSL_free(t->data);
  1660. t->data = NULL;
  1661. }
  1662. /*
  1663. * For tests that do not accept any custom keywords.
  1664. */
  1665. static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
  1666. {
  1667. return 0;
  1668. }
  1669. static int keypair_test_run(EVP_TEST *t)
  1670. {
  1671. int rv = 0;
  1672. const KEYPAIR_TEST_DATA *pair = t->data;
  1673. if (pair->privk == NULL || pair->pubk == NULL) {
  1674. /*
  1675. * this can only happen if only one of the keys is not set
  1676. * which means that one of them was unsupported while the
  1677. * other isn't: hence a key type mismatch.
  1678. */
  1679. t->err = "KEYPAIR_TYPE_MISMATCH";
  1680. rv = 1;
  1681. goto end;
  1682. }
  1683. if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
  1684. if ( 0 == rv ) {
  1685. t->err = "KEYPAIR_MISMATCH";
  1686. } else if ( -1 == rv ) {
  1687. t->err = "KEYPAIR_TYPE_MISMATCH";
  1688. } else if ( -2 == rv ) {
  1689. t->err = "UNSUPPORTED_KEY_COMPARISON";
  1690. } else {
  1691. TEST_error("Unexpected error in key comparison");
  1692. rv = 0;
  1693. goto end;
  1694. }
  1695. rv = 1;
  1696. goto end;
  1697. }
  1698. rv = 1;
  1699. t->err = NULL;
  1700. end:
  1701. return rv;
  1702. }
  1703. static const EVP_TEST_METHOD keypair_test_method = {
  1704. "PrivPubKeyPair",
  1705. keypair_test_init,
  1706. keypair_test_cleanup,
  1707. void_test_parse,
  1708. keypair_test_run
  1709. };
  1710. /**
  1711. *** KEYGEN TEST
  1712. **/
  1713. typedef struct keygen_test_data_st {
  1714. EVP_PKEY_CTX *genctx; /* Keygen context to use */
  1715. char *keyname; /* Key name to store key or NULL */
  1716. } KEYGEN_TEST_DATA;
  1717. static int keygen_test_init(EVP_TEST *t, const char *alg)
  1718. {
  1719. KEYGEN_TEST_DATA *data;
  1720. EVP_PKEY_CTX *genctx;
  1721. int nid = OBJ_sn2nid(alg);
  1722. if (nid == NID_undef) {
  1723. nid = OBJ_ln2nid(alg);
  1724. if (nid == NID_undef)
  1725. return 0;
  1726. }
  1727. if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) {
  1728. /* assume algorithm disabled */
  1729. t->skip = 1;
  1730. return 1;
  1731. }
  1732. if (EVP_PKEY_keygen_init(genctx) <= 0) {
  1733. t->err = "KEYGEN_INIT_ERROR";
  1734. goto err;
  1735. }
  1736. if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
  1737. goto err;
  1738. data->genctx = genctx;
  1739. data->keyname = NULL;
  1740. t->data = data;
  1741. t->err = NULL;
  1742. return 1;
  1743. err:
  1744. EVP_PKEY_CTX_free(genctx);
  1745. return 0;
  1746. }
  1747. static void keygen_test_cleanup(EVP_TEST *t)
  1748. {
  1749. KEYGEN_TEST_DATA *keygen = t->data;
  1750. EVP_PKEY_CTX_free(keygen->genctx);
  1751. OPENSSL_free(keygen->keyname);
  1752. OPENSSL_free(t->data);
  1753. t->data = NULL;
  1754. }
  1755. static int keygen_test_parse(EVP_TEST *t,
  1756. const char *keyword, const char *value)
  1757. {
  1758. KEYGEN_TEST_DATA *keygen = t->data;
  1759. if (strcmp(keyword, "KeyName") == 0)
  1760. return TEST_ptr(keygen->keyname = OPENSSL_strdup(value));
  1761. if (strcmp(keyword, "Ctrl") == 0)
  1762. return pkey_test_ctrl(t, keygen->genctx, value);
  1763. return 0;
  1764. }
  1765. static int keygen_test_run(EVP_TEST *t)
  1766. {
  1767. KEYGEN_TEST_DATA *keygen = t->data;
  1768. EVP_PKEY *pkey = NULL;
  1769. t->err = NULL;
  1770. if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) {
  1771. t->err = "KEYGEN_GENERATE_ERROR";
  1772. goto err;
  1773. }
  1774. if (keygen->keyname != NULL) {
  1775. KEY_LIST *key;
  1776. if (find_key(NULL, keygen->keyname, private_keys)) {
  1777. TEST_info("Duplicate key %s", keygen->keyname);
  1778. goto err;
  1779. }
  1780. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  1781. goto err;
  1782. key->name = keygen->keyname;
  1783. keygen->keyname = NULL;
  1784. key->key = pkey;
  1785. key->next = private_keys;
  1786. private_keys = key;
  1787. } else {
  1788. EVP_PKEY_free(pkey);
  1789. }
  1790. return 1;
  1791. err:
  1792. EVP_PKEY_free(pkey);
  1793. return 0;
  1794. }
  1795. static const EVP_TEST_METHOD keygen_test_method = {
  1796. "KeyGen",
  1797. keygen_test_init,
  1798. keygen_test_cleanup,
  1799. keygen_test_parse,
  1800. keygen_test_run,
  1801. };
  1802. /**
  1803. *** DIGEST SIGN+VERIFY TESTS
  1804. **/
  1805. typedef struct {
  1806. int is_verify; /* Set to 1 if verifying */
  1807. int is_oneshot; /* Set to 1 for one shot operation */
  1808. const EVP_MD *md; /* Digest to use */
  1809. EVP_MD_CTX *ctx; /* Digest context */
  1810. EVP_PKEY_CTX *pctx;
  1811. STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */
  1812. unsigned char *osin; /* Input data if one shot */
  1813. size_t osin_len; /* Input length data if one shot */
  1814. unsigned char *output; /* Expected output */
  1815. size_t output_len; /* Expected output length */
  1816. } DIGESTSIGN_DATA;
  1817. static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify,
  1818. int is_oneshot)
  1819. {
  1820. const EVP_MD *md = NULL;
  1821. DIGESTSIGN_DATA *mdat;
  1822. if (strcmp(alg, "NULL") != 0) {
  1823. if ((md = EVP_get_digestbyname(alg)) == NULL) {
  1824. /* If alg has an OID assume disabled algorithm */
  1825. if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
  1826. t->skip = 1;
  1827. return 1;
  1828. }
  1829. return 0;
  1830. }
  1831. }
  1832. if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat))))
  1833. return 0;
  1834. mdat->md = md;
  1835. if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) {
  1836. OPENSSL_free(mdat);
  1837. return 0;
  1838. }
  1839. mdat->is_verify = is_verify;
  1840. mdat->is_oneshot = is_oneshot;
  1841. t->data = mdat;
  1842. return 1;
  1843. }
  1844. static int digestsign_test_init(EVP_TEST *t, const char *alg)
  1845. {
  1846. return digestsigver_test_init(t, alg, 0, 0);
  1847. }
  1848. static void digestsigver_test_cleanup(EVP_TEST *t)
  1849. {
  1850. DIGESTSIGN_DATA *mdata = t->data;
  1851. EVP_MD_CTX_free(mdata->ctx);
  1852. sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free);
  1853. OPENSSL_free(mdata->osin);
  1854. OPENSSL_free(mdata->output);
  1855. OPENSSL_free(mdata);
  1856. t->data = NULL;
  1857. }
  1858. static int digestsigver_test_parse(EVP_TEST *t,
  1859. const char *keyword, const char *value)
  1860. {
  1861. DIGESTSIGN_DATA *mdata = t->data;
  1862. if (strcmp(keyword, "Key") == 0) {
  1863. EVP_PKEY *pkey = NULL;
  1864. int rv = 0;
  1865. if (mdata->is_verify)
  1866. rv = find_key(&pkey, value, public_keys);
  1867. if (rv == 0)
  1868. rv = find_key(&pkey, value, private_keys);
  1869. if (rv == 0 || pkey == NULL) {
  1870. t->skip = 1;
  1871. return 1;
  1872. }
  1873. if (mdata->is_verify) {
  1874. if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md,
  1875. NULL, pkey))
  1876. t->err = "DIGESTVERIFYINIT_ERROR";
  1877. return 1;
  1878. }
  1879. if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL,
  1880. pkey))
  1881. t->err = "DIGESTSIGNINIT_ERROR";
  1882. return 1;
  1883. }
  1884. if (strcmp(keyword, "Input") == 0) {
  1885. if (mdata->is_oneshot)
  1886. return parse_bin(value, &mdata->osin, &mdata->osin_len);
  1887. return evp_test_buffer_append(value, &mdata->input);
  1888. }
  1889. if (strcmp(keyword, "Output") == 0)
  1890. return parse_bin(value, &mdata->output, &mdata->output_len);
  1891. if (!mdata->is_oneshot) {
  1892. if (strcmp(keyword, "Count") == 0)
  1893. return evp_test_buffer_set_count(value, mdata->input);
  1894. if (strcmp(keyword, "Ncopy") == 0)
  1895. return evp_test_buffer_ncopy(value, mdata->input);
  1896. }
  1897. if (strcmp(keyword, "Ctrl") == 0) {
  1898. if (mdata->pctx == NULL)
  1899. return -1;
  1900. return pkey_test_ctrl(t, mdata->pctx, value);
  1901. }
  1902. return 0;
  1903. }
  1904. static int digestsign_update_fn(void *ctx, const unsigned char *buf,
  1905. size_t buflen)
  1906. {
  1907. return EVP_DigestSignUpdate(ctx, buf, buflen);
  1908. }
  1909. static int digestsign_test_run(EVP_TEST *t)
  1910. {
  1911. DIGESTSIGN_DATA *expected = t->data;
  1912. unsigned char *got = NULL;
  1913. size_t got_len;
  1914. if (!evp_test_buffer_do(expected->input, digestsign_update_fn,
  1915. expected->ctx)) {
  1916. t->err = "DIGESTUPDATE_ERROR";
  1917. goto err;
  1918. }
  1919. if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) {
  1920. t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
  1921. goto err;
  1922. }
  1923. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1924. t->err = "MALLOC_FAILURE";
  1925. goto err;
  1926. }
  1927. if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) {
  1928. t->err = "DIGESTSIGNFINAL_ERROR";
  1929. goto err;
  1930. }
  1931. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  1932. expected->output, expected->output_len,
  1933. got, got_len))
  1934. goto err;
  1935. t->err = NULL;
  1936. err:
  1937. OPENSSL_free(got);
  1938. return 1;
  1939. }
  1940. static const EVP_TEST_METHOD digestsign_test_method = {
  1941. "DigestSign",
  1942. digestsign_test_init,
  1943. digestsigver_test_cleanup,
  1944. digestsigver_test_parse,
  1945. digestsign_test_run
  1946. };
  1947. static int digestverify_test_init(EVP_TEST *t, const char *alg)
  1948. {
  1949. return digestsigver_test_init(t, alg, 1, 0);
  1950. }
  1951. static int digestverify_update_fn(void *ctx, const unsigned char *buf,
  1952. size_t buflen)
  1953. {
  1954. return EVP_DigestVerifyUpdate(ctx, buf, buflen);
  1955. }
  1956. static int digestverify_test_run(EVP_TEST *t)
  1957. {
  1958. DIGESTSIGN_DATA *mdata = t->data;
  1959. if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) {
  1960. t->err = "DIGESTUPDATE_ERROR";
  1961. return 1;
  1962. }
  1963. if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output,
  1964. mdata->output_len) <= 0)
  1965. t->err = "VERIFY_ERROR";
  1966. return 1;
  1967. }
  1968. static const EVP_TEST_METHOD digestverify_test_method = {
  1969. "DigestVerify",
  1970. digestverify_test_init,
  1971. digestsigver_test_cleanup,
  1972. digestsigver_test_parse,
  1973. digestverify_test_run
  1974. };
  1975. static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg)
  1976. {
  1977. return digestsigver_test_init(t, alg, 0, 1);
  1978. }
  1979. static int oneshot_digestsign_test_run(EVP_TEST *t)
  1980. {
  1981. DIGESTSIGN_DATA *expected = t->data;
  1982. unsigned char *got = NULL;
  1983. size_t got_len;
  1984. if (!EVP_DigestSign(expected->ctx, NULL, &got_len,
  1985. expected->osin, expected->osin_len)) {
  1986. t->err = "DIGESTSIGN_LENGTH_ERROR";
  1987. goto err;
  1988. }
  1989. if (!TEST_ptr(got = OPENSSL_malloc(got_len))) {
  1990. t->err = "MALLOC_FAILURE";
  1991. goto err;
  1992. }
  1993. if (!EVP_DigestSign(expected->ctx, got, &got_len,
  1994. expected->osin, expected->osin_len)) {
  1995. t->err = "DIGESTSIGN_ERROR";
  1996. goto err;
  1997. }
  1998. if (!memory_err_compare(t, "SIGNATURE_MISMATCH",
  1999. expected->output, expected->output_len,
  2000. got, got_len))
  2001. goto err;
  2002. t->err = NULL;
  2003. err:
  2004. OPENSSL_free(got);
  2005. return 1;
  2006. }
  2007. static const EVP_TEST_METHOD oneshot_digestsign_test_method = {
  2008. "OneShotDigestSign",
  2009. oneshot_digestsign_test_init,
  2010. digestsigver_test_cleanup,
  2011. digestsigver_test_parse,
  2012. oneshot_digestsign_test_run
  2013. };
  2014. static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg)
  2015. {
  2016. return digestsigver_test_init(t, alg, 1, 1);
  2017. }
  2018. static int oneshot_digestverify_test_run(EVP_TEST *t)
  2019. {
  2020. DIGESTSIGN_DATA *mdata = t->data;
  2021. if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len,
  2022. mdata->osin, mdata->osin_len) <= 0)
  2023. t->err = "VERIFY_ERROR";
  2024. return 1;
  2025. }
  2026. static const EVP_TEST_METHOD oneshot_digestverify_test_method = {
  2027. "OneShotDigestVerify",
  2028. oneshot_digestverify_test_init,
  2029. digestsigver_test_cleanup,
  2030. digestsigver_test_parse,
  2031. oneshot_digestverify_test_run
  2032. };
  2033. /**
  2034. *** PARSING AND DISPATCH
  2035. **/
  2036. static const EVP_TEST_METHOD *evp_test_list[] = {
  2037. &cipher_test_method,
  2038. &digest_test_method,
  2039. &digestsign_test_method,
  2040. &digestverify_test_method,
  2041. &encode_test_method,
  2042. &kdf_test_method,
  2043. &keypair_test_method,
  2044. &keygen_test_method,
  2045. &mac_test_method,
  2046. &oneshot_digestsign_test_method,
  2047. &oneshot_digestverify_test_method,
  2048. &pbe_test_method,
  2049. &pdecrypt_test_method,
  2050. &pderive_test_method,
  2051. &psign_test_method,
  2052. &pverify_recover_test_method,
  2053. &pverify_test_method,
  2054. NULL
  2055. };
  2056. static const EVP_TEST_METHOD *find_test(const char *name)
  2057. {
  2058. const EVP_TEST_METHOD **tt;
  2059. for (tt = evp_test_list; *tt; tt++) {
  2060. if (strcmp(name, (*tt)->name) == 0)
  2061. return *tt;
  2062. }
  2063. return NULL;
  2064. }
  2065. static void clear_test(EVP_TEST *t)
  2066. {
  2067. test_clearstanza(&t->s);
  2068. ERR_clear_error();
  2069. if (t->data != NULL) {
  2070. if (t->meth != NULL)
  2071. t->meth->cleanup(t);
  2072. OPENSSL_free(t->data);
  2073. t->data = NULL;
  2074. }
  2075. OPENSSL_free(t->expected_err);
  2076. t->expected_err = NULL;
  2077. OPENSSL_free(t->func);
  2078. t->func = NULL;
  2079. OPENSSL_free(t->reason);
  2080. t->reason = NULL;
  2081. /* Text literal. */
  2082. t->err = NULL;
  2083. t->skip = 0;
  2084. t->meth = NULL;
  2085. }
  2086. /*
  2087. * Check for errors in the test structure; return 1 if okay, else 0.
  2088. */
  2089. static int check_test_error(EVP_TEST *t)
  2090. {
  2091. unsigned long err;
  2092. const char *func;
  2093. const char *reason;
  2094. if (t->err == NULL && t->expected_err == NULL)
  2095. return 1;
  2096. if (t->err != NULL && t->expected_err == NULL) {
  2097. if (t->aux_err != NULL) {
  2098. TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
  2099. t->s.test_file, t->s.start, t->aux_err, t->err);
  2100. } else {
  2101. TEST_info("%s:%d: Source of above error; unexpected error %s",
  2102. t->s.test_file, t->s.start, t->err);
  2103. }
  2104. return 0;
  2105. }
  2106. if (t->err == NULL && t->expected_err != NULL) {
  2107. TEST_info("%s:%d: Succeeded but was expecting %s",
  2108. t->s.test_file, t->s.start, t->expected_err);
  2109. return 0;
  2110. }
  2111. if (strcmp(t->err, t->expected_err) != 0) {
  2112. TEST_info("%s:%d: Expected %s got %s",
  2113. t->s.test_file, t->s.start, t->expected_err, t->err);
  2114. return 0;
  2115. }
  2116. if (t->func == NULL && t->reason == NULL)
  2117. return 1;
  2118. if (t->func == NULL || t->reason == NULL) {
  2119. TEST_info("%s:%d: Test is missing function or reason code",
  2120. t->s.test_file, t->s.start);
  2121. return 0;
  2122. }
  2123. err = ERR_peek_error();
  2124. if (err == 0) {
  2125. TEST_info("%s:%d: Expected error \"%s:%s\" not set",
  2126. t->s.test_file, t->s.start, t->func, t->reason);
  2127. return 0;
  2128. }
  2129. func = ERR_func_error_string(err);
  2130. reason = ERR_reason_error_string(err);
  2131. if (func == NULL && reason == NULL) {
  2132. TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
  2133. " Assuming ok.",
  2134. t->s.test_file, t->s.start, t->func, t->reason);
  2135. return 1;
  2136. }
  2137. if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
  2138. return 1;
  2139. TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
  2140. t->s.test_file, t->s.start, t->func, t->reason, func, reason);
  2141. return 0;
  2142. }
  2143. /*
  2144. * Run a parsed test. Log a message and return 0 on error.
  2145. */
  2146. static int run_test(EVP_TEST *t)
  2147. {
  2148. if (t->meth == NULL)
  2149. return 1;
  2150. t->s.numtests++;
  2151. if (t->skip) {
  2152. t->s.numskip++;
  2153. } else {
  2154. /* run the test */
  2155. if (t->err == NULL && t->meth->run_test(t) != 1) {
  2156. TEST_info("%s:%d %s error",
  2157. t->s.test_file, t->s.start, t->meth->name);
  2158. return 0;
  2159. }
  2160. if (!check_test_error(t)) {
  2161. TEST_openssl_errors();
  2162. t->s.errors++;
  2163. }
  2164. }
  2165. /* clean it up */
  2166. return 1;
  2167. }
  2168. static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
  2169. {
  2170. for (; lst != NULL; lst = lst->next) {
  2171. if (strcmp(lst->name, name) == 0) {
  2172. if (ppk != NULL)
  2173. *ppk = lst->key;
  2174. return 1;
  2175. }
  2176. }
  2177. return 0;
  2178. }
  2179. static void free_key_list(KEY_LIST *lst)
  2180. {
  2181. while (lst != NULL) {
  2182. KEY_LIST *next = lst->next;
  2183. EVP_PKEY_free(lst->key);
  2184. OPENSSL_free(lst->name);
  2185. OPENSSL_free(lst);
  2186. lst = next;
  2187. }
  2188. }
  2189. /*
  2190. * Is the key type an unsupported algorithm?
  2191. */
  2192. static int key_unsupported(void)
  2193. {
  2194. long err = ERR_peek_error();
  2195. if (ERR_GET_LIB(err) == ERR_LIB_EVP
  2196. && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
  2197. ERR_clear_error();
  2198. return 1;
  2199. }
  2200. #ifndef OPENSSL_NO_EC
  2201. /*
  2202. * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
  2203. * hint to an unsupported algorithm/curve (e.g. if binary EC support is
  2204. * disabled).
  2205. */
  2206. if (ERR_GET_LIB(err) == ERR_LIB_EC
  2207. && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
  2208. ERR_clear_error();
  2209. return 1;
  2210. }
  2211. #endif /* OPENSSL_NO_EC */
  2212. return 0;
  2213. }
  2214. /*
  2215. * NULL out the value from |pp| but return it. This "steals" a pointer.
  2216. */
  2217. static char *take_value(PAIR *pp)
  2218. {
  2219. char *p = pp->value;
  2220. pp->value = NULL;
  2221. return p;
  2222. }
  2223. /*
  2224. * Read and parse one test. Return 0 if failure, 1 if okay.
  2225. */
  2226. static int parse(EVP_TEST *t)
  2227. {
  2228. KEY_LIST *key, **klist;
  2229. EVP_PKEY *pkey;
  2230. PAIR *pp;
  2231. int i;
  2232. top:
  2233. do {
  2234. if (BIO_eof(t->s.fp))
  2235. return EOF;
  2236. clear_test(t);
  2237. if (!test_readstanza(&t->s))
  2238. return 0;
  2239. } while (t->s.numpairs == 0);
  2240. pp = &t->s.pairs[0];
  2241. /* Are we adding a key? */
  2242. klist = NULL;
  2243. pkey = NULL;
  2244. if (strcmp(pp->key, "PrivateKey") == 0) {
  2245. pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL);
  2246. if (pkey == NULL && !key_unsupported()) {
  2247. EVP_PKEY_free(pkey);
  2248. TEST_info("Can't read private key %s", pp->value);
  2249. TEST_openssl_errors();
  2250. return 0;
  2251. }
  2252. klist = &private_keys;
  2253. } else if (strcmp(pp->key, "PublicKey") == 0) {
  2254. pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL);
  2255. if (pkey == NULL && !key_unsupported()) {
  2256. EVP_PKEY_free(pkey);
  2257. TEST_info("Can't read public key %s", pp->value);
  2258. TEST_openssl_errors();
  2259. return 0;
  2260. }
  2261. klist = &public_keys;
  2262. } else if (strcmp(pp->key, "PrivateKeyRaw") == 0
  2263. || strcmp(pp->key, "PublicKeyRaw") == 0 ) {
  2264. char *strnid = NULL, *keydata = NULL;
  2265. unsigned char *keybin;
  2266. size_t keylen;
  2267. int nid;
  2268. if (strcmp(pp->key, "PrivateKeyRaw") == 0)
  2269. klist = &private_keys;
  2270. else
  2271. klist = &public_keys;
  2272. strnid = strchr(pp->value, ':');
  2273. if (strnid != NULL) {
  2274. *strnid++ = '\0';
  2275. keydata = strchr(strnid, ':');
  2276. if (keydata != NULL)
  2277. *keydata++ = '\0';
  2278. }
  2279. if (keydata == NULL) {
  2280. TEST_info("Failed to parse %s value", pp->key);
  2281. return 0;
  2282. }
  2283. nid = OBJ_txt2nid(strnid);
  2284. if (nid == NID_undef) {
  2285. TEST_info("Uncrecognised algorithm NID");
  2286. return 0;
  2287. }
  2288. if (!parse_bin(keydata, &keybin, &keylen)) {
  2289. TEST_info("Failed to create binary key");
  2290. return 0;
  2291. }
  2292. if (klist == &private_keys)
  2293. pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen);
  2294. else
  2295. pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen);
  2296. if (pkey == NULL && !key_unsupported()) {
  2297. TEST_info("Can't read %s data", pp->key);
  2298. OPENSSL_free(keybin);
  2299. TEST_openssl_errors();
  2300. return 0;
  2301. }
  2302. OPENSSL_free(keybin);
  2303. }
  2304. /* If we have a key add to list */
  2305. if (klist != NULL) {
  2306. if (find_key(NULL, pp->value, *klist)) {
  2307. TEST_info("Duplicate key %s", pp->value);
  2308. return 0;
  2309. }
  2310. if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key))))
  2311. return 0;
  2312. key->name = take_value(pp);
  2313. /* Hack to detect SM2 keys */
  2314. if(pkey != NULL && strstr(key->name, "SM2") != NULL) {
  2315. #ifdef OPENSSL_NO_SM2
  2316. EVP_PKEY_free(pkey);
  2317. pkey = NULL;
  2318. #else
  2319. EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
  2320. #endif
  2321. }
  2322. key->key = pkey;
  2323. key->next = *klist;
  2324. *klist = key;
  2325. /* Go back and start a new stanza. */
  2326. if (t->s.numpairs != 1)
  2327. TEST_info("Line %d: missing blank line\n", t->s.curr);
  2328. goto top;
  2329. }
  2330. /* Find the test, based on first keyword. */
  2331. if (!TEST_ptr(t->meth = find_test(pp->key)))
  2332. return 0;
  2333. if (!t->meth->init(t, pp->value)) {
  2334. TEST_error("unknown %s: %s\n", pp->key, pp->value);
  2335. return 0;
  2336. }
  2337. if (t->skip == 1) {
  2338. /* TEST_info("skipping %s %s", pp->key, pp->value); */
  2339. return 0;
  2340. }
  2341. for (pp++, i = 1; i < t->s.numpairs; pp++, i++) {
  2342. if (strcmp(pp->key, "Result") == 0) {
  2343. if (t->expected_err != NULL) {
  2344. TEST_info("Line %d: multiple result lines", t->s.curr);
  2345. return 0;
  2346. }
  2347. t->expected_err = take_value(pp);
  2348. } else if (strcmp(pp->key, "Function") == 0) {
  2349. if (t->func != NULL) {
  2350. TEST_info("Line %d: multiple function lines\n", t->s.curr);
  2351. return 0;
  2352. }
  2353. t->func = take_value(pp);
  2354. } else if (strcmp(pp->key, "Reason") == 0) {
  2355. if (t->reason != NULL) {
  2356. TEST_info("Line %d: multiple reason lines", t->s.curr);
  2357. return 0;
  2358. }
  2359. t->reason = take_value(pp);
  2360. } else {
  2361. /* Must be test specific line: try to parse it */
  2362. int rv = t->meth->parse(t, pp->key, pp->value);
  2363. if (rv == 0) {
  2364. TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key);
  2365. return 0;
  2366. }
  2367. if (rv < 0) {
  2368. TEST_info("Line %d: error processing keyword %s = %s\n",
  2369. t->s.curr, pp->key, pp->value);
  2370. return 0;
  2371. }
  2372. }
  2373. }
  2374. return 1;
  2375. }
  2376. static int run_file_tests(int i)
  2377. {
  2378. EVP_TEST *t;
  2379. const char *testfile = test_get_argument(i);
  2380. int c;
  2381. if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t))))
  2382. return 0;
  2383. if (!test_start_file(&t->s, testfile)) {
  2384. OPENSSL_free(t);
  2385. return 0;
  2386. }
  2387. while (!BIO_eof(t->s.fp)) {
  2388. c = parse(t);
  2389. if (t->skip)
  2390. continue;
  2391. if (c == 0 || !run_test(t)) {
  2392. t->s.errors++;
  2393. break;
  2394. }
  2395. }
  2396. test_end_file(&t->s);
  2397. clear_test(t);
  2398. free_key_list(public_keys);
  2399. free_key_list(private_keys);
  2400. BIO_free(t->s.key);
  2401. c = t->s.errors;
  2402. OPENSSL_free(t);
  2403. return c == 0;
  2404. }
  2405. int setup_tests(void)
  2406. {
  2407. size_t n = test_get_argument_count();
  2408. if (n == 0) {
  2409. TEST_error("Usage: %s file...", test_get_program_name());
  2410. return 0;
  2411. }
  2412. ADD_ALL_TESTS(run_file_tests, n);
  2413. return 1;
  2414. }