evp_test.c 76 KB

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