test.h 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702
  1. /* test.h */
  2. #ifndef wolfSSL_TEST_H
  3. #define wolfSSL_TEST_H
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <ctype.h>
  8. #include <wolfssl/wolfcrypt/types.h>
  9. #include <wolfssl/wolfcrypt/error-crypt.h>
  10. #include <wolfssl/wolfcrypt/random.h>
  11. #include <wolfssl/wolfcrypt/mem_track.h>
  12. #if defined(SHOW_CERTS) && \
  13. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  14. #include <wolfssl/wolfcrypt/asn.h> /* for domain component NID value */
  15. #endif
  16. #ifdef ATOMIC_USER
  17. #include <wolfssl/wolfcrypt/aes.h>
  18. #include <wolfssl/wolfcrypt/arc4.h>
  19. #include <wolfssl/wolfcrypt/hmac.h>
  20. #endif
  21. #ifdef HAVE_PK_CALLBACKS
  22. #include <wolfssl/wolfcrypt/asn.h>
  23. #ifndef NO_RSA
  24. #include <wolfssl/wolfcrypt/rsa.h>
  25. #endif
  26. #ifdef HAVE_ECC
  27. #include <wolfssl/wolfcrypt/ecc.h>
  28. #endif /* HAVE_ECC */
  29. #ifndef NO_DH
  30. #include <wolfssl/wolfcrypt/dh.h>
  31. #endif /* !NO_DH */
  32. #ifdef HAVE_ED25519
  33. #include <wolfssl/wolfcrypt/ed25519.h>
  34. #endif /* HAVE_ED25519 */
  35. #ifdef HAVE_CURVE25519
  36. #include <wolfssl/wolfcrypt/curve25519.h>
  37. #endif /* HAVE_ECC */
  38. #ifdef HAVE_ED448
  39. #include <wolfssl/wolfcrypt/ed448.h>
  40. #endif /* HAVE_ED448 */
  41. #ifdef HAVE_CURVE448
  42. #include <wolfssl/wolfcrypt/curve448.h>
  43. #endif /* HAVE_ECC */
  44. #endif /*HAVE_PK_CALLBACKS */
  45. #ifdef USE_WINDOWS_API
  46. #include <winsock2.h>
  47. #include <process.h>
  48. #ifdef TEST_IPV6 /* don't require newer SDK for IPV4 */
  49. #include <ws2tcpip.h>
  50. #include <wspiapi.h>
  51. #endif
  52. #define SOCKET_T SOCKET
  53. #define SNPRINTF _snprintf
  54. #define XSLEEP_MS(t) Sleep(t)
  55. #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  56. #include <string.h>
  57. #include "rl_net.h"
  58. #define SOCKET_T int
  59. typedef int socklen_t ;
  60. #define inet_addr wolfSSL_inet_addr
  61. static unsigned long wolfSSL_inet_addr(const char *cp)
  62. {
  63. unsigned int a[4] ; unsigned long ret ;
  64. sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) ;
  65. ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
  66. return(ret) ;
  67. }
  68. #if defined(HAVE_KEIL_RTX)
  69. #define XSLEEP_MS(t) os_dly_wait(t)
  70. #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
  71. #define XSLEEP_MS(t) osDelay(t)
  72. #endif
  73. #elif defined(WOLFSSL_TIRTOS)
  74. #include <string.h>
  75. #include <netdb.h>
  76. #include <sys/types.h>
  77. #include <arpa/inet.h>
  78. #include <sys/socket.h>
  79. #include <ti/sysbios/knl/Task.h>
  80. struct hostent {
  81. char *h_name; /* official name of host */
  82. char **h_aliases; /* alias list */
  83. int h_addrtype; /* host address type */
  84. int h_length; /* length of address */
  85. char **h_addr_list; /* list of addresses from name server */
  86. };
  87. #define SOCKET_T int
  88. #define XSLEEP_MS(t) Task_sleep(t/1000)
  89. #elif defined(WOLFSSL_VXWORKS)
  90. #include <hostLib.h>
  91. #include <sockLib.h>
  92. #include <arpa/inet.h>
  93. #include <string.h>
  94. #include <selectLib.h>
  95. #include <sys/types.h>
  96. #include <netinet/in.h>
  97. #include <fcntl.h>
  98. #include <sys/time.h>
  99. #include <netdb.h>
  100. #include <pthread.h>
  101. #define SOCKET_T int
  102. #elif defined(WOLFSSL_ZEPHYR)
  103. #include <string.h>
  104. #include <sys/types.h>
  105. #include <net/socket.h>
  106. #define SOCKET_T int
  107. #define SOL_SOCKET 1
  108. #define SO_REUSEADDR 201
  109. #define WOLFSSL_USE_GETADDRINFO
  110. static unsigned long inet_addr(const char *cp)
  111. {
  112. unsigned int a[4]; unsigned long ret;
  113. int i, j;
  114. for (i=0, j=0; i<4; i++) {
  115. a[i] = 0;
  116. while (cp[j] != '.' && cp[j] != '\0') {
  117. a[i] *= 10;
  118. a[i] += cp[j] - '0';
  119. j++;
  120. }
  121. }
  122. ret = ((a[3]<<24) + (a[2]<<16) + (a[1]<<8) + a[0]) ;
  123. return(ret) ;
  124. }
  125. #else
  126. #include <string.h>
  127. #include <sys/types.h>
  128. #ifndef WOLFSSL_LEANPSK
  129. #include <unistd.h>
  130. #include <netdb.h>
  131. #include <netinet/in.h>
  132. #include <netinet/tcp.h>
  133. #include <arpa/inet.h>
  134. #include <sys/ioctl.h>
  135. #include <sys/time.h>
  136. #include <sys/socket.h>
  137. #include <pthread.h>
  138. #include <fcntl.h>
  139. #ifdef TEST_IPV6
  140. #include <netdb.h>
  141. #endif
  142. #endif
  143. #define SOCKET_T int
  144. #ifndef SO_NOSIGPIPE
  145. #include <signal.h> /* ignore SIGPIPE */
  146. #endif
  147. #define SNPRINTF snprintf
  148. #define XSELECT_WAIT(x,y) do { \
  149. struct timeval tv = {(x),(y)}; \
  150. select(0, NULL, NULL, NULL, &tv); \
  151. } while (0)
  152. #define XSLEEP_US(u) XSELECT_WAIT(0,u)
  153. #define XSLEEP_MS(m) XSELECT_WAIT(0,(m)*1000)
  154. #endif /* USE_WINDOWS_API */
  155. #ifndef XSLEEP_MS
  156. #define XSLEEP_MS(t) sleep(t/1000)
  157. #endif
  158. #ifdef WOLFSSL_ASYNC_CRYPT
  159. #include <wolfssl/wolfcrypt/async.h>
  160. #endif
  161. #ifdef HAVE_CAVIUM
  162. #include <wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h>
  163. #endif
  164. #ifdef _MSC_VER
  165. /* disable conversion warning */
  166. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  167. #pragma warning(disable:4244 4996)
  168. #endif
  169. #ifndef WOLFSSL_CIPHER_LIST_MAX_SIZE
  170. #define WOLFSSL_CIPHER_LIST_MAX_SIZE 4096
  171. #endif
  172. /* Buffer for benchmark tests */
  173. #ifndef TEST_BUFFER_SIZE
  174. #define TEST_BUFFER_SIZE 16384
  175. #endif
  176. #ifndef WOLFSSL_HAVE_MIN
  177. #define WOLFSSL_HAVE_MIN
  178. static WC_INLINE word32 min(word32 a, word32 b)
  179. {
  180. return a > b ? b : a;
  181. }
  182. #endif /* WOLFSSL_HAVE_MIN */
  183. /* Socket Handling */
  184. #ifndef WOLFSSL_SOCKET_INVALID
  185. #ifdef USE_WINDOWS_API
  186. #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
  187. #elif defined(WOLFSSL_TIRTOS)
  188. #define WOLFSSL_SOCKET_INVALID ((SOCKET_T)-1)
  189. #else
  190. #define WOLFSSL_SOCKET_INVALID (SOCKET_T)(0)
  191. #endif
  192. #endif /* WOLFSSL_SOCKET_INVALID */
  193. #ifndef WOLFSSL_SOCKET_IS_INVALID
  194. #if defined(USE_WINDOWS_API) || defined(WOLFSSL_TIRTOS)
  195. #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) == WOLFSSL_SOCKET_INVALID)
  196. #else
  197. #define WOLFSSL_SOCKET_IS_INVALID(s) ((SOCKET_T)(s) < WOLFSSL_SOCKET_INVALID)
  198. #endif
  199. #endif /* WOLFSSL_SOCKET_IS_INVALID */
  200. #if defined(__MACH__) || defined(USE_WINDOWS_API)
  201. #ifndef _SOCKLEN_T
  202. typedef int socklen_t;
  203. #endif
  204. #endif
  205. /* HPUX doesn't use socklent_t for third parameter to accept, unless
  206. _XOPEN_SOURCE_EXTENDED is defined */
  207. #if !defined(__hpux__) && !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_IAR_ARM)\
  208. && !defined(WOLFSSL_ROWLEY_ARM) && !defined(WOLFSSL_KEIL_TCP_NET)
  209. typedef socklen_t* ACCEPT_THIRD_T;
  210. #else
  211. #if defined _XOPEN_SOURCE_EXTENDED
  212. typedef socklen_t* ACCEPT_THIRD_T;
  213. #else
  214. typedef int* ACCEPT_THIRD_T;
  215. #endif
  216. #endif
  217. #ifdef SINGLE_THREADED
  218. typedef unsigned int THREAD_RETURN;
  219. typedef void* THREAD_TYPE;
  220. #define WOLFSSL_THREAD
  221. #else
  222. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  223. typedef void* THREAD_RETURN;
  224. typedef pthread_t THREAD_TYPE;
  225. #define WOLFSSL_THREAD
  226. #define INFINITE -1
  227. #define WAIT_OBJECT_0 0L
  228. #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET)
  229. typedef unsigned int THREAD_RETURN;
  230. typedef int THREAD_TYPE;
  231. #define WOLFSSL_THREAD
  232. #elif defined(WOLFSSL_TIRTOS)
  233. typedef void THREAD_RETURN;
  234. typedef Task_Handle THREAD_TYPE;
  235. #define WOLFSSL_THREAD
  236. #elif defined(WOLFSSL_ZEPHYR)
  237. typedef void THREAD_RETURN;
  238. typedef struct k_thread THREAD_TYPE;
  239. #define WOLFSSL_THREAD
  240. #else
  241. typedef unsigned int THREAD_RETURN;
  242. typedef intptr_t THREAD_TYPE;
  243. #define WOLFSSL_THREAD __stdcall
  244. #endif
  245. #endif
  246. #ifdef TEST_IPV6
  247. typedef struct sockaddr_in6 SOCKADDR_IN_T;
  248. #define AF_INET_V AF_INET6
  249. #else
  250. typedef struct sockaddr_in SOCKADDR_IN_T;
  251. #define AF_INET_V AF_INET
  252. #endif
  253. #ifndef WOLFSSL_NO_TLS12
  254. #define SERVER_DEFAULT_VERSION 3
  255. #else
  256. #define SERVER_DEFAULT_VERSION 4
  257. #endif
  258. #define SERVER_DTLS_DEFAULT_VERSION (-2)
  259. #define SERVER_INVALID_VERSION (-99)
  260. #define SERVER_DOWNGRADE_VERSION (-98)
  261. #ifndef WOLFSSL_NO_TLS12
  262. #define CLIENT_DEFAULT_VERSION 3
  263. #else
  264. #define CLIENT_DEFAULT_VERSION 4
  265. #endif
  266. #define CLIENT_DTLS_DEFAULT_VERSION (-2)
  267. #define CLIENT_INVALID_VERSION (-99)
  268. #define CLIENT_DOWNGRADE_VERSION (-98)
  269. #define EITHER_DOWNGRADE_VERSION (-97)
  270. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
  271. #define DEFAULT_MIN_DHKEY_BITS 2048
  272. #define DEFAULT_MAX_DHKEY_BITS 3072
  273. #else
  274. #define DEFAULT_MIN_DHKEY_BITS 1024
  275. #define DEFAULT_MAX_DHKEY_BITS 2048
  276. #endif
  277. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
  278. #define DEFAULT_MIN_RSAKEY_BITS 2048
  279. #else
  280. #define DEFAULT_MIN_RSAKEY_BITS 1024
  281. #endif
  282. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_MAX_STRENGTH)
  283. #define DEFAULT_MIN_ECCKEY_BITS 256
  284. #else
  285. #define DEFAULT_MIN_ECCKEY_BITS 224
  286. #endif
  287. /* all certs relative to wolfSSL home directory now */
  288. #if defined(WOLFSSL_NO_CURRDIR) || defined(WOLFSSL_MDK_SHELL)
  289. #define caCertFile "certs/ca-cert.pem"
  290. #define eccCertFile "certs/server-ecc.pem"
  291. #define eccKeyFile "certs/ecc-key.pem"
  292. #define eccRsaCertFile "certs/server-ecc-rsa.pem"
  293. #define svrCertFile "certs/server-cert.pem"
  294. #define svrKeyFile "certs/server-key.pem"
  295. #define cliCertFile "certs/client-cert.pem"
  296. #define cliCertDerFile "certs/client-cert.der"
  297. #define cliCertFileExt "certs/client-cert-ext.pem"
  298. #define cliCertDerFileExt "certs/client-cert-ext.der"
  299. #define cliKeyFile "certs/client-key.pem"
  300. #define ntruCertFile "certs/ntru-cert.pem"
  301. #define ntruKeyFile "certs/ntru-key.raw"
  302. #define dhParamFile "certs/dh2048.pem"
  303. #define cliEccKeyFile "certs/ecc-client-key.pem"
  304. #define cliEccCertFile "certs/client-ecc-cert.pem"
  305. #define caEccCertFile "certs/ca-ecc-cert.pem"
  306. #define crlPemDir "certs/crl"
  307. #define edCertFile "certs/ed25519/server-ed25519-cert.pem"
  308. #define edKeyFile "certs/ed25519/server-ed25519-priv.pem"
  309. #define cliEdCertFile "certs/ed25519/client-ed25519.pem"
  310. #define cliEdKeyFile "certs/ed25519/client-ed25519-priv.pem"
  311. #define caEdCertFile "certs/ed25519/ca-ed25519.pem"
  312. #define ed448CertFile "certs/ed448/server-ed448-cert.pem"
  313. #define ed448KeyFile "certs/ed448/server-ed448-priv.pem"
  314. #define cliEd448CertFile "certs/ed448/client-ed448.pem"
  315. #define cliEd448KeyFile "certs/ed448/client-ed448-priv.pem"
  316. #define caEd448CertFile "certs/ed448/ca-ed448.pem"
  317. #ifdef HAVE_WNR
  318. /* Whitewood netRandom default config file */
  319. #define wnrConfig "wnr-example.conf"
  320. #endif
  321. #else
  322. #define caCertFile "./certs/ca-cert.pem"
  323. #define eccCertFile "./certs/server-ecc.pem"
  324. #define eccKeyFile "./certs/ecc-key.pem"
  325. #define eccRsaCertFile "./certs/server-ecc-rsa.pem"
  326. #define svrCertFile "./certs/server-cert.pem"
  327. #define svrKeyFile "./certs/server-key.pem"
  328. #define cliCertFile "./certs/client-cert.pem"
  329. #define cliCertDerFile "./certs/client-cert.der"
  330. #define cliCertFileExt "./certs/client-cert-ext.pem"
  331. #define cliCertDerFileExt "./certs/client-cert-ext.der"
  332. #define cliKeyFile "./certs/client-key.pem"
  333. #define ntruCertFile "./certs/ntru-cert.pem"
  334. #define ntruKeyFile "./certs/ntru-key.raw"
  335. #define dhParamFile "./certs/dh2048.pem"
  336. #define cliEccKeyFile "./certs/ecc-client-key.pem"
  337. #define cliEccCertFile "./certs/client-ecc-cert.pem"
  338. #define caEccCertFile "./certs/ca-ecc-cert.pem"
  339. #define crlPemDir "./certs/crl"
  340. #define edCertFile "./certs/ed25519/server-ed25519-cert.pem"
  341. #define edKeyFile "./certs/ed25519/server-ed25519-priv.pem"
  342. #define cliEdCertFile "./certs/ed25519/client-ed25519.pem"
  343. #define cliEdKeyFile "./certs/ed25519/client-ed25519-priv.pem"
  344. #define caEdCertFile "./certs/ed25519/ca-ed25519.pem"
  345. #define ed448CertFile "./certs/ed448/server-ed448-cert.pem"
  346. #define ed448KeyFile "./certs/ed448/server-ed448-priv.pem"
  347. #define cliEd448CertFile "./certs/ed448/client-ed448.pem"
  348. #define cliEd448KeyFile "./certs/ed448/client-ed448-priv.pem"
  349. #define caEd448CertFile "./certs/ed448/ca-ed448.pem"
  350. #ifdef HAVE_WNR
  351. /* Whitewood netRandom default config file */
  352. #define wnrConfig "./wnr-example.conf"
  353. #endif
  354. #endif
  355. typedef struct tcp_ready {
  356. word16 ready; /* predicate */
  357. word16 port;
  358. char* srfName; /* server ready file name */
  359. #if defined(_POSIX_THREADS) && !defined(__MINGW32__)
  360. pthread_mutex_t mutex;
  361. pthread_cond_t cond;
  362. #endif
  363. } tcp_ready;
  364. static WC_INLINE void InitTcpReady(tcp_ready* ready)
  365. {
  366. ready->ready = 0;
  367. ready->port = 0;
  368. ready->srfName = NULL;
  369. #ifdef SINGLE_THREADED
  370. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  371. pthread_mutex_init(&ready->mutex, 0);
  372. pthread_cond_init(&ready->cond, 0);
  373. #endif
  374. }
  375. static WC_INLINE void FreeTcpReady(tcp_ready* ready)
  376. {
  377. #ifdef SINGLE_THREADED
  378. (void)ready;
  379. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  380. pthread_mutex_destroy(&ready->mutex);
  381. pthread_cond_destroy(&ready->cond);
  382. #else
  383. (void)ready;
  384. #endif
  385. }
  386. typedef WOLFSSL_METHOD* (*method_provider)(void);
  387. typedef void (*ctx_callback)(WOLFSSL_CTX* ctx);
  388. typedef void (*ssl_callback)(WOLFSSL* ssl);
  389. typedef struct callback_functions {
  390. method_provider method;
  391. ctx_callback ctx_ready;
  392. ssl_callback ssl_ready;
  393. ssl_callback on_result;
  394. WOLFSSL_CTX* ctx;
  395. unsigned char isSharedCtx:1;
  396. } callback_functions;
  397. typedef struct func_args {
  398. int argc;
  399. char** argv;
  400. int return_code;
  401. tcp_ready* signal;
  402. callback_functions *callbacks;
  403. } func_args;
  404. void wait_tcp_ready(func_args*);
  405. #ifdef WOLFSSL_ZEPHYR
  406. typedef void THREAD_FUNC(void*, void*, void*);
  407. #else
  408. typedef THREAD_RETURN WOLFSSL_THREAD THREAD_FUNC(void*);
  409. #endif
  410. void start_thread(THREAD_FUNC, func_args*, THREAD_TYPE*);
  411. void join_thread(THREAD_TYPE);
  412. /* wolfSSL */
  413. #ifndef TEST_IPV6
  414. static const char* const wolfSSLIP = "127.0.0.1";
  415. #else
  416. static const char* const wolfSSLIP = "::1";
  417. #endif
  418. static const word16 wolfSSLPort = 11111;
  419. #ifndef MY_EX_USAGE
  420. #define MY_EX_USAGE 2
  421. #endif
  422. #ifndef EXIT_FAILURE
  423. #define EXIT_FAILURE 1
  424. #endif
  425. #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR)
  426. #ifndef EXIT_SUCCESS
  427. #define EXIT_SUCCESS 0
  428. #endif
  429. #define XEXIT(rc) return rc
  430. #define XEXIT_T(rc) return (THREAD_RETURN)rc
  431. #else
  432. #define XEXIT(rc) exit((int)(rc))
  433. #define XEXIT_T(rc) exit((int)(rc))
  434. #endif
  435. static WC_INLINE
  436. #if defined(WOLFSSL_FORCE_MALLOC_FAIL_TEST) || defined(WOLFSSL_ZEPHYR)
  437. THREAD_RETURN
  438. #else
  439. WC_NORETURN void
  440. #endif
  441. err_sys(const char* msg)
  442. {
  443. printf("wolfSSL error: %s\n", msg);
  444. #if !defined(__GNUC__)
  445. /* scan-build (which pretends to be gnuc) can get confused and think the
  446. * msg pointer can be null even when hardcoded and then it won't exit,
  447. * making null pointer checks above the err_sys() call useless.
  448. * We could just always exit() but some compilers will complain about no
  449. * possible return, with gcc we know the attribute to handle that with
  450. * WC_NORETURN. */
  451. if (msg)
  452. #endif
  453. {
  454. XEXIT_T(EXIT_FAILURE);
  455. }
  456. }
  457. extern int myoptind;
  458. extern char* myoptarg;
  459. static WC_INLINE int mygetopt(int argc, char** argv, const char* optstring)
  460. {
  461. static char* next = NULL;
  462. char c;
  463. char* cp;
  464. /* Added sanity check becuase scan-build complains argv[myoptind] access
  465. * results in a null pointer dereference. */
  466. if (argv == NULL) {
  467. myoptarg = NULL;
  468. return -1;
  469. }
  470. if (myoptind == 0)
  471. next = NULL; /* we're starting new/over */
  472. if (next == NULL || *next == '\0') {
  473. if (myoptind == 0)
  474. myoptind++;
  475. if (myoptind >= argc || argv[myoptind] == NULL ||
  476. argv[myoptind][0] != '-' || argv[myoptind][1] == '\0') {
  477. myoptarg = NULL;
  478. if (myoptind < argc)
  479. myoptarg = argv[myoptind];
  480. return -1;
  481. }
  482. if (strcmp(argv[myoptind], "--") == 0) {
  483. myoptind++;
  484. myoptarg = NULL;
  485. if (myoptind < argc)
  486. myoptarg = argv[myoptind];
  487. return -1;
  488. }
  489. next = argv[myoptind];
  490. next++; /* skip - */
  491. myoptind++;
  492. }
  493. c = *next++;
  494. /* The C++ strchr can return a different value */
  495. cp = (char*)strchr(optstring, c);
  496. if (cp == NULL || c == ':')
  497. return '?';
  498. cp++;
  499. if (*cp == ':') {
  500. if (*next != '\0') {
  501. myoptarg = next;
  502. next = NULL;
  503. }
  504. else if (myoptind < argc) {
  505. myoptarg = argv[myoptind];
  506. myoptind++;
  507. }
  508. else
  509. return '?';
  510. }
  511. return c;
  512. }
  513. #ifdef WOLFSSL_ENCRYPTED_KEYS
  514. static WC_INLINE int PasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  515. {
  516. (void)rw;
  517. (void)userdata;
  518. if (userdata != NULL) {
  519. strncpy(passwd, (char*)userdata, sz);
  520. return (int)XSTRLEN((char*)userdata);
  521. }
  522. else {
  523. strncpy(passwd, "yassl123", sz);
  524. return 8;
  525. }
  526. }
  527. #endif
  528. static const char* client_showpeer_msg[][8] = {
  529. /* English */
  530. {
  531. "SSL version is",
  532. "SSL cipher suite is",
  533. "SSL curve name is",
  534. "SSL DH size is",
  535. "SSL reused session",
  536. "Alternate cert chain used",
  537. "peer's cert info:",
  538. NULL
  539. },
  540. #ifndef NO_MULTIBYTE_PRINT
  541. /* Japanese */
  542. {
  543. "SSL バージョンは",
  544. "SSL 暗号スイートは",
  545. "SSL 曲線名は",
  546. "SSL DH サイズは",
  547. "SSL 再利用セッション",
  548. "代替証明チェーンを使用",
  549. "相手方証明書情報",
  550. NULL
  551. },
  552. #endif
  553. };
  554. #if defined(KEEP_PEER_CERT) || defined(KEEP_OUR_CERT) || defined(SESSION_CERTS)
  555. static const char* client_showx509_msg[][5] = {
  556. /* English */
  557. {
  558. "issuer",
  559. "subject",
  560. "altname",
  561. "serial number",
  562. NULL
  563. },
  564. #ifndef NO_MULTIBYTE_PRINT
  565. /* Japanese */
  566. {
  567. "発行者",
  568. "サブジェクト",
  569. "代替名",
  570. "シリアル番号",
  571. NULL
  572. },
  573. #endif
  574. };
  575. /* lng_index is to specify the language for displaying message. */
  576. /* 0:English, 1:Japanese */
  577. static WC_INLINE void ShowX509Ex(WOLFSSL_X509* x509, const char* hdr,
  578. int lng_index)
  579. {
  580. char* altName;
  581. char* issuer;
  582. char* subject;
  583. byte serial[32];
  584. int ret;
  585. int sz = sizeof(serial);
  586. const char** words = client_showx509_msg[lng_index];
  587. if (x509 == NULL) {
  588. printf("%s No Cert\n", hdr);
  589. return;
  590. }
  591. issuer = wolfSSL_X509_NAME_oneline(
  592. wolfSSL_X509_get_issuer_name(x509), 0, 0);
  593. subject = wolfSSL_X509_NAME_oneline(
  594. wolfSSL_X509_get_subject_name(x509), 0, 0);
  595. printf("%s\n %s : %s\n %s: %s\n", hdr, words[0], issuer, words[1], subject);
  596. while ( (altName = wolfSSL_X509_get_next_altname(x509)) != NULL)
  597. printf(" %s = %s\n", words[2], altName);
  598. ret = wolfSSL_X509_get_serial_number(x509, serial, &sz);
  599. if (ret == WOLFSSL_SUCCESS) {
  600. int i;
  601. int strLen;
  602. char serialMsg[80];
  603. /* testsuite has multiple threads writing to stdout, get output
  604. message ready to write once */
  605. strLen = sprintf(serialMsg, " %s", words[3]);
  606. for (i = 0; i < sz; i++)
  607. sprintf(serialMsg + strLen + (i*3), ":%02x ", serial[i]);
  608. printf("%s\n", serialMsg);
  609. }
  610. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  611. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  612. #if defined(SHOW_CERTS) && defined(OPENSSL_EXTRA)
  613. {
  614. WOLFSSL_BIO* bio;
  615. char buf[256]; /* should be size of ASN_NAME_MAX */
  616. int textSz;
  617. /* print out domain component if certificate has it */
  618. textSz = wolfSSL_X509_NAME_get_text_by_NID(
  619. wolfSSL_X509_get_subject_name(x509), NID_domainComponent,
  620. buf, sizeof(buf));
  621. if (textSz > 0) {
  622. printf("Domain Component = %s\n", buf);
  623. }
  624. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  625. if (bio != NULL) {
  626. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  627. wolfSSL_X509_print(bio, x509);
  628. wolfSSL_BIO_free(bio);
  629. }
  630. }
  631. #endif /* SHOW_CERTS && OPENSSL_EXTRA */
  632. }
  633. /* original ShowX509 to maintain compatibility */
  634. static WC_INLINE void ShowX509(WOLFSSL_X509* x509, const char* hdr)
  635. {
  636. ShowX509Ex(x509, hdr, 0);
  637. }
  638. #endif /* KEEP_PEER_CERT || KEEP_OUR_CERT || SESSION_CERTS */
  639. #if defined(SHOW_CERTS) && defined(SESSION_CERTS) && \
  640. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  641. static WC_INLINE void ShowX509Chain(WOLFSSL_X509_CHAIN* chain, int count,
  642. const char* hdr)
  643. {
  644. int i;
  645. int length;
  646. unsigned char buffer[3072];
  647. WOLFSSL_X509* chainX509;
  648. for (i = 0; i < count; i++) {
  649. wolfSSL_get_chain_cert_pem(chain, i, buffer, sizeof(buffer), &length);
  650. buffer[length] = 0;
  651. printf("\n%s: %d has length %d data = \n%s\n", hdr, i, length, buffer);
  652. chainX509 = wolfSSL_get_chain_X509(chain, i);
  653. if (chainX509)
  654. ShowX509(chainX509, hdr);
  655. else
  656. printf("get_chain_X509 failed\n");
  657. wolfSSL_FreeX509(chainX509);
  658. }
  659. }
  660. #endif /* SHOW_CERTS && SESSION_CERTS */
  661. /* lng_index is to specify the language for displaying message. */
  662. /* 0:English, 1:Japanese */
  663. static WC_INLINE void showPeerEx(WOLFSSL* ssl, int lng_index)
  664. {
  665. WOLFSSL_CIPHER* cipher;
  666. const char** words = client_showpeer_msg[lng_index];
  667. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \
  668. !defined(NO_DH)
  669. const char *name;
  670. #endif
  671. #ifndef NO_DH
  672. int bits;
  673. #endif
  674. #ifdef KEEP_PEER_CERT
  675. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  676. if (peer)
  677. ShowX509Ex(peer, words[6], lng_index);
  678. else
  679. printf("peer has no cert!\n");
  680. wolfSSL_FreeX509(peer);
  681. #endif
  682. #if defined(SHOW_CERTS) && defined(KEEP_OUR_CERT) && \
  683. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  684. ShowX509(wolfSSL_get_certificate(ssl), "our cert info:");
  685. printf("Peer verify result = %lu\n", wolfSSL_get_verify_result(ssl));
  686. #endif /* SHOW_CERTS && KEEP_OUR_CERT */
  687. printf("%s %s\n", words[0], wolfSSL_get_version(ssl));
  688. cipher = wolfSSL_get_current_cipher(ssl);
  689. #ifdef HAVE_QSH
  690. printf("%s %s%s\n", words[1], (wolfSSL_isQSH(ssl))? "QSH:": "",
  691. wolfSSL_CIPHER_get_name(cipher));
  692. #else
  693. printf("%s %s\n", words[1], wolfSSL_CIPHER_get_name(cipher));
  694. #endif
  695. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \
  696. !defined(NO_DH)
  697. if ((name = wolfSSL_get_curve_name(ssl)) != NULL)
  698. printf("%s %s\n", words[2], name);
  699. #endif
  700. #ifndef NO_DH
  701. else if ((bits = wolfSSL_GetDhKey_Sz(ssl)) > 0)
  702. printf("%s %d bits\n", words[3], bits);
  703. #endif
  704. if (wolfSSL_session_reused(ssl))
  705. printf("%s\n", words[4]);
  706. #ifdef WOLFSSL_ALT_CERT_CHAINS
  707. if (wolfSSL_is_peer_alt_cert_chain(ssl))
  708. printf("%s\n", words[5]);
  709. #endif
  710. #if defined(SHOW_CERTS) && defined(SESSION_CERTS) && \
  711. (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
  712. {
  713. WOLFSSL_X509_CHAIN* chain;
  714. chain = wolfSSL_get_peer_chain(ssl);
  715. ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "session cert");
  716. #ifdef WOLFSSL_ALT_CERT_CHAINS
  717. if (wolfSSL_is_peer_alt_cert_chain(ssl)) {
  718. chain = wolfSSL_get_peer_alt_chain(ssl);
  719. ShowX509Chain(chain, wolfSSL_get_chain_count(chain), "alt cert");
  720. }
  721. #endif
  722. }
  723. #endif /* SHOW_CERTS && SESSION_CERTS */
  724. (void)ssl;
  725. }
  726. /* original showPeer to maintain compatibility */
  727. static WC_INLINE void showPeer(WOLFSSL* ssl)
  728. {
  729. showPeerEx(ssl, 0);
  730. }
  731. static WC_INLINE void build_addr(SOCKADDR_IN_T* addr, const char* peer,
  732. word16 port, int udp, int sctp)
  733. {
  734. int useLookup = 0;
  735. (void)useLookup;
  736. (void)udp;
  737. (void)sctp;
  738. if (addr == NULL)
  739. err_sys("invalid argument to build_addr, addr is NULL");
  740. XMEMSET(addr, 0, sizeof(SOCKADDR_IN_T));
  741. #ifndef TEST_IPV6
  742. /* peer could be in human readable form */
  743. if ( ((size_t)peer != INADDR_ANY) && isalpha((int)peer[0])) {
  744. #ifndef WOLFSSL_USE_GETADDRINFO
  745. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  746. int err;
  747. struct hostent* entry = gethostbyname(peer, &err);
  748. #elif defined(WOLFSSL_TIRTOS)
  749. struct hostent* entry = DNSGetHostByName(peer);
  750. #elif defined(WOLFSSL_VXWORKS)
  751. struct hostent* entry = (struct hostent*)hostGetByName((char*)peer);
  752. #else
  753. struct hostent* entry = gethostbyname(peer);
  754. #endif
  755. if (entry) {
  756. XMEMCPY(&addr->sin_addr.s_addr, entry->h_addr_list[0],
  757. entry->h_length);
  758. useLookup = 1;
  759. }
  760. #else
  761. struct zsock_addrinfo hints, *addrInfo;
  762. char portStr[6];
  763. XSNPRINTF(portStr, sizeof(portStr), "%d", port);
  764. memset(&hints, 0, sizeof(hints));
  765. hints.ai_family = AF_UNSPEC;
  766. hints.ai_socktype = udp ? SOCK_DGRAM : SOCK_STREAM;
  767. hints.ai_protocol = udp ? IPPROTO_UDP : IPPROTO_TCP;
  768. if (getaddrinfo((char*)peer, portStr, &hints, &addrInfo) == 0) {
  769. XMEMCPY(addr, addrInfo->ai_addr, sizeof(*addr));
  770. useLookup = 1;
  771. }
  772. #endif
  773. else
  774. err_sys("no entry for host");
  775. }
  776. #endif
  777. #ifndef TEST_IPV6
  778. #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
  779. addr->sin_family = PF_INET;
  780. #else
  781. addr->sin_family = AF_INET_V;
  782. #endif
  783. addr->sin_port = XHTONS(port);
  784. if ((size_t)peer == INADDR_ANY)
  785. addr->sin_addr.s_addr = INADDR_ANY;
  786. else {
  787. if (!useLookup)
  788. addr->sin_addr.s_addr = inet_addr(peer);
  789. }
  790. #else
  791. addr->sin6_family = AF_INET_V;
  792. addr->sin6_port = XHTONS(port);
  793. if ((size_t)peer == INADDR_ANY) {
  794. addr->sin6_addr = in6addr_any;
  795. }
  796. else {
  797. #if defined(HAVE_GETADDRINFO) || defined(WOLF_C99)
  798. struct addrinfo hints;
  799. struct addrinfo* answer = NULL;
  800. int ret;
  801. char strPort[80];
  802. XMEMSET(&hints, 0, sizeof(hints));
  803. hints.ai_family = AF_INET_V;
  804. if (udp) {
  805. hints.ai_socktype = SOCK_DGRAM;
  806. hints.ai_protocol = IPPROTO_UDP;
  807. }
  808. #ifdef WOLFSSL_SCTP
  809. else if (sctp) {
  810. hints.ai_socktype = SOCK_STREAM;
  811. hints.ai_protocol = IPPROTO_SCTP;
  812. }
  813. #endif
  814. else {
  815. hints.ai_socktype = SOCK_STREAM;
  816. hints.ai_protocol = IPPROTO_TCP;
  817. }
  818. SNPRINTF(strPort, sizeof(strPort), "%d", port);
  819. strPort[79] = '\0';
  820. ret = getaddrinfo(peer, strPort, &hints, &answer);
  821. if (ret < 0 || answer == NULL)
  822. err_sys("getaddrinfo failed");
  823. XMEMCPY(addr, answer->ai_addr, answer->ai_addrlen);
  824. freeaddrinfo(answer);
  825. #else
  826. printf("no ipv6 getaddrinfo, loopback only tests/examples\n");
  827. addr->sin6_addr = in6addr_loopback;
  828. #endif
  829. }
  830. #endif
  831. }
  832. static WC_INLINE void tcp_socket(SOCKET_T* sockfd, int udp, int sctp)
  833. {
  834. (void)sctp;
  835. if (udp)
  836. *sockfd = socket(AF_INET_V, SOCK_DGRAM, IPPROTO_UDP);
  837. #ifdef WOLFSSL_SCTP
  838. else if (sctp)
  839. *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_SCTP);
  840. #endif
  841. else
  842. *sockfd = socket(AF_INET_V, SOCK_STREAM, IPPROTO_TCP);
  843. if(WOLFSSL_SOCKET_IS_INVALID(*sockfd)) {
  844. err_sys("socket failed\n");
  845. }
  846. #ifndef USE_WINDOWS_API
  847. #ifdef SO_NOSIGPIPE
  848. {
  849. int on = 1;
  850. socklen_t len = sizeof(on);
  851. int res = setsockopt(*sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, len);
  852. if (res < 0)
  853. err_sys("setsockopt SO_NOSIGPIPE failed\n");
  854. }
  855. #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\
  856. defined(WOLFSSL_KEIL_TCP_NET) || defined(WOLFSSL_ZEPHYR)
  857. /* nothing to define */
  858. #else /* no S_NOSIGPIPE */
  859. signal(SIGPIPE, SIG_IGN);
  860. #endif /* S_NOSIGPIPE */
  861. #if defined(TCP_NODELAY)
  862. if (!udp && !sctp)
  863. {
  864. int on = 1;
  865. socklen_t len = sizeof(on);
  866. int res = setsockopt(*sockfd, IPPROTO_TCP, TCP_NODELAY, &on, len);
  867. if (res < 0)
  868. err_sys("setsockopt TCP_NODELAY failed\n");
  869. }
  870. #endif
  871. #endif /* USE_WINDOWS_API */
  872. }
  873. static WC_INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port,
  874. int udp, int sctp, WOLFSSL* ssl)
  875. {
  876. SOCKADDR_IN_T addr;
  877. build_addr(&addr, ip, port, udp, sctp);
  878. if (udp) {
  879. wolfSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
  880. }
  881. tcp_socket(sockfd, udp, sctp);
  882. if (!udp) {
  883. if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  884. err_sys("tcp connect failed");
  885. }
  886. }
  887. static WC_INLINE void udp_connect(SOCKET_T* sockfd, void* addr, int addrSz)
  888. {
  889. if (connect(*sockfd, (const struct sockaddr*)addr, addrSz) != 0)
  890. err_sys("tcp connect failed");
  891. }
  892. enum {
  893. TEST_SELECT_FAIL,
  894. TEST_TIMEOUT,
  895. TEST_RECV_READY,
  896. TEST_SEND_READY,
  897. TEST_ERROR_READY
  898. };
  899. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && \
  900. !defined(WOLFSSL_TIRTOS)
  901. static WC_INLINE int tcp_select_ex(SOCKET_T socketfd, int to_sec, int rx)
  902. {
  903. fd_set fds, errfds;
  904. fd_set* recvfds = NULL;
  905. fd_set* sendfds = NULL;
  906. SOCKET_T nfds = socketfd + 1;
  907. #if !defined(__INTEGRITY)
  908. struct timeval timeout = {(to_sec > 0) ? to_sec : 0, 0};
  909. #else
  910. struct timeval timeout;
  911. #endif
  912. int result;
  913. FD_ZERO(&fds);
  914. FD_SET(socketfd, &fds);
  915. FD_ZERO(&errfds);
  916. FD_SET(socketfd, &errfds);
  917. if (rx)
  918. recvfds = &fds;
  919. else
  920. sendfds = &fds;
  921. #if defined(__INTEGRITY)
  922. timeout.tv_sec = (long long)(to_sec > 0) ? to_sec : 0, 0;
  923. #endif
  924. result = select(nfds, recvfds, sendfds, &errfds, &timeout);
  925. if (result == 0)
  926. return TEST_TIMEOUT;
  927. else if (result > 0) {
  928. if (FD_ISSET(socketfd, &fds)) {
  929. if (rx)
  930. return TEST_RECV_READY;
  931. else
  932. return TEST_SEND_READY;
  933. }
  934. else if(FD_ISSET(socketfd, &errfds))
  935. return TEST_ERROR_READY;
  936. }
  937. return TEST_SELECT_FAIL;
  938. }
  939. static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
  940. {
  941. return tcp_select_ex(socketfd, to_sec, 1);
  942. }
  943. static WC_INLINE int tcp_select_tx(SOCKET_T socketfd, int to_sec)
  944. {
  945. return tcp_select_ex(socketfd, to_sec, 0);
  946. }
  947. #elif defined(WOLFSSL_TIRTOS) || defined(WOLFSSL_KEIL_TCP_NET)
  948. static WC_INLINE int tcp_select(SOCKET_T socketfd, int to_sec)
  949. {
  950. return TEST_RECV_READY;
  951. }
  952. static WC_INLINE int tcp_select_tx(SOCKET_T socketfd, int to_sec)
  953. {
  954. return TEST_SEND_READY;
  955. }
  956. #endif /* !WOLFSSL_MDK_ARM */
  957. static WC_INLINE void tcp_listen(SOCKET_T* sockfd, word16* port, int useAnyAddr,
  958. int udp, int sctp)
  959. {
  960. SOCKADDR_IN_T addr;
  961. /* don't use INADDR_ANY by default, firewall may block, make user switch
  962. on */
  963. build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), *port, udp, sctp);
  964. tcp_socket(sockfd, udp, sctp);
  965. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM)\
  966. && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR)
  967. {
  968. int res, on = 1;
  969. socklen_t len = sizeof(on);
  970. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  971. if (res < 0)
  972. err_sys("setsockopt SO_REUSEADDR failed\n");
  973. }
  974. #endif
  975. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  976. err_sys("tcp bind failed");
  977. if (!udp) {
  978. #ifdef WOLFSSL_KEIL_TCP_NET
  979. #define SOCK_LISTEN_MAX_QUEUE 1
  980. #else
  981. #define SOCK_LISTEN_MAX_QUEUE 5
  982. #endif
  983. if (listen(*sockfd, SOCK_LISTEN_MAX_QUEUE) != 0)
  984. err_sys("tcp listen failed");
  985. }
  986. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_TIRTOS) \
  987. && !defined(WOLFSSL_ZEPHYR)
  988. if (*port == 0) {
  989. socklen_t len = sizeof(addr);
  990. if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
  991. #ifndef TEST_IPV6
  992. *port = XNTOHS(addr.sin_port);
  993. #else
  994. *port = XNTOHS(addr.sin6_port);
  995. #endif
  996. }
  997. }
  998. #endif
  999. }
  1000. #if 0
  1001. static WC_INLINE int udp_read_connect(SOCKET_T sockfd)
  1002. {
  1003. SOCKADDR_IN_T cliaddr;
  1004. byte b[1500];
  1005. int n;
  1006. socklen_t len = sizeof(cliaddr);
  1007. n = (int)recvfrom(sockfd, (char*)b, sizeof(b), MSG_PEEK,
  1008. (struct sockaddr*)&cliaddr, &len);
  1009. if (n > 0) {
  1010. if (connect(sockfd, (const struct sockaddr*)&cliaddr,
  1011. sizeof(cliaddr)) != 0)
  1012. err_sys("udp connect failed");
  1013. }
  1014. else
  1015. err_sys("recvfrom failed");
  1016. return sockfd;
  1017. }
  1018. #endif
  1019. static WC_INLINE void udp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
  1020. int useAnyAddr, word16 port, func_args* args)
  1021. {
  1022. SOCKADDR_IN_T addr;
  1023. (void)args;
  1024. build_addr(&addr, (useAnyAddr ? INADDR_ANY : wolfSSLIP), port, 1, 0);
  1025. tcp_socket(sockfd, 1, 0);
  1026. #if !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_ARM) \
  1027. && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_ZEPHYR)
  1028. {
  1029. int res, on = 1;
  1030. socklen_t len = sizeof(on);
  1031. res = setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, len);
  1032. if (res < 0)
  1033. err_sys("setsockopt SO_REUSEADDR failed\n");
  1034. }
  1035. #endif
  1036. if (bind(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
  1037. err_sys("tcp bind failed");
  1038. #if (defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API)) && !defined(WOLFSSL_TIRTOS)
  1039. if (port == 0) {
  1040. socklen_t len = sizeof(addr);
  1041. if (getsockname(*sockfd, (struct sockaddr*)&addr, &len) == 0) {
  1042. #ifndef TEST_IPV6
  1043. port = XNTOHS(addr.sin_port);
  1044. #else
  1045. port = XNTOHS(addr.sin6_port);
  1046. #endif
  1047. }
  1048. }
  1049. #endif
  1050. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
  1051. /* signal ready to accept data */
  1052. {
  1053. tcp_ready* ready = args->signal;
  1054. pthread_mutex_lock(&ready->mutex);
  1055. ready->ready = 1;
  1056. ready->port = port;
  1057. pthread_cond_signal(&ready->cond);
  1058. pthread_mutex_unlock(&ready->mutex);
  1059. }
  1060. #elif defined (WOLFSSL_TIRTOS)
  1061. /* Need mutex? */
  1062. tcp_ready* ready = args->signal;
  1063. ready->ready = 1;
  1064. ready->port = port;
  1065. #else
  1066. (void)port;
  1067. #endif
  1068. *clientfd = *sockfd;
  1069. }
  1070. static WC_INLINE void tcp_accept(SOCKET_T* sockfd, SOCKET_T* clientfd,
  1071. func_args* args, word16 port, int useAnyAddr,
  1072. int udp, int sctp, int ready_file, int do_listen)
  1073. {
  1074. SOCKADDR_IN_T client;
  1075. socklen_t client_len = sizeof(client);
  1076. tcp_ready* ready = NULL;
  1077. (void) ready; /* Account for case when "ready" is not used */
  1078. if (udp) {
  1079. udp_accept(sockfd, clientfd, useAnyAddr, port, args);
  1080. return;
  1081. }
  1082. if(do_listen) {
  1083. tcp_listen(sockfd, &port, useAnyAddr, udp, sctp);
  1084. #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
  1085. /* signal ready to tcp_accept */
  1086. if (args)
  1087. ready = args->signal;
  1088. if (ready) {
  1089. pthread_mutex_lock(&ready->mutex);
  1090. ready->ready = 1;
  1091. ready->port = port;
  1092. pthread_cond_signal(&ready->cond);
  1093. pthread_mutex_unlock(&ready->mutex);
  1094. }
  1095. #elif defined (WOLFSSL_TIRTOS)
  1096. /* Need mutex? */
  1097. if (args)
  1098. ready = args->signal;
  1099. if (ready) {
  1100. ready->ready = 1;
  1101. ready->port = port;
  1102. }
  1103. #endif
  1104. if (ready_file) {
  1105. #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
  1106. XFILE srf = NULL;
  1107. if (args)
  1108. ready = args->signal;
  1109. if (ready) {
  1110. srf = XFOPEN(ready->srfName, "w");
  1111. if (srf) {
  1112. /* let's write port sever is listening on to ready file
  1113. external monitor can then do ephemeral ports by passing
  1114. -p 0 to server on supported platforms with -R ready_file
  1115. client can then wait for existence of ready_file and see
  1116. which port the server is listening on. */
  1117. fprintf(srf, "%d\n", (int)port);
  1118. fclose(srf);
  1119. }
  1120. }
  1121. #endif
  1122. }
  1123. }
  1124. *clientfd = accept(*sockfd, (struct sockaddr*)&client,
  1125. (ACCEPT_THIRD_T)&client_len);
  1126. if(WOLFSSL_SOCKET_IS_INVALID(*clientfd)) {
  1127. err_sys("tcp accept failed");
  1128. }
  1129. }
  1130. static WC_INLINE void tcp_set_nonblocking(SOCKET_T* sockfd)
  1131. {
  1132. #ifdef USE_WINDOWS_API
  1133. unsigned long blocking = 1;
  1134. int ret = ioctlsocket(*sockfd, FIONBIO, &blocking);
  1135. if (ret == SOCKET_ERROR)
  1136. err_sys("ioctlsocket failed");
  1137. #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
  1138. || defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) \
  1139. || defined(WOLFSSL_ZEPHYR)
  1140. /* non blocking not supported, for now */
  1141. #else
  1142. int flags = fcntl(*sockfd, F_GETFL, 0);
  1143. if (flags < 0)
  1144. err_sys("fcntl get failed");
  1145. flags = fcntl(*sockfd, F_SETFL, flags | O_NONBLOCK);
  1146. if (flags < 0)
  1147. err_sys("fcntl set failed");
  1148. #endif
  1149. }
  1150. #ifndef NO_PSK
  1151. /* identity is OpenSSL testing default for openssl s_client, keep same */
  1152. static const char* kIdentityStr = "Client_identity";
  1153. static WC_INLINE unsigned int my_psk_client_cb(WOLFSSL* ssl, const char* hint,
  1154. char* identity, unsigned int id_max_len, unsigned char* key,
  1155. unsigned int key_max_len)
  1156. {
  1157. (void)ssl;
  1158. (void)hint;
  1159. (void)key_max_len;
  1160. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  1161. XSTRNCPY(identity, kIdentityStr, id_max_len);
  1162. if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
  1163. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  1164. unsigned binary */
  1165. key[0] = 0x1a;
  1166. key[1] = 0x2b;
  1167. key[2] = 0x3c;
  1168. key[3] = 0x4d;
  1169. return 4; /* length of key in octets or 0 for error */
  1170. }
  1171. else {
  1172. int i;
  1173. int b = 0x01;
  1174. for (i = 0; i < 32; i++, b += 0x22) {
  1175. if (b >= 0x100)
  1176. b = 0x01;
  1177. key[i] = b;
  1178. }
  1179. return 32; /* length of key in octets or 0 for error */
  1180. }
  1181. }
  1182. static WC_INLINE unsigned int my_psk_server_cb(WOLFSSL* ssl, const char* identity,
  1183. unsigned char* key, unsigned int key_max_len)
  1184. {
  1185. (void)ssl;
  1186. (void)key_max_len;
  1187. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  1188. if (XSTRNCMP(identity, kIdentityStr, XSTRLEN(kIdentityStr)) != 0)
  1189. return 0;
  1190. if (wolfSSL_GetVersion(ssl) < WOLFSSL_TLSV1_3) {
  1191. /* test key in hex is 0x1a2b3c4d , in decimal 439,041,101 , we're using
  1192. unsigned binary */
  1193. key[0] = 0x1a;
  1194. key[1] = 0x2b;
  1195. key[2] = 0x3c;
  1196. key[3] = 0x4d;
  1197. return 4; /* length of key in octets or 0 for error */
  1198. }
  1199. else {
  1200. int i;
  1201. int b = 0x01;
  1202. for (i = 0; i < 32; i++, b += 0x22) {
  1203. if (b >= 0x100)
  1204. b = 0x01;
  1205. key[i] = b;
  1206. }
  1207. return 32; /* length of key in octets or 0 for error */
  1208. }
  1209. }
  1210. static WC_INLINE unsigned int my_psk_client_tls13_cb(WOLFSSL* ssl,
  1211. const char* hint, char* identity, unsigned int id_max_len,
  1212. unsigned char* key, unsigned int key_max_len, const char** ciphersuite)
  1213. {
  1214. int i;
  1215. int b = 0x01;
  1216. const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl);
  1217. (void)ssl;
  1218. (void)hint;
  1219. (void)key_max_len;
  1220. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  1221. XSTRNCPY(identity, kIdentityStr, id_max_len);
  1222. for (i = 0; i < 32; i++, b += 0x22) {
  1223. if (b >= 0x100)
  1224. b = 0x01;
  1225. key[i] = b;
  1226. }
  1227. *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256";
  1228. return 32; /* length of key in octets or 0 for error */
  1229. }
  1230. static WC_INLINE unsigned int my_psk_server_tls13_cb(WOLFSSL* ssl,
  1231. const char* identity, unsigned char* key, unsigned int key_max_len,
  1232. const char** ciphersuite)
  1233. {
  1234. int i;
  1235. int b = 0x01;
  1236. const char* userCipher = (const char*)wolfSSL_get_psk_callback_ctx(ssl);
  1237. (void)ssl;
  1238. (void)key_max_len;
  1239. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  1240. if (XSTRNCMP(identity, kIdentityStr, XSTRLEN(kIdentityStr)) != 0)
  1241. return 0;
  1242. for (i = 0; i < 32; i++, b += 0x22) {
  1243. if (b >= 0x100)
  1244. b = 0x01;
  1245. key[i] = b;
  1246. }
  1247. *ciphersuite = userCipher ? userCipher : "TLS13-AES128-GCM-SHA256";
  1248. return 32; /* length of key in octets or 0 for error */
  1249. }
  1250. #endif /* !NO_PSK */
  1251. #if defined(WOLFSSL_USER_CURRTIME)
  1252. extern double current_time(int reset);
  1253. #elif defined(USE_WINDOWS_API)
  1254. #define WIN32_LEAN_AND_MEAN
  1255. #include <windows.h>
  1256. static WC_INLINE double current_time(int reset)
  1257. {
  1258. static int init = 0;
  1259. static LARGE_INTEGER freq;
  1260. LARGE_INTEGER count;
  1261. if (!init) {
  1262. QueryPerformanceFrequency(&freq);
  1263. init = 1;
  1264. }
  1265. QueryPerformanceCounter(&count);
  1266. (void)reset;
  1267. return (double)count.QuadPart / freq.QuadPart;
  1268. }
  1269. #elif defined(WOLFSSL_TIRTOS)
  1270. extern double current_time();
  1271. #elif defined(WOLFSSL_ZEPHYR)
  1272. extern double current_time();
  1273. #else
  1274. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_TCP_NET) && !defined(WOLFSSL_CHIBIOS)
  1275. #include <sys/time.h>
  1276. static WC_INLINE double current_time(int reset)
  1277. {
  1278. struct timeval tv;
  1279. gettimeofday(&tv, 0);
  1280. (void)reset;
  1281. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  1282. }
  1283. #else
  1284. extern double current_time(int reset);
  1285. #endif
  1286. #endif /* USE_WINDOWS_API */
  1287. #if defined(HAVE_OCSP) && defined(WOLFSSL_NONBLOCK_OCSP)
  1288. static WC_INLINE int OCSPIOCb(void* ioCtx, const char* url, int urlSz,
  1289. unsigned char* request, int requestSz, unsigned char** response)
  1290. {
  1291. #ifdef TEST_NONBLOCK_CERTS
  1292. static int ioCbCnt = 0;
  1293. #endif
  1294. (void)ioCtx;
  1295. (void)url;
  1296. (void)urlSz;
  1297. (void)request;
  1298. (void)requestSz;
  1299. (void)response;
  1300. #ifdef TEST_NONBLOCK_CERTS
  1301. if (ioCbCnt) {
  1302. ioCbCnt = 0;
  1303. return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response);
  1304. }
  1305. else {
  1306. ioCbCnt = 1;
  1307. return WOLFSSL_CBIO_ERR_WANT_READ;
  1308. }
  1309. #else
  1310. return EmbedOcspLookup(ioCtx, url, urlSz, request, requestSz, response);
  1311. #endif
  1312. }
  1313. static WC_INLINE void OCSPRespFreeCb(void* ioCtx, unsigned char* response)
  1314. {
  1315. (void)ioCtx;
  1316. (void)response;
  1317. }
  1318. #endif
  1319. #if !defined(NO_CERTS)
  1320. #if !defined(NO_FILESYSTEM) || \
  1321. (defined(NO_FILESYSTEM) && defined(FORCE_BUFFER_TEST))
  1322. /* reads file size, allocates buffer, reads into buffer, returns buffer */
  1323. static WC_INLINE int load_file(const char* fname, byte** buf, size_t* bufLen)
  1324. {
  1325. int ret;
  1326. long int fileSz;
  1327. XFILE file;
  1328. if (fname == NULL || buf == NULL || bufLen == NULL)
  1329. return BAD_FUNC_ARG;
  1330. /* set defaults */
  1331. *buf = NULL;
  1332. *bufLen = 0;
  1333. /* open file (read-only binary) */
  1334. file = XFOPEN(fname, "rb");
  1335. if (!file) {
  1336. printf("Error loading %s\n", fname);
  1337. return BAD_PATH_ERROR;
  1338. }
  1339. fseek(file, 0, SEEK_END);
  1340. fileSz = (int)ftell(file);
  1341. rewind(file);
  1342. if (fileSz > 0) {
  1343. *bufLen = (size_t)fileSz;
  1344. *buf = (byte*)malloc(*bufLen);
  1345. if (*buf == NULL) {
  1346. ret = MEMORY_E;
  1347. printf("Error allocating %lu bytes\n", (unsigned long)*bufLen);
  1348. }
  1349. else {
  1350. size_t readLen = fread(*buf, *bufLen, 1, file);
  1351. /* check response code */
  1352. ret = (readLen > 0) ? 0 : -1;
  1353. }
  1354. }
  1355. else {
  1356. ret = BUFFER_E;
  1357. }
  1358. fclose(file);
  1359. return ret;
  1360. }
  1361. enum {
  1362. WOLFSSL_CA = 1,
  1363. WOLFSSL_CERT = 2,
  1364. WOLFSSL_KEY = 3,
  1365. WOLFSSL_CERT_CHAIN = 4,
  1366. };
  1367. static WC_INLINE void load_buffer(WOLFSSL_CTX* ctx, const char* fname, int type)
  1368. {
  1369. int format = WOLFSSL_FILETYPE_PEM;
  1370. byte* buff = NULL;
  1371. size_t sz = 0;
  1372. if (load_file(fname, &buff, &sz) != 0) {
  1373. err_sys("can't open file for buffer load "
  1374. "Please run from wolfSSL home directory if not");
  1375. }
  1376. /* determine format */
  1377. if (strstr(fname, ".der"))
  1378. format = WOLFSSL_FILETYPE_ASN1;
  1379. if (type == WOLFSSL_CA) {
  1380. if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format)
  1381. != WOLFSSL_SUCCESS)
  1382. err_sys("can't load buffer ca file");
  1383. }
  1384. else if (type == WOLFSSL_CERT) {
  1385. if (wolfSSL_CTX_use_certificate_buffer(ctx, buff, (long)sz,
  1386. format) != WOLFSSL_SUCCESS)
  1387. err_sys("can't load buffer cert file");
  1388. }
  1389. else if (type == WOLFSSL_KEY) {
  1390. if (wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, (long)sz,
  1391. format) != WOLFSSL_SUCCESS)
  1392. err_sys("can't load buffer key file");
  1393. }
  1394. else if (type == WOLFSSL_CERT_CHAIN) {
  1395. if (wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, buff,
  1396. (long)sz, format) != WOLFSSL_SUCCESS)
  1397. err_sys("can't load cert chain buffer");
  1398. }
  1399. if (buff)
  1400. free(buff);
  1401. }
  1402. static WC_INLINE void load_ssl_buffer(WOLFSSL* ssl, const char* fname, int type)
  1403. {
  1404. int format = WOLFSSL_FILETYPE_PEM;
  1405. byte* buff = NULL;
  1406. size_t sz = 0;
  1407. if (load_file(fname, &buff, &sz) != 0) {
  1408. err_sys("can't open file for buffer load "
  1409. "Please run from wolfSSL home directory if not");
  1410. }
  1411. /* determine format */
  1412. if (strstr(fname, ".der"))
  1413. format = WOLFSSL_FILETYPE_ASN1;
  1414. if (type == WOLFSSL_CA) {
  1415. /* verify certs (CA's) use the shared ctx->cm (WOLFSSL_CERT_MANAGER) */
  1416. WOLFSSL_CTX* ctx = wolfSSL_get_SSL_CTX(ssl);
  1417. if (wolfSSL_CTX_load_verify_buffer(ctx, buff, (long)sz, format)
  1418. != WOLFSSL_SUCCESS)
  1419. err_sys("can't load buffer ca file");
  1420. }
  1421. else if (type == WOLFSSL_CERT) {
  1422. if (wolfSSL_use_certificate_buffer(ssl, buff, (long)sz,
  1423. format) != WOLFSSL_SUCCESS)
  1424. err_sys("can't load buffer cert file");
  1425. }
  1426. else if (type == WOLFSSL_KEY) {
  1427. if (wolfSSL_use_PrivateKey_buffer(ssl, buff, (long)sz,
  1428. format) != WOLFSSL_SUCCESS)
  1429. err_sys("can't load buffer key file");
  1430. }
  1431. else if (type == WOLFSSL_CERT_CHAIN) {
  1432. if (wolfSSL_use_certificate_chain_buffer_format(ssl, buff,
  1433. (long)sz, format) != WOLFSSL_SUCCESS)
  1434. err_sys("can't load cert chain buffer");
  1435. }
  1436. if (buff)
  1437. free(buff);
  1438. }
  1439. #ifdef TEST_PK_PRIVKEY
  1440. static WC_INLINE int load_key_file(const char* fname, byte** derBuf, word32* derLen)
  1441. {
  1442. int ret;
  1443. byte* buf = NULL;
  1444. size_t bufLen;
  1445. ret = load_file(fname, &buf, &bufLen);
  1446. if (ret != 0)
  1447. return ret;
  1448. *derBuf = (byte*)malloc(bufLen);
  1449. if (*derBuf == NULL) {
  1450. free(buf);
  1451. return MEMORY_E;
  1452. }
  1453. ret = wc_KeyPemToDer(buf, (word32)bufLen, *derBuf, (word32)bufLen, NULL);
  1454. if (ret < 0) {
  1455. free(buf);
  1456. free(*derBuf);
  1457. return ret;
  1458. }
  1459. *derLen = ret;
  1460. free(buf);
  1461. return 0;
  1462. }
  1463. #endif /* TEST_PK_PRIVKEY */
  1464. #endif /* !NO_FILESYSTEM || (NO_FILESYSTEM && FORCE_BUFFER_TEST) */
  1465. #endif /* !NO_CERTS */
  1466. enum {
  1467. VERIFY_OVERRIDE_ERROR,
  1468. VERIFY_FORCE_FAIL,
  1469. VERIFY_USE_PREVERFIY,
  1470. VERIFY_OVERRIDE_DATE_ERR,
  1471. };
  1472. static THREAD_LS_T int myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1473. /* The verify callback is called for every certificate only when
  1474. * --enable-opensslextra is defined because it sets WOLFSSL_ALWAYS_VERIFY_CB and
  1475. * WOLFSSL_VERIFY_CB_ALL_CERTS.
  1476. * Normal cases of the verify callback only occur on certificate failures when the
  1477. * wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, myVerifyCb); is called
  1478. */
  1479. static WC_INLINE int myVerify(int preverify, WOLFSSL_X509_STORE_CTX* store)
  1480. {
  1481. char buffer[WOLFSSL_MAX_ERROR_SZ];
  1482. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1483. WOLFSSL_X509* peer;
  1484. #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM)
  1485. WOLFSSL_BIO* bio = NULL;
  1486. WOLFSSL_STACK* sk = NULL;
  1487. X509* x509 = NULL;
  1488. int i = 0;
  1489. #endif
  1490. #endif
  1491. (void)preverify;
  1492. /* Verify Callback Arguments:
  1493. * preverify: 1=Verify Okay, 0=Failure
  1494. * store->error: Failure error code (0 indicates no failure)
  1495. * store->current_cert: Current WOLFSSL_X509 object (only with OPENSSL_EXTRA)
  1496. * store->error_depth: Current Index
  1497. * store->domain: Subject CN as string (null term)
  1498. * store->totalCerts: Number of certs presented by peer
  1499. * store->certs[i]: A `WOLFSSL_BUFFER_INFO` with plain DER for each cert
  1500. * store->store: WOLFSSL_X509_STORE with CA cert chain
  1501. * store->store->cm: WOLFSSL_CERT_MANAGER
  1502. * store->ex_data: The WOLFSSL object pointer
  1503. * store->discardSessionCerts: When set to non-zero value session certs
  1504. will be discarded (only with SESSION_CERTS)
  1505. */
  1506. printf("In verification callback, error = %d, %s\n", store->error,
  1507. wolfSSL_ERR_error_string(store->error, buffer));
  1508. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1509. peer = store->current_cert;
  1510. if (peer) {
  1511. char* issuer = wolfSSL_X509_NAME_oneline(
  1512. wolfSSL_X509_get_issuer_name(peer), 0, 0);
  1513. char* subject = wolfSSL_X509_NAME_oneline(
  1514. wolfSSL_X509_get_subject_name(peer), 0, 0);
  1515. printf("\tPeer's cert info:\n issuer : %s\n subject: %s\n", issuer,
  1516. subject);
  1517. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  1518. XFREE(issuer, 0, DYNAMIC_TYPE_OPENSSL);
  1519. #if defined(SHOW_CERTS) && !defined(NO_FILESYSTEM)
  1520. /* avoid printing duplicate certs */
  1521. if (store->depth == 1) {
  1522. /* retrieve x509 certs and display them on stdout */
  1523. sk = wolfSSL_X509_STORE_GetCerts(store);
  1524. for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
  1525. x509 = wolfSSL_sk_X509_value(sk, i);
  1526. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  1527. if (bio != NULL) {
  1528. wolfSSL_BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1529. wolfSSL_X509_print(bio, x509);
  1530. wolfSSL_BIO_free(bio);
  1531. }
  1532. }
  1533. wolfSSL_sk_X509_free(sk);
  1534. }
  1535. #endif
  1536. }
  1537. else
  1538. printf("\tPeer has no cert!\n");
  1539. #else
  1540. printf("\tPeer certs: %d\n", store->totalCerts);
  1541. #ifdef SHOW_CERTS
  1542. { int i;
  1543. for (i=0; i<store->totalCerts; i++) {
  1544. WOLFSSL_BUFFER_INFO* cert = &store->certs[i];
  1545. printf("\t\tCert %d: Ptr %p, Len %u\n", i, cert->buffer, cert->length);
  1546. }
  1547. }
  1548. #endif /* SHOW_CERTS */
  1549. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  1550. printf("\tSubject's domain name at %d is %s\n", store->error_depth, store->domain);
  1551. /* Testing forced fail case by return zero */
  1552. if (myVerifyAction == VERIFY_FORCE_FAIL) {
  1553. return 0; /* test failure case */
  1554. }
  1555. if (myVerifyAction == VERIFY_OVERRIDE_DATE_ERR &&
  1556. (store->error == ASN_BEFORE_DATE_E || store->error == ASN_AFTER_DATE_E)) {
  1557. printf("Overriding cert date error as example for bad clock testing\n");
  1558. return 1;
  1559. }
  1560. /* If error indicate we are overriding it for testing purposes */
  1561. if (store->error != 0 && myVerifyAction == VERIFY_OVERRIDE_ERROR) {
  1562. printf("\tAllowing failed certificate check, testing only "
  1563. "(shouldn't do this in production)\n");
  1564. }
  1565. /* A non-zero return code indicates failure override */
  1566. return (myVerifyAction == VERIFY_OVERRIDE_ERROR) ? 1 : preverify;
  1567. }
  1568. #ifdef HAVE_EXT_CACHE
  1569. static WC_INLINE WOLFSSL_SESSION* mySessGetCb(WOLFSSL* ssl, unsigned char* id,
  1570. int id_len, int* copy)
  1571. {
  1572. (void)ssl;
  1573. (void)id;
  1574. (void)id_len;
  1575. (void)copy;
  1576. /* using internal cache, this is for testing only */
  1577. return NULL;
  1578. }
  1579. static WC_INLINE int mySessNewCb(WOLFSSL* ssl, WOLFSSL_SESSION* session)
  1580. {
  1581. (void)ssl;
  1582. (void)session;
  1583. /* using internal cache, this is for testing only */
  1584. return 0;
  1585. }
  1586. static WC_INLINE void mySessRemCb(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
  1587. {
  1588. (void)ctx;
  1589. (void)session;
  1590. /* using internal cache, this is for testing only */
  1591. }
  1592. #endif /* HAVE_EXT_CACHE */
  1593. #ifdef HAVE_CRL
  1594. static WC_INLINE void CRL_CallBack(const char* url)
  1595. {
  1596. printf("CRL callback url = %s\n", url);
  1597. }
  1598. #endif
  1599. #ifndef NO_DH
  1600. static WC_INLINE void SetDH(WOLFSSL* ssl)
  1601. {
  1602. /* dh1024 p */
  1603. static const unsigned char p[] =
  1604. {
  1605. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  1606. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  1607. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  1608. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  1609. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  1610. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  1611. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  1612. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  1613. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  1614. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  1615. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  1616. };
  1617. /* dh1024 g */
  1618. static const unsigned char g[] =
  1619. {
  1620. 0x02,
  1621. };
  1622. wolfSSL_SetTmpDH(ssl, p, sizeof(p), g, sizeof(g));
  1623. }
  1624. static WC_INLINE void SetDHCtx(WOLFSSL_CTX* ctx)
  1625. {
  1626. /* dh1024 p */
  1627. static const unsigned char p[] =
  1628. {
  1629. 0xE6, 0x96, 0x9D, 0x3D, 0x49, 0x5B, 0xE3, 0x2C, 0x7C, 0xF1, 0x80, 0xC3,
  1630. 0xBD, 0xD4, 0x79, 0x8E, 0x91, 0xB7, 0x81, 0x82, 0x51, 0xBB, 0x05, 0x5E,
  1631. 0x2A, 0x20, 0x64, 0x90, 0x4A, 0x79, 0xA7, 0x70, 0xFA, 0x15, 0xA2, 0x59,
  1632. 0xCB, 0xD5, 0x23, 0xA6, 0xA6, 0xEF, 0x09, 0xC4, 0x30, 0x48, 0xD5, 0xA2,
  1633. 0x2F, 0x97, 0x1F, 0x3C, 0x20, 0x12, 0x9B, 0x48, 0x00, 0x0E, 0x6E, 0xDD,
  1634. 0x06, 0x1C, 0xBC, 0x05, 0x3E, 0x37, 0x1D, 0x79, 0x4E, 0x53, 0x27, 0xDF,
  1635. 0x61, 0x1E, 0xBB, 0xBE, 0x1B, 0xAC, 0x9B, 0x5C, 0x60, 0x44, 0xCF, 0x02,
  1636. 0x3D, 0x76, 0xE0, 0x5E, 0xEA, 0x9B, 0xAD, 0x99, 0x1B, 0x13, 0xA6, 0x3C,
  1637. 0x97, 0x4E, 0x9E, 0xF1, 0x83, 0x9E, 0xB5, 0xDB, 0x12, 0x51, 0x36, 0xF7,
  1638. 0x26, 0x2E, 0x56, 0xA8, 0x87, 0x15, 0x38, 0xDF, 0xD8, 0x23, 0xC6, 0x50,
  1639. 0x50, 0x85, 0xE2, 0x1F, 0x0D, 0xD5, 0xC8, 0x6B,
  1640. };
  1641. /* dh1024 g */
  1642. static const unsigned char g[] =
  1643. {
  1644. 0x02,
  1645. };
  1646. wolfSSL_CTX_SetTmpDH(ctx, p, sizeof(p), g, sizeof(g));
  1647. }
  1648. #endif /* NO_DH */
  1649. #ifndef NO_CERTS
  1650. static WC_INLINE void CaCb(unsigned char* der, int sz, int type)
  1651. {
  1652. (void)der;
  1653. printf("Got CA cache add callback, derSz = %d, type = %d\n", sz, type);
  1654. }
  1655. #endif /* !NO_CERTS */
  1656. /* Wolf Root Directory Helper */
  1657. /* KEIL-RL File System does not support relative directory */
  1658. #if !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS)
  1659. /* Maximum depth to search for WolfSSL root */
  1660. #define MAX_WOLF_ROOT_DEPTH 5
  1661. static WC_INLINE int ChangeToWolfRoot(void)
  1662. {
  1663. #if !defined(NO_FILESYSTEM) || defined(FORCE_BUFFER_TEST)
  1664. int depth, res;
  1665. XFILE file;
  1666. for(depth = 0; depth <= MAX_WOLF_ROOT_DEPTH; depth++) {
  1667. file = XFOPEN(ntruKeyFile, "rb");
  1668. if (file != NULL) {
  1669. fclose(file);
  1670. return depth;
  1671. }
  1672. #ifdef USE_WINDOWS_API
  1673. res = SetCurrentDirectoryA("..\\");
  1674. #else
  1675. res = chdir("../");
  1676. #endif
  1677. if (res < 0) {
  1678. printf("chdir to ../ failed!\n");
  1679. break;
  1680. }
  1681. }
  1682. err_sys("wolf root not found");
  1683. return -1;
  1684. #else
  1685. return 0;
  1686. #endif
  1687. }
  1688. #endif /* !defined(WOLFSSL_MDK_ARM) && !defined(WOLFSSL_KEIL_FS) && !defined(WOLFSSL_TIRTOS) */
  1689. #ifdef HAVE_STACK_SIZE
  1690. typedef THREAD_RETURN WOLFSSL_THREAD (*thread_func)(void* args);
  1691. #define STACK_CHECK_VAL 0x01
  1692. static WC_INLINE int StackSizeCheck(func_args* args, thread_func tf)
  1693. {
  1694. int ret, i, used;
  1695. void* status;
  1696. unsigned char* myStack = NULL;
  1697. int stackSize = 1024*152;
  1698. pthread_attr_t myAttr;
  1699. pthread_t threadId;
  1700. #ifdef PTHREAD_STACK_MIN
  1701. if (stackSize < PTHREAD_STACK_MIN)
  1702. stackSize = PTHREAD_STACK_MIN;
  1703. #endif
  1704. ret = posix_memalign((void**)&myStack, sysconf(_SC_PAGESIZE), stackSize);
  1705. if (ret != 0 || myStack == NULL)
  1706. err_sys("posix_memalign failed\n");
  1707. XMEMSET(myStack, STACK_CHECK_VAL, stackSize);
  1708. ret = pthread_attr_init(&myAttr);
  1709. if (ret != 0)
  1710. err_sys("attr_init failed");
  1711. ret = pthread_attr_setstack(&myAttr, myStack, stackSize);
  1712. if (ret != 0)
  1713. err_sys("attr_setstackaddr failed");
  1714. ret = pthread_create(&threadId, &myAttr, tf, args);
  1715. if (ret != 0) {
  1716. perror("pthread_create failed");
  1717. exit(EXIT_FAILURE);
  1718. }
  1719. ret = pthread_join(threadId, &status);
  1720. if (ret != 0)
  1721. err_sys("pthread_join failed");
  1722. for (i = 0; i < stackSize; i++) {
  1723. if (myStack[i] != STACK_CHECK_VAL) {
  1724. break;
  1725. }
  1726. }
  1727. free(myStack);
  1728. used = stackSize - i;
  1729. printf("stack used = %d\n", used);
  1730. return (int)((size_t)status);
  1731. }
  1732. #endif /* HAVE_STACK_SIZE */
  1733. #ifdef STACK_TRAP
  1734. /* good settings
  1735. --enable-debug --disable-shared C_EXTRA_FLAGS="-DUSER_TIME -DTFM_TIMING_RESISTANT -DPOSITIVE_EXP_ONLY -DSTACK_TRAP"
  1736. */
  1737. #ifdef HAVE_STACK_SIZE
  1738. /* client only for now, setrlimit will fail if pthread_create() called */
  1739. /* STACK_SIZE does pthread_create() on client */
  1740. #error "can't use STACK_TRAP with STACK_SIZE, setrlimit will fail"
  1741. #endif /* HAVE_STACK_SIZE */
  1742. static WC_INLINE void StackTrap(void)
  1743. {
  1744. struct rlimit rl;
  1745. if (getrlimit(RLIMIT_STACK, &rl) != 0)
  1746. err_sys("getrlimit failed");
  1747. printf("rlim_cur = %llu\n", rl.rlim_cur);
  1748. rl.rlim_cur = 1024*21; /* adjust trap size here */
  1749. if (setrlimit(RLIMIT_STACK, &rl) != 0) {
  1750. perror("setrlimit");
  1751. err_sys("setrlimit failed");
  1752. }
  1753. }
  1754. #else /* STACK_TRAP */
  1755. static WC_INLINE void StackTrap(void)
  1756. {
  1757. }
  1758. #endif /* STACK_TRAP */
  1759. #if defined(ATOMIC_USER) && !defined(WOLFSSL_AEAD_ONLY)
  1760. /* Atomic Encrypt Context example */
  1761. typedef struct AtomicEncCtx {
  1762. int keySetup; /* have we done key setup yet */
  1763. Aes aes; /* for aes example */
  1764. } AtomicEncCtx;
  1765. /* Atomic Decrypt Context example */
  1766. typedef struct AtomicDecCtx {
  1767. int keySetup; /* have we done key setup yet */
  1768. Aes aes; /* for aes example */
  1769. } AtomicDecCtx;
  1770. static WC_INLINE int myMacEncryptCb(WOLFSSL* ssl, unsigned char* macOut,
  1771. const unsigned char* macIn, unsigned int macInSz, int macContent,
  1772. int macVerify, unsigned char* encOut, const unsigned char* encIn,
  1773. unsigned int encSz, void* ctx)
  1774. {
  1775. int ret;
  1776. Hmac hmac;
  1777. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  1778. AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx;
  1779. const char* tlsStr = "TLS";
  1780. /* example supports (d)tls aes */
  1781. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  1782. printf("myMacEncryptCb not using AES\n");
  1783. return -1;
  1784. }
  1785. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  1786. printf("myMacEncryptCb not using (D)TLS\n");
  1787. return -1;
  1788. }
  1789. /* hmac, not needed if aead mode */
  1790. wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
  1791. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  1792. if (ret != 0)
  1793. return ret;
  1794. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1795. wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl));
  1796. if (ret != 0)
  1797. return ret;
  1798. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1799. if (ret != 0)
  1800. return ret;
  1801. ret = wc_HmacUpdate(&hmac, macIn, macInSz);
  1802. if (ret != 0)
  1803. return ret;
  1804. ret = wc_HmacFinal(&hmac, macOut);
  1805. if (ret != 0)
  1806. return ret;
  1807. /* encrypt setup on first time */
  1808. if (encCtx->keySetup == 0) {
  1809. int keyLen = wolfSSL_GetKeySize(ssl);
  1810. const byte* key;
  1811. const byte* iv;
  1812. if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) {
  1813. key = wolfSSL_GetClientWriteKey(ssl);
  1814. iv = wolfSSL_GetClientWriteIV(ssl);
  1815. }
  1816. else {
  1817. key = wolfSSL_GetServerWriteKey(ssl);
  1818. iv = wolfSSL_GetServerWriteIV(ssl);
  1819. }
  1820. ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION);
  1821. if (ret != 0) {
  1822. printf("AesSetKey failed in myMacEncryptCb\n");
  1823. return ret;
  1824. }
  1825. encCtx->keySetup = 1;
  1826. }
  1827. /* encrypt */
  1828. return wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz);
  1829. }
  1830. static WC_INLINE int myDecryptVerifyCb(WOLFSSL* ssl,
  1831. unsigned char* decOut, const unsigned char* decIn,
  1832. unsigned int decSz, int macContent, int macVerify,
  1833. unsigned int* padSz, void* ctx)
  1834. {
  1835. AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx;
  1836. int ret = 0;
  1837. int macInSz = 0;
  1838. int ivExtra = 0;
  1839. int digestSz = wolfSSL_GetHmacSize(ssl);
  1840. unsigned int pad = 0;
  1841. unsigned int padByte = 0;
  1842. Hmac hmac;
  1843. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  1844. byte verify[WC_MAX_DIGEST_SIZE];
  1845. const char* tlsStr = "TLS";
  1846. /* example supports (d)tls aes */
  1847. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  1848. printf("myMacEncryptCb not using AES\n");
  1849. return -1;
  1850. }
  1851. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  1852. printf("myMacEncryptCb not using (D)TLS\n");
  1853. return -1;
  1854. }
  1855. /*decrypt */
  1856. if (decCtx->keySetup == 0) {
  1857. int keyLen = wolfSSL_GetKeySize(ssl);
  1858. const byte* key;
  1859. const byte* iv;
  1860. /* decrypt is from other side (peer) */
  1861. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  1862. key = wolfSSL_GetClientWriteKey(ssl);
  1863. iv = wolfSSL_GetClientWriteIV(ssl);
  1864. }
  1865. else {
  1866. key = wolfSSL_GetServerWriteKey(ssl);
  1867. iv = wolfSSL_GetServerWriteIV(ssl);
  1868. }
  1869. ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION);
  1870. if (ret != 0) {
  1871. printf("AesSetKey failed in myDecryptVerifyCb\n");
  1872. return ret;
  1873. }
  1874. decCtx->keySetup = 1;
  1875. }
  1876. /* decrypt */
  1877. ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz);
  1878. if (ret != 0)
  1879. return ret;
  1880. if (wolfSSL_GetCipherType(ssl) == WOLFSSL_AEAD_TYPE) {
  1881. *padSz = wolfSSL_GetAeadMacSize(ssl);
  1882. return 0; /* hmac, not needed if aead mode */
  1883. }
  1884. if (wolfSSL_GetCipherType(ssl) == WOLFSSL_BLOCK_TYPE) {
  1885. pad = *(decOut + decSz - 1);
  1886. padByte = 1;
  1887. if (wolfSSL_IsTLSv1_1(ssl))
  1888. ivExtra = wolfSSL_GetCipherBlockSize(ssl);
  1889. }
  1890. *padSz = wolfSSL_GetHmacSize(ssl) + pad + padByte;
  1891. macInSz = decSz - ivExtra - digestSz - pad - padByte;
  1892. wolfSSL_SetTlsHmacInner(ssl, myInner, macInSz, macContent, macVerify);
  1893. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  1894. if (ret != 0)
  1895. return ret;
  1896. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1897. wolfSSL_GetMacSecret(ssl, macVerify), digestSz);
  1898. if (ret != 0)
  1899. return ret;
  1900. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1901. if (ret != 0)
  1902. return ret;
  1903. ret = wc_HmacUpdate(&hmac, decOut + ivExtra, macInSz);
  1904. if (ret != 0)
  1905. return ret;
  1906. ret = wc_HmacFinal(&hmac, verify);
  1907. if (ret != 0)
  1908. return ret;
  1909. if (XMEMCMP(verify, decOut + decSz - digestSz - pad - padByte,
  1910. digestSz) != 0) {
  1911. printf("myDecryptVerify verify failed\n");
  1912. return -1;
  1913. }
  1914. return ret;
  1915. }
  1916. #if defined(HAVE_ENCRYPT_THEN_MAC)
  1917. static WC_INLINE int myEncryptMacCb(WOLFSSL* ssl, unsigned char* macOut,
  1918. int content, int macVerify, unsigned char* encOut,
  1919. const unsigned char* encIn, unsigned int encSz, void* ctx)
  1920. {
  1921. int ret;
  1922. Hmac hmac;
  1923. AtomicEncCtx* encCtx = (AtomicEncCtx*)ctx;
  1924. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  1925. const char* tlsStr = "TLS";
  1926. /* example supports (d)tls aes */
  1927. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  1928. printf("myMacEncryptCb not using AES\n");
  1929. return -1;
  1930. }
  1931. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  1932. printf("myMacEncryptCb not using (D)TLS\n");
  1933. return -1;
  1934. }
  1935. /* encrypt setup on first time */
  1936. if (encCtx->keySetup == 0) {
  1937. int keyLen = wolfSSL_GetKeySize(ssl);
  1938. const byte* key;
  1939. const byte* iv;
  1940. if (wolfSSL_GetSide(ssl) == WOLFSSL_CLIENT_END) {
  1941. key = wolfSSL_GetClientWriteKey(ssl);
  1942. iv = wolfSSL_GetClientWriteIV(ssl);
  1943. }
  1944. else {
  1945. key = wolfSSL_GetServerWriteKey(ssl);
  1946. iv = wolfSSL_GetServerWriteIV(ssl);
  1947. }
  1948. ret = wc_AesSetKey(&encCtx->aes, key, keyLen, iv, AES_ENCRYPTION);
  1949. if (ret != 0) {
  1950. printf("AesSetKey failed in myMacEncryptCb\n");
  1951. return ret;
  1952. }
  1953. encCtx->keySetup = 1;
  1954. }
  1955. /* encrypt */
  1956. ret = wc_AesCbcEncrypt(&encCtx->aes, encOut, encIn, encSz);
  1957. if (ret != 0)
  1958. return ret;
  1959. /* Reconstruct record header. */
  1960. wolfSSL_SetTlsHmacInner(ssl, myInner, encSz, content, macVerify);
  1961. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  1962. if (ret != 0)
  1963. return ret;
  1964. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1965. wolfSSL_GetMacSecret(ssl, macVerify), wolfSSL_GetHmacSize(ssl));
  1966. if (ret != 0)
  1967. return ret;
  1968. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1969. if (ret != 0)
  1970. return ret;
  1971. ret = wc_HmacUpdate(&hmac, encOut, encSz);
  1972. if (ret != 0)
  1973. return ret;
  1974. return wc_HmacFinal(&hmac, macOut);
  1975. }
  1976. static WC_INLINE int myVerifyDecryptCb(WOLFSSL* ssl,
  1977. unsigned char* decOut, const unsigned char* decIn,
  1978. unsigned int decSz, int content, int macVerify,
  1979. unsigned int* padSz, void* ctx)
  1980. {
  1981. AtomicDecCtx* decCtx = (AtomicDecCtx*)ctx;
  1982. int ret = 0;
  1983. int digestSz = wolfSSL_GetHmacSize(ssl);
  1984. Hmac hmac;
  1985. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  1986. byte verify[WC_MAX_DIGEST_SIZE];
  1987. const char* tlsStr = "TLS";
  1988. /* example supports (d)tls aes */
  1989. if (wolfSSL_GetBulkCipher(ssl) != wolfssl_aes) {
  1990. printf("myMacEncryptCb not using AES\n");
  1991. return -1;
  1992. }
  1993. if (strstr(wolfSSL_get_version(ssl), tlsStr) == NULL) {
  1994. printf("myMacEncryptCb not using (D)TLS\n");
  1995. return -1;
  1996. }
  1997. /* Reconstruct record header. */
  1998. wolfSSL_SetTlsHmacInner(ssl, myInner, decSz, content, macVerify);
  1999. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  2000. if (ret != 0)
  2001. return ret;
  2002. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  2003. wolfSSL_GetMacSecret(ssl, macVerify), digestSz);
  2004. if (ret != 0)
  2005. return ret;
  2006. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  2007. if (ret != 0)
  2008. return ret;
  2009. ret = wc_HmacUpdate(&hmac, decIn, decSz);
  2010. if (ret != 0)
  2011. return ret;
  2012. ret = wc_HmacFinal(&hmac, verify);
  2013. if (ret != 0)
  2014. return ret;
  2015. if (XMEMCMP(verify, decOut + decSz, digestSz) != 0) {
  2016. printf("myDecryptVerify verify failed\n");
  2017. return -1;
  2018. }
  2019. /* decrypt */
  2020. if (decCtx->keySetup == 0) {
  2021. int keyLen = wolfSSL_GetKeySize(ssl);
  2022. const byte* key;
  2023. const byte* iv;
  2024. /* decrypt is from other side (peer) */
  2025. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  2026. key = wolfSSL_GetClientWriteKey(ssl);
  2027. iv = wolfSSL_GetClientWriteIV(ssl);
  2028. }
  2029. else {
  2030. key = wolfSSL_GetServerWriteKey(ssl);
  2031. iv = wolfSSL_GetServerWriteIV(ssl);
  2032. }
  2033. ret = wc_AesSetKey(&decCtx->aes, key, keyLen, iv, AES_DECRYPTION);
  2034. if (ret != 0) {
  2035. printf("AesSetKey failed in myDecryptVerifyCb\n");
  2036. return ret;
  2037. }
  2038. decCtx->keySetup = 1;
  2039. }
  2040. /* decrypt */
  2041. ret = wc_AesCbcDecrypt(&decCtx->aes, decOut, decIn, decSz);
  2042. if (ret != 0)
  2043. return ret;
  2044. *padSz = *(decOut + decSz - 1) + 1;
  2045. return 0;
  2046. }
  2047. #endif
  2048. static WC_INLINE void SetupAtomicUser(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
  2049. {
  2050. AtomicEncCtx* encCtx;
  2051. AtomicDecCtx* decCtx;
  2052. encCtx = (AtomicEncCtx*)malloc(sizeof(AtomicEncCtx));
  2053. if (encCtx == NULL)
  2054. err_sys("AtomicEncCtx malloc failed");
  2055. XMEMSET(encCtx, 0, sizeof(AtomicEncCtx));
  2056. decCtx = (AtomicDecCtx*)malloc(sizeof(AtomicDecCtx));
  2057. if (decCtx == NULL) {
  2058. free(encCtx);
  2059. err_sys("AtomicDecCtx malloc failed");
  2060. }
  2061. XMEMSET(decCtx, 0, sizeof(AtomicDecCtx));
  2062. wolfSSL_CTX_SetMacEncryptCb(ctx, myMacEncryptCb);
  2063. wolfSSL_SetMacEncryptCtx(ssl, encCtx);
  2064. wolfSSL_CTX_SetDecryptVerifyCb(ctx, myDecryptVerifyCb);
  2065. wolfSSL_SetDecryptVerifyCtx(ssl, decCtx);
  2066. #if defined(HAVE_ENCRYPT_THEN_MAC)
  2067. wolfSSL_CTX_SetEncryptMacCb(ctx, myEncryptMacCb);
  2068. wolfSSL_SetEncryptMacCtx(ssl, encCtx);
  2069. wolfSSL_CTX_SetVerifyDecryptCb(ctx, myVerifyDecryptCb);
  2070. wolfSSL_SetVerifyDecryptCtx(ssl, decCtx);
  2071. #endif
  2072. }
  2073. static WC_INLINE void FreeAtomicUser(WOLFSSL* ssl)
  2074. {
  2075. AtomicEncCtx* encCtx = (AtomicEncCtx*)wolfSSL_GetMacEncryptCtx(ssl);
  2076. AtomicDecCtx* decCtx = (AtomicDecCtx*)wolfSSL_GetDecryptVerifyCtx(ssl);
  2077. /* Encrypt-Then-MAC callbacks use same contexts. */
  2078. free(decCtx);
  2079. free(encCtx);
  2080. }
  2081. #endif /* ATOMIC_USER */
  2082. #ifdef WOLFSSL_STATIC_MEMORY
  2083. static WC_INLINE int wolfSSL_PrintStats(WOLFSSL_MEM_STATS* stats)
  2084. {
  2085. word16 i;
  2086. if (stats == NULL) {
  2087. return 0;
  2088. }
  2089. /* print to stderr so is on the same pipe as WOLFSSL_DEBUG */
  2090. fprintf(stderr, "Total mallocs = %d\n", stats->totalAlloc);
  2091. fprintf(stderr, "Total frees = %d\n", stats->totalFr);
  2092. fprintf(stderr, "Current mallocs = %d\n", stats->curAlloc);
  2093. fprintf(stderr, "Available IO = %d\n", stats->avaIO);
  2094. fprintf(stderr, "Max con. handshakes = %d\n", stats->maxHa);
  2095. fprintf(stderr, "Max con. IO = %d\n", stats->maxIO);
  2096. fprintf(stderr, "State of memory blocks: size : available \n");
  2097. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  2098. fprintf(stderr, " : %d\t : %d\n", stats->blockSz[i],
  2099. stats->avaBlock[i]);
  2100. }
  2101. return 1;
  2102. }
  2103. #endif /* WOLFSSL_STATIC_MEMORY */
  2104. #ifdef HAVE_PK_CALLBACKS
  2105. typedef struct PkCbInfo {
  2106. const char* ourKey;
  2107. #ifdef TEST_PK_PRIVKEY
  2108. union {
  2109. #ifdef HAVE_ECC
  2110. ecc_key ecc;
  2111. #endif
  2112. #ifdef HAVE_CURVE25519
  2113. curve25519_key curve;
  2114. #endif
  2115. #ifdef HAVE_CURVE448
  2116. curve448_key curve;
  2117. #endif
  2118. } keyGen;
  2119. #endif
  2120. } PkCbInfo;
  2121. #if defined(DEBUG_PK_CB) || defined(TEST_PK_PRIVKEY)
  2122. #define WOLFSSL_PKMSG(_f_, ...) printf(_f_, ##__VA_ARGS__)
  2123. #else
  2124. #define WOLFSSL_PKMSG(_f_, ...)
  2125. #endif
  2126. #ifdef HAVE_ECC
  2127. static WC_INLINE int myEccKeyGen(WOLFSSL* ssl, ecc_key* key, word32 keySz,
  2128. int ecc_curve, void* ctx)
  2129. {
  2130. int ret;
  2131. WC_RNG rng;
  2132. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2133. ecc_key* new_key;
  2134. #ifdef TEST_PK_PRIVKEY
  2135. byte qx[MAX_ECC_BYTES], qy[MAX_ECC_BYTES];
  2136. word32 qxLen = sizeof(qx), qyLen = sizeof(qy);
  2137. new_key = &cbInfo->keyGen.ecc;
  2138. #else
  2139. new_key = key;
  2140. #endif
  2141. (void)ssl;
  2142. (void)cbInfo;
  2143. WOLFSSL_PKMSG("PK ECC KeyGen: keySz %d, Curve ID %d\n", keySz, ecc_curve);
  2144. ret = wc_InitRng(&rng);
  2145. if (ret != 0)
  2146. return ret;
  2147. ret = wc_ecc_init(new_key);
  2148. if (ret == 0) {
  2149. /* create new key */
  2150. ret = wc_ecc_make_key_ex(&rng, keySz, new_key, ecc_curve);
  2151. #ifdef TEST_PK_PRIVKEY
  2152. if (ret == 0) {
  2153. /* extract public portion from new key into `key` arg */
  2154. ret = wc_ecc_export_public_raw(new_key, qx, &qxLen, qy, &qyLen);
  2155. if (ret == 0) {
  2156. /* load public portion only into key */
  2157. ret = wc_ecc_import_unsigned(key, qx, qy, NULL, ecc_curve);
  2158. }
  2159. (void)qxLen;
  2160. (void)qyLen;
  2161. }
  2162. #endif
  2163. }
  2164. WOLFSSL_PKMSG("PK ECC KeyGen: ret %d\n", ret);
  2165. wc_FreeRng(&rng);
  2166. return ret;
  2167. }
  2168. static WC_INLINE int myEccSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  2169. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  2170. {
  2171. int ret;
  2172. WC_RNG rng;
  2173. word32 idx = 0;
  2174. ecc_key myKey;
  2175. byte* keyBuf = (byte*)key;
  2176. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2177. (void)ssl;
  2178. (void)cbInfo;
  2179. WOLFSSL_PKMSG("PK ECC Sign: inSz %d, keySz %d\n", inSz, keySz);
  2180. #ifdef TEST_PK_PRIVKEY
  2181. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2182. if (ret != 0)
  2183. return ret;
  2184. #endif
  2185. ret = wc_InitRng(&rng);
  2186. if (ret != 0)
  2187. return ret;
  2188. ret = wc_ecc_init(&myKey);
  2189. if (ret == 0) {
  2190. ret = wc_EccPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2191. if (ret == 0) {
  2192. WOLFSSL_PKMSG("PK ECC Sign: Curve ID %d\n", myKey.dp->id);
  2193. ret = wc_ecc_sign_hash(in, inSz, out, outSz, &rng, &myKey);
  2194. }
  2195. wc_ecc_free(&myKey);
  2196. }
  2197. wc_FreeRng(&rng);
  2198. #ifdef TEST_PK_PRIVKEY
  2199. free(keyBuf);
  2200. #endif
  2201. WOLFSSL_PKMSG("PK ECC Sign: ret %d outSz %d\n", ret, *outSz);
  2202. return ret;
  2203. }
  2204. static WC_INLINE int myEccVerify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
  2205. const byte* hash, word32 hashSz, const byte* key, word32 keySz,
  2206. int* result, void* ctx)
  2207. {
  2208. int ret;
  2209. word32 idx = 0;
  2210. ecc_key myKey;
  2211. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2212. (void)ssl;
  2213. (void)cbInfo;
  2214. WOLFSSL_PKMSG("PK ECC Verify: sigSz %d, hashSz %d, keySz %d\n", sigSz, hashSz, keySz);
  2215. ret = wc_ecc_init(&myKey);
  2216. if (ret == 0) {
  2217. ret = wc_EccPublicKeyDecode(key, &idx, &myKey, keySz);
  2218. if (ret == 0)
  2219. ret = wc_ecc_verify_hash(sig, sigSz, hash, hashSz, result, &myKey);
  2220. wc_ecc_free(&myKey);
  2221. }
  2222. WOLFSSL_PKMSG("PK ECC Verify: ret %d, result %d\n", ret, *result);
  2223. return ret;
  2224. }
  2225. static WC_INLINE int myEccSharedSecret(WOLFSSL* ssl, ecc_key* otherKey,
  2226. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  2227. unsigned char* out, unsigned int* outlen,
  2228. int side, void* ctx)
  2229. {
  2230. int ret;
  2231. ecc_key* privKey = NULL;
  2232. ecc_key* pubKey = NULL;
  2233. ecc_key tmpKey;
  2234. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2235. (void)ssl;
  2236. (void)cbInfo;
  2237. WOLFSSL_PKMSG("PK ECC PMS: Side %s, Peer Curve %d\n",
  2238. side == WOLFSSL_CLIENT_END ? "client" : "server", otherKey->dp->id);
  2239. ret = wc_ecc_init(&tmpKey);
  2240. if (ret != 0) {
  2241. return ret;
  2242. }
  2243. /* for client: create and export public key */
  2244. if (side == WOLFSSL_CLIENT_END) {
  2245. WC_RNG rng;
  2246. privKey = &tmpKey;
  2247. pubKey = otherKey;
  2248. ret = wc_InitRng(&rng);
  2249. if (ret == 0) {
  2250. ret = wc_ecc_make_key_ex(&rng, 0, privKey, otherKey->dp->id);
  2251. #ifdef WOLFSSL_ASYNC_CRYPT
  2252. if (ret == WC_PENDING_E) {
  2253. ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_NONE);
  2254. }
  2255. #endif
  2256. if (ret == 0)
  2257. ret = wc_ecc_export_x963(privKey, pubKeyDer, pubKeySz);
  2258. wc_FreeRng(&rng);
  2259. }
  2260. }
  2261. /* for server: import public key */
  2262. else if (side == WOLFSSL_SERVER_END) {
  2263. #ifdef TEST_PK_PRIVKEY
  2264. privKey = &cbInfo->keyGen.ecc;
  2265. #else
  2266. privKey = otherKey;
  2267. #endif
  2268. pubKey = &tmpKey;
  2269. ret = wc_ecc_import_x963_ex(pubKeyDer, *pubKeySz, pubKey,
  2270. otherKey->dp->id);
  2271. }
  2272. else {
  2273. ret = BAD_FUNC_ARG;
  2274. }
  2275. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_FIPS) && \
  2276. !defined(HAVE_SELFTEST)
  2277. if (ret == 0) {
  2278. ret = wc_ecc_set_rng(privKey, wolfSSL_GetRNG(ssl));
  2279. }
  2280. #endif
  2281. /* generate shared secret and return it */
  2282. if (ret == 0) {
  2283. ret = wc_ecc_shared_secret(privKey, pubKey, out, outlen);
  2284. #ifdef WOLFSSL_ASYNC_CRYPT
  2285. if (ret == WC_PENDING_E) {
  2286. ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  2287. }
  2288. #endif
  2289. }
  2290. #ifdef TEST_PK_PRIVKEY
  2291. if (side == WOLFSSL_SERVER_END) {
  2292. wc_ecc_free(&cbInfo->keyGen.ecc);
  2293. }
  2294. #endif
  2295. wc_ecc_free(&tmpKey);
  2296. WOLFSSL_PKMSG("PK ECC PMS: ret %d, PubKeySz %d, OutLen %d\n", ret, *pubKeySz, *outlen);
  2297. return ret;
  2298. }
  2299. #ifdef HAVE_ED25519
  2300. static WC_INLINE int myEd25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
  2301. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  2302. {
  2303. int ret;
  2304. word32 idx = 0;
  2305. ed25519_key myKey;
  2306. byte* keyBuf = (byte*)key;
  2307. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2308. (void)ssl;
  2309. (void)cbInfo;
  2310. WOLFSSL_PKMSG("PK 25519 Sign: inSz %d, keySz %d\n", inSz, keySz);
  2311. #ifdef TEST_PK_PRIVKEY
  2312. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2313. if (ret != 0)
  2314. return ret;
  2315. #endif
  2316. ret = wc_ed25519_init(&myKey);
  2317. if (ret == 0) {
  2318. ret = wc_Ed25519PrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2319. if (ret == 0)
  2320. ret = wc_ed25519_sign_msg(in, inSz, out, outSz, &myKey);
  2321. wc_ed25519_free(&myKey);
  2322. }
  2323. #ifdef TEST_PK_PRIVKEY
  2324. free(keyBuf);
  2325. #endif
  2326. WOLFSSL_PKMSG("PK 25519 Sign: ret %d, outSz %d\n", ret, *outSz);
  2327. return ret;
  2328. }
  2329. static WC_INLINE int myEd25519Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
  2330. const byte* msg, word32 msgSz, const byte* key, word32 keySz,
  2331. int* result, void* ctx)
  2332. {
  2333. int ret;
  2334. ed25519_key myKey;
  2335. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2336. (void)ssl;
  2337. (void)cbInfo;
  2338. WOLFSSL_PKMSG("PK 25519 Verify: sigSz %d, msgSz %d, keySz %d\n", sigSz, msgSz, keySz);
  2339. ret = wc_ed25519_init(&myKey);
  2340. if (ret == 0) {
  2341. ret = wc_ed25519_import_public(key, keySz, &myKey);
  2342. if (ret == 0) {
  2343. ret = wc_ed25519_verify_msg(sig, sigSz, msg, msgSz, result, &myKey);
  2344. }
  2345. wc_ed25519_free(&myKey);
  2346. }
  2347. WOLFSSL_PKMSG("PK 25519 Verify: ret %d, result %d\n", ret, *result);
  2348. return ret;
  2349. }
  2350. #endif /* HAVE_ED25519 */
  2351. #ifdef HAVE_CURVE25519
  2352. static WC_INLINE int myX25519KeyGen(WOLFSSL* ssl, curve25519_key* key,
  2353. unsigned int keySz, void* ctx)
  2354. {
  2355. int ret;
  2356. WC_RNG rng;
  2357. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2358. (void)ssl;
  2359. (void)cbInfo;
  2360. WOLFSSL_PKMSG("PK 25519 KeyGen: keySz %d\n", keySz);
  2361. ret = wc_InitRng(&rng);
  2362. if (ret != 0)
  2363. return ret;
  2364. ret = wc_curve25519_make_key(&rng, keySz, key);
  2365. wc_FreeRng(&rng);
  2366. WOLFSSL_PKMSG("PK 25519 KeyGen: ret %d\n", ret);
  2367. return ret;
  2368. }
  2369. static WC_INLINE int myX25519SharedSecret(WOLFSSL* ssl, curve25519_key* otherKey,
  2370. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  2371. unsigned char* out, unsigned int* outlen,
  2372. int side, void* ctx)
  2373. {
  2374. int ret;
  2375. curve25519_key* privKey = NULL;
  2376. curve25519_key* pubKey = NULL;
  2377. curve25519_key tmpKey;
  2378. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2379. (void)ssl;
  2380. (void)cbInfo;
  2381. WOLFSSL_PKMSG("PK 25519 PMS: side %s\n",
  2382. side == WOLFSSL_CLIENT_END ? "client" : "server");
  2383. ret = wc_curve25519_init(&tmpKey);
  2384. if (ret != 0) {
  2385. return ret;
  2386. }
  2387. /* for client: create and export public key */
  2388. if (side == WOLFSSL_CLIENT_END) {
  2389. WC_RNG rng;
  2390. privKey = &tmpKey;
  2391. pubKey = otherKey;
  2392. ret = wc_InitRng(&rng);
  2393. if (ret == 0) {
  2394. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, privKey);
  2395. if (ret == 0) {
  2396. ret = wc_curve25519_export_public_ex(privKey, pubKeyDer,
  2397. pubKeySz, EC25519_LITTLE_ENDIAN);
  2398. }
  2399. wc_FreeRng(&rng);
  2400. }
  2401. }
  2402. /* for server: import public key */
  2403. else if (side == WOLFSSL_SERVER_END) {
  2404. privKey = otherKey;
  2405. pubKey = &tmpKey;
  2406. ret = wc_curve25519_import_public_ex(pubKeyDer, *pubKeySz, pubKey,
  2407. EC25519_LITTLE_ENDIAN);
  2408. }
  2409. else {
  2410. ret = BAD_FUNC_ARG;
  2411. }
  2412. /* generate shared secret and return it */
  2413. if (ret == 0) {
  2414. ret = wc_curve25519_shared_secret_ex(privKey, pubKey, out, outlen,
  2415. EC25519_LITTLE_ENDIAN);
  2416. }
  2417. wc_curve25519_free(&tmpKey);
  2418. WOLFSSL_PKMSG("PK 25519 PMS: ret %d, pubKeySz %d, outLen %d\n",
  2419. ret, *pubKeySz, *outlen);
  2420. return ret;
  2421. }
  2422. #endif /* HAVE_CURVE25519 */
  2423. #ifdef HAVE_ED448
  2424. static WC_INLINE int myEd448Sign(WOLFSSL* ssl, const byte* in, word32 inSz,
  2425. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  2426. {
  2427. int ret;
  2428. word32 idx = 0;
  2429. ed448_key myKey;
  2430. byte* keyBuf = (byte*)key;
  2431. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2432. (void)ssl;
  2433. (void)cbInfo;
  2434. WOLFSSL_PKMSG("PK 448 Sign: inSz %d, keySz %d\n", inSz, keySz);
  2435. #ifdef TEST_PK_PRIVKEY
  2436. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2437. if (ret != 0)
  2438. return ret;
  2439. #endif
  2440. ret = wc_ed448_init(&myKey);
  2441. if (ret == 0) {
  2442. ret = wc_Ed448PrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2443. if (ret == 0)
  2444. ret = wc_ed448_sign_msg(in, inSz, out, outSz, &myKey, NULL, 0);
  2445. wc_ed448_free(&myKey);
  2446. }
  2447. #ifdef TEST_PK_PRIVKEY
  2448. free(keyBuf);
  2449. #endif
  2450. WOLFSSL_PKMSG("PK 448 Sign: ret %d, outSz %d\n", ret, *outSz);
  2451. return ret;
  2452. }
  2453. static WC_INLINE int myEd448Verify(WOLFSSL* ssl, const byte* sig, word32 sigSz,
  2454. const byte* msg, word32 msgSz, const byte* key, word32 keySz,
  2455. int* result, void* ctx)
  2456. {
  2457. int ret;
  2458. ed448_key myKey;
  2459. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2460. (void)ssl;
  2461. (void)cbInfo;
  2462. WOLFSSL_PKMSG("PK 448 Verify: sigSz %d, msgSz %d, keySz %d\n", sigSz, msgSz,
  2463. keySz);
  2464. ret = wc_ed448_init(&myKey);
  2465. if (ret == 0) {
  2466. ret = wc_ed448_import_public(key, keySz, &myKey);
  2467. if (ret == 0) {
  2468. ret = wc_ed448_verify_msg(sig, sigSz, msg, msgSz, result, &myKey,
  2469. NULL, 0);
  2470. }
  2471. wc_ed448_free(&myKey);
  2472. }
  2473. WOLFSSL_PKMSG("PK 448 Verify: ret %d, result %d\n", ret, *result);
  2474. return ret;
  2475. }
  2476. #endif /* HAVE_ED448 */
  2477. #ifdef HAVE_CURVE448
  2478. static WC_INLINE int myX448KeyGen(WOLFSSL* ssl, curve448_key* key,
  2479. unsigned int keySz, void* ctx)
  2480. {
  2481. int ret;
  2482. WC_RNG rng;
  2483. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2484. (void)ssl;
  2485. (void)cbInfo;
  2486. WOLFSSL_PKMSG("PK 448 KeyGen: keySz %d\n", keySz);
  2487. ret = wc_InitRng(&rng);
  2488. if (ret != 0)
  2489. return ret;
  2490. ret = wc_curve448_make_key(&rng, keySz, key);
  2491. wc_FreeRng(&rng);
  2492. WOLFSSL_PKMSG("PK 448 KeyGen: ret %d\n", ret);
  2493. return ret;
  2494. }
  2495. static WC_INLINE int myX448SharedSecret(WOLFSSL* ssl, curve448_key* otherKey,
  2496. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  2497. unsigned char* out, unsigned int* outlen,
  2498. int side, void* ctx)
  2499. {
  2500. int ret;
  2501. curve448_key* privKey = NULL;
  2502. curve448_key* pubKey = NULL;
  2503. curve448_key tmpKey;
  2504. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2505. (void)ssl;
  2506. (void)cbInfo;
  2507. WOLFSSL_PKMSG("PK 448 PMS: side %s\n",
  2508. side == WOLFSSL_CLIENT_END ? "client" : "server");
  2509. ret = wc_curve448_init(&tmpKey);
  2510. if (ret != 0) {
  2511. return ret;
  2512. }
  2513. /* for client: create and export public key */
  2514. if (side == WOLFSSL_CLIENT_END) {
  2515. WC_RNG rng;
  2516. privKey = &tmpKey;
  2517. pubKey = otherKey;
  2518. ret = wc_InitRng(&rng);
  2519. if (ret == 0) {
  2520. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, privKey);
  2521. if (ret == 0) {
  2522. ret = wc_curve448_export_public_ex(privKey, pubKeyDer,
  2523. pubKeySz, EC448_LITTLE_ENDIAN);
  2524. }
  2525. wc_FreeRng(&rng);
  2526. }
  2527. }
  2528. /* for server: import public key */
  2529. else if (side == WOLFSSL_SERVER_END) {
  2530. privKey = otherKey;
  2531. pubKey = &tmpKey;
  2532. ret = wc_curve448_import_public_ex(pubKeyDer, *pubKeySz, pubKey,
  2533. EC448_LITTLE_ENDIAN);
  2534. }
  2535. else {
  2536. ret = BAD_FUNC_ARG;
  2537. }
  2538. /* generate shared secret and return it */
  2539. if (ret == 0) {
  2540. ret = wc_curve448_shared_secret_ex(privKey, pubKey, out, outlen,
  2541. EC448_LITTLE_ENDIAN);
  2542. }
  2543. wc_curve448_free(&tmpKey);
  2544. WOLFSSL_PKMSG("PK 448 PMS: ret %d, pubKeySz %d, outLen %d\n",
  2545. ret, *pubKeySz, *outlen);
  2546. return ret;
  2547. }
  2548. #endif /* HAVE_CURVE448 */
  2549. #endif /* HAVE_ECC */
  2550. #ifndef NO_DH
  2551. static WC_INLINE int myDhCallback(WOLFSSL* ssl, struct DhKey* key,
  2552. const unsigned char* priv, unsigned int privSz,
  2553. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  2554. unsigned char* out, unsigned int* outlen,
  2555. void* ctx)
  2556. {
  2557. int ret;
  2558. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2559. (void)ssl;
  2560. (void)cbInfo;
  2561. /* return 0 on success */
  2562. ret = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  2563. WOLFSSL_PKMSG("PK ED Agree: ret %d, privSz %d, pubKeySz %d, outlen %d\n",
  2564. ret, privSz, pubKeySz, *outlen);
  2565. return ret;
  2566. };
  2567. #endif /* !NO_DH */
  2568. #ifndef NO_RSA
  2569. static WC_INLINE int myRsaSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  2570. byte* out, word32* outSz, const byte* key, word32 keySz, void* ctx)
  2571. {
  2572. WC_RNG rng;
  2573. int ret;
  2574. word32 idx = 0;
  2575. RsaKey myKey;
  2576. byte* keyBuf = (byte*)key;
  2577. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2578. (void)ssl;
  2579. (void)cbInfo;
  2580. WOLFSSL_PKMSG("PK RSA Sign: inSz %d, keySz %d\n", inSz, keySz);
  2581. #ifdef TEST_PK_PRIVKEY
  2582. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2583. if (ret != 0)
  2584. return ret;
  2585. #endif
  2586. ret = wc_InitRng(&rng);
  2587. if (ret != 0)
  2588. return ret;
  2589. ret = wc_InitRsaKey(&myKey, NULL);
  2590. if (ret == 0) {
  2591. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2592. if (ret == 0)
  2593. ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, &myKey, &rng);
  2594. if (ret > 0) { /* save and convert to 0 success */
  2595. *outSz = ret;
  2596. ret = 0;
  2597. }
  2598. wc_FreeRsaKey(&myKey);
  2599. }
  2600. wc_FreeRng(&rng);
  2601. #ifdef TEST_PK_PRIVKEY
  2602. free(keyBuf);
  2603. #endif
  2604. WOLFSSL_PKMSG("PK RSA Sign: ret %d, outSz %d\n", ret, *outSz);
  2605. return ret;
  2606. }
  2607. static WC_INLINE int myRsaVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
  2608. byte** out, const byte* key, word32 keySz, void* ctx)
  2609. {
  2610. int ret;
  2611. word32 idx = 0;
  2612. RsaKey myKey;
  2613. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2614. (void)ssl;
  2615. (void)cbInfo;
  2616. WOLFSSL_PKMSG("PK RSA Verify: sigSz %d, keySz %d\n", sigSz, keySz);
  2617. ret = wc_InitRsaKey(&myKey, NULL);
  2618. if (ret == 0) {
  2619. ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
  2620. if (ret == 0)
  2621. ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
  2622. wc_FreeRsaKey(&myKey);
  2623. }
  2624. WOLFSSL_PKMSG("PK RSA Verify: ret %d\n", ret);
  2625. return ret;
  2626. }
  2627. static WC_INLINE int myRsaSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz,
  2628. byte** out, const byte* key, word32 keySz, void* ctx)
  2629. {
  2630. int ret;
  2631. word32 idx = 0;
  2632. RsaKey myKey;
  2633. byte* keyBuf = (byte*)key;
  2634. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2635. (void)ssl;
  2636. (void)cbInfo;
  2637. WOLFSSL_PKMSG("PK RSA SignCheck: sigSz %d, keySz %d\n", sigSz, keySz);
  2638. #ifdef TEST_PK_PRIVKEY
  2639. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2640. if (ret != 0)
  2641. return ret;
  2642. #endif
  2643. ret = wc_InitRsaKey(&myKey, NULL);
  2644. if (ret == 0) {
  2645. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2646. if (ret == 0)
  2647. ret = wc_RsaSSL_VerifyInline(sig, sigSz, out, &myKey);
  2648. wc_FreeRsaKey(&myKey);
  2649. }
  2650. #ifdef TEST_PK_PRIVKEY
  2651. free(keyBuf);
  2652. #endif
  2653. WOLFSSL_PKMSG("PK RSA SignCheck: ret %d\n", ret);
  2654. return ret;
  2655. }
  2656. #ifdef WC_RSA_PSS
  2657. static WC_INLINE int myRsaPssSign(WOLFSSL* ssl, const byte* in, word32 inSz,
  2658. byte* out, word32* outSz, int hash, int mgf, const byte* key,
  2659. word32 keySz, void* ctx)
  2660. {
  2661. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  2662. WC_RNG rng;
  2663. int ret;
  2664. word32 idx = 0;
  2665. RsaKey myKey;
  2666. byte* keyBuf = (byte*)key;
  2667. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2668. (void)ssl;
  2669. (void)cbInfo;
  2670. WOLFSSL_PKMSG("PK RSA PSS Sign: inSz %d, hash %d, mgf %d, keySz %d\n",
  2671. inSz, hash, mgf, keySz);
  2672. #ifdef TEST_PK_PRIVKEY
  2673. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2674. if (ret != 0)
  2675. return ret;
  2676. #endif
  2677. switch (hash) {
  2678. #ifndef NO_SHA256
  2679. case SHA256h:
  2680. hashType = WC_HASH_TYPE_SHA256;
  2681. break;
  2682. #endif
  2683. #ifdef WOLFSSL_SHA384
  2684. case SHA384h:
  2685. hashType = WC_HASH_TYPE_SHA384;
  2686. break;
  2687. #endif
  2688. #ifdef WOLFSSL_SHA512
  2689. case SHA512h:
  2690. hashType = WC_HASH_TYPE_SHA512;
  2691. break;
  2692. #endif
  2693. }
  2694. ret = wc_InitRng(&rng);
  2695. if (ret != 0)
  2696. return ret;
  2697. ret = wc_InitRsaKey(&myKey, NULL);
  2698. if (ret == 0) {
  2699. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2700. if (ret == 0) {
  2701. ret = wc_RsaPSS_Sign(in, inSz, out, *outSz, hashType, mgf, &myKey,
  2702. &rng);
  2703. }
  2704. if (ret > 0) { /* save and convert to 0 success */
  2705. *outSz = ret;
  2706. ret = 0;
  2707. }
  2708. wc_FreeRsaKey(&myKey);
  2709. }
  2710. wc_FreeRng(&rng);
  2711. #ifdef TEST_PK_PRIVKEY
  2712. free(keyBuf);
  2713. #endif
  2714. WOLFSSL_PKMSG("PK RSA PSS Sign: ret %d, outSz %d\n", ret, *outSz);
  2715. return ret;
  2716. }
  2717. static WC_INLINE int myRsaPssVerify(WOLFSSL* ssl, byte* sig, word32 sigSz,
  2718. byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx)
  2719. {
  2720. int ret;
  2721. word32 idx = 0;
  2722. RsaKey myKey;
  2723. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2724. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  2725. (void)ssl;
  2726. (void)cbInfo;
  2727. WOLFSSL_PKMSG("PK RSA PSS Verify: sigSz %d, hash %d, mgf %d, keySz %d\n",
  2728. sigSz, hash, mgf, keySz);
  2729. switch (hash) {
  2730. #ifndef NO_SHA256
  2731. case SHA256h:
  2732. hashType = WC_HASH_TYPE_SHA256;
  2733. break;
  2734. #endif
  2735. #ifdef WOLFSSL_SHA384
  2736. case SHA384h:
  2737. hashType = WC_HASH_TYPE_SHA384;
  2738. break;
  2739. #endif
  2740. #ifdef WOLFSSL_SHA512
  2741. case SHA512h:
  2742. hashType = WC_HASH_TYPE_SHA512;
  2743. break;
  2744. #endif
  2745. }
  2746. ret = wc_InitRsaKey(&myKey, NULL);
  2747. if (ret == 0) {
  2748. ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
  2749. if (ret == 0) {
  2750. ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf,
  2751. &myKey);
  2752. }
  2753. wc_FreeRsaKey(&myKey);
  2754. }
  2755. WOLFSSL_PKMSG("PK RSA PSS Verify: ret %d\n", ret);
  2756. return ret;
  2757. }
  2758. static WC_INLINE int myRsaPssSignCheck(WOLFSSL* ssl, byte* sig, word32 sigSz,
  2759. byte** out, int hash, int mgf, const byte* key, word32 keySz, void* ctx)
  2760. {
  2761. int ret;
  2762. word32 idx = 0;
  2763. RsaKey myKey;
  2764. byte* keyBuf = (byte*)key;
  2765. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2766. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  2767. (void)ssl;
  2768. (void)cbInfo;
  2769. WOLFSSL_PKMSG("PK RSA PSS SignCheck: sigSz %d, hash %d, mgf %d, keySz %d\n",
  2770. sigSz, hash, mgf, keySz);
  2771. #ifdef TEST_PK_PRIVKEY
  2772. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2773. if (ret != 0)
  2774. return ret;
  2775. #endif
  2776. switch (hash) {
  2777. #ifndef NO_SHA256
  2778. case SHA256h:
  2779. hashType = WC_HASH_TYPE_SHA256;
  2780. break;
  2781. #endif
  2782. #ifdef WOLFSSL_SHA384
  2783. case SHA384h:
  2784. hashType = WC_HASH_TYPE_SHA384;
  2785. break;
  2786. #endif
  2787. #ifdef WOLFSSL_SHA512
  2788. case SHA512h:
  2789. hashType = WC_HASH_TYPE_SHA512;
  2790. break;
  2791. #endif
  2792. }
  2793. ret = wc_InitRsaKey(&myKey, NULL);
  2794. if (ret == 0) {
  2795. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2796. if (ret == 0) {
  2797. ret = wc_RsaPSS_VerifyInline(sig, sigSz, out, hashType, mgf,
  2798. &myKey);
  2799. }
  2800. wc_FreeRsaKey(&myKey);
  2801. }
  2802. #ifdef TEST_PK_PRIVKEY
  2803. free(keyBuf);
  2804. #endif
  2805. WOLFSSL_PKMSG("PK RSA PSS SignCheck: ret %d\n", ret);
  2806. return ret;
  2807. }
  2808. #endif
  2809. static WC_INLINE int myRsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz,
  2810. byte* out, word32* outSz, const byte* key,
  2811. word32 keySz, void* ctx)
  2812. {
  2813. int ret;
  2814. word32 idx = 0;
  2815. RsaKey myKey;
  2816. WC_RNG rng;
  2817. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2818. (void)ssl;
  2819. (void)cbInfo;
  2820. WOLFSSL_PKMSG("PK RSA Enc: inSz %d, keySz %d\n", inSz, keySz);
  2821. ret = wc_InitRng(&rng);
  2822. if (ret != 0)
  2823. return ret;
  2824. ret = wc_InitRsaKey(&myKey, NULL);
  2825. if (ret == 0) {
  2826. ret = wc_RsaPublicKeyDecode(key, &idx, &myKey, keySz);
  2827. if (ret == 0) {
  2828. ret = wc_RsaPublicEncrypt(in, inSz, out, *outSz, &myKey, &rng);
  2829. if (ret > 0) {
  2830. *outSz = ret;
  2831. ret = 0; /* reset to success */
  2832. }
  2833. }
  2834. wc_FreeRsaKey(&myKey);
  2835. }
  2836. wc_FreeRng(&rng);
  2837. WOLFSSL_PKMSG("PK RSA Enc: ret %d, outSz %d\n", ret, *outSz);
  2838. return ret;
  2839. }
  2840. static WC_INLINE int myRsaDec(WOLFSSL* ssl, byte* in, word32 inSz,
  2841. byte** out,
  2842. const byte* key, word32 keySz, void* ctx)
  2843. {
  2844. int ret;
  2845. word32 idx = 0;
  2846. RsaKey myKey;
  2847. byte* keyBuf = (byte*)key;
  2848. PkCbInfo* cbInfo = (PkCbInfo*)ctx;
  2849. (void)ssl;
  2850. (void)cbInfo;
  2851. WOLFSSL_PKMSG("PK RSA Dec: inSz %d, keySz %d\n", inSz, keySz);
  2852. #ifdef TEST_PK_PRIVKEY
  2853. ret = load_key_file(cbInfo->ourKey, &keyBuf, &keySz);
  2854. if (ret != 0)
  2855. return ret;
  2856. #endif
  2857. ret = wc_InitRsaKey(&myKey, NULL);
  2858. if (ret == 0) {
  2859. ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, keySz);
  2860. if (ret == 0) {
  2861. #ifdef WC_RSA_BLINDING
  2862. ret = wc_RsaSetRNG(&myKey, wolfSSL_GetRNG(ssl));
  2863. if (ret != 0) {
  2864. wc_FreeRsaKey(&myKey);
  2865. return ret;
  2866. }
  2867. #endif
  2868. ret = wc_RsaPrivateDecryptInline(in, inSz, out, &myKey);
  2869. }
  2870. wc_FreeRsaKey(&myKey);
  2871. }
  2872. #ifdef TEST_PK_PRIVKEY
  2873. free(keyBuf);
  2874. #endif
  2875. WOLFSSL_PKMSG("PK RSA Dec: ret %d\n", ret);
  2876. return ret;
  2877. }
  2878. #endif /* NO_RSA */
  2879. static WC_INLINE void SetupPkCallbacks(WOLFSSL_CTX* ctx)
  2880. {
  2881. (void)ctx;
  2882. #ifdef HAVE_ECC
  2883. wolfSSL_CTX_SetEccKeyGenCb(ctx, myEccKeyGen);
  2884. wolfSSL_CTX_SetEccSignCb(ctx, myEccSign);
  2885. wolfSSL_CTX_SetEccVerifyCb(ctx, myEccVerify);
  2886. wolfSSL_CTX_SetEccSharedSecretCb(ctx, myEccSharedSecret);
  2887. #endif /* HAVE_ECC */
  2888. #ifndef NO_DH
  2889. wolfSSL_CTX_SetDhAgreeCb(ctx, myDhCallback);
  2890. #endif
  2891. #ifdef HAVE_ED25519
  2892. wolfSSL_CTX_SetEd25519SignCb(ctx, myEd25519Sign);
  2893. wolfSSL_CTX_SetEd25519VerifyCb(ctx, myEd25519Verify);
  2894. #endif
  2895. #ifdef HAVE_CURVE25519
  2896. wolfSSL_CTX_SetX25519KeyGenCb(ctx, myX25519KeyGen);
  2897. wolfSSL_CTX_SetX25519SharedSecretCb(ctx, myX25519SharedSecret);
  2898. #endif
  2899. #ifdef HAVE_ED448
  2900. wolfSSL_CTX_SetEd448SignCb(ctx, myEd448Sign);
  2901. wolfSSL_CTX_SetEd448VerifyCb(ctx, myEd448Verify);
  2902. #endif
  2903. #ifdef HAVE_CURVE448
  2904. wolfSSL_CTX_SetX448KeyGenCb(ctx, myX448KeyGen);
  2905. wolfSSL_CTX_SetX448SharedSecretCb(ctx, myX448SharedSecret);
  2906. #endif
  2907. #ifndef NO_RSA
  2908. wolfSSL_CTX_SetRsaSignCb(ctx, myRsaSign);
  2909. wolfSSL_CTX_SetRsaVerifyCb(ctx, myRsaVerify);
  2910. wolfSSL_CTX_SetRsaSignCheckCb(ctx, myRsaSignCheck);
  2911. #ifdef WC_RSA_PSS
  2912. wolfSSL_CTX_SetRsaPssSignCb(ctx, myRsaPssSign);
  2913. wolfSSL_CTX_SetRsaPssVerifyCb(ctx, myRsaPssVerify);
  2914. wolfSSL_CTX_SetRsaPssSignCheckCb(ctx, myRsaPssSignCheck);
  2915. #endif
  2916. wolfSSL_CTX_SetRsaEncCb(ctx, myRsaEnc);
  2917. wolfSSL_CTX_SetRsaDecCb(ctx, myRsaDec);
  2918. #endif /* NO_RSA */
  2919. }
  2920. static WC_INLINE void SetupPkCallbackContexts(WOLFSSL* ssl, void* myCtx)
  2921. {
  2922. #ifdef HAVE_ECC
  2923. wolfSSL_SetEccKeyGenCtx(ssl, myCtx);
  2924. wolfSSL_SetEccSignCtx(ssl, myCtx);
  2925. wolfSSL_SetEccVerifyCtx(ssl, myCtx);
  2926. wolfSSL_SetEccSharedSecretCtx(ssl, myCtx);
  2927. #endif /* HAVE_ECC */
  2928. #ifndef NO_DH
  2929. wolfSSL_SetDhAgreeCtx(ssl, myCtx);
  2930. #endif
  2931. #ifdef HAVE_ED25519
  2932. wolfSSL_SetEd25519SignCtx(ssl, myCtx);
  2933. wolfSSL_SetEd25519VerifyCtx(ssl, myCtx);
  2934. #endif
  2935. #ifdef HAVE_CURVE25519
  2936. wolfSSL_SetX25519KeyGenCtx(ssl, myCtx);
  2937. wolfSSL_SetX25519SharedSecretCtx(ssl, myCtx);
  2938. #endif
  2939. #ifdef HAVE_ED448
  2940. wolfSSL_SetEd448SignCtx(ssl, myCtx);
  2941. wolfSSL_SetEd448VerifyCtx(ssl, myCtx);
  2942. #endif
  2943. #ifdef HAVE_CURVE448
  2944. wolfSSL_SetX448KeyGenCtx(ssl, myCtx);
  2945. wolfSSL_SetX448SharedSecretCtx(ssl, myCtx);
  2946. #endif
  2947. #ifndef NO_RSA
  2948. wolfSSL_SetRsaSignCtx(ssl, myCtx);
  2949. wolfSSL_SetRsaVerifyCtx(ssl, myCtx);
  2950. #ifdef WC_RSA_PSS
  2951. wolfSSL_SetRsaPssSignCtx(ssl, myCtx);
  2952. wolfSSL_SetRsaPssVerifyCtx(ssl, myCtx);
  2953. #endif
  2954. wolfSSL_SetRsaEncCtx(ssl, myCtx);
  2955. wolfSSL_SetRsaDecCtx(ssl, myCtx);
  2956. #endif /* NO_RSA */
  2957. }
  2958. #endif /* HAVE_PK_CALLBACKS */
  2959. #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \
  2960. || defined(_MSC_VER)
  2961. /* HP/UX doesn't have strsep, needed by test/suites.c */
  2962. static WC_INLINE char* strsep(char **stringp, const char *delim)
  2963. {
  2964. char* start;
  2965. char* end;
  2966. start = *stringp;
  2967. if (start == NULL)
  2968. return NULL;
  2969. if ((end = strpbrk(start, delim))) {
  2970. *end++ = '\0';
  2971. *stringp = end;
  2972. } else {
  2973. *stringp = NULL;
  2974. }
  2975. return start;
  2976. }
  2977. #endif /* __hpux__ and others */
  2978. /* Create unique filename, len is length of tempfn name, assuming
  2979. len does not include null terminating character,
  2980. num is number of characters in tempfn name to randomize */
  2981. static WC_INLINE const char* mymktemp(char *tempfn, int len, int num)
  2982. {
  2983. int x, size;
  2984. static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2985. "abcdefghijklmnopqrstuvwxyz";
  2986. WC_RNG rng;
  2987. byte out;
  2988. if (tempfn == NULL || len < 1 || num < 1 || len <= num) {
  2989. printf("Bad input\n");
  2990. return NULL;
  2991. }
  2992. size = len - 1;
  2993. if (wc_InitRng(&rng) != 0) {
  2994. printf("InitRng failed\n");
  2995. return NULL;
  2996. }
  2997. for (x = size; x > size - num; x--) {
  2998. if (wc_RNG_GenerateBlock(&rng,(byte*)&out, sizeof(out)) != 0) {
  2999. printf("RNG_GenerateBlock failed\n");
  3000. return NULL;
  3001. }
  3002. tempfn[x] = alphanum[out % (sizeof(alphanum) - 1)];
  3003. }
  3004. tempfn[len] = '\0';
  3005. wc_FreeRng(&rng);
  3006. (void)rng; /* for WC_NO_RNG case */
  3007. return tempfn;
  3008. }
  3009. #if defined(HAVE_SESSION_TICKET) && defined(HAVE_CHACHA) && \
  3010. defined(HAVE_POLY1305)
  3011. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  3012. typedef struct key_ctx {
  3013. byte name[WOLFSSL_TICKET_NAME_SZ]; /* name for this context */
  3014. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE]; /* cipher key */
  3015. } key_ctx;
  3016. static THREAD_LS_T key_ctx myKey_ctx;
  3017. static THREAD_LS_T WC_RNG myKey_rng;
  3018. static WC_INLINE int TicketInit(void)
  3019. {
  3020. int ret = wc_InitRng(&myKey_rng);
  3021. if (ret != 0) return ret;
  3022. ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.key, sizeof(myKey_ctx.key));
  3023. if (ret != 0) return ret;
  3024. ret = wc_RNG_GenerateBlock(&myKey_rng, myKey_ctx.name,sizeof(myKey_ctx.name));
  3025. if (ret != 0) return ret;
  3026. return 0;
  3027. }
  3028. static WC_INLINE void TicketCleanup(void)
  3029. {
  3030. wc_FreeRng(&myKey_rng);
  3031. }
  3032. static WC_INLINE int myTicketEncCb(WOLFSSL* ssl,
  3033. byte key_name[WOLFSSL_TICKET_NAME_SZ],
  3034. byte iv[WOLFSSL_TICKET_IV_SZ],
  3035. byte mac[WOLFSSL_TICKET_MAC_SZ],
  3036. int enc, byte* ticket, int inLen, int* outLen,
  3037. void* userCtx)
  3038. {
  3039. int ret;
  3040. word16 sLen = XHTONS(inLen);
  3041. byte aad[WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2];
  3042. int aadSz = WOLFSSL_TICKET_NAME_SZ + WOLFSSL_TICKET_IV_SZ + 2;
  3043. byte* tmp = aad;
  3044. (void)ssl;
  3045. (void)userCtx;
  3046. /* encrypt */
  3047. if (enc) {
  3048. XMEMCPY(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ);
  3049. ret = wc_RNG_GenerateBlock(&myKey_rng, iv, WOLFSSL_TICKET_IV_SZ);
  3050. if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
  3051. /* build aad from key name, iv, and length */
  3052. XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
  3053. tmp += WOLFSSL_TICKET_NAME_SZ;
  3054. XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
  3055. tmp += WOLFSSL_TICKET_IV_SZ;
  3056. XMEMCPY(tmp, &sLen, 2);
  3057. ret = wc_ChaCha20Poly1305_Encrypt(myKey_ctx.key, iv,
  3058. aad, aadSz,
  3059. ticket, inLen,
  3060. ticket,
  3061. mac);
  3062. if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
  3063. *outLen = inLen; /* no padding in this mode */
  3064. }
  3065. /* decrypt */
  3066. else {
  3067. /* see if we know this key */
  3068. if (XMEMCMP(key_name, myKey_ctx.name, WOLFSSL_TICKET_NAME_SZ) != 0){
  3069. printf("client presented unknown ticket key name ");
  3070. return WOLFSSL_TICKET_RET_FATAL;
  3071. }
  3072. /* build aad from key name, iv, and length */
  3073. XMEMCPY(tmp, key_name, WOLFSSL_TICKET_NAME_SZ);
  3074. tmp += WOLFSSL_TICKET_NAME_SZ;
  3075. XMEMCPY(tmp, iv, WOLFSSL_TICKET_IV_SZ);
  3076. tmp += WOLFSSL_TICKET_IV_SZ;
  3077. XMEMCPY(tmp, &sLen, 2);
  3078. ret = wc_ChaCha20Poly1305_Decrypt(myKey_ctx.key, iv,
  3079. aad, aadSz,
  3080. ticket, inLen,
  3081. mac,
  3082. ticket);
  3083. if (ret != 0) return WOLFSSL_TICKET_RET_REJECT;
  3084. *outLen = inLen; /* no padding in this mode */
  3085. }
  3086. return WOLFSSL_TICKET_RET_OK;
  3087. }
  3088. #endif /* HAVE_SESSION_TICKET && HAVE_CHACHA && HAVE_POLY1305 */
  3089. static WC_INLINE word16 GetRandomPort(void)
  3090. {
  3091. word16 port = 0;
  3092. /* Generate random port for testing */
  3093. WC_RNG rng;
  3094. if (wc_InitRng(&rng) == 0) {
  3095. if (wc_RNG_GenerateBlock(&rng, (byte*)&port, sizeof(port)) == 0) {
  3096. port |= 0xC000; /* Make sure its in the 49152 - 65535 range */
  3097. }
  3098. wc_FreeRng(&rng);
  3099. }
  3100. (void)rng; /* for WC_NO_RNG case */
  3101. return port;
  3102. }
  3103. #endif /* wolfSSL_TEST_H */