test_suite_ssl.function 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552
  1. /* BEGIN_HEADER */
  2. #include <mbedtls/ssl.h>
  3. #include <mbedtls/ssl_internal.h>
  4. #include <mbedtls/ctr_drbg.h>
  5. #include <mbedtls/entropy.h>
  6. #include <mbedtls/certs.h>
  7. #include <mbedtls/timing.h>
  8. #include <mbedtls/debug.h>
  9. #include <ssl_tls13_keys.h>
  10. #include <constant_time_internal.h>
  11. #include <test/constant_flow.h>
  12. enum
  13. {
  14. #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
  15. tls1_3_label_ ## name,
  16. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  17. #undef MBEDTLS_SSL_TLS1_3_LABEL
  18. };
  19. typedef struct log_pattern
  20. {
  21. const char *pattern;
  22. size_t counter;
  23. } log_pattern;
  24. /*
  25. * This function can be passed to mbedtls to receive output logs from it. In
  26. * this case, it will count the instances of a log_pattern in the received
  27. * logged messages.
  28. */
  29. void log_analyzer( void *ctx, int level,
  30. const char *file, int line,
  31. const char *str )
  32. {
  33. log_pattern *p = (log_pattern *) ctx;
  34. (void) level;
  35. (void) line;
  36. (void) file;
  37. if( NULL != p &&
  38. NULL != p->pattern &&
  39. NULL != strstr( str, p->pattern ) )
  40. {
  41. p->counter++;
  42. }
  43. }
  44. /* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
  45. #define TEST_SSL_MINOR_VERSION_NONE -1
  46. typedef struct handshake_test_options
  47. {
  48. const char *cipher;
  49. int client_min_version;
  50. int client_max_version;
  51. int server_min_version;
  52. int server_max_version;
  53. int expected_negotiated_version;
  54. int pk_alg;
  55. data_t *psk_str;
  56. int dtls;
  57. int srv_auth_mode;
  58. int serialize;
  59. int mfl;
  60. int cli_msg_len;
  61. int srv_msg_len;
  62. int expected_cli_fragments;
  63. int expected_srv_fragments;
  64. int renegotiate;
  65. int legacy_renegotiation;
  66. void *srv_log_obj;
  67. void *cli_log_obj;
  68. void (*srv_log_fun)(void *, int, const char *, int, const char *);
  69. void (*cli_log_fun)(void *, int, const char *, int, const char *);
  70. int resize_buffers;
  71. } handshake_test_options;
  72. void init_handshake_options( handshake_test_options *opts )
  73. {
  74. opts->cipher = "";
  75. opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
  76. opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
  77. opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
  78. opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
  79. opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
  80. opts->pk_alg = MBEDTLS_PK_RSA;
  81. opts->psk_str = NULL;
  82. opts->dtls = 0;
  83. opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
  84. opts->serialize = 0;
  85. opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
  86. opts->cli_msg_len = 100;
  87. opts->srv_msg_len = 100;
  88. opts->expected_cli_fragments = 1;
  89. opts->expected_srv_fragments = 1;
  90. opts->renegotiate = 0;
  91. opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
  92. opts->srv_log_obj = NULL;
  93. opts->srv_log_obj = NULL;
  94. opts->srv_log_fun = NULL;
  95. opts->cli_log_fun = NULL;
  96. opts->resize_buffers = 1;
  97. }
  98. /*
  99. * Buffer structure for custom I/O callbacks.
  100. */
  101. typedef struct mbedtls_test_buffer
  102. {
  103. size_t start;
  104. size_t content_length;
  105. size_t capacity;
  106. unsigned char *buffer;
  107. } mbedtls_test_buffer;
  108. /*
  109. * Initialises \p buf. After calling this function it is safe to call
  110. * `mbedtls_test_buffer_free()` on \p buf.
  111. */
  112. void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
  113. {
  114. memset( buf, 0, sizeof( *buf ) );
  115. }
  116. /*
  117. * Sets up \p buf. After calling this function it is safe to call
  118. * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
  119. */
  120. int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
  121. {
  122. buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
  123. sizeof(unsigned char) );
  124. if( NULL == buf->buffer )
  125. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  126. buf->capacity = capacity;
  127. return 0;
  128. }
  129. void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
  130. {
  131. if( buf->buffer != NULL )
  132. mbedtls_free( buf->buffer );
  133. memset( buf, 0, sizeof( *buf ) );
  134. }
  135. /*
  136. * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
  137. *
  138. * \p buf must have been initialized and set up by calling
  139. * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
  140. *
  141. * \retval \p input_len, if the data fits.
  142. * \retval 0 <= value < \p input_len, if the data does not fit.
  143. * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
  144. * zero and \p input is NULL.
  145. */
  146. int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
  147. const unsigned char *input, size_t input_len )
  148. {
  149. size_t overflow = 0;
  150. if( ( buf == NULL ) || ( buf->buffer == NULL ) )
  151. return -1;
  152. /* Reduce input_len to a number that fits in the buffer. */
  153. if ( ( buf->content_length + input_len ) > buf->capacity )
  154. {
  155. input_len = buf->capacity - buf->content_length;
  156. }
  157. if( input == NULL )
  158. {
  159. return ( input_len == 0 ) ? 0 : -1;
  160. }
  161. /* Check if the buffer has not come full circle and free space is not in
  162. * the middle */
  163. if( buf->start + buf->content_length < buf->capacity )
  164. {
  165. /* Calculate the number of bytes that need to be placed at lower memory
  166. * address */
  167. if( buf->start + buf->content_length + input_len
  168. > buf->capacity )
  169. {
  170. overflow = ( buf->start + buf->content_length + input_len )
  171. % buf->capacity;
  172. }
  173. memcpy( buf->buffer + buf->start + buf->content_length, input,
  174. input_len - overflow );
  175. memcpy( buf->buffer, input + input_len - overflow, overflow );
  176. }
  177. else
  178. {
  179. /* The buffer has come full circle and free space is in the middle */
  180. memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
  181. input, input_len );
  182. }
  183. buf->content_length += input_len;
  184. return input_len;
  185. }
  186. /*
  187. * Gets \p output_len bytes from the ring buffer \p buf into the
  188. * \p output buffer. The output buffer can be NULL, in this case a part of the
  189. * ring buffer will be dropped, if the requested length is available.
  190. *
  191. * \p buf must have been initialized and set up by calling
  192. * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
  193. *
  194. * \retval \p output_len, if the data is available.
  195. * \retval 0 <= value < \p output_len, if the data is not available.
  196. * \retval -1, if \buf is NULL or it hasn't been set up.
  197. */
  198. int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
  199. unsigned char* output, size_t output_len )
  200. {
  201. size_t overflow = 0;
  202. if( ( buf == NULL ) || ( buf->buffer == NULL ) )
  203. return -1;
  204. if( output == NULL && output_len == 0 )
  205. return 0;
  206. if( buf->content_length < output_len )
  207. output_len = buf->content_length;
  208. /* Calculate the number of bytes that need to be drawn from lower memory
  209. * address */
  210. if( buf->start + output_len > buf->capacity )
  211. {
  212. overflow = ( buf->start + output_len ) % buf->capacity;
  213. }
  214. if( output != NULL )
  215. {
  216. memcpy( output, buf->buffer + buf->start, output_len - overflow );
  217. memcpy( output + output_len - overflow, buf->buffer, overflow );
  218. }
  219. buf->content_length -= output_len;
  220. buf->start = ( buf->start + output_len ) % buf->capacity;
  221. return output_len;
  222. }
  223. /*
  224. * Errors used in the message transport mock tests
  225. */
  226. #define MBEDTLS_TEST_ERROR_ARG_NULL -11
  227. #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
  228. /*
  229. * Context for a message metadata queue (fifo) that is on top of the ring buffer.
  230. */
  231. typedef struct mbedtls_test_message_queue
  232. {
  233. size_t *messages;
  234. int pos;
  235. int num;
  236. int capacity;
  237. } mbedtls_test_message_queue;
  238. /*
  239. * Setup and free functions for the message metadata queue.
  240. *
  241. * \p capacity describes the number of message metadata chunks that can be held
  242. * within the queue.
  243. *
  244. * \retval 0, if a metadata queue of a given length can be allocated.
  245. * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
  246. */
  247. int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
  248. size_t capacity )
  249. {
  250. queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
  251. if( NULL == queue->messages )
  252. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  253. queue->capacity = capacity;
  254. queue->pos = 0;
  255. queue->num = 0;
  256. return 0;
  257. }
  258. void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
  259. {
  260. if( queue == NULL )
  261. return;
  262. if( queue->messages != NULL )
  263. mbedtls_free( queue->messages );
  264. memset( queue, 0, sizeof( *queue ) );
  265. }
  266. /*
  267. * Push message length information onto the message metadata queue.
  268. * This will become the last element to leave it (fifo).
  269. *
  270. * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
  271. * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
  272. * \retval \p len, if the push was successful.
  273. */
  274. int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
  275. size_t len )
  276. {
  277. int place;
  278. if( queue == NULL )
  279. return MBEDTLS_TEST_ERROR_ARG_NULL;
  280. if( queue->num >= queue->capacity )
  281. return MBEDTLS_ERR_SSL_WANT_WRITE;
  282. place = ( queue->pos + queue->num ) % queue->capacity;
  283. queue->messages[place] = len;
  284. queue->num++;
  285. return len;
  286. }
  287. /*
  288. * Pop information about the next message length from the queue. This will be
  289. * the oldest inserted message length(fifo). \p msg_len can be null, in which
  290. * case the data will be popped from the queue but not copied anywhere.
  291. *
  292. * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
  293. * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
  294. * \retval message length, if the pop was successful, up to the given
  295. \p buf_len.
  296. */
  297. int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
  298. size_t buf_len )
  299. {
  300. size_t message_length;
  301. if( queue == NULL )
  302. return MBEDTLS_TEST_ERROR_ARG_NULL;
  303. if( queue->num == 0 )
  304. return MBEDTLS_ERR_SSL_WANT_READ;
  305. message_length = queue->messages[queue->pos];
  306. queue->messages[queue->pos] = 0;
  307. queue->num--;
  308. queue->pos++;
  309. queue->pos %= queue->capacity;
  310. if( queue->pos < 0 )
  311. queue->pos += queue->capacity;
  312. return ( message_length > buf_len ) ? buf_len : message_length;
  313. }
  314. /*
  315. * Take a peek on the info about the next message length from the queue.
  316. * This will be the oldest inserted message length(fifo).
  317. *
  318. * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
  319. * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
  320. * \retval 0, if the peek was successful.
  321. * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
  322. * too small to fit the message. In this case the \p msg_len will be
  323. * set to the full message length so that the
  324. * caller knows what portion of the message can be dropped.
  325. */
  326. int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
  327. size_t buf_len, size_t* msg_len )
  328. {
  329. if( queue == NULL || msg_len == NULL )
  330. return MBEDTLS_TEST_ERROR_ARG_NULL;
  331. if( queue->num == 0 )
  332. return MBEDTLS_ERR_SSL_WANT_READ;
  333. *msg_len = queue->messages[queue->pos];
  334. return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
  335. }
  336. /*
  337. * Context for the I/O callbacks simulating network connection.
  338. */
  339. #define MBEDTLS_MOCK_SOCKET_CONNECTED 1
  340. typedef struct mbedtls_mock_socket
  341. {
  342. int status;
  343. mbedtls_test_buffer *input;
  344. mbedtls_test_buffer *output;
  345. struct mbedtls_mock_socket *peer;
  346. } mbedtls_mock_socket;
  347. /*
  348. * Setup and teardown functions for mock sockets.
  349. */
  350. void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
  351. {
  352. memset( socket, 0, sizeof( *socket ) );
  353. }
  354. /*
  355. * Closes the socket \p socket.
  356. *
  357. * \p socket must have been previously initialized by calling
  358. * mbedtls_mock_socket_init().
  359. *
  360. * This function frees all allocated resources and both sockets are aware of the
  361. * new connection state.
  362. *
  363. * That is, this function does not simulate half-open TCP connections and the
  364. * phenomenon that when closing a UDP connection the peer is not aware of the
  365. * connection having been closed.
  366. */
  367. void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
  368. {
  369. if( socket == NULL )
  370. return;
  371. if( socket->input != NULL )
  372. {
  373. mbedtls_test_buffer_free( socket->input );
  374. mbedtls_free( socket->input );
  375. }
  376. if( socket->output != NULL )
  377. {
  378. mbedtls_test_buffer_free( socket->output );
  379. mbedtls_free( socket->output );
  380. }
  381. if( socket->peer != NULL )
  382. memset( socket->peer, 0, sizeof( *socket->peer ) );
  383. memset( socket, 0, sizeof( *socket ) );
  384. }
  385. /*
  386. * Establishes a connection between \p peer1 and \p peer2.
  387. *
  388. * \p peer1 and \p peer2 must have been previously initialized by calling
  389. * mbedtls_mock_socket_init().
  390. *
  391. * The capacites of the internal buffers are set to \p bufsize. Setting this to
  392. * the correct value allows for simulation of MTU, sanity testing the mock
  393. * implementation and mocking TCP connections with lower memory cost.
  394. */
  395. int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
  396. mbedtls_mock_socket* peer2,
  397. size_t bufsize )
  398. {
  399. int ret = -1;
  400. peer1->output =
  401. (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
  402. if( peer1->output == NULL )
  403. {
  404. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  405. goto exit;
  406. }
  407. mbedtls_test_buffer_init( peer1->output );
  408. if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
  409. {
  410. goto exit;
  411. }
  412. peer2->output =
  413. (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
  414. if( peer2->output == NULL )
  415. {
  416. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  417. goto exit;
  418. }
  419. mbedtls_test_buffer_init( peer2->output );
  420. if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
  421. {
  422. goto exit;
  423. }
  424. peer1->peer = peer2;
  425. peer2->peer = peer1;
  426. peer1->input = peer2->output;
  427. peer2->input = peer1->output;
  428. peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
  429. ret = 0;
  430. exit:
  431. if( ret != 0 )
  432. {
  433. mbedtls_mock_socket_close( peer1 );
  434. mbedtls_mock_socket_close( peer2 );
  435. }
  436. return ret;
  437. }
  438. /*
  439. * Callbacks for simulating blocking I/O over connection-oriented transport.
  440. */
  441. int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
  442. {
  443. mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
  444. if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
  445. return -1;
  446. return mbedtls_test_buffer_put( socket->output, buf, len );
  447. }
  448. int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
  449. {
  450. mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
  451. if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
  452. return -1;
  453. return mbedtls_test_buffer_get( socket->input, buf, len );
  454. }
  455. /*
  456. * Callbacks for simulating non-blocking I/O over connection-oriented transport.
  457. */
  458. int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
  459. {
  460. mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
  461. if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
  462. return -1;
  463. if( socket->output->capacity == socket->output->content_length )
  464. {
  465. return MBEDTLS_ERR_SSL_WANT_WRITE;
  466. }
  467. return mbedtls_test_buffer_put( socket->output, buf, len );
  468. }
  469. int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
  470. {
  471. mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
  472. if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
  473. return -1;
  474. if( socket->input->content_length == 0 )
  475. {
  476. return MBEDTLS_ERR_SSL_WANT_READ;
  477. }
  478. return mbedtls_test_buffer_get( socket->input, buf, len );
  479. }
  480. /* Errors used in the message socket mocks */
  481. #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
  482. #define MBEDTLS_TEST_ERROR_SEND_FAILED -66
  483. #define MBEDTLS_TEST_ERROR_RECV_FAILED -77
  484. /*
  485. * Structure used as an addon, or a wrapper, around the mocked sockets.
  486. * Contains an input queue, to which the other socket pushes metadata,
  487. * and an output queue, to which this one pushes metadata. This context is
  488. * considered as an owner of the input queue only, which is initialized and
  489. * freed in the respective setup and free calls.
  490. */
  491. typedef struct mbedtls_test_message_socket_context
  492. {
  493. mbedtls_test_message_queue* queue_input;
  494. mbedtls_test_message_queue* queue_output;
  495. mbedtls_mock_socket* socket;
  496. } mbedtls_test_message_socket_context;
  497. void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
  498. {
  499. ctx->queue_input = NULL;
  500. ctx->queue_output = NULL;
  501. ctx->socket = NULL;
  502. }
  503. /*
  504. * Setup a given mesasge socket context including initialization of
  505. * input/output queues to a chosen capacity of messages. Also set the
  506. * corresponding mock socket.
  507. *
  508. * \retval 0, if everything succeeds.
  509. * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
  510. * queue failed.
  511. */
  512. int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
  513. mbedtls_test_message_queue* queue_output,
  514. size_t queue_capacity,
  515. mbedtls_mock_socket* socket,
  516. mbedtls_test_message_socket_context* ctx )
  517. {
  518. int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
  519. if( ret != 0 )
  520. return ret;
  521. ctx->queue_input = queue_input;
  522. ctx->queue_output = queue_output;
  523. ctx->socket = socket;
  524. mbedtls_mock_socket_init( socket );
  525. return 0;
  526. }
  527. /*
  528. * Close a given message socket context, along with the socket itself. Free the
  529. * memory allocated by the input queue.
  530. */
  531. void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
  532. {
  533. if( ctx == NULL )
  534. return;
  535. mbedtls_test_message_queue_free( ctx->queue_input );
  536. mbedtls_mock_socket_close( ctx->socket );
  537. memset( ctx, 0, sizeof( *ctx ) );
  538. }
  539. /*
  540. * Send one message through a given message socket context.
  541. *
  542. * \retval \p len, if everything succeeds.
  543. * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
  544. * elements or the context itself is null.
  545. * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
  546. * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
  547. *
  548. * This function will also return any error from
  549. * mbedtls_test_message_queue_push_info.
  550. */
  551. int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
  552. {
  553. mbedtls_test_message_queue* queue;
  554. mbedtls_mock_socket* socket;
  555. mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
  556. if( context == NULL || context->socket == NULL
  557. || context->queue_output == NULL )
  558. {
  559. return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
  560. }
  561. queue = context->queue_output;
  562. socket = context->socket;
  563. if( queue->num >= queue->capacity )
  564. return MBEDTLS_ERR_SSL_WANT_WRITE;
  565. if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
  566. return MBEDTLS_TEST_ERROR_SEND_FAILED;
  567. return mbedtls_test_message_queue_push_info( queue, len );
  568. }
  569. /*
  570. * Receive one message from a given message socket context and return message
  571. * length or an error.
  572. *
  573. * \retval message length, if everything succeeds.
  574. * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
  575. * elements or the context itself is null.
  576. * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
  577. *
  578. * This function will also return any error other than
  579. * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
  580. */
  581. int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
  582. {
  583. mbedtls_test_message_queue* queue;
  584. mbedtls_mock_socket* socket;
  585. mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
  586. size_t drop_len = 0;
  587. size_t msg_len;
  588. int ret;
  589. if( context == NULL || context->socket == NULL
  590. || context->queue_input == NULL )
  591. {
  592. return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
  593. }
  594. queue = context->queue_input;
  595. socket = context->socket;
  596. /* Peek first, so that in case of a socket error the data remains in
  597. * the queue. */
  598. ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
  599. if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
  600. {
  601. /* Calculate how much to drop */
  602. drop_len = msg_len - buf_len;
  603. /* Set the requested message len to be buffer length */
  604. msg_len = buf_len;
  605. } else if( ret != 0 )
  606. {
  607. return ret;
  608. }
  609. if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
  610. return MBEDTLS_TEST_ERROR_RECV_FAILED;
  611. if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
  612. {
  613. /* Drop the remaining part of the message */
  614. if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
  615. {
  616. /* Inconsistent state - part of the message was read,
  617. * and a part couldn't. Not much we can do here, but it should not
  618. * happen in test environment, unless forced manually. */
  619. }
  620. }
  621. mbedtls_test_message_queue_pop_info( queue, buf_len );
  622. return msg_len;
  623. }
  624. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  625. defined(MBEDTLS_ENTROPY_C) && \
  626. defined(MBEDTLS_CTR_DRBG_C)
  627. /*
  628. * Structure with endpoint's certificates for SSL communication tests.
  629. */
  630. typedef struct mbedtls_endpoint_certificate
  631. {
  632. mbedtls_x509_crt ca_cert;
  633. mbedtls_x509_crt cert;
  634. mbedtls_pk_context pkey;
  635. } mbedtls_endpoint_certificate;
  636. /*
  637. * Endpoint structure for SSL communication tests.
  638. */
  639. typedef struct mbedtls_endpoint
  640. {
  641. const char *name;
  642. mbedtls_ssl_context ssl;
  643. mbedtls_ssl_config conf;
  644. mbedtls_ctr_drbg_context ctr_drbg;
  645. mbedtls_entropy_context entropy;
  646. mbedtls_mock_socket socket;
  647. mbedtls_endpoint_certificate cert;
  648. } mbedtls_endpoint;
  649. /*
  650. * Initializes \p ep_cert structure and assigns it to endpoint
  651. * represented by \p ep.
  652. *
  653. * \retval 0 on success, otherwise error code.
  654. */
  655. int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
  656. {
  657. int i = 0;
  658. int ret = -1;
  659. mbedtls_endpoint_certificate *cert;
  660. if( ep == NULL )
  661. {
  662. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  663. }
  664. cert = &( ep->cert );
  665. mbedtls_x509_crt_init( &( cert->ca_cert ) );
  666. mbedtls_x509_crt_init( &( cert->cert ) );
  667. mbedtls_pk_init( &( cert->pkey ) );
  668. /* Load the trusted CA */
  669. for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
  670. {
  671. ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
  672. (const unsigned char *) mbedtls_test_cas_der[i],
  673. mbedtls_test_cas_der_len[i] );
  674. TEST_ASSERT( ret == 0 );
  675. }
  676. /* Load own certificate and private key */
  677. if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
  678. {
  679. if( pk_alg == MBEDTLS_PK_RSA )
  680. {
  681. ret = mbedtls_x509_crt_parse( &( cert->cert ),
  682. (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
  683. mbedtls_test_srv_crt_rsa_sha256_der_len );
  684. TEST_ASSERT( ret == 0 );
  685. ret = mbedtls_pk_parse_key( &( cert->pkey ),
  686. (const unsigned char*) mbedtls_test_srv_key_rsa_der,
  687. mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
  688. TEST_ASSERT( ret == 0 );
  689. }
  690. else
  691. {
  692. ret = mbedtls_x509_crt_parse( &( cert->cert ),
  693. (const unsigned char*) mbedtls_test_srv_crt_ec_der,
  694. mbedtls_test_srv_crt_ec_der_len );
  695. TEST_ASSERT( ret == 0 );
  696. ret = mbedtls_pk_parse_key( &( cert->pkey ),
  697. (const unsigned char*) mbedtls_test_srv_key_ec_der,
  698. mbedtls_test_srv_key_ec_der_len, NULL, 0 );
  699. TEST_ASSERT( ret == 0 );
  700. }
  701. }
  702. else
  703. {
  704. if( pk_alg == MBEDTLS_PK_RSA )
  705. {
  706. ret = mbedtls_x509_crt_parse( &( cert->cert ),
  707. (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
  708. mbedtls_test_cli_crt_rsa_der_len );
  709. TEST_ASSERT( ret == 0 );
  710. ret = mbedtls_pk_parse_key( &( cert->pkey ),
  711. (const unsigned char *) mbedtls_test_cli_key_rsa_der,
  712. mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
  713. TEST_ASSERT( ret == 0 );
  714. }
  715. else
  716. {
  717. ret = mbedtls_x509_crt_parse( &( cert->cert ),
  718. (const unsigned char *) mbedtls_test_cli_crt_ec_der,
  719. mbedtls_test_cli_crt_ec_len );
  720. TEST_ASSERT( ret == 0 );
  721. ret = mbedtls_pk_parse_key( &( cert->pkey ),
  722. (const unsigned char *) mbedtls_test_cli_key_ec_der,
  723. mbedtls_test_cli_key_ec_der_len, NULL, 0 );
  724. TEST_ASSERT( ret == 0 );
  725. }
  726. }
  727. mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
  728. ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
  729. &( cert->pkey ) );
  730. TEST_ASSERT( ret == 0 );
  731. exit:
  732. if( ret != 0 )
  733. {
  734. mbedtls_x509_crt_free( &( cert->ca_cert ) );
  735. mbedtls_x509_crt_free( &( cert->cert ) );
  736. mbedtls_pk_free( &( cert->pkey ) );
  737. }
  738. return ret;
  739. }
  740. /*
  741. * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
  742. * after calling this function even if it fails.
  743. *
  744. * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
  745. * MBEDTLS_SSL_IS_CLIENT.
  746. * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
  747. * MBEDTLS_PK_ECDSA are supported.
  748. * \p dtls_context - in case of DTLS - this is the context handling metadata.
  749. * \p input_queue - used only in case of DTLS.
  750. * \p output_queue - used only in case of DTLS.
  751. *
  752. * \retval 0 on success, otherwise error code.
  753. */
  754. int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
  755. mbedtls_test_message_socket_context *dtls_context,
  756. mbedtls_test_message_queue *input_queue,
  757. mbedtls_test_message_queue *output_queue )
  758. {
  759. int ret = -1;
  760. if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
  761. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  762. if( ep == NULL )
  763. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  764. memset( ep, 0, sizeof( *ep ) );
  765. ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
  766. mbedtls_ssl_init( &( ep->ssl ) );
  767. mbedtls_ssl_config_init( &( ep->conf ) );
  768. mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
  769. mbedtls_ssl_conf_rng( &( ep->conf ),
  770. mbedtls_ctr_drbg_random,
  771. &( ep->ctr_drbg ) );
  772. mbedtls_entropy_init( &( ep->entropy ) );
  773. if( dtls_context != NULL )
  774. {
  775. TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
  776. 100, &( ep->socket ),
  777. dtls_context ) == 0 );
  778. }
  779. else
  780. {
  781. mbedtls_mock_socket_init( &( ep->socket ) );
  782. }
  783. ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
  784. &( ep->entropy ), (const unsigned char *) ( ep->name ),
  785. strlen( ep->name ) );
  786. TEST_ASSERT( ret == 0 );
  787. /* Non-blocking callbacks without timeout */
  788. if( dtls_context != NULL )
  789. {
  790. mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
  791. mbedtls_mock_tcp_send_msg,
  792. mbedtls_mock_tcp_recv_msg,
  793. NULL );
  794. }
  795. else
  796. {
  797. mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
  798. mbedtls_mock_tcp_send_nb,
  799. mbedtls_mock_tcp_recv_nb,
  800. NULL );
  801. }
  802. ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
  803. ( dtls_context != NULL ) ?
  804. MBEDTLS_SSL_TRANSPORT_DATAGRAM :
  805. MBEDTLS_SSL_TRANSPORT_STREAM,
  806. MBEDTLS_SSL_PRESET_DEFAULT );
  807. TEST_ASSERT( ret == 0 );
  808. ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
  809. TEST_ASSERT( ret == 0 );
  810. #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
  811. if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
  812. mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
  813. #endif
  814. ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
  815. TEST_ASSERT( ret == 0 );
  816. exit:
  817. return ret;
  818. }
  819. /*
  820. * Deinitializes certificates from endpoint represented by \p ep.
  821. */
  822. void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
  823. {
  824. mbedtls_endpoint_certificate *cert = &( ep->cert );
  825. mbedtls_x509_crt_free( &( cert->ca_cert ) );
  826. mbedtls_x509_crt_free( &( cert->cert ) );
  827. mbedtls_pk_free( &( cert->pkey ) );
  828. }
  829. /*
  830. * Deinitializes endpoint represented by \p ep.
  831. */
  832. void mbedtls_endpoint_free( mbedtls_endpoint *ep,
  833. mbedtls_test_message_socket_context *context )
  834. {
  835. mbedtls_endpoint_certificate_free( ep );
  836. mbedtls_ssl_free( &( ep->ssl ) );
  837. mbedtls_ssl_config_free( &( ep->conf ) );
  838. mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
  839. mbedtls_entropy_free( &( ep->entropy ) );
  840. if( context != NULL )
  841. {
  842. mbedtls_message_socket_close( context );
  843. }
  844. else
  845. {
  846. mbedtls_mock_socket_close( &( ep->socket ) );
  847. }
  848. }
  849. /*
  850. * This function moves ssl handshake from \p ssl to prescribed \p state.
  851. * /p second_ssl is used as second endpoint and their sockets have to be
  852. * connected before calling this function.
  853. *
  854. * \retval 0 on success, otherwise error code.
  855. */
  856. int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
  857. mbedtls_ssl_context *second_ssl,
  858. int state )
  859. {
  860. enum { BUFFSIZE = 1024 };
  861. int max_steps = 1000;
  862. int ret = 0;
  863. if( ssl == NULL || second_ssl == NULL )
  864. {
  865. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  866. }
  867. /* Perform communication via connected sockets */
  868. while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
  869. {
  870. /* If /p second_ssl ends the handshake procedure before /p ssl then
  871. * there is no need to call the next step */
  872. if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
  873. {
  874. ret = mbedtls_ssl_handshake_step( second_ssl );
  875. if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
  876. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  877. {
  878. return ret;
  879. }
  880. }
  881. /* We only care about the \p ssl state and returns, so we call it last,
  882. * to leave the iteration as soon as the state is as expected. */
  883. ret = mbedtls_ssl_handshake_step( ssl );
  884. if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
  885. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  886. {
  887. return ret;
  888. }
  889. }
  890. return ( max_steps >= 0 ) ? ret : -1;
  891. }
  892. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
  893. /*
  894. * Write application data. Increase write counter if necessary.
  895. */
  896. int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
  897. int buf_len, int *written,
  898. const int expected_fragments )
  899. {
  900. int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
  901. if( ret > 0 )
  902. {
  903. *written += ret;
  904. }
  905. if( expected_fragments == 0 )
  906. {
  907. /* Used for DTLS and the message size larger than MFL. In that case
  908. * the message can not be fragmented and the library should return
  909. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
  910. * to prevent a dead loop inside mbedtls_exchange_data(). */
  911. return ret;
  912. }
  913. else if( expected_fragments == 1 )
  914. {
  915. /* Used for TLS/DTLS and the message size lower than MFL */
  916. TEST_ASSERT( ret == buf_len ||
  917. ret == MBEDTLS_ERR_SSL_WANT_READ ||
  918. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  919. }
  920. else
  921. {
  922. /* Used for TLS and the message size larger than MFL */
  923. TEST_ASSERT( expected_fragments > 1 );
  924. TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
  925. ret == MBEDTLS_ERR_SSL_WANT_READ ||
  926. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  927. }
  928. return 0;
  929. exit:
  930. /* Some of the tests failed */
  931. return -1;
  932. }
  933. /*
  934. * Read application data and increase read counter and fragments counter if necessary.
  935. */
  936. int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
  937. int buf_len, int *read,
  938. int *fragments, const int expected_fragments )
  939. {
  940. int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
  941. if( ret > 0 )
  942. {
  943. ( *fragments )++;
  944. *read += ret;
  945. }
  946. if( expected_fragments == 0 )
  947. {
  948. TEST_ASSERT( ret == 0 );
  949. }
  950. else if( expected_fragments == 1 )
  951. {
  952. TEST_ASSERT( ret == buf_len ||
  953. ret == MBEDTLS_ERR_SSL_WANT_READ ||
  954. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  955. }
  956. else
  957. {
  958. TEST_ASSERT( expected_fragments > 1 );
  959. TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
  960. ret == MBEDTLS_ERR_SSL_WANT_READ ||
  961. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  962. }
  963. return 0;
  964. exit:
  965. /* Some of the tests failed */
  966. return -1;
  967. }
  968. /*
  969. * Helper function setting up inverse record transformations
  970. * using given cipher, hash, EtM mode, authentication tag length,
  971. * and version.
  972. */
  973. #define CHK( x ) \
  974. do \
  975. { \
  976. if( !( x ) ) \
  977. { \
  978. ret = -1; \
  979. goto cleanup; \
  980. } \
  981. } while( 0 )
  982. void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
  983. int* forced_ciphersuite )
  984. {
  985. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  986. forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
  987. forced_ciphersuite[1] = 0;
  988. ciphersuite_info =
  989. mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
  990. TEST_ASSERT( ciphersuite_info != NULL );
  991. TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
  992. TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
  993. if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
  994. {
  995. conf->max_minor_ver = ciphersuite_info->max_minor_ver;
  996. }
  997. if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
  998. {
  999. conf->min_minor_ver = ciphersuite_info->min_minor_ver;
  1000. }
  1001. mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
  1002. exit:
  1003. return;
  1004. }
  1005. int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
  1006. const unsigned char *name, size_t name_len )
  1007. {
  1008. (void) p_info;
  1009. (void) ssl;
  1010. (void) name;
  1011. (void) name_len;
  1012. return ( 0 );
  1013. }
  1014. #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
  1015. #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
  1016. #else
  1017. #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
  1018. #endif
  1019. static int build_transforms( mbedtls_ssl_transform *t_in,
  1020. mbedtls_ssl_transform *t_out,
  1021. int cipher_type, int hash_id,
  1022. int etm, int tag_mode, int ver,
  1023. size_t cid0_len,
  1024. size_t cid1_len )
  1025. {
  1026. mbedtls_cipher_info_t const *cipher_info;
  1027. int ret = 0;
  1028. size_t keylen, maclen, ivlen;
  1029. unsigned char *key0 = NULL, *key1 = NULL;
  1030. unsigned char *md0 = NULL, *md1 = NULL;
  1031. unsigned char iv_enc[16], iv_dec[16];
  1032. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1033. unsigned char cid0[ SSL_CID_LEN_MIN ];
  1034. unsigned char cid1[ SSL_CID_LEN_MIN ];
  1035. mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
  1036. mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
  1037. #else
  1038. ((void) cid0_len);
  1039. ((void) cid1_len);
  1040. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1041. maclen = 0;
  1042. /* Pick cipher */
  1043. cipher_info = mbedtls_cipher_info_from_type( cipher_type );
  1044. CHK( cipher_info != NULL );
  1045. CHK( cipher_info->iv_size <= 16 );
  1046. CHK( cipher_info->key_bitlen % 8 == 0 );
  1047. /* Pick keys */
  1048. keylen = cipher_info->key_bitlen / 8;
  1049. /* Allocate `keylen + 1` bytes to ensure that we get
  1050. * a non-NULL pointers from `mbedtls_calloc` even if
  1051. * `keylen == 0` in the case of the NULL cipher. */
  1052. CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
  1053. CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
  1054. memset( key0, 0x1, keylen );
  1055. memset( key1, 0x2, keylen );
  1056. /* Setup cipher contexts */
  1057. CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
  1058. CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
  1059. CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
  1060. CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
  1061. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  1062. if( cipher_info->mode == MBEDTLS_MODE_CBC )
  1063. {
  1064. CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
  1065. MBEDTLS_PADDING_NONE ) == 0 );
  1066. CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
  1067. MBEDTLS_PADDING_NONE ) == 0 );
  1068. CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
  1069. MBEDTLS_PADDING_NONE ) == 0 );
  1070. CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
  1071. MBEDTLS_PADDING_NONE ) == 0 );
  1072. }
  1073. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  1074. CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
  1075. keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
  1076. CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
  1077. keylen << 3, MBEDTLS_DECRYPT ) == 0 );
  1078. CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
  1079. keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
  1080. CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
  1081. keylen << 3, MBEDTLS_DECRYPT ) == 0 );
  1082. /* Setup MAC contexts */
  1083. #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  1084. if( cipher_info->mode == MBEDTLS_MODE_CBC ||
  1085. cipher_info->mode == MBEDTLS_MODE_STREAM )
  1086. {
  1087. mbedtls_md_info_t const *md_info;
  1088. /* Pick hash */
  1089. md_info = mbedtls_md_info_from_type( hash_id );
  1090. CHK( md_info != NULL );
  1091. /* Pick hash keys */
  1092. maclen = mbedtls_md_get_size( md_info );
  1093. CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
  1094. CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
  1095. memset( md0, 0x5, maclen );
  1096. memset( md1, 0x6, maclen );
  1097. CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
  1098. CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
  1099. CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
  1100. CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
  1101. if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
  1102. {
  1103. CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
  1104. md0, maclen ) == 0 );
  1105. CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
  1106. md1, maclen ) == 0 );
  1107. CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
  1108. md1, maclen ) == 0 );
  1109. CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
  1110. md0, maclen ) == 0 );
  1111. }
  1112. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  1113. else
  1114. {
  1115. memcpy( &t_in->mac_enc, md0, maclen );
  1116. memcpy( &t_in->mac_dec, md1, maclen );
  1117. memcpy( &t_out->mac_enc, md1, maclen );
  1118. memcpy( &t_out->mac_dec, md0, maclen );
  1119. }
  1120. #endif
  1121. }
  1122. #else
  1123. ((void) hash_id);
  1124. #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
  1125. /* Pick IV's (regardless of whether they
  1126. * are being used by the transform). */
  1127. ivlen = cipher_info->iv_size;
  1128. memset( iv_enc, 0x3, sizeof( iv_enc ) );
  1129. memset( iv_dec, 0x4, sizeof( iv_dec ) );
  1130. /*
  1131. * Setup transforms
  1132. */
  1133. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
  1134. defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
  1135. t_out->encrypt_then_mac = etm;
  1136. t_in->encrypt_then_mac = etm;
  1137. #else
  1138. ((void) etm);
  1139. #endif
  1140. t_out->minor_ver = ver;
  1141. t_in->minor_ver = ver;
  1142. t_out->ivlen = ivlen;
  1143. t_in->ivlen = ivlen;
  1144. switch( cipher_info->mode )
  1145. {
  1146. case MBEDTLS_MODE_GCM:
  1147. case MBEDTLS_MODE_CCM:
  1148. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1149. if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
  1150. {
  1151. t_out->fixed_ivlen = 12;
  1152. t_in->fixed_ivlen = 12;
  1153. }
  1154. else
  1155. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1156. {
  1157. t_out->fixed_ivlen = 4;
  1158. t_in->fixed_ivlen = 4;
  1159. }
  1160. t_out->maclen = 0;
  1161. t_in->maclen = 0;
  1162. switch( tag_mode )
  1163. {
  1164. case 0: /* Full tag */
  1165. t_out->taglen = 16;
  1166. t_in->taglen = 16;
  1167. break;
  1168. case 1: /* Partial tag */
  1169. t_out->taglen = 8;
  1170. t_in->taglen = 8;
  1171. break;
  1172. default:
  1173. ret = 1;
  1174. goto cleanup;
  1175. }
  1176. break;
  1177. case MBEDTLS_MODE_CHACHAPOLY:
  1178. t_out->fixed_ivlen = 12;
  1179. t_in->fixed_ivlen = 12;
  1180. t_out->maclen = 0;
  1181. t_in->maclen = 0;
  1182. switch( tag_mode )
  1183. {
  1184. case 0: /* Full tag */
  1185. t_out->taglen = 16;
  1186. t_in->taglen = 16;
  1187. break;
  1188. case 1: /* Partial tag */
  1189. t_out->taglen = 8;
  1190. t_in->taglen = 8;
  1191. break;
  1192. default:
  1193. ret = 1;
  1194. goto cleanup;
  1195. }
  1196. break;
  1197. case MBEDTLS_MODE_STREAM:
  1198. case MBEDTLS_MODE_CBC:
  1199. t_out->fixed_ivlen = 0; /* redundant, must be 0 */
  1200. t_in->fixed_ivlen = 0; /* redundant, must be 0 */
  1201. t_out->taglen = 0;
  1202. t_in->taglen = 0;
  1203. switch( tag_mode )
  1204. {
  1205. case 0: /* Full tag */
  1206. t_out->maclen = maclen;
  1207. t_in->maclen = maclen;
  1208. break;
  1209. case 1: /* Partial tag */
  1210. t_out->maclen = 10;
  1211. t_in->maclen = 10;
  1212. break;
  1213. default:
  1214. ret = 1;
  1215. goto cleanup;
  1216. }
  1217. break;
  1218. default:
  1219. ret = 1;
  1220. goto cleanup;
  1221. break;
  1222. }
  1223. /* Setup IV's */
  1224. memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
  1225. memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
  1226. memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
  1227. memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
  1228. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1229. /* Add CID */
  1230. memcpy( &t_in->in_cid, cid0, cid0_len );
  1231. memcpy( &t_in->out_cid, cid1, cid1_len );
  1232. t_in->in_cid_len = cid0_len;
  1233. t_in->out_cid_len = cid1_len;
  1234. memcpy( &t_out->in_cid, cid1, cid1_len );
  1235. memcpy( &t_out->out_cid, cid0, cid0_len );
  1236. t_out->in_cid_len = cid1_len;
  1237. t_out->out_cid_len = cid0_len;
  1238. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1239. cleanup:
  1240. mbedtls_free( key0 );
  1241. mbedtls_free( key1 );
  1242. mbedtls_free( md0 );
  1243. mbedtls_free( md1 );
  1244. return( ret );
  1245. }
  1246. /*
  1247. * Populate a session structure for serialization tests.
  1248. * Choose dummy values, mostly non-0 to distinguish from the init default.
  1249. */
  1250. static int ssl_populate_session( mbedtls_ssl_session *session,
  1251. int ticket_len,
  1252. const char *crt_file )
  1253. {
  1254. #if defined(MBEDTLS_HAVE_TIME)
  1255. session->start = mbedtls_time( NULL ) - 42;
  1256. #endif
  1257. session->ciphersuite = 0xabcd;
  1258. session->compression = 1;
  1259. session->id_len = sizeof( session->id );
  1260. memset( session->id, 66, session->id_len );
  1261. memset( session->master, 17, sizeof( session->master ) );
  1262. #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
  1263. if( strlen( crt_file ) != 0 )
  1264. {
  1265. mbedtls_x509_crt tmp_crt;
  1266. int ret;
  1267. mbedtls_x509_crt_init( &tmp_crt );
  1268. ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
  1269. if( ret != 0 )
  1270. return( ret );
  1271. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1272. /* Move temporary CRT. */
  1273. session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
  1274. if( session->peer_cert == NULL )
  1275. return( -1 );
  1276. *session->peer_cert = tmp_crt;
  1277. memset( &tmp_crt, 0, sizeof( tmp_crt ) );
  1278. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1279. /* Calculate digest of temporary CRT. */
  1280. session->peer_cert_digest =
  1281. mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
  1282. if( session->peer_cert_digest == NULL )
  1283. return( -1 );
  1284. ret = mbedtls_md( mbedtls_md_info_from_type(
  1285. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
  1286. tmp_crt.raw.p, tmp_crt.raw.len,
  1287. session->peer_cert_digest );
  1288. if( ret != 0 )
  1289. return( ret );
  1290. session->peer_cert_digest_type =
  1291. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
  1292. session->peer_cert_digest_len =
  1293. MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
  1294. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1295. mbedtls_x509_crt_free( &tmp_crt );
  1296. }
  1297. #else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
  1298. (void) crt_file;
  1299. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
  1300. session->verify_result = 0xdeadbeef;
  1301. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  1302. if( ticket_len != 0 )
  1303. {
  1304. session->ticket = mbedtls_calloc( 1, ticket_len );
  1305. if( session->ticket == NULL )
  1306. return( -1 );
  1307. memset( session->ticket, 33, ticket_len );
  1308. }
  1309. session->ticket_len = ticket_len;
  1310. session->ticket_lifetime = 86401;
  1311. #else
  1312. (void) ticket_len;
  1313. #endif
  1314. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1315. session->mfl_code = 1;
  1316. #endif
  1317. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1318. session->trunc_hmac = 1;
  1319. #endif
  1320. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1321. session->encrypt_then_mac = 1;
  1322. #endif
  1323. return( 0 );
  1324. }
  1325. /*
  1326. * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
  1327. * message was sent in the correct number of fragments.
  1328. *
  1329. * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
  1330. * of them must be initialized and connected beforehand.
  1331. * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
  1332. * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
  1333. * fragments the message should be sent.
  1334. * expected_fragments is 0: can be used for DTLS testing while the message
  1335. * size is larger than MFL. In that case the message
  1336. * cannot be fragmented and sent to the second endpoint.
  1337. * This value can be used for negative tests.
  1338. * expected_fragments is 1: can be used for TLS/DTLS testing while the
  1339. * message size is below MFL
  1340. * expected_fragments > 1: can be used for TLS testing while the message
  1341. * size is larger than MFL
  1342. *
  1343. * \retval 0 on success, otherwise error code.
  1344. */
  1345. int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
  1346. int msg_len_1, const int expected_fragments_1,
  1347. mbedtls_ssl_context *ssl_2,
  1348. int msg_len_2, const int expected_fragments_2 )
  1349. {
  1350. unsigned char *msg_buf_1 = malloc( msg_len_1 );
  1351. unsigned char *msg_buf_2 = malloc( msg_len_2 );
  1352. unsigned char *in_buf_1 = malloc( msg_len_2 );
  1353. unsigned char *in_buf_2 = malloc( msg_len_1 );
  1354. int msg_type, ret = -1;
  1355. /* Perform this test with two message types. At first use a message
  1356. * consisting of only 0x00 for the client and only 0xFF for the server.
  1357. * At the second time use message with generated data */
  1358. for( msg_type = 0; msg_type < 2; msg_type++ )
  1359. {
  1360. int written_1 = 0;
  1361. int written_2 = 0;
  1362. int read_1 = 0;
  1363. int read_2 = 0;
  1364. int fragments_1 = 0;
  1365. int fragments_2 = 0;
  1366. if( msg_type == 0 )
  1367. {
  1368. memset( msg_buf_1, 0x00, msg_len_1 );
  1369. memset( msg_buf_2, 0xff, msg_len_2 );
  1370. }
  1371. else
  1372. {
  1373. int i, j = 0;
  1374. for( i = 0; i < msg_len_1; i++ )
  1375. {
  1376. msg_buf_1[i] = j++ & 0xFF;
  1377. }
  1378. for( i = 0; i < msg_len_2; i++ )
  1379. {
  1380. msg_buf_2[i] = ( j -= 5 ) & 0xFF;
  1381. }
  1382. }
  1383. while( read_1 < msg_len_2 || read_2 < msg_len_1 )
  1384. {
  1385. /* ssl_1 sending */
  1386. if( msg_len_1 > written_1 )
  1387. {
  1388. ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
  1389. msg_len_1, &written_1,
  1390. expected_fragments_1 );
  1391. if( expected_fragments_1 == 0 )
  1392. {
  1393. /* This error is expected when the message is too large and
  1394. * cannot be fragmented */
  1395. TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1396. msg_len_1 = 0;
  1397. }
  1398. else
  1399. {
  1400. TEST_ASSERT( ret == 0 );
  1401. }
  1402. }
  1403. /* ssl_2 sending */
  1404. if( msg_len_2 > written_2 )
  1405. {
  1406. ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
  1407. msg_len_2, &written_2,
  1408. expected_fragments_2 );
  1409. if( expected_fragments_2 == 0 )
  1410. {
  1411. /* This error is expected when the message is too large and
  1412. * cannot be fragmented */
  1413. TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1414. msg_len_2 = 0;
  1415. }
  1416. else
  1417. {
  1418. TEST_ASSERT( ret == 0 );
  1419. }
  1420. }
  1421. /* ssl_1 reading */
  1422. if( read_1 < msg_len_2 )
  1423. {
  1424. ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
  1425. msg_len_2, &read_1,
  1426. &fragments_2,
  1427. expected_fragments_2 );
  1428. TEST_ASSERT( ret == 0 );
  1429. }
  1430. /* ssl_2 reading */
  1431. if( read_2 < msg_len_1 )
  1432. {
  1433. ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
  1434. msg_len_1, &read_2,
  1435. &fragments_1,
  1436. expected_fragments_1 );
  1437. TEST_ASSERT( ret == 0 );
  1438. }
  1439. }
  1440. ret = -1;
  1441. TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
  1442. TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
  1443. TEST_ASSERT( fragments_1 == expected_fragments_1 );
  1444. TEST_ASSERT( fragments_2 == expected_fragments_2 );
  1445. }
  1446. ret = 0;
  1447. exit:
  1448. free( msg_buf_1 );
  1449. free( in_buf_1 );
  1450. free( msg_buf_2 );
  1451. free( in_buf_2 );
  1452. return ret;
  1453. }
  1454. /*
  1455. * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
  1456. * must be initialized and connected beforehand.
  1457. *
  1458. * \retval 0 on success, otherwise error code.
  1459. */
  1460. int exchange_data( mbedtls_ssl_context *ssl_1,
  1461. mbedtls_ssl_context *ssl_2 )
  1462. {
  1463. return mbedtls_exchange_data( ssl_1, 256, 1,
  1464. ssl_2, 256, 1 );
  1465. }
  1466. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  1467. defined(MBEDTLS_ENTROPY_C) && \
  1468. defined(MBEDTLS_CTR_DRBG_C)
  1469. void perform_handshake( handshake_test_options* options )
  1470. {
  1471. /* forced_ciphersuite needs to last until the end of the handshake */
  1472. int forced_ciphersuite[2];
  1473. enum { BUFFSIZE = 17000 };
  1474. mbedtls_endpoint client, server;
  1475. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1476. const char *psk_identity = "foo";
  1477. #endif
  1478. #if defined(MBEDTLS_TIMING_C)
  1479. mbedtls_timing_delay_context timer_client, timer_server;
  1480. #endif
  1481. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  1482. unsigned char *context_buf = NULL;
  1483. size_t context_buf_len;
  1484. #endif
  1485. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1486. int ret = -1;
  1487. #endif
  1488. int expected_handshake_result = 0;
  1489. mbedtls_test_message_queue server_queue, client_queue;
  1490. mbedtls_test_message_socket_context server_context, client_context;
  1491. mbedtls_message_socket_init( &server_context );
  1492. mbedtls_message_socket_init( &client_context );
  1493. /* Client side */
  1494. if( options->dtls != 0 )
  1495. {
  1496. TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
  1497. options->pk_alg, &client_context,
  1498. &client_queue,
  1499. &server_queue ) == 0 );
  1500. #if defined(MBEDTLS_TIMING_C)
  1501. mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
  1502. mbedtls_timing_set_delay,
  1503. mbedtls_timing_get_delay );
  1504. #endif
  1505. }
  1506. else
  1507. {
  1508. TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
  1509. options->pk_alg, NULL, NULL,
  1510. NULL ) == 0 );
  1511. }
  1512. if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
  1513. {
  1514. mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1515. options->client_min_version );
  1516. }
  1517. if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
  1518. {
  1519. mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1520. options->client_max_version );
  1521. }
  1522. if( strlen( options->cipher ) > 0 )
  1523. {
  1524. set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
  1525. }
  1526. #if defined (MBEDTLS_DEBUG_C)
  1527. if( options->cli_log_fun )
  1528. {
  1529. mbedtls_debug_set_threshold( 4 );
  1530. mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
  1531. options->cli_log_obj );
  1532. }
  1533. #endif
  1534. /* Server side */
  1535. if( options->dtls != 0 )
  1536. {
  1537. TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
  1538. options->pk_alg, &server_context,
  1539. &server_queue,
  1540. &client_queue) == 0 );
  1541. #if defined(MBEDTLS_TIMING_C)
  1542. mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
  1543. mbedtls_timing_set_delay,
  1544. mbedtls_timing_get_delay );
  1545. #endif
  1546. }
  1547. else
  1548. {
  1549. TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
  1550. options->pk_alg, NULL, NULL, NULL ) == 0 );
  1551. }
  1552. mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
  1553. if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
  1554. {
  1555. mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1556. options->server_min_version );
  1557. }
  1558. if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
  1559. {
  1560. mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1561. options->server_max_version );
  1562. }
  1563. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1564. TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
  1565. (unsigned char) options->mfl ) == 0 );
  1566. TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
  1567. (unsigned char) options->mfl ) == 0 );
  1568. #else
  1569. TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
  1570. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1571. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1572. if( options->psk_str != NULL && options->psk_str->len > 0 )
  1573. {
  1574. TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
  1575. options->psk_str->len,
  1576. (const unsigned char *) psk_identity,
  1577. strlen( psk_identity ) ) == 0 );
  1578. TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
  1579. options->psk_str->len,
  1580. (const unsigned char *) psk_identity,
  1581. strlen( psk_identity ) ) == 0 );
  1582. mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
  1583. }
  1584. #endif
  1585. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1586. if( options->renegotiate )
  1587. {
  1588. mbedtls_ssl_conf_renegotiation( &(server.conf),
  1589. MBEDTLS_SSL_RENEGOTIATION_ENABLED );
  1590. mbedtls_ssl_conf_renegotiation( &(client.conf),
  1591. MBEDTLS_SSL_RENEGOTIATION_ENABLED );
  1592. mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
  1593. options->legacy_renegotiation );
  1594. mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
  1595. options->legacy_renegotiation );
  1596. }
  1597. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1598. #if defined (MBEDTLS_DEBUG_C)
  1599. if( options->srv_log_fun )
  1600. {
  1601. mbedtls_debug_set_threshold( 4 );
  1602. mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
  1603. options->srv_log_obj );
  1604. }
  1605. #endif
  1606. TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
  1607. &(server.socket),
  1608. BUFFSIZE ) == 0 );
  1609. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  1610. if( options->resize_buffers != 0 )
  1611. {
  1612. /* Ensure that the buffer sizes are appropriate before resizes */
  1613. TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
  1614. TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
  1615. TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
  1616. TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
  1617. }
  1618. #endif
  1619. if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
  1620. {
  1621. expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
  1622. }
  1623. TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
  1624. &(server.ssl),
  1625. MBEDTLS_SSL_HANDSHAKE_OVER )
  1626. == expected_handshake_result );
  1627. if( expected_handshake_result != 0 )
  1628. {
  1629. /* Connection will have failed by this point, skip to cleanup */
  1630. goto exit;
  1631. }
  1632. TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
  1633. TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
  1634. /* Check that we agree on the version... */
  1635. TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
  1636. /* And check that the version negotiated is the expected one. */
  1637. TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
  1638. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  1639. if( options->resize_buffers != 0 )
  1640. {
  1641. if( options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
  1642. options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1 )
  1643. {
  1644. /* A server, when using DTLS, might delay a buffer resize to happen
  1645. * after it receives a message, so we force it. */
  1646. TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
  1647. TEST_ASSERT( client.ssl.out_buf_len ==
  1648. mbedtls_ssl_get_output_buflen( &client.ssl ) );
  1649. TEST_ASSERT( client.ssl.in_buf_len ==
  1650. mbedtls_ssl_get_input_buflen( &client.ssl ) );
  1651. TEST_ASSERT( server.ssl.out_buf_len ==
  1652. mbedtls_ssl_get_output_buflen( &server.ssl ) );
  1653. TEST_ASSERT( server.ssl.in_buf_len ==
  1654. mbedtls_ssl_get_input_buflen( &server.ssl ) );
  1655. }
  1656. }
  1657. #endif
  1658. if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
  1659. {
  1660. /* Start data exchanging test */
  1661. TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
  1662. options->expected_cli_fragments,
  1663. &(server.ssl), options->srv_msg_len,
  1664. options->expected_srv_fragments )
  1665. == 0 );
  1666. }
  1667. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  1668. if( options->serialize == 1 )
  1669. {
  1670. TEST_ASSERT( options->dtls == 1 );
  1671. TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
  1672. 0, &context_buf_len )
  1673. == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  1674. context_buf = mbedtls_calloc( 1, context_buf_len );
  1675. TEST_ASSERT( context_buf != NULL );
  1676. TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
  1677. context_buf_len,
  1678. &context_buf_len ) == 0 );
  1679. mbedtls_ssl_free( &(server.ssl) );
  1680. mbedtls_ssl_init( &(server.ssl) );
  1681. TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
  1682. mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
  1683. mbedtls_mock_tcp_send_msg,
  1684. mbedtls_mock_tcp_recv_msg,
  1685. NULL );
  1686. #if defined(MBEDTLS_TIMING_C)
  1687. mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
  1688. mbedtls_timing_set_delay,
  1689. mbedtls_timing_get_delay );
  1690. #endif
  1691. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  1692. if( options->resize_buffers != 0 )
  1693. {
  1694. /* Ensure that the buffer sizes are appropriate before resizes */
  1695. TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
  1696. TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
  1697. }
  1698. #endif
  1699. TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
  1700. context_buf_len ) == 0 );
  1701. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  1702. /* Validate buffer sizes after context deserialization */
  1703. if( options->resize_buffers != 0 )
  1704. {
  1705. TEST_ASSERT( server.ssl.out_buf_len ==
  1706. mbedtls_ssl_get_output_buflen( &server.ssl ) );
  1707. TEST_ASSERT( server.ssl.in_buf_len ==
  1708. mbedtls_ssl_get_input_buflen( &server.ssl ) );
  1709. }
  1710. #endif
  1711. /* Retest writing/reading */
  1712. if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
  1713. {
  1714. TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
  1715. options->cli_msg_len,
  1716. options->expected_cli_fragments,
  1717. &(server.ssl),
  1718. options->srv_msg_len,
  1719. options->expected_srv_fragments )
  1720. == 0 );
  1721. }
  1722. }
  1723. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  1724. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1725. if( options->renegotiate )
  1726. {
  1727. /* Start test with renegotiation */
  1728. TEST_ASSERT( server.ssl.renego_status ==
  1729. MBEDTLS_SSL_INITIAL_HANDSHAKE );
  1730. TEST_ASSERT( client.ssl.renego_status ==
  1731. MBEDTLS_SSL_INITIAL_HANDSHAKE );
  1732. /* After calling this function for the server, it only sends a handshake
  1733. * request. All renegotiation should happen during data exchanging */
  1734. TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
  1735. TEST_ASSERT( server.ssl.renego_status ==
  1736. MBEDTLS_SSL_RENEGOTIATION_PENDING );
  1737. TEST_ASSERT( client.ssl.renego_status ==
  1738. MBEDTLS_SSL_INITIAL_HANDSHAKE );
  1739. TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
  1740. TEST_ASSERT( server.ssl.renego_status ==
  1741. MBEDTLS_SSL_RENEGOTIATION_DONE );
  1742. TEST_ASSERT( client.ssl.renego_status ==
  1743. MBEDTLS_SSL_RENEGOTIATION_DONE );
  1744. /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
  1745. * should happen inside this function. However in this test, we cannot
  1746. * perform simultaneous communication betwen client and server so this
  1747. * function will return waiting error on the socket. All rest of
  1748. * renegotiation should happen during data exchanging */
  1749. ret = mbedtls_ssl_renegotiate( &(client.ssl) );
  1750. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  1751. if( options->resize_buffers != 0 )
  1752. {
  1753. /* Ensure that the buffer sizes are appropriate before resizes */
  1754. TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
  1755. TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
  1756. }
  1757. #endif
  1758. TEST_ASSERT( ret == 0 ||
  1759. ret == MBEDTLS_ERR_SSL_WANT_READ ||
  1760. ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  1761. TEST_ASSERT( server.ssl.renego_status ==
  1762. MBEDTLS_SSL_RENEGOTIATION_DONE );
  1763. TEST_ASSERT( client.ssl.renego_status ==
  1764. MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
  1765. TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
  1766. TEST_ASSERT( server.ssl.renego_status ==
  1767. MBEDTLS_SSL_RENEGOTIATION_DONE );
  1768. TEST_ASSERT( client.ssl.renego_status ==
  1769. MBEDTLS_SSL_RENEGOTIATION_DONE );
  1770. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  1771. /* Validate buffer sizes after renegotiation */
  1772. if( options->resize_buffers != 0 )
  1773. {
  1774. TEST_ASSERT( client.ssl.out_buf_len ==
  1775. mbedtls_ssl_get_output_buflen( &client.ssl ) );
  1776. TEST_ASSERT( client.ssl.in_buf_len ==
  1777. mbedtls_ssl_get_input_buflen( &client.ssl ) );
  1778. TEST_ASSERT( server.ssl.out_buf_len ==
  1779. mbedtls_ssl_get_output_buflen( &server.ssl ) );
  1780. TEST_ASSERT( server.ssl.in_buf_len ==
  1781. mbedtls_ssl_get_input_buflen( &server.ssl ) );
  1782. }
  1783. #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
  1784. }
  1785. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1786. exit:
  1787. mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
  1788. mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
  1789. #if defined (MBEDTLS_DEBUG_C)
  1790. if( options->cli_log_fun || options->srv_log_fun )
  1791. {
  1792. mbedtls_debug_set_threshold( 0 );
  1793. }
  1794. #endif
  1795. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  1796. if( context_buf != NULL )
  1797. mbedtls_free( context_buf );
  1798. #endif
  1799. }
  1800. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
  1801. /* END_HEADER */
  1802. /* BEGIN_DEPENDENCIES
  1803. * depends_on:MBEDTLS_SSL_TLS_C
  1804. * END_DEPENDENCIES
  1805. */
  1806. /* BEGIN_CASE */
  1807. void test_callback_buffer_sanity()
  1808. {
  1809. enum { MSGLEN = 10 };
  1810. mbedtls_test_buffer buf;
  1811. unsigned char input[MSGLEN];
  1812. unsigned char output[MSGLEN];
  1813. memset( input, 0, sizeof(input) );
  1814. /* Make sure calling put and get on NULL buffer results in error. */
  1815. TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
  1816. == -1 );
  1817. TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
  1818. == -1 );
  1819. TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
  1820. TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
  1821. TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
  1822. /* Make sure calling put and get on a buffer that hasn't been set up results
  1823. * in eror. */
  1824. mbedtls_test_buffer_init( &buf );
  1825. TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
  1826. TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
  1827. == -1 );
  1828. TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
  1829. TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
  1830. TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
  1831. /* Make sure calling put and get on NULL input only results in
  1832. * error if the length is not zero, and that a NULL output is valid for data
  1833. * dropping.
  1834. */
  1835. TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
  1836. TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
  1837. TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
  1838. == 0 );
  1839. TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
  1840. TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
  1841. /* Make sure calling put several times in the row is safe */
  1842. TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
  1843. == sizeof( input ) );
  1844. TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
  1845. TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
  1846. TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
  1847. TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
  1848. exit:
  1849. mbedtls_test_buffer_free( &buf );
  1850. }
  1851. /* END_CASE */
  1852. /*
  1853. * Test if the implementation of `mbedtls_test_buffer` related functions is
  1854. * correct and works as expected.
  1855. *
  1856. * That is
  1857. * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
  1858. * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
  1859. * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
  1860. * bytes.
  1861. * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
  1862. * - All of the bytes we got match the bytes we put in in a FIFO manner.
  1863. */
  1864. /* BEGIN_CASE */
  1865. void test_callback_buffer( int size, int put1, int put1_ret,
  1866. int get1, int get1_ret, int put2, int put2_ret,
  1867. int get2, int get2_ret )
  1868. {
  1869. enum { ROUNDS = 2 };
  1870. size_t put[ROUNDS];
  1871. int put_ret[ROUNDS];
  1872. size_t get[ROUNDS];
  1873. int get_ret[ROUNDS];
  1874. mbedtls_test_buffer buf;
  1875. unsigned char* input = NULL;
  1876. size_t input_len;
  1877. unsigned char* output = NULL;
  1878. size_t output_len;
  1879. size_t i, j, written, read;
  1880. mbedtls_test_buffer_init( &buf );
  1881. TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
  1882. /* Check the sanity of input parameters and initialise local variables. That
  1883. * is, ensure that the amount of data is not negative and that we are not
  1884. * expecting more to put or get than we actually asked for. */
  1885. TEST_ASSERT( put1 >= 0 );
  1886. put[0] = put1;
  1887. put_ret[0] = put1_ret;
  1888. TEST_ASSERT( put1_ret <= put1 );
  1889. TEST_ASSERT( put2 >= 0 );
  1890. put[1] = put2;
  1891. put_ret[1] = put2_ret;
  1892. TEST_ASSERT( put2_ret <= put2 );
  1893. TEST_ASSERT( get1 >= 0 );
  1894. get[0] = get1;
  1895. get_ret[0] = get1_ret;
  1896. TEST_ASSERT( get1_ret <= get1 );
  1897. TEST_ASSERT( get2 >= 0 );
  1898. get[1] = get2;
  1899. get_ret[1] = get2_ret;
  1900. TEST_ASSERT( get2_ret <= get2 );
  1901. input_len = 0;
  1902. /* Calculate actual input and output lengths */
  1903. for( j = 0; j < ROUNDS; j++ )
  1904. {
  1905. if( put_ret[j] > 0 )
  1906. {
  1907. input_len += put_ret[j];
  1908. }
  1909. }
  1910. /* In order to always have a valid pointer we always allocate at least 1
  1911. * byte. */
  1912. if( input_len == 0 )
  1913. input_len = 1;
  1914. ASSERT_ALLOC( input, input_len );
  1915. output_len = 0;
  1916. for( j = 0; j < ROUNDS; j++ )
  1917. {
  1918. if( get_ret[j] > 0 )
  1919. {
  1920. output_len += get_ret[j];
  1921. }
  1922. }
  1923. TEST_ASSERT( output_len <= input_len );
  1924. /* In order to always have a valid pointer we always allocate at least 1
  1925. * byte. */
  1926. if( output_len == 0 )
  1927. output_len = 1;
  1928. ASSERT_ALLOC( output, output_len );
  1929. /* Fill up the buffer with structured data so that unwanted changes
  1930. * can be detected */
  1931. for( i = 0; i < input_len; i++ )
  1932. {
  1933. input[i] = i & 0xFF;
  1934. }
  1935. written = read = 0;
  1936. for( j = 0; j < ROUNDS; j++ )
  1937. {
  1938. TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
  1939. input + written, put[j] ) );
  1940. written += put_ret[j];
  1941. TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
  1942. output + read, get[j] ) );
  1943. read += get_ret[j];
  1944. TEST_ASSERT( read <= written );
  1945. if( get_ret[j] > 0 )
  1946. {
  1947. TEST_ASSERT( memcmp( output + read - get_ret[j],
  1948. input + read - get_ret[j], get_ret[j] )
  1949. == 0 );
  1950. }
  1951. }
  1952. exit:
  1953. mbedtls_free( input );
  1954. mbedtls_free( output );
  1955. mbedtls_test_buffer_free( &buf );
  1956. }
  1957. /* END_CASE */
  1958. /*
  1959. * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
  1960. * correct and works as expected on unconnected sockets.
  1961. */
  1962. /* BEGIN_CASE */
  1963. void ssl_mock_sanity( )
  1964. {
  1965. enum { MSGLEN = 105 };
  1966. unsigned char message[MSGLEN] = { 0 };
  1967. unsigned char received[MSGLEN] = { 0 };
  1968. mbedtls_mock_socket socket;
  1969. mbedtls_mock_socket_init( &socket );
  1970. TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
  1971. mbedtls_mock_socket_close( &socket );
  1972. mbedtls_mock_socket_init( &socket );
  1973. TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
  1974. mbedtls_mock_socket_close( &socket );
  1975. mbedtls_mock_socket_init( &socket );
  1976. TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
  1977. mbedtls_mock_socket_close( &socket );
  1978. mbedtls_mock_socket_init( &socket );
  1979. TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
  1980. mbedtls_mock_socket_close( &socket );
  1981. exit:
  1982. mbedtls_mock_socket_close( &socket );
  1983. }
  1984. /* END_CASE */
  1985. /*
  1986. * Test if the implementation of `mbedtls_mock_socket` related functions can
  1987. * send a single message from the client to the server.
  1988. */
  1989. /* BEGIN_CASE */
  1990. void ssl_mock_tcp( int blocking )
  1991. {
  1992. enum { MSGLEN = 105 };
  1993. enum { BUFLEN = MSGLEN / 5 };
  1994. unsigned char message[MSGLEN];
  1995. unsigned char received[MSGLEN];
  1996. mbedtls_mock_socket client;
  1997. mbedtls_mock_socket server;
  1998. size_t written, read;
  1999. int send_ret, recv_ret;
  2000. mbedtls_ssl_send_t *send;
  2001. mbedtls_ssl_recv_t *recv;
  2002. unsigned i;
  2003. if( blocking == 0 )
  2004. {
  2005. send = mbedtls_mock_tcp_send_nb;
  2006. recv = mbedtls_mock_tcp_recv_nb;
  2007. }
  2008. else
  2009. {
  2010. send = mbedtls_mock_tcp_send_b;
  2011. recv = mbedtls_mock_tcp_recv_b;
  2012. }
  2013. mbedtls_mock_socket_init( &client );
  2014. mbedtls_mock_socket_init( &server );
  2015. /* Fill up the buffer with structured data so that unwanted changes
  2016. * can be detected */
  2017. for( i = 0; i < MSGLEN; i++ )
  2018. {
  2019. message[i] = i & 0xFF;
  2020. }
  2021. /* Make sure that sending a message takes a few iterations. */
  2022. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
  2023. /* Send the message to the server */
  2024. send_ret = recv_ret = 1;
  2025. written = read = 0;
  2026. while( send_ret != 0 || recv_ret != 0 )
  2027. {
  2028. send_ret = send( &client, message + written, MSGLEN - written );
  2029. TEST_ASSERT( send_ret >= 0 );
  2030. TEST_ASSERT( send_ret <= BUFLEN );
  2031. written += send_ret;
  2032. /* If the buffer is full we can test blocking and non-blocking send */
  2033. if ( send_ret == BUFLEN )
  2034. {
  2035. int blocking_ret = send( &client, message , 1 );
  2036. if ( blocking )
  2037. {
  2038. TEST_ASSERT( blocking_ret == 0 );
  2039. }
  2040. else
  2041. {
  2042. TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  2043. }
  2044. }
  2045. recv_ret = recv( &server, received + read, MSGLEN - read );
  2046. /* The result depends on whether any data was sent */
  2047. if ( send_ret > 0 )
  2048. {
  2049. TEST_ASSERT( recv_ret > 0 );
  2050. TEST_ASSERT( recv_ret <= BUFLEN );
  2051. read += recv_ret;
  2052. }
  2053. else if( blocking )
  2054. {
  2055. TEST_ASSERT( recv_ret == 0 );
  2056. }
  2057. else
  2058. {
  2059. TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
  2060. recv_ret = 0;
  2061. }
  2062. /* If the buffer is empty we can test blocking and non-blocking read */
  2063. if ( recv_ret == BUFLEN )
  2064. {
  2065. int blocking_ret = recv( &server, received, 1 );
  2066. if ( blocking )
  2067. {
  2068. TEST_ASSERT( blocking_ret == 0 );
  2069. }
  2070. else
  2071. {
  2072. TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
  2073. }
  2074. }
  2075. }
  2076. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2077. exit:
  2078. mbedtls_mock_socket_close( &client );
  2079. mbedtls_mock_socket_close( &server );
  2080. }
  2081. /* END_CASE */
  2082. /*
  2083. * Test if the implementation of `mbedtls_mock_socket` related functions can
  2084. * send messages in both direction at the same time (with the I/O calls
  2085. * interleaving).
  2086. */
  2087. /* BEGIN_CASE */
  2088. void ssl_mock_tcp_interleaving( int blocking )
  2089. {
  2090. enum { ROUNDS = 2 };
  2091. enum { MSGLEN = 105 };
  2092. enum { BUFLEN = MSGLEN / 5 };
  2093. unsigned char message[ROUNDS][MSGLEN];
  2094. unsigned char received[ROUNDS][MSGLEN];
  2095. mbedtls_mock_socket client;
  2096. mbedtls_mock_socket server;
  2097. size_t written[ROUNDS];
  2098. size_t read[ROUNDS];
  2099. int send_ret[ROUNDS];
  2100. int recv_ret[ROUNDS];
  2101. unsigned i, j, progress;
  2102. mbedtls_ssl_send_t *send;
  2103. mbedtls_ssl_recv_t *recv;
  2104. if( blocking == 0 )
  2105. {
  2106. send = mbedtls_mock_tcp_send_nb;
  2107. recv = mbedtls_mock_tcp_recv_nb;
  2108. }
  2109. else
  2110. {
  2111. send = mbedtls_mock_tcp_send_b;
  2112. recv = mbedtls_mock_tcp_recv_b;
  2113. }
  2114. mbedtls_mock_socket_init( &client );
  2115. mbedtls_mock_socket_init( &server );
  2116. /* Fill up the buffers with structured data so that unwanted changes
  2117. * can be detected */
  2118. for( i = 0; i < ROUNDS; i++ )
  2119. {
  2120. for( j = 0; j < MSGLEN; j++ )
  2121. {
  2122. message[i][j] = ( i * MSGLEN + j ) & 0xFF;
  2123. }
  2124. }
  2125. /* Make sure that sending a message takes a few iterations. */
  2126. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
  2127. /* Send the message from both sides, interleaving. */
  2128. progress = 1;
  2129. for( i = 0; i < ROUNDS; i++ )
  2130. {
  2131. written[i] = 0;
  2132. read[i] = 0;
  2133. }
  2134. /* This loop does not stop as long as there was a successful write or read
  2135. * of at least one byte on either side. */
  2136. while( progress != 0 )
  2137. {
  2138. mbedtls_mock_socket *socket;
  2139. for( i = 0; i < ROUNDS; i++ )
  2140. {
  2141. /* First sending is from the client */
  2142. socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
  2143. send_ret[i] = send( socket, message[i] + written[i],
  2144. MSGLEN - written[i] );
  2145. TEST_ASSERT( send_ret[i] >= 0 );
  2146. TEST_ASSERT( send_ret[i] <= BUFLEN );
  2147. written[i] += send_ret[i];
  2148. /* If the buffer is full we can test blocking and non-blocking
  2149. * send */
  2150. if ( send_ret[i] == BUFLEN )
  2151. {
  2152. int blocking_ret = send( socket, message[i] , 1 );
  2153. if ( blocking )
  2154. {
  2155. TEST_ASSERT( blocking_ret == 0 );
  2156. }
  2157. else
  2158. {
  2159. TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  2160. }
  2161. }
  2162. }
  2163. for( i = 0; i < ROUNDS; i++ )
  2164. {
  2165. /* First receiving is from the server */
  2166. socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
  2167. recv_ret[i] = recv( socket, received[i] + read[i],
  2168. MSGLEN - read[i] );
  2169. /* The result depends on whether any data was sent */
  2170. if ( send_ret[i] > 0 )
  2171. {
  2172. TEST_ASSERT( recv_ret[i] > 0 );
  2173. TEST_ASSERT( recv_ret[i] <= BUFLEN );
  2174. read[i] += recv_ret[i];
  2175. }
  2176. else if( blocking )
  2177. {
  2178. TEST_ASSERT( recv_ret[i] == 0 );
  2179. }
  2180. else
  2181. {
  2182. TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
  2183. recv_ret[i] = 0;
  2184. }
  2185. /* If the buffer is empty we can test blocking and non-blocking
  2186. * read */
  2187. if ( recv_ret[i] == BUFLEN )
  2188. {
  2189. int blocking_ret = recv( socket, received[i], 1 );
  2190. if ( blocking )
  2191. {
  2192. TEST_ASSERT( blocking_ret == 0 );
  2193. }
  2194. else
  2195. {
  2196. TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
  2197. }
  2198. }
  2199. }
  2200. progress = 0;
  2201. for( i = 0; i < ROUNDS; i++ )
  2202. {
  2203. progress += send_ret[i] + recv_ret[i];
  2204. }
  2205. }
  2206. for( i = 0; i < ROUNDS; i++ )
  2207. TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
  2208. exit:
  2209. mbedtls_mock_socket_close( &client );
  2210. mbedtls_mock_socket_close( &server );
  2211. }
  2212. /* END_CASE */
  2213. /* BEGIN_CASE */
  2214. void ssl_message_queue_sanity( )
  2215. {
  2216. mbedtls_test_message_queue queue;
  2217. /* Trying to push/pull to an empty queue */
  2218. TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
  2219. == MBEDTLS_TEST_ERROR_ARG_NULL );
  2220. TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
  2221. == MBEDTLS_TEST_ERROR_ARG_NULL );
  2222. TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
  2223. TEST_ASSERT( queue.capacity == 3 );
  2224. TEST_ASSERT( queue.num == 0 );
  2225. exit:
  2226. mbedtls_test_message_queue_free( &queue );
  2227. }
  2228. /* END_CASE */
  2229. /* BEGIN_CASE */
  2230. void ssl_message_queue_basic( )
  2231. {
  2232. mbedtls_test_message_queue queue;
  2233. TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
  2234. /* Sanity test - 3 pushes and 3 pops with sufficient space */
  2235. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
  2236. TEST_ASSERT( queue.capacity == 3 );
  2237. TEST_ASSERT( queue.num == 1 );
  2238. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
  2239. TEST_ASSERT( queue.capacity == 3 );
  2240. TEST_ASSERT( queue.num == 2 );
  2241. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
  2242. TEST_ASSERT( queue.capacity == 3 );
  2243. TEST_ASSERT( queue.num == 3 );
  2244. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
  2245. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
  2246. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
  2247. exit:
  2248. mbedtls_test_message_queue_free( &queue );
  2249. }
  2250. /* END_CASE */
  2251. /* BEGIN_CASE */
  2252. void ssl_message_queue_overflow_underflow( )
  2253. {
  2254. mbedtls_test_message_queue queue;
  2255. TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
  2256. /* 4 pushes (last one with an error), 4 pops (last one with an error) */
  2257. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
  2258. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
  2259. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
  2260. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
  2261. == MBEDTLS_ERR_SSL_WANT_WRITE );
  2262. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
  2263. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
  2264. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
  2265. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
  2266. == MBEDTLS_ERR_SSL_WANT_READ );
  2267. exit:
  2268. mbedtls_test_message_queue_free( &queue );
  2269. }
  2270. /* END_CASE */
  2271. /* BEGIN_CASE */
  2272. void ssl_message_queue_interleaved( )
  2273. {
  2274. mbedtls_test_message_queue queue;
  2275. TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
  2276. /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
  2277. * (to wrap around the buffer) */
  2278. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
  2279. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
  2280. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
  2281. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
  2282. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
  2283. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
  2284. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
  2285. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
  2286. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
  2287. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
  2288. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
  2289. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
  2290. exit:
  2291. mbedtls_test_message_queue_free( &queue );
  2292. }
  2293. /* END_CASE */
  2294. /* BEGIN_CASE */
  2295. void ssl_message_queue_insufficient_buffer( )
  2296. {
  2297. mbedtls_test_message_queue queue;
  2298. size_t message_len = 10;
  2299. size_t buffer_len = 5;
  2300. TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
  2301. /* Popping without a sufficient buffer */
  2302. TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
  2303. == (int) message_len );
  2304. TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
  2305. == (int) buffer_len );
  2306. exit:
  2307. mbedtls_test_message_queue_free( &queue );
  2308. }
  2309. /* END_CASE */
  2310. /* BEGIN_CASE */
  2311. void ssl_message_mock_uninitialized( )
  2312. {
  2313. enum { MSGLEN = 10 };
  2314. unsigned char message[MSGLEN] = {0}, received[MSGLEN];
  2315. mbedtls_mock_socket client, server;
  2316. mbedtls_test_message_queue server_queue, client_queue;
  2317. mbedtls_test_message_socket_context server_context, client_context;
  2318. mbedtls_message_socket_init( &server_context );
  2319. mbedtls_message_socket_init( &client_context );
  2320. /* Send with a NULL context */
  2321. TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
  2322. == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
  2323. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
  2324. == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
  2325. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
  2326. &server,
  2327. &server_context ) == 0 );
  2328. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
  2329. &client,
  2330. &client_context ) == 0 );
  2331. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
  2332. == MBEDTLS_TEST_ERROR_SEND_FAILED );
  2333. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2334. == MBEDTLS_ERR_SSL_WANT_READ );
  2335. /* Push directly to a queue to later simulate a disconnected behavior */
  2336. TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
  2337. == MSGLEN );
  2338. /* Test if there's an error when trying to read from a disconnected
  2339. * socket */
  2340. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2341. == MBEDTLS_TEST_ERROR_RECV_FAILED );
  2342. exit:
  2343. mbedtls_message_socket_close( &server_context );
  2344. mbedtls_message_socket_close( &client_context );
  2345. }
  2346. /* END_CASE */
  2347. /* BEGIN_CASE */
  2348. void ssl_message_mock_basic( )
  2349. {
  2350. enum { MSGLEN = 10 };
  2351. unsigned char message[MSGLEN], received[MSGLEN];
  2352. mbedtls_mock_socket client, server;
  2353. unsigned i;
  2354. mbedtls_test_message_queue server_queue, client_queue;
  2355. mbedtls_test_message_socket_context server_context, client_context;
  2356. mbedtls_message_socket_init( &server_context );
  2357. mbedtls_message_socket_init( &client_context );
  2358. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
  2359. &server,
  2360. &server_context ) == 0 );
  2361. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
  2362. &client,
  2363. &client_context ) == 0 );
  2364. /* Fill up the buffer with structured data so that unwanted changes
  2365. * can be detected */
  2366. for( i = 0; i < MSGLEN; i++ )
  2367. {
  2368. message[i] = i & 0xFF;
  2369. }
  2370. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2371. MSGLEN ) );
  2372. /* Send the message to the server */
  2373. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2374. MSGLEN ) == MSGLEN );
  2375. /* Read from the server */
  2376. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2377. == MSGLEN );
  2378. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2379. memset( received, 0, MSGLEN );
  2380. /* Send the message to the client */
  2381. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
  2382. MSGLEN ) == MSGLEN );
  2383. /* Read from the client */
  2384. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
  2385. == MSGLEN );
  2386. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2387. exit:
  2388. mbedtls_message_socket_close( &server_context );
  2389. mbedtls_message_socket_close( &client_context );
  2390. }
  2391. /* END_CASE */
  2392. /* BEGIN_CASE */
  2393. void ssl_message_mock_queue_overflow_underflow( )
  2394. {
  2395. enum { MSGLEN = 10 };
  2396. unsigned char message[MSGLEN], received[MSGLEN];
  2397. mbedtls_mock_socket client, server;
  2398. unsigned i;
  2399. mbedtls_test_message_queue server_queue, client_queue;
  2400. mbedtls_test_message_socket_context server_context, client_context;
  2401. mbedtls_message_socket_init( &server_context );
  2402. mbedtls_message_socket_init( &client_context );
  2403. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
  2404. &server,
  2405. &server_context ) == 0 );
  2406. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
  2407. &client,
  2408. &client_context ) == 0 );
  2409. /* Fill up the buffer with structured data so that unwanted changes
  2410. * can be detected */
  2411. for( i = 0; i < MSGLEN; i++ )
  2412. {
  2413. message[i] = i & 0xFF;
  2414. }
  2415. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2416. MSGLEN*2 ) );
  2417. /* Send three message to the server, last one with an error */
  2418. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2419. MSGLEN - 1 ) == MSGLEN - 1 );
  2420. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2421. MSGLEN ) == MSGLEN );
  2422. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2423. MSGLEN )
  2424. == MBEDTLS_ERR_SSL_WANT_WRITE );
  2425. /* Read three messages from the server, last one with an error */
  2426. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
  2427. MSGLEN - 1 ) == MSGLEN - 1 );
  2428. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2429. == MSGLEN );
  2430. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2431. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2432. == MBEDTLS_ERR_SSL_WANT_READ );
  2433. exit:
  2434. mbedtls_message_socket_close( &server_context );
  2435. mbedtls_message_socket_close( &client_context );
  2436. }
  2437. /* END_CASE */
  2438. /* BEGIN_CASE */
  2439. void ssl_message_mock_socket_overflow( )
  2440. {
  2441. enum { MSGLEN = 10 };
  2442. unsigned char message[MSGLEN], received[MSGLEN];
  2443. mbedtls_mock_socket client, server;
  2444. unsigned i;
  2445. mbedtls_test_message_queue server_queue, client_queue;
  2446. mbedtls_test_message_socket_context server_context, client_context;
  2447. mbedtls_message_socket_init( &server_context );
  2448. mbedtls_message_socket_init( &client_context );
  2449. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
  2450. &server,
  2451. &server_context ) == 0 );
  2452. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
  2453. &client,
  2454. &client_context ) == 0 );
  2455. /* Fill up the buffer with structured data so that unwanted changes
  2456. * can be detected */
  2457. for( i = 0; i < MSGLEN; i++ )
  2458. {
  2459. message[i] = i & 0xFF;
  2460. }
  2461. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2462. MSGLEN ) );
  2463. /* Send two message to the server, second one with an error */
  2464. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2465. MSGLEN ) == MSGLEN );
  2466. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2467. MSGLEN )
  2468. == MBEDTLS_TEST_ERROR_SEND_FAILED );
  2469. /* Read the only message from the server */
  2470. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2471. == MSGLEN );
  2472. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2473. exit:
  2474. mbedtls_message_socket_close( &server_context );
  2475. mbedtls_message_socket_close( &client_context );
  2476. }
  2477. /* END_CASE */
  2478. /* BEGIN_CASE */
  2479. void ssl_message_mock_truncated( )
  2480. {
  2481. enum { MSGLEN = 10 };
  2482. unsigned char message[MSGLEN], received[MSGLEN];
  2483. mbedtls_mock_socket client, server;
  2484. unsigned i;
  2485. mbedtls_test_message_queue server_queue, client_queue;
  2486. mbedtls_test_message_socket_context server_context, client_context;
  2487. mbedtls_message_socket_init( &server_context );
  2488. mbedtls_message_socket_init( &client_context );
  2489. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
  2490. &server,
  2491. &server_context ) == 0 );
  2492. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
  2493. &client,
  2494. &client_context ) == 0 );
  2495. memset( received, 0, MSGLEN );
  2496. /* Fill up the buffer with structured data so that unwanted changes
  2497. * can be detected */
  2498. for( i = 0; i < MSGLEN; i++ )
  2499. {
  2500. message[i] = i & 0xFF;
  2501. }
  2502. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2503. 2 * MSGLEN ) );
  2504. /* Send two messages to the server, the second one small enough to fit in the
  2505. * receiver's buffer. */
  2506. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2507. MSGLEN ) == MSGLEN );
  2508. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2509. MSGLEN / 2 ) == MSGLEN / 2 );
  2510. /* Read a truncated message from the server */
  2511. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
  2512. == MSGLEN/2 );
  2513. /* Test that the first half of the message is valid, and second one isn't */
  2514. TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
  2515. TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
  2516. != 0 );
  2517. memset( received, 0, MSGLEN );
  2518. /* Read a full message from the server */
  2519. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
  2520. == MSGLEN / 2 );
  2521. /* Test that the first half of the message is valid */
  2522. TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
  2523. exit:
  2524. mbedtls_message_socket_close( &server_context );
  2525. mbedtls_message_socket_close( &client_context );
  2526. }
  2527. /* END_CASE */
  2528. /* BEGIN_CASE */
  2529. void ssl_message_mock_socket_read_error( )
  2530. {
  2531. enum { MSGLEN = 10 };
  2532. unsigned char message[MSGLEN], received[MSGLEN];
  2533. mbedtls_mock_socket client, server;
  2534. unsigned i;
  2535. mbedtls_test_message_queue server_queue, client_queue;
  2536. mbedtls_test_message_socket_context server_context, client_context;
  2537. mbedtls_message_socket_init( &server_context );
  2538. mbedtls_message_socket_init( &client_context );
  2539. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
  2540. &server,
  2541. &server_context ) == 0 );
  2542. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
  2543. &client,
  2544. &client_context ) == 0 );
  2545. /* Fill up the buffer with structured data so that unwanted changes
  2546. * can be detected */
  2547. for( i = 0; i < MSGLEN; i++ )
  2548. {
  2549. message[i] = i & 0xFF;
  2550. }
  2551. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2552. MSGLEN ) );
  2553. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2554. MSGLEN ) == MSGLEN );
  2555. /* Force a read error by disconnecting the socket by hand */
  2556. server.status = 0;
  2557. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2558. == MBEDTLS_TEST_ERROR_RECV_FAILED );
  2559. /* Return to a valid state */
  2560. server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
  2561. memset( received, 0, sizeof( received ) );
  2562. /* Test that even though the server tried to read once disconnected, the
  2563. * continuity is preserved */
  2564. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2565. == MSGLEN );
  2566. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2567. exit:
  2568. mbedtls_message_socket_close( &server_context );
  2569. mbedtls_message_socket_close( &client_context );
  2570. }
  2571. /* END_CASE */
  2572. /* BEGIN_CASE */
  2573. void ssl_message_mock_interleaved_one_way( )
  2574. {
  2575. enum { MSGLEN = 10 };
  2576. unsigned char message[MSGLEN], received[MSGLEN];
  2577. mbedtls_mock_socket client, server;
  2578. unsigned i;
  2579. mbedtls_test_message_queue server_queue, client_queue;
  2580. mbedtls_test_message_socket_context server_context, client_context;
  2581. mbedtls_message_socket_init( &server_context );
  2582. mbedtls_message_socket_init( &client_context );
  2583. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
  2584. &server,
  2585. &server_context ) == 0 );
  2586. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
  2587. &client,
  2588. &client_context ) == 0 );
  2589. /* Fill up the buffer with structured data so that unwanted changes
  2590. * can be detected */
  2591. for( i = 0; i < MSGLEN; i++ )
  2592. {
  2593. message[i] = i & 0xFF;
  2594. }
  2595. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2596. MSGLEN*3 ) );
  2597. /* Interleaved test - [2 sends, 1 read] twice, and then two reads
  2598. * (to wrap around the buffer) */
  2599. for( i = 0; i < 2; i++ )
  2600. {
  2601. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2602. MSGLEN ) == MSGLEN );
  2603. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2604. MSGLEN ) == MSGLEN );
  2605. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
  2606. MSGLEN ) == MSGLEN );
  2607. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2608. memset( received, 0, sizeof( received ) );
  2609. }
  2610. for( i = 0; i < 2; i++ )
  2611. {
  2612. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
  2613. MSGLEN ) == MSGLEN );
  2614. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2615. }
  2616. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2617. == MBEDTLS_ERR_SSL_WANT_READ );
  2618. exit:
  2619. mbedtls_message_socket_close( &server_context );
  2620. mbedtls_message_socket_close( &client_context );
  2621. }
  2622. /* END_CASE */
  2623. /* BEGIN_CASE */
  2624. void ssl_message_mock_interleaved_two_ways( )
  2625. {
  2626. enum { MSGLEN = 10 };
  2627. unsigned char message[MSGLEN], received[MSGLEN];
  2628. mbedtls_mock_socket client, server;
  2629. unsigned i;
  2630. mbedtls_test_message_queue server_queue, client_queue;
  2631. mbedtls_test_message_socket_context server_context, client_context;
  2632. mbedtls_message_socket_init( &server_context );
  2633. mbedtls_message_socket_init( &client_context );
  2634. TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
  2635. &server,
  2636. &server_context ) == 0 );
  2637. TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
  2638. &client,
  2639. &client_context ) == 0 );
  2640. /* Fill up the buffer with structured data so that unwanted changes
  2641. * can be detected */
  2642. for( i = 0; i < MSGLEN; i++ )
  2643. {
  2644. message[i] = i & 0xFF;
  2645. }
  2646. TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
  2647. MSGLEN*3 ) );
  2648. /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
  2649. * (to wrap around the buffer) both ways. */
  2650. for( i = 0; i < 2; i++ )
  2651. {
  2652. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2653. MSGLEN ) == MSGLEN );
  2654. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
  2655. MSGLEN ) == MSGLEN );
  2656. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
  2657. MSGLEN ) == MSGLEN );
  2658. TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
  2659. MSGLEN ) == MSGLEN );
  2660. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
  2661. MSGLEN ) == MSGLEN );
  2662. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2663. memset( received, 0, sizeof( received ) );
  2664. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
  2665. MSGLEN ) == MSGLEN );
  2666. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2667. memset( received, 0, sizeof( received ) );
  2668. }
  2669. for( i = 0; i < 2; i++ )
  2670. {
  2671. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
  2672. MSGLEN ) == MSGLEN );
  2673. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2674. memset( received, 0, sizeof( received ) );
  2675. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
  2676. MSGLEN ) == MSGLEN );
  2677. TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
  2678. memset( received, 0, sizeof( received ) );
  2679. }
  2680. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
  2681. == MBEDTLS_ERR_SSL_WANT_READ );
  2682. TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
  2683. == MBEDTLS_ERR_SSL_WANT_READ );
  2684. exit:
  2685. mbedtls_message_socket_close( &server_context );
  2686. mbedtls_message_socket_close( &client_context );
  2687. }
  2688. /* END_CASE */
  2689. /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  2690. void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
  2691. {
  2692. uint32_t len = 0;
  2693. mbedtls_ssl_context ssl;
  2694. mbedtls_ssl_config conf;
  2695. mbedtls_ssl_init( &ssl );
  2696. mbedtls_ssl_config_init( &conf );
  2697. TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
  2698. MBEDTLS_SSL_IS_CLIENT,
  2699. MBEDTLS_SSL_TRANSPORT_DATAGRAM,
  2700. MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
  2701. TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
  2702. /* Read previous record numbers */
  2703. for( len = 0; len < prevs->len; len += 6 )
  2704. {
  2705. memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
  2706. mbedtls_ssl_dtls_replay_update( &ssl );
  2707. }
  2708. /* Check new number */
  2709. memcpy( ssl.in_ctr + 2, new->x, 6 );
  2710. TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
  2711. mbedtls_ssl_free( &ssl );
  2712. mbedtls_ssl_config_free( &conf );
  2713. }
  2714. /* END_CASE */
  2715. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
  2716. void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
  2717. {
  2718. mbedtls_ssl_context ssl;
  2719. mbedtls_ssl_init( &ssl );
  2720. TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
  2721. TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
  2722. mbedtls_ssl_free( &ssl );
  2723. }
  2724. /* END_CASE */
  2725. /* BEGIN_CASE */
  2726. void ssl_crypt_record( int cipher_type, int hash_id,
  2727. int etm, int tag_mode, int ver,
  2728. int cid0_len, int cid1_len )
  2729. {
  2730. /*
  2731. * Test several record encryptions and decryptions
  2732. * with plenty of space before and after the data
  2733. * within the record buffer.
  2734. */
  2735. int ret;
  2736. int num_records = 16;
  2737. mbedtls_ssl_context ssl; /* ONLY for debugging */
  2738. mbedtls_ssl_transform t0, t1;
  2739. unsigned char *buf = NULL;
  2740. size_t const buflen = 512;
  2741. mbedtls_record rec, rec_backup;
  2742. mbedtls_ssl_init( &ssl );
  2743. mbedtls_ssl_transform_init( &t0 );
  2744. mbedtls_ssl_transform_init( &t1 );
  2745. TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
  2746. etm, tag_mode, ver,
  2747. (size_t) cid0_len,
  2748. (size_t) cid1_len ) == 0 );
  2749. TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
  2750. while( num_records-- > 0 )
  2751. {
  2752. mbedtls_ssl_transform *t_dec, *t_enc;
  2753. /* Take turns in who's sending and who's receiving. */
  2754. if( num_records % 3 == 0 )
  2755. {
  2756. t_dec = &t0;
  2757. t_enc = &t1;
  2758. }
  2759. else
  2760. {
  2761. t_dec = &t1;
  2762. t_enc = &t0;
  2763. }
  2764. /*
  2765. * The record header affects the transformation in two ways:
  2766. * 1) It determines the AEAD additional data
  2767. * 2) The record counter sometimes determines the IV.
  2768. *
  2769. * Apart from that, the fields don't have influence.
  2770. * In particular, it is currently not the responsibility
  2771. * of ssl_encrypt/decrypt_buf to check if the transform
  2772. * version matches the record version, or that the
  2773. * type is sensible.
  2774. */
  2775. memset( rec.ctr, num_records, sizeof( rec.ctr ) );
  2776. rec.type = 42;
  2777. rec.ver[0] = num_records;
  2778. rec.ver[1] = num_records;
  2779. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2780. rec.cid_len = 0;
  2781. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2782. rec.buf = buf;
  2783. rec.buf_len = buflen;
  2784. rec.data_offset = 16;
  2785. /* Make sure to vary the length to exercise different
  2786. * paddings. */
  2787. rec.data_len = 1 + num_records;
  2788. memset( rec.buf + rec.data_offset, 42, rec.data_len );
  2789. /* Make a copy for later comparison */
  2790. rec_backup = rec;
  2791. /* Encrypt record */
  2792. ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
  2793. mbedtls_test_rnd_std_rand, NULL );
  2794. TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2795. if( ret != 0 )
  2796. {
  2797. continue;
  2798. }
  2799. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2800. if( rec.cid_len != 0 )
  2801. {
  2802. /* DTLS 1.2 + CID hides the real content type and
  2803. * uses a special CID content type in the protected
  2804. * record. Double-check this. */
  2805. TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
  2806. }
  2807. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2808. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  2809. if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
  2810. {
  2811. /* TLS 1.3 hides the real content type and
  2812. * always uses Application Data as the content type
  2813. * for protected records. Double-check this. */
  2814. TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
  2815. }
  2816. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  2817. /* Decrypt record with t_dec */
  2818. ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
  2819. TEST_ASSERT( ret == 0 );
  2820. /* Compare results */
  2821. TEST_ASSERT( rec.type == rec_backup.type );
  2822. TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
  2823. TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
  2824. TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
  2825. TEST_ASSERT( rec.data_len == rec_backup.data_len );
  2826. TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
  2827. TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
  2828. rec_backup.buf + rec_backup.data_offset,
  2829. rec.data_len ) == 0 );
  2830. }
  2831. exit:
  2832. /* Cleanup */
  2833. mbedtls_ssl_free( &ssl );
  2834. mbedtls_ssl_transform_free( &t0 );
  2835. mbedtls_ssl_transform_free( &t1 );
  2836. mbedtls_free( buf );
  2837. }
  2838. /* END_CASE */
  2839. /* BEGIN_CASE */
  2840. void ssl_crypt_record_small( int cipher_type, int hash_id,
  2841. int etm, int tag_mode, int ver,
  2842. int cid0_len, int cid1_len )
  2843. {
  2844. /*
  2845. * Test pairs of encryption and decryption with an increasing
  2846. * amount of space in the record buffer - in more detail:
  2847. * 1) Try to encrypt with 0, 1, 2, ... bytes available
  2848. * in front of the plaintext, and expect the encryption
  2849. * to succeed starting from some offset. Always keep
  2850. * enough space in the end of the buffer.
  2851. * 2) Try to encrypt with 0, 1, 2, ... bytes available
  2852. * at the end of the plaintext, and expect the encryption
  2853. * to succeed starting from some offset. Always keep
  2854. * enough space at the beginning of the buffer.
  2855. * 3) Try to encrypt with 0, 1, 2, ... bytes available
  2856. * both at the front and end of the plaintext,
  2857. * and expect the encryption to succeed starting from
  2858. * some offset.
  2859. *
  2860. * If encryption succeeds, check that decryption succeeds
  2861. * and yields the original record.
  2862. */
  2863. mbedtls_ssl_context ssl; /* ONLY for debugging */
  2864. mbedtls_ssl_transform t0, t1;
  2865. unsigned char *buf = NULL;
  2866. size_t const buflen = 256;
  2867. mbedtls_record rec, rec_backup;
  2868. int ret;
  2869. int mode; /* Mode 1, 2 or 3 as explained above */
  2870. size_t offset; /* Available space at beginning/end/both */
  2871. size_t threshold = 96; /* Maximum offset to test against */
  2872. size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
  2873. size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
  2874. int seen_success; /* Indicates if in the current mode we've
  2875. * already seen a successful test. */
  2876. mbedtls_ssl_init( &ssl );
  2877. mbedtls_ssl_transform_init( &t0 );
  2878. mbedtls_ssl_transform_init( &t1 );
  2879. TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
  2880. etm, tag_mode, ver,
  2881. (size_t) cid0_len,
  2882. (size_t) cid1_len ) == 0 );
  2883. TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
  2884. for( mode=1; mode <= 3; mode++ )
  2885. {
  2886. seen_success = 0;
  2887. for( offset=0; offset <= threshold; offset++ )
  2888. {
  2889. mbedtls_ssl_transform *t_dec, *t_enc;
  2890. t_dec = &t0;
  2891. t_enc = &t1;
  2892. memset( rec.ctr, offset, sizeof( rec.ctr ) );
  2893. rec.type = 42;
  2894. rec.ver[0] = offset;
  2895. rec.ver[1] = offset;
  2896. rec.buf = buf;
  2897. rec.buf_len = buflen;
  2898. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2899. rec.cid_len = 0;
  2900. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2901. switch( mode )
  2902. {
  2903. case 1: /* Space in the beginning */
  2904. rec.data_offset = offset;
  2905. rec.data_len = buflen - offset - default_post_padding;
  2906. break;
  2907. case 2: /* Space in the end */
  2908. rec.data_offset = default_pre_padding;
  2909. rec.data_len = buflen - default_pre_padding - offset;
  2910. break;
  2911. case 3: /* Space in the beginning and end */
  2912. rec.data_offset = offset;
  2913. rec.data_len = buflen - 2 * offset;
  2914. break;
  2915. default:
  2916. TEST_ASSERT( 0 );
  2917. break;
  2918. }
  2919. memset( rec.buf + rec.data_offset, 42, rec.data_len );
  2920. /* Make a copy for later comparison */
  2921. rec_backup = rec;
  2922. /* Encrypt record */
  2923. ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
  2924. mbedtls_test_rnd_std_rand, NULL );
  2925. if( ( mode == 1 || mode == 2 ) && seen_success )
  2926. {
  2927. TEST_ASSERT( ret == 0 );
  2928. }
  2929. else
  2930. {
  2931. TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2932. if( ret == 0 )
  2933. seen_success = 1;
  2934. }
  2935. if( ret != 0 )
  2936. continue;
  2937. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2938. if( rec.cid_len != 0 )
  2939. {
  2940. /* DTLS 1.2 + CID hides the real content type and
  2941. * uses a special CID content type in the protected
  2942. * record. Double-check this. */
  2943. TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
  2944. }
  2945. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2946. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  2947. if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
  2948. {
  2949. /* TLS 1.3 hides the real content type and
  2950. * always uses Application Data as the content type
  2951. * for protected records. Double-check this. */
  2952. TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
  2953. }
  2954. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  2955. /* Decrypt record with t_dec */
  2956. TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
  2957. /* Compare results */
  2958. TEST_ASSERT( rec.type == rec_backup.type );
  2959. TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
  2960. TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
  2961. TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
  2962. TEST_ASSERT( rec.data_len == rec_backup.data_len );
  2963. TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
  2964. TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
  2965. rec_backup.buf + rec_backup.data_offset,
  2966. rec.data_len ) == 0 );
  2967. }
  2968. TEST_ASSERT( seen_success == 1 );
  2969. }
  2970. exit:
  2971. /* Cleanup */
  2972. mbedtls_ssl_free( &ssl );
  2973. mbedtls_ssl_transform_free( &t0 );
  2974. mbedtls_ssl_transform_free( &t1 );
  2975. mbedtls_free( buf );
  2976. }
  2977. /* END_CASE */
  2978. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */
  2979. void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
  2980. int length_selector )
  2981. {
  2982. /*
  2983. * Test record decryption for CBC without EtM, focused on the verification
  2984. * of padding and MAC.
  2985. *
  2986. * Actually depends on TLS >= 1.0 (SSL 3.0 computes the MAC differently),
  2987. * and either AES, ARIA, Camellia or DES, but since the test framework
  2988. * doesn't support alternation in dependency statements, just depend on
  2989. * TLS 1.2 and AES.
  2990. *
  2991. * The length_selector argument is interpreted as follows:
  2992. * - if it's -1, the plaintext length is 0 and minimal padding is applied
  2993. * - if it's -2, the plaintext length is 0 and maximal padding is applied
  2994. * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
  2995. * it's the length of the rest of the padding, that is, excluding the
  2996. * byte that encodes the length. The minimal non-zero plaintext length
  2997. * that gives this padding_length is automatically selected.
  2998. */
  2999. mbedtls_ssl_context ssl; /* ONLY for debugging */
  3000. mbedtls_ssl_transform t0, t1;
  3001. mbedtls_record rec, rec_save;
  3002. unsigned char *buf = NULL, *buf_save = NULL;
  3003. size_t buflen, olen = 0;
  3004. size_t plaintext_len, block_size, i;
  3005. unsigned char padlen; /* excluding the padding_length byte */
  3006. unsigned char add_data[13];
  3007. unsigned char mac[MBEDTLS_MD_MAX_SIZE];
  3008. int exp_ret;
  3009. const unsigned char pad_max_len = 255; /* Per the standard */
  3010. mbedtls_ssl_init( &ssl );
  3011. mbedtls_ssl_transform_init( &t0 );
  3012. mbedtls_ssl_transform_init( &t1 );
  3013. /* Set up transforms with dummy keys */
  3014. TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
  3015. 0, trunc_hmac,
  3016. MBEDTLS_SSL_MINOR_VERSION_3,
  3017. 0 , 0 ) == 0 );
  3018. /* Determine padding/plaintext length */
  3019. TEST_ASSERT( length_selector >= -2 && length_selector <= 255 );
  3020. block_size = t0.ivlen;
  3021. if( length_selector < 0 )
  3022. {
  3023. plaintext_len = 0;
  3024. /* Minimal padding
  3025. * The +1 is for the padding_length byte, not counted in padlen. */
  3026. padlen = block_size - ( t0.maclen + 1 ) % block_size;
  3027. /* Maximal padding? */
  3028. if( length_selector == -2 )
  3029. padlen += block_size * ( ( pad_max_len - padlen ) / block_size );
  3030. }
  3031. else
  3032. {
  3033. padlen = length_selector;
  3034. /* Minimal non-zero plaintext_length giving desired padding.
  3035. * The +1 is for the padding_length byte, not counted in padlen. */
  3036. plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size;
  3037. }
  3038. /* Prepare a buffer for record data */
  3039. buflen = block_size
  3040. + plaintext_len
  3041. + t0.maclen
  3042. + padlen + 1;
  3043. ASSERT_ALLOC( buf, buflen );
  3044. ASSERT_ALLOC( buf_save, buflen );
  3045. /* Prepare a dummy record header */
  3046. memset( rec.ctr, 0, sizeof( rec.ctr ) );
  3047. rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
  3048. rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3;
  3049. rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3;
  3050. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  3051. rec.cid_len = 0;
  3052. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  3053. /* Prepare dummy record content */
  3054. rec.buf = buf;
  3055. rec.buf_len = buflen;
  3056. rec.data_offset = block_size;
  3057. rec.data_len = plaintext_len;
  3058. memset( rec.buf + rec.data_offset, 42, rec.data_len );
  3059. /* Serialized version of record header for MAC purposes */
  3060. memcpy( add_data, rec.ctr, 8 );
  3061. add_data[8] = rec.type;
  3062. add_data[9] = rec.ver[0];
  3063. add_data[10] = rec.ver[1];
  3064. add_data[11] = ( rec.data_len >> 8 ) & 0xff;
  3065. add_data[12] = ( rec.data_len >> 0 ) & 0xff;
  3066. /* Set dummy IV */
  3067. memset( t0.iv_enc, 0x55, t0.ivlen );
  3068. memcpy( rec.buf, t0.iv_enc, t0.ivlen );
  3069. /*
  3070. * Prepare a pre-encryption record (with MAC and padding), and save it.
  3071. */
  3072. /* MAC with additional data */
  3073. TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) );
  3074. TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc,
  3075. rec.buf + rec.data_offset,
  3076. rec.data_len ) );
  3077. TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) );
  3078. memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen );
  3079. rec.data_len += t0.maclen;
  3080. /* Pad */
  3081. memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 );
  3082. rec.data_len += padlen + 1;
  3083. /* Save correct pre-encryption record */
  3084. rec_save = rec;
  3085. rec_save.buf = buf_save;
  3086. memcpy( buf_save, buf, buflen );
  3087. /*
  3088. * Encrypt and decrypt the correct record, expecting success
  3089. */
  3090. TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
  3091. t0.iv_enc, t0.ivlen,
  3092. rec.buf + rec.data_offset, rec.data_len,
  3093. rec.buf + rec.data_offset, &olen ) );
  3094. rec.data_offset -= t0.ivlen;
  3095. rec.data_len += t0.ivlen;
  3096. TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
  3097. /*
  3098. * Modify each byte of the pre-encryption record before encrypting and
  3099. * decrypting it, expecting failure every time.
  3100. */
  3101. for( i = block_size; i < buflen; i++ )
  3102. {
  3103. mbedtls_test_set_step( i );
  3104. /* Restore correct pre-encryption record */
  3105. rec = rec_save;
  3106. rec.buf = buf;
  3107. memcpy( buf, buf_save, buflen );
  3108. /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
  3109. rec.buf[i] ^= 0x01;
  3110. /* Encrypt */
  3111. TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
  3112. t0.iv_enc, t0.ivlen,
  3113. rec.buf + rec.data_offset, rec.data_len,
  3114. rec.buf + rec.data_offset, &olen ) );
  3115. rec.data_offset -= t0.ivlen;
  3116. rec.data_len += t0.ivlen;
  3117. /* Decrypt and expect failure */
  3118. TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC,
  3119. mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
  3120. }
  3121. /*
  3122. * Use larger values of the padding bytes - with small buffers, this tests
  3123. * the case where the announced padlen would be larger than the buffer
  3124. * (and before that, than the buffer minus the size of the MAC), to make
  3125. * sure our padding checking code does not perform any out-of-bounds reads
  3126. * in this case. (With larger buffers, ie when the plaintext is long or
  3127. * maximal length padding is used, this is less relevant but still doesn't
  3128. * hurt to test.)
  3129. *
  3130. * (Start the loop with correct padding, just to double-check that record
  3131. * saving did work, and that we're overwriting the correct bytes.)
  3132. */
  3133. for( i = padlen; i <= pad_max_len; i++ )
  3134. {
  3135. mbedtls_test_set_step( i );
  3136. /* Restore correct pre-encryption record */
  3137. rec = rec_save;
  3138. rec.buf = buf;
  3139. memcpy( buf, buf_save, buflen );
  3140. /* Set padding bytes to new value */
  3141. memset( buf + buflen - padlen - 1, i, padlen + 1 );
  3142. /* Encrypt */
  3143. TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
  3144. t0.iv_enc, t0.ivlen,
  3145. rec.buf + rec.data_offset, rec.data_len,
  3146. rec.buf + rec.data_offset, &olen ) );
  3147. rec.data_offset -= t0.ivlen;
  3148. rec.data_len += t0.ivlen;
  3149. /* Decrypt and expect failure except the first time */
  3150. exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
  3151. TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
  3152. }
  3153. exit:
  3154. mbedtls_ssl_free( &ssl );
  3155. mbedtls_ssl_transform_free( &t0 );
  3156. mbedtls_ssl_transform_free( &t1 );
  3157. mbedtls_free( buf );
  3158. mbedtls_free( buf_save );
  3159. }
  3160. /* END_CASE */
  3161. /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  3162. void ssl_tls1_3_hkdf_expand_label( int hash_alg,
  3163. data_t *secret,
  3164. int label_idx,
  3165. data_t *ctx,
  3166. int desired_length,
  3167. data_t *expected )
  3168. {
  3169. unsigned char dst[ 100 ];
  3170. unsigned char const *lbl = NULL;
  3171. size_t lbl_len;
  3172. #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
  3173. if( label_idx == (int) tls1_3_label_ ## name ) \
  3174. { \
  3175. lbl = mbedtls_ssl_tls1_3_labels.name; \
  3176. lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \
  3177. }
  3178. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  3179. #undef MBEDTLS_SSL_TLS1_3_LABEL
  3180. TEST_ASSERT( lbl != NULL );
  3181. /* Check sanity of test parameters. */
  3182. TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
  3183. TEST_ASSERT( (size_t) desired_length == expected->len );
  3184. TEST_ASSERT( mbedtls_ssl_tls1_3_hkdf_expand_label(
  3185. (mbedtls_md_type_t) hash_alg,
  3186. secret->x, secret->len,
  3187. lbl, lbl_len,
  3188. ctx->x, ctx->len,
  3189. dst, desired_length ) == 0 );
  3190. ASSERT_COMPARE( dst, (size_t) desired_length,
  3191. expected->x, (size_t) expected->len );
  3192. }
  3193. /* END_CASE */
  3194. /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  3195. void ssl_tls1_3_traffic_key_generation( int hash_alg,
  3196. data_t *server_secret,
  3197. data_t *client_secret,
  3198. int desired_iv_len,
  3199. int desired_key_len,
  3200. data_t *expected_server_write_key,
  3201. data_t *expected_server_write_iv,
  3202. data_t *expected_client_write_key,
  3203. data_t *expected_client_write_iv )
  3204. {
  3205. mbedtls_ssl_key_set keys;
  3206. /* Check sanity of test parameters. */
  3207. TEST_ASSERT( client_secret->len == server_secret->len );
  3208. TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len &&
  3209. expected_client_write_iv->len == (size_t) desired_iv_len );
  3210. TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len &&
  3211. expected_client_write_key->len == (size_t) desired_key_len );
  3212. TEST_ASSERT( mbedtls_ssl_tls1_3_make_traffic_keys(
  3213. (mbedtls_md_type_t) hash_alg,
  3214. client_secret->x,
  3215. server_secret->x,
  3216. client_secret->len /* == server_secret->len */,
  3217. desired_key_len, desired_iv_len,
  3218. &keys ) == 0 );
  3219. ASSERT_COMPARE( keys.client_write_key,
  3220. keys.key_len,
  3221. expected_client_write_key->x,
  3222. (size_t) desired_key_len );
  3223. ASSERT_COMPARE( keys.server_write_key,
  3224. keys.key_len,
  3225. expected_server_write_key->x,
  3226. (size_t) desired_key_len );
  3227. ASSERT_COMPARE( keys.client_write_iv,
  3228. keys.iv_len,
  3229. expected_client_write_iv->x,
  3230. (size_t) desired_iv_len );
  3231. ASSERT_COMPARE( keys.server_write_iv,
  3232. keys.iv_len,
  3233. expected_server_write_iv->x,
  3234. (size_t) desired_iv_len );
  3235. }
  3236. /* END_CASE */
  3237. /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  3238. void ssl_tls1_3_derive_secret( int hash_alg,
  3239. data_t *secret,
  3240. int label_idx,
  3241. data_t *ctx,
  3242. int desired_length,
  3243. int already_hashed,
  3244. data_t *expected )
  3245. {
  3246. unsigned char dst[ 100 ];
  3247. unsigned char const *lbl = NULL;
  3248. size_t lbl_len;
  3249. #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
  3250. if( label_idx == (int) tls1_3_label_ ## name ) \
  3251. { \
  3252. lbl = mbedtls_ssl_tls1_3_labels.name; \
  3253. lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \
  3254. }
  3255. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  3256. #undef MBEDTLS_SSL_TLS1_3_LABEL
  3257. TEST_ASSERT( lbl != NULL );
  3258. /* Check sanity of test parameters. */
  3259. TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
  3260. TEST_ASSERT( (size_t) desired_length == expected->len );
  3261. TEST_ASSERT( mbedtls_ssl_tls1_3_derive_secret(
  3262. (mbedtls_md_type_t) hash_alg,
  3263. secret->x, secret->len,
  3264. lbl, lbl_len,
  3265. ctx->x, ctx->len,
  3266. already_hashed,
  3267. dst, desired_length ) == 0 );
  3268. ASSERT_COMPARE( dst, desired_length,
  3269. expected->x, desired_length );
  3270. }
  3271. /* END_CASE */
  3272. /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  3273. void ssl_tls1_3_key_evolution( int hash_alg,
  3274. data_t *secret,
  3275. data_t *input,
  3276. data_t *expected )
  3277. {
  3278. unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ];
  3279. TEST_ASSERT( mbedtls_ssl_tls1_3_evolve_secret(
  3280. (mbedtls_md_type_t) hash_alg,
  3281. secret->len ? secret->x : NULL,
  3282. input->len ? input->x : NULL, input->len,
  3283. secret_new ) == 0 );
  3284. ASSERT_COMPARE( secret_new, (size_t) expected->len,
  3285. expected->x, (size_t) expected->len );
  3286. }
  3287. /* END_CASE */
  3288. /* BEGIN_CASE */
  3289. void ssl_tls_prf( int type, data_t * secret, data_t * random,
  3290. char *label, data_t *result_str, int exp_ret )
  3291. {
  3292. unsigned char *output;
  3293. output = mbedtls_calloc( 1, result_str->len );
  3294. if( output == NULL )
  3295. goto exit;
  3296. USE_PSA_INIT( );
  3297. TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
  3298. label, random->x, random->len,
  3299. output, result_str->len ) == exp_ret );
  3300. if( exp_ret == 0 )
  3301. {
  3302. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  3303. result_str->len, result_str->len ) == 0 );
  3304. }
  3305. exit:
  3306. mbedtls_free( output );
  3307. USE_PSA_DONE( );
  3308. }
  3309. /* END_CASE */
  3310. /* BEGIN_CASE */
  3311. void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
  3312. {
  3313. mbedtls_ssl_session original, restored;
  3314. unsigned char *buf = NULL;
  3315. size_t len;
  3316. /*
  3317. * Test that a save-load pair is the identity
  3318. */
  3319. mbedtls_ssl_session_init( &original );
  3320. mbedtls_ssl_session_init( &restored );
  3321. /* Prepare a dummy session to work on */
  3322. TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
  3323. /* Serialize it */
  3324. TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
  3325. == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3326. TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
  3327. TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
  3328. == 0 );
  3329. /* Restore session from serialized data */
  3330. TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
  3331. /*
  3332. * Make sure both session structures are identical
  3333. */
  3334. #if defined(MBEDTLS_HAVE_TIME)
  3335. TEST_ASSERT( original.start == restored.start );
  3336. #endif
  3337. TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
  3338. TEST_ASSERT( original.compression == restored.compression );
  3339. TEST_ASSERT( original.id_len == restored.id_len );
  3340. TEST_ASSERT( memcmp( original.id,
  3341. restored.id, sizeof( original.id ) ) == 0 );
  3342. TEST_ASSERT( memcmp( original.master,
  3343. restored.master, sizeof( original.master ) ) == 0 );
  3344. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  3345. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  3346. TEST_ASSERT( ( original.peer_cert == NULL ) ==
  3347. ( restored.peer_cert == NULL ) );
  3348. if( original.peer_cert != NULL )
  3349. {
  3350. TEST_ASSERT( original.peer_cert->raw.len ==
  3351. restored.peer_cert->raw.len );
  3352. TEST_ASSERT( memcmp( original.peer_cert->raw.p,
  3353. restored.peer_cert->raw.p,
  3354. original.peer_cert->raw.len ) == 0 );
  3355. }
  3356. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3357. TEST_ASSERT( original.peer_cert_digest_type ==
  3358. restored.peer_cert_digest_type );
  3359. TEST_ASSERT( original.peer_cert_digest_len ==
  3360. restored.peer_cert_digest_len );
  3361. TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
  3362. ( restored.peer_cert_digest == NULL ) );
  3363. if( original.peer_cert_digest != NULL )
  3364. {
  3365. TEST_ASSERT( memcmp( original.peer_cert_digest,
  3366. restored.peer_cert_digest,
  3367. original.peer_cert_digest_len ) == 0 );
  3368. }
  3369. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3370. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  3371. TEST_ASSERT( original.verify_result == restored.verify_result );
  3372. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  3373. TEST_ASSERT( original.ticket_len == restored.ticket_len );
  3374. if( original.ticket_len != 0 )
  3375. {
  3376. TEST_ASSERT( original.ticket != NULL );
  3377. TEST_ASSERT( restored.ticket != NULL );
  3378. TEST_ASSERT( memcmp( original.ticket,
  3379. restored.ticket, original.ticket_len ) == 0 );
  3380. }
  3381. TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
  3382. #endif
  3383. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  3384. TEST_ASSERT( original.mfl_code == restored.mfl_code );
  3385. #endif
  3386. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  3387. TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
  3388. #endif
  3389. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  3390. TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
  3391. #endif
  3392. exit:
  3393. mbedtls_ssl_session_free( &original );
  3394. mbedtls_ssl_session_free( &restored );
  3395. mbedtls_free( buf );
  3396. }
  3397. /* END_CASE */
  3398. /* BEGIN_CASE */
  3399. void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
  3400. {
  3401. mbedtls_ssl_session session;
  3402. unsigned char *buf1 = NULL, *buf2 = NULL;
  3403. size_t len0, len1, len2;
  3404. /*
  3405. * Test that a load-save pair is the identity
  3406. */
  3407. mbedtls_ssl_session_init( &session );
  3408. /* Prepare a dummy session to work on */
  3409. TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
  3410. /* Get desired buffer size for serializing */
  3411. TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
  3412. == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3413. /* Allocate first buffer */
  3414. buf1 = mbedtls_calloc( 1, len0 );
  3415. TEST_ASSERT( buf1 != NULL );
  3416. /* Serialize to buffer and free live session */
  3417. TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
  3418. == 0 );
  3419. TEST_ASSERT( len0 == len1 );
  3420. mbedtls_ssl_session_free( &session );
  3421. /* Restore session from serialized data */
  3422. TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
  3423. /* Allocate second buffer and serialize to it */
  3424. buf2 = mbedtls_calloc( 1, len0 );
  3425. TEST_ASSERT( buf2 != NULL );
  3426. TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
  3427. == 0 );
  3428. /* Make sure both serialized versions are identical */
  3429. TEST_ASSERT( len1 == len2 );
  3430. TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
  3431. exit:
  3432. mbedtls_ssl_session_free( &session );
  3433. mbedtls_free( buf1 );
  3434. mbedtls_free( buf2 );
  3435. }
  3436. /* END_CASE */
  3437. /* BEGIN_CASE */
  3438. void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
  3439. {
  3440. mbedtls_ssl_session session;
  3441. unsigned char *buf = NULL;
  3442. size_t good_len, bad_len, test_len;
  3443. /*
  3444. * Test that session_save() fails cleanly on small buffers
  3445. */
  3446. mbedtls_ssl_session_init( &session );
  3447. /* Prepare dummy session and get serialized size */
  3448. TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
  3449. TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
  3450. == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3451. /* Try all possible bad lengths */
  3452. for( bad_len = 1; bad_len < good_len; bad_len++ )
  3453. {
  3454. /* Allocate exact size so that asan/valgrind can detect any overwrite */
  3455. mbedtls_free( buf );
  3456. TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
  3457. TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
  3458. &test_len )
  3459. == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3460. TEST_ASSERT( test_len == good_len );
  3461. }
  3462. exit:
  3463. mbedtls_ssl_session_free( &session );
  3464. mbedtls_free( buf );
  3465. }
  3466. /* END_CASE */
  3467. /* BEGIN_CASE */
  3468. void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
  3469. {
  3470. mbedtls_ssl_session session;
  3471. unsigned char *good_buf = NULL, *bad_buf = NULL;
  3472. size_t good_len, bad_len;
  3473. /*
  3474. * Test that session_load() fails cleanly on small buffers
  3475. */
  3476. mbedtls_ssl_session_init( &session );
  3477. /* Prepare serialized session data */
  3478. TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
  3479. TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
  3480. == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3481. TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
  3482. TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
  3483. &good_len ) == 0 );
  3484. mbedtls_ssl_session_free( &session );
  3485. /* Try all possible bad lengths */
  3486. for( bad_len = 0; bad_len < good_len; bad_len++ )
  3487. {
  3488. /* Allocate exact size so that asan/valgrind can detect any overread */
  3489. mbedtls_free( bad_buf );
  3490. bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
  3491. TEST_ASSERT( bad_buf != NULL );
  3492. memcpy( bad_buf, good_buf, bad_len );
  3493. TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
  3494. == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3495. }
  3496. exit:
  3497. mbedtls_ssl_session_free( &session );
  3498. mbedtls_free( good_buf );
  3499. mbedtls_free( bad_buf );
  3500. }
  3501. /* END_CASE */
  3502. /* BEGIN_CASE */
  3503. void ssl_session_serialize_version_check( int corrupt_major,
  3504. int corrupt_minor,
  3505. int corrupt_patch,
  3506. int corrupt_config )
  3507. {
  3508. unsigned char serialized_session[ 2048 ];
  3509. size_t serialized_session_len;
  3510. unsigned cur_byte;
  3511. mbedtls_ssl_session session;
  3512. uint8_t should_corrupt_byte[] = { corrupt_major == 1,
  3513. corrupt_minor == 1,
  3514. corrupt_patch == 1,
  3515. corrupt_config == 1,
  3516. corrupt_config == 1 };
  3517. mbedtls_ssl_session_init( &session );
  3518. /* Infer length of serialized session. */
  3519. TEST_ASSERT( mbedtls_ssl_session_save( &session,
  3520. serialized_session,
  3521. sizeof( serialized_session ),
  3522. &serialized_session_len ) == 0 );
  3523. mbedtls_ssl_session_free( &session );
  3524. /* Without any modification, we should be able to successfully
  3525. * de-serialize the session - double-check that. */
  3526. TEST_ASSERT( mbedtls_ssl_session_load( &session,
  3527. serialized_session,
  3528. serialized_session_len ) == 0 );
  3529. mbedtls_ssl_session_free( &session );
  3530. /* Go through the bytes in the serialized session header and
  3531. * corrupt them bit-by-bit. */
  3532. for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
  3533. {
  3534. int cur_bit;
  3535. unsigned char * const byte = &serialized_session[ cur_byte ];
  3536. if( should_corrupt_byte[ cur_byte ] == 0 )
  3537. continue;
  3538. for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
  3539. {
  3540. unsigned char const corrupted_bit = 0x1u << cur_bit;
  3541. /* Modify a single bit in the serialized session. */
  3542. *byte ^= corrupted_bit;
  3543. /* Attempt to deserialize */
  3544. TEST_ASSERT( mbedtls_ssl_session_load( &session,
  3545. serialized_session,
  3546. serialized_session_len ) ==
  3547. MBEDTLS_ERR_SSL_VERSION_MISMATCH );
  3548. /* Undo the change */
  3549. *byte ^= corrupted_bit;
  3550. }
  3551. }
  3552. }
  3553. /* END_CASE */
  3554. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3555. void mbedtls_endpoint_sanity( int endpoint_type )
  3556. {
  3557. enum { BUFFSIZE = 1024 };
  3558. mbedtls_endpoint ep;
  3559. int ret = -1;
  3560. ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
  3561. NULL, NULL, NULL );
  3562. TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
  3563. ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
  3564. TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
  3565. ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
  3566. NULL, NULL, NULL );
  3567. TEST_ASSERT( ret == 0 );
  3568. exit:
  3569. mbedtls_endpoint_free( &ep, NULL );
  3570. }
  3571. /* END_CASE */
  3572. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3573. void move_handshake_to_state(int endpoint_type, int state, int need_pass)
  3574. {
  3575. enum { BUFFSIZE = 1024 };
  3576. mbedtls_endpoint base_ep, second_ep;
  3577. int ret = -1;
  3578. ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
  3579. NULL, NULL, NULL );
  3580. TEST_ASSERT( ret == 0 );
  3581. ret = mbedtls_endpoint_init( &second_ep,
  3582. ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
  3583. MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
  3584. MBEDTLS_PK_RSA, NULL, NULL, NULL );
  3585. TEST_ASSERT( ret == 0 );
  3586. ret = mbedtls_mock_socket_connect( &(base_ep.socket),
  3587. &(second_ep.socket),
  3588. BUFFSIZE );
  3589. TEST_ASSERT( ret == 0 );
  3590. ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
  3591. &(second_ep.ssl),
  3592. state );
  3593. if( need_pass )
  3594. {
  3595. TEST_ASSERT( ret == 0 );
  3596. TEST_ASSERT( base_ep.ssl.state == state );
  3597. }
  3598. else
  3599. {
  3600. TEST_ASSERT( ret != 0 );
  3601. TEST_ASSERT( base_ep.ssl.state != state );
  3602. }
  3603. exit:
  3604. mbedtls_endpoint_free( &base_ep, NULL );
  3605. mbedtls_endpoint_free( &second_ep, NULL );
  3606. }
  3607. /* END_CASE */
  3608. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3609. void handshake_version( int dtls, int client_min_version, int client_max_version,
  3610. int server_min_version, int server_max_version,
  3611. int expected_negotiated_version )
  3612. {
  3613. handshake_test_options options;
  3614. init_handshake_options( &options );
  3615. options.client_min_version = client_min_version;
  3616. options.client_max_version = client_max_version;
  3617. options.server_min_version = server_min_version;
  3618. options.server_max_version = server_max_version;
  3619. options.expected_negotiated_version = expected_negotiated_version;
  3620. options.dtls = dtls;
  3621. /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so
  3622. * the number of fragments will be twice as big. */
  3623. if( expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 ||
  3624. expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1 )
  3625. {
  3626. options.expected_cli_fragments = 2;
  3627. options.expected_srv_fragments = 2;
  3628. }
  3629. perform_handshake( &options );
  3630. /* The goto below is used to avoid an "unused label" warning.*/
  3631. goto exit;
  3632. }
  3633. /* END_CASE */
  3634. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3635. void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
  3636. {
  3637. handshake_test_options options;
  3638. init_handshake_options( &options );
  3639. options.cipher = cipher;
  3640. options.dtls = dtls;
  3641. options.psk_str = psk_str;
  3642. options.pk_alg = pk_alg;
  3643. perform_handshake( &options );
  3644. /* The goto below is used to avoid an "unused label" warning.*/
  3645. goto exit;
  3646. }
  3647. /* END_CASE */
  3648. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3649. void handshake_cipher( char* cipher, int pk_alg, int dtls )
  3650. {
  3651. test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
  3652. /* The goto below is used to avoid an "unused label" warning.*/
  3653. goto exit;
  3654. }
  3655. /* END_CASE */
  3656. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3657. void app_data( int mfl, int cli_msg_len, int srv_msg_len,
  3658. int expected_cli_fragments,
  3659. int expected_srv_fragments, int dtls )
  3660. {
  3661. handshake_test_options options;
  3662. init_handshake_options( &options );
  3663. options.mfl = mfl;
  3664. options.cli_msg_len = cli_msg_len;
  3665. options.srv_msg_len = srv_msg_len;
  3666. options.expected_cli_fragments = expected_cli_fragments;
  3667. options.expected_srv_fragments = expected_srv_fragments;
  3668. options.dtls = dtls;
  3669. perform_handshake( &options );
  3670. /* The goto below is used to avoid an "unused label" warning.*/
  3671. goto exit;
  3672. }
  3673. /* END_CASE */
  3674. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3675. void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
  3676. int expected_cli_fragments,
  3677. int expected_srv_fragments )
  3678. {
  3679. test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
  3680. expected_srv_fragments, 0 );
  3681. /* The goto below is used to avoid an "unused label" warning.*/
  3682. goto exit;
  3683. }
  3684. /* END_CASE */
  3685. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3686. void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
  3687. int expected_cli_fragments,
  3688. int expected_srv_fragments )
  3689. {
  3690. test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
  3691. expected_srv_fragments, 1 );
  3692. /* The goto below is used to avoid an "unused label" warning.*/
  3693. goto exit;
  3694. }
  3695. /* END_CASE */
  3696. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3697. void handshake_serialization( )
  3698. {
  3699. handshake_test_options options;
  3700. init_handshake_options( &options );
  3701. options.serialize = 1;
  3702. options.dtls = 1;
  3703. perform_handshake( &options );
  3704. /* The goto below is used to avoid an "unused label" warning.*/
  3705. goto exit;
  3706. }
  3707. /* END_CASE */
  3708. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3709. void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
  3710. {
  3711. handshake_test_options options;
  3712. log_pattern srv_pattern, cli_pattern;
  3713. srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
  3714. srv_pattern.counter = 0;
  3715. cli_pattern.counter = 0;
  3716. init_handshake_options( &options );
  3717. options.dtls = 1;
  3718. options.mfl = mfl;
  3719. /* Set cipher to one using CBC so that record splitting can be tested */
  3720. options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
  3721. options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  3722. options.srv_log_obj = &srv_pattern;
  3723. options.cli_log_obj = &cli_pattern;
  3724. options.srv_log_fun = log_analyzer;
  3725. options.cli_log_fun = log_analyzer;
  3726. perform_handshake( &options );
  3727. /* Test if the server received a fragmented handshake */
  3728. if( expected_srv_hs_fragmentation )
  3729. {
  3730. TEST_ASSERT( srv_pattern.counter >= 1 );
  3731. }
  3732. /* Test if the client received a fragmented handshake */
  3733. if( expected_cli_hs_fragmentation )
  3734. {
  3735. TEST_ASSERT( cli_pattern.counter >= 1 );
  3736. }
  3737. }
  3738. /* END_CASE */
  3739. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3740. void renegotiation( int legacy_renegotiation )
  3741. {
  3742. handshake_test_options options;
  3743. init_handshake_options( &options );
  3744. options.renegotiate = 1;
  3745. options.legacy_renegotiation = legacy_renegotiation;
  3746. options.dtls = 1;
  3747. perform_handshake( &options );
  3748. /* The goto below is used to avoid an "unused label" warning.*/
  3749. goto exit;
  3750. }
  3751. /* END_CASE */
  3752. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3753. void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
  3754. int serialize, int dtls, char *cipher )
  3755. {
  3756. handshake_test_options options;
  3757. init_handshake_options( &options );
  3758. options.mfl = mfl;
  3759. options.cipher = cipher;
  3760. options.renegotiate = renegotiation;
  3761. options.legacy_renegotiation = legacy_renegotiation;
  3762. options.serialize = serialize;
  3763. options.dtls = dtls;
  3764. options.resize_buffers = 1;
  3765. perform_handshake( &options );
  3766. /* The goto below is used to avoid an "unused label" warning.*/
  3767. goto exit;
  3768. }
  3769. /* END_CASE */
  3770. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3771. void resize_buffers_serialize_mfl( int mfl )
  3772. {
  3773. test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
  3774. (char *) "" );
  3775. /* The goto below is used to avoid an "unused label" warning.*/
  3776. goto exit;
  3777. }
  3778. /* END_CASE */
  3779. /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  3780. void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
  3781. char *cipher )
  3782. {
  3783. test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
  3784. /* The goto below is used to avoid an "unused label" warning.*/
  3785. goto exit;
  3786. }
  3787. /* END_CASE */
  3788. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
  3789. void ssl_cf_hmac( int hash )
  3790. {
  3791. /*
  3792. * Test the function mbedtls_ct_hmac() against a reference
  3793. * implementation.
  3794. */
  3795. mbedtls_md_context_t ctx, ref_ctx;
  3796. const mbedtls_md_info_t *md_info;
  3797. size_t out_len, block_size;
  3798. size_t min_in_len, in_len, max_in_len, i;
  3799. /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
  3800. unsigned char add_data[13];
  3801. unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
  3802. unsigned char *data = NULL;
  3803. unsigned char *out = NULL;
  3804. unsigned char rec_num = 0;
  3805. mbedtls_md_init( &ctx );
  3806. mbedtls_md_init( &ref_ctx );
  3807. md_info = mbedtls_md_info_from_type( hash );
  3808. TEST_ASSERT( md_info != NULL );
  3809. out_len = mbedtls_md_get_size( md_info );
  3810. TEST_ASSERT( out_len != 0 );
  3811. block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
  3812. /* Use allocated out buffer to catch overwrites */
  3813. ASSERT_ALLOC( out, out_len );
  3814. /* Set up contexts with the given hash and a dummy key */
  3815. TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
  3816. TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
  3817. memset( ref_out, 42, sizeof( ref_out ) );
  3818. TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
  3819. TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
  3820. memset( ref_out, 0, sizeof( ref_out ) );
  3821. /*
  3822. * Test all possible lengths up to a point. The difference between
  3823. * max_in_len and min_in_len is at most 255, and make sure they both vary
  3824. * by at least one block size.
  3825. */
  3826. for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
  3827. {
  3828. mbedtls_test_set_step( max_in_len * 10000 );
  3829. /* Use allocated in buffer to catch overreads */
  3830. ASSERT_ALLOC( data, max_in_len );
  3831. min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
  3832. for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
  3833. {
  3834. mbedtls_test_set_step( max_in_len * 10000 + in_len );
  3835. /* Set up dummy data and add_data */
  3836. rec_num++;
  3837. memset( add_data, rec_num, sizeof( add_data ) );
  3838. for( i = 0; i < in_len; i++ )
  3839. data[i] = ( i & 0xff ) ^ rec_num;
  3840. /* Get the function's result */
  3841. TEST_CF_SECRET( &in_len, sizeof( in_len ) );
  3842. TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ),
  3843. data, in_len,
  3844. min_in_len, max_in_len,
  3845. out ) );
  3846. TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
  3847. TEST_CF_PUBLIC( out, out_len );
  3848. /* Compute the reference result */
  3849. TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
  3850. sizeof( add_data ) ) );
  3851. TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
  3852. TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
  3853. TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
  3854. /* Compare */
  3855. ASSERT_COMPARE( out, out_len, ref_out, out_len );
  3856. }
  3857. mbedtls_free( data );
  3858. data = NULL;
  3859. }
  3860. exit:
  3861. mbedtls_md_free( &ref_ctx );
  3862. mbedtls_md_free( &ctx );
  3863. mbedtls_free( data );
  3864. mbedtls_free( out );
  3865. }
  3866. /* END_CASE */
  3867. /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
  3868. void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
  3869. {
  3870. unsigned char *dst = NULL;
  3871. unsigned char *src = NULL;
  3872. size_t src_len = offset_max + len;
  3873. size_t secret;
  3874. ASSERT_ALLOC( dst, len );
  3875. ASSERT_ALLOC( src, src_len );
  3876. /* Fill src in a way that we can detect if we copied the right bytes */
  3877. mbedtls_test_rnd_std_rand( NULL, src, src_len );
  3878. for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
  3879. {
  3880. mbedtls_test_set_step( (int) secret );
  3881. TEST_CF_SECRET( &secret, sizeof( secret ) );
  3882. mbedtls_ct_memcpy_offset( dst, src, secret,
  3883. offset_min, offset_max, len );
  3884. TEST_CF_PUBLIC( &secret, sizeof( secret ) );
  3885. TEST_CF_PUBLIC( dst, len );
  3886. ASSERT_COMPARE( dst, len, src + secret, len );
  3887. }
  3888. exit:
  3889. mbedtls_free( dst );
  3890. mbedtls_free( src );
  3891. }
  3892. /* END_CASE */