wc_pkcs11.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732
  1. /* wc_pkcs11.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef HAVE_PKCS11
  26. #include <dlfcn.h>
  27. #include <wolfssl/wolfcrypt/wc_pkcs11.h>
  28. #include <wolfssl/wolfcrypt/error-crypt.h>
  29. #include <wolfssl/wolfcrypt/asn.h>
  30. #include <wolfssl/wolfcrypt/logging.h>
  31. #ifndef NO_RSA
  32. #include <wolfssl/wolfcrypt/rsa.h>
  33. #endif
  34. #ifdef NO_INLINE
  35. #include <wolfssl/wolfcrypt/misc.h>
  36. #else
  37. #define WOLFSSL_MISC_INCLUDED
  38. #include <wolfcrypt/src/misc.c>
  39. #endif
  40. #if defined(NO_PKCS11_RSA) && !defined(NO_RSA)
  41. #define NO_RSA
  42. #endif
  43. #if defined(NO_PKCS11_ECC) && defined(HAVE_ECC)
  44. #undef HAVE_ECC
  45. #endif
  46. #if defined(NO_PKCS11_AES) && !defined(NO_AES)
  47. #define NO_AES
  48. #endif
  49. #if defined(NO_PKCS11_AESGCM) && defined(HAVE_AESGCM)
  50. #undef HAVE_AESGCM
  51. #endif
  52. #if defined(NO_PKCS11_AESCBC) && defined(HAVE_AES_CBC)
  53. #undef HAVE_AES_CBC
  54. #endif
  55. #if defined(NO_PKCS11_HMAC) && !defined(NO_HMAC)
  56. #define NO_HMAC
  57. #endif
  58. #if defined(NO_PKCS11_RNG) && !defined(WC_NO_RNG)
  59. #define WC_NO_RNG
  60. #endif
  61. /* Maximim length of the EC parameter string. */
  62. #define MAX_EC_PARAM_LEN 16
  63. #if defined(HAVE_ECC) && !defined(NO_PKCS11_ECDH)
  64. /* Pointer to false required for templates. */
  65. static CK_BBOOL ckFalse = CK_FALSE;
  66. #endif
  67. #if !defined(NO_RSA) || defined(HAVE_ECC) || (!defined(NO_AES) && \
  68. (defined(HAVE_AESGCM) || defined(HAVE_AES_CBC))) || !defined(NO_HMAC)
  69. /* Pointer to true required for templates. */
  70. static CK_BBOOL ckTrue = CK_TRUE;
  71. #endif
  72. #ifndef NO_RSA
  73. /* Pointer to RSA key type required for templates. */
  74. static CK_KEY_TYPE rsaKeyType = CKK_RSA;
  75. #endif
  76. #ifdef HAVE_ECC
  77. /* Pointer to EC key type required for templates. */
  78. static CK_KEY_TYPE ecKeyType = CKK_EC;
  79. #endif
  80. #if !defined(NO_RSA) || defined(HAVE_ECC)
  81. /* Pointer to public key class required for templates. */
  82. static CK_OBJECT_CLASS pubKeyClass = CKO_PUBLIC_KEY;
  83. /* Pointer to private key class required for templates. */
  84. static CK_OBJECT_CLASS privKeyClass = CKO_PRIVATE_KEY;
  85. #endif
  86. #if (!defined(NO_AES) && (defined(HAVE_AESGCM) || defined(HAVE_AES_CBC))) || \
  87. !defined(NO_HMAC) || (defined(HAVE_ECC) && !defined(NO_PKCS11_ECDH))
  88. /* Pointer to secret key class required for templates. */
  89. static CK_OBJECT_CLASS secretKeyClass = CKO_SECRET_KEY;
  90. #endif
  91. #ifdef WOLFSSL_DEBUG_PKCS11
  92. /* Formats of template items - used to instruct how to log information. */
  93. enum PKCS11_TYPE_FORMATS {
  94. PKCS11_FMT_BOOLEAN,
  95. PKCS11_FMT_CLASS,
  96. PKCS11_FMT_KEY_TYPE,
  97. PKCS11_FMT_STRING,
  98. PKCS11_FMT_NUMBER,
  99. PKCS11_FMT_DATA,
  100. PKCS11_FMT_POINTER
  101. };
  102. /* Information for logging a template item. */
  103. static struct PKCS11_TYPE_STR {
  104. /** Attribute type in template. */
  105. CK_ATTRIBUTE_TYPE type;
  106. /** String to log corresponding to attribute type. */
  107. const char* str;
  108. /** Format of data associated with template item. */
  109. int format;
  110. } typeStr[] = {
  111. { CKA_CLASS, "CKA_CLASS", PKCS11_FMT_CLASS },
  112. { CKA_TOKEN, "CKA_TOKEN", PKCS11_FMT_POINTER },
  113. { CKA_PRIVATE, "CKA_PRIVATE", PKCS11_FMT_BOOLEAN },
  114. { CKA_LABEL, "CKA_LABEL", PKCS11_FMT_STRING },
  115. { CKA_VALUE, "CKA_VALUE", PKCS11_FMT_DATA },
  116. { CKA_OBJECT_ID, "CKA_OBJECT_ID", PKCS11_FMT_POINTER },
  117. { CKA_KEY_TYPE, "CKA_KEY_TYPE", PKCS11_FMT_KEY_TYPE },
  118. { CKA_ID, "CKA_ID", PKCS11_FMT_DATA },
  119. { CKA_SENSITIVE, "CKA_SENSITIVE", PKCS11_FMT_BOOLEAN },
  120. { CKA_ENCRYPT, "CKA_ENCRYPT", PKCS11_FMT_BOOLEAN },
  121. { CKA_DECRYPT, "CKA_DECRYPT", PKCS11_FMT_BOOLEAN },
  122. { CKA_SIGN, "CKA_SIGN", PKCS11_FMT_BOOLEAN },
  123. { CKA_VERIFY, "CKA_VERIFY", PKCS11_FMT_BOOLEAN },
  124. { CKA_DERIVE, "CKA_DERIVE", PKCS11_FMT_BOOLEAN },
  125. { CKA_MODULUS_BITS, "CKA_MODULUS_BITS", PKCS11_FMT_NUMBER },
  126. { CKA_MODULUS, "CKA_MODULUS", PKCS11_FMT_DATA },
  127. { CKA_PUBLIC_EXPONENT, "CKA_PUBLIC_EXPONENT", PKCS11_FMT_DATA },
  128. { CKA_PRIVATE_EXPONENT, "CKA_PRIVATE_EXPONENT", PKCS11_FMT_DATA },
  129. { CKA_PRIME_1, "CKA_PRIME_1", PKCS11_FMT_DATA },
  130. { CKA_PRIME_2, "CKA_PRIME_2", PKCS11_FMT_DATA },
  131. { CKA_EXPONENT_1, "CKA_EXPONENT_1", PKCS11_FMT_DATA },
  132. { CKA_EXPONENT_2, "CKA_EXPONENT_2", PKCS11_FMT_DATA },
  133. { CKA_VALUE_LEN, "CKA_VALUE_LEN", PKCS11_FMT_NUMBER },
  134. { CKA_COEFFICIENT, "CKA_COEFFICIENT", PKCS11_FMT_DATA },
  135. { CKA_EXTRACTABLE, "CKA_EXTRACTABLE", PKCS11_FMT_BOOLEAN },
  136. { CKA_EC_PARAMS, "CKA_EC_PARAMS", PKCS11_FMT_DATA },
  137. { CKA_EC_POINT, "CKA_EC_POINT", PKCS11_FMT_DATA },
  138. };
  139. /* Count of known attribute types for logging. */
  140. #define PKCS11_TYPE_STR_CNT ((int)(sizeof(typeStr) / sizeof(*typeStr)))
  141. /*
  142. * Dump/log the PKCS #11 template.
  143. *
  144. * This is only for debugging purposes. Only the values needed are recognised.
  145. *
  146. * @param [in] templ PKCS #11 template to dump.
  147. * @param [in] cnt Count of template entries.
  148. */
  149. static void pkcs11_dump_template(CK_ATTRIBUTE* templ, CK_ULONG cnt)
  150. {
  151. CK_ULONG i;
  152. int j;
  153. char line[80];
  154. char type[25];
  155. int format;
  156. CK_KEY_TYPE keyType;
  157. CK_OBJECT_CLASS keyClass;
  158. for (i = 0; i < cnt; i++) {
  159. format = PKCS11_FMT_POINTER;
  160. for (j = 0; j < PKCS11_TYPE_STR_CNT; j++) {
  161. if (templ[i].type == typeStr[j].type) {
  162. XSNPRINTF(type, sizeof(type), "%s", typeStr[j].str);
  163. format = typeStr[j].format;
  164. break;
  165. }
  166. }
  167. if (j == PKCS11_TYPE_STR_CNT) {
  168. XSNPRINTF(type, sizeof(type), "%08lxUL", templ[i].type);
  169. }
  170. switch (format) {
  171. case PKCS11_FMT_BOOLEAN:
  172. #if !defined(NO_RSA) || defined(HAVE_ECC) || (!defined(NO_AES) && \
  173. (defined(HAVE_AESGCM) || defined(HAVE_AES_CBC))) || !defined(NO_HMAC)
  174. if (templ[i].pValue == &ckTrue) {
  175. XSNPRINTF(line, sizeof(line), "%25s: TRUE", type);
  176. WOLFSSL_MSG(line);
  177. }
  178. else
  179. #endif
  180. #if defined(HAVE_ECC) && !defined(NO_PKCS11_ECDH)
  181. if (templ[i].pValue == &ckFalse) {
  182. XSNPRINTF(line, sizeof(line), "%25s: FALSE", type);
  183. WOLFSSL_MSG(line);
  184. }
  185. else
  186. #endif
  187. {
  188. XSNPRINTF(line, sizeof(line), "%25s: INVALID (%p)", type,
  189. templ[i].pValue);
  190. WOLFSSL_MSG(line);
  191. }
  192. break;
  193. case PKCS11_FMT_CLASS:
  194. keyClass = *(CK_OBJECT_CLASS*)templ[i].pValue;
  195. if (keyClass == CKO_PUBLIC_KEY) {
  196. XSNPRINTF(line, sizeof(line), "%25s: PUBLIC", type);
  197. WOLFSSL_MSG(line);
  198. }
  199. else if (keyClass == CKO_PRIVATE_KEY) {
  200. XSNPRINTF(line, sizeof(line), "%25s: PRIVATE", type);
  201. WOLFSSL_MSG(line);
  202. }
  203. else if (keyClass == CKO_SECRET_KEY) {
  204. XSNPRINTF(line, sizeof(line), "%25s: SECRET", type);
  205. WOLFSSL_MSG(line);
  206. }
  207. else
  208. {
  209. XSNPRINTF(line, sizeof(line), "%25s: UNKNOWN (%p)", type,
  210. templ[i].pValue);
  211. WOLFSSL_MSG(line);
  212. }
  213. break;
  214. case PKCS11_FMT_KEY_TYPE:
  215. keyType = *(CK_KEY_TYPE*)templ[i].pValue;
  216. switch (keyType) {
  217. case CKK_RSA:
  218. XSNPRINTF(line, sizeof(line), "%25s: RSA", type);
  219. break;
  220. case CKK_DH:
  221. XSNPRINTF(line, sizeof(line), "%25s: DH", type);
  222. break;
  223. case CKK_EC:
  224. XSNPRINTF(line, sizeof(line), "%25s: EC", type);
  225. break;
  226. case CKK_GENERIC_SECRET:
  227. XSNPRINTF(line, sizeof(line), "%25s: GENERIC_SECRET", type);
  228. break;
  229. case CKK_AES:
  230. XSNPRINTF(line, sizeof(line), "%25s: AES", type);
  231. break;
  232. case CKK_MD5_HMAC:
  233. XSNPRINTF(line, sizeof(line), "%25s: MD5_HMAC", type);
  234. break;
  235. case CKK_SHA_1_HMAC:
  236. XSNPRINTF(line, sizeof(line), "%25s: SHA_1_HMAC", type);
  237. break;
  238. case CKK_SHA256_HMAC:
  239. XSNPRINTF(line, sizeof(line), "%25s: SHA256_HMAC", type);
  240. break;
  241. case CKK_SHA384_HMAC:
  242. XSNPRINTF(line, sizeof(line), "%25s: SHA384_HMAC", type);
  243. break;
  244. case CKK_SHA512_HMAC:
  245. XSNPRINTF(line, sizeof(line), "%25s: SHA512_HMAC", type);
  246. break;
  247. case CKK_SHA224_HMAC:
  248. XSNPRINTF(line, sizeof(line), "%25s: SHA224_HMAC", type);
  249. break;
  250. default:
  251. XSNPRINTF(line, sizeof(line), "%25s: UNKNOWN (%08lx)", type,
  252. keyType);
  253. break;
  254. }
  255. WOLFSSL_MSG(line);
  256. break;
  257. case PKCS11_FMT_STRING:
  258. XSNPRINTF(line, sizeof(line), "%25s: %s", type,
  259. (char*)templ[i].pValue);
  260. WOLFSSL_MSG(line);
  261. break;
  262. case PKCS11_FMT_NUMBER:
  263. if (templ[i].ulValueLen <= 1) {
  264. XSNPRINTF(line, sizeof(line), "%25s: 0x%02x (%d)", type,
  265. *(byte*)templ[i].pValue, *(byte*)templ[i].pValue);
  266. }
  267. else if (templ[i].ulValueLen <= 2) {
  268. XSNPRINTF(line, sizeof(line), "%25s: 0x%04x (%d)", type,
  269. *(word16*)templ[i].pValue, *(word16*)templ[i].pValue);
  270. }
  271. else if (templ[i].ulValueLen <= 4) {
  272. XSNPRINTF(line, sizeof(line), "%25s: 0x%08x (%d)", type,
  273. *(word32*)templ[i].pValue, *(word32*)templ[i].pValue);
  274. }
  275. else if (templ[i].ulValueLen <= 8) {
  276. XSNPRINTF(line, sizeof(line), "%25s: 0x%016lx (%ld)", type,
  277. *(word64*)templ[i].pValue, *(word64*)templ[i].pValue);
  278. }
  279. else {
  280. XSNPRINTF(line, sizeof(line), "%25s: INVALID (%ld)", type,
  281. templ[i].ulValueLen);
  282. }
  283. WOLFSSL_MSG(line);
  284. break;
  285. case PKCS11_FMT_DATA:
  286. XSNPRINTF(line, sizeof(line), "%25s: %ld", type,
  287. templ[i].ulValueLen);
  288. WOLFSSL_MSG(line);
  289. if (templ[i].pValue == NULL) {
  290. XSNPRINTF(line, sizeof(line), "%27s(nil)", "");
  291. WOLFSSL_MSG(line);
  292. break;
  293. }
  294. XSNPRINTF(line, sizeof(line), "%27s", "");
  295. for (j = 0; j < (int)templ[i].ulValueLen && j < 80; j++) {
  296. char hex[6];
  297. XSNPRINTF(hex, sizeof(hex), "0x%02x,",
  298. ((byte*)templ[i].pValue)[j]);
  299. XSTRNCAT(line, hex, 5);
  300. if ((j % 8) == 7) {
  301. WOLFSSL_MSG(line);
  302. XSNPRINTF(line, sizeof(line), "%27s", "");
  303. }
  304. }
  305. if (j == (int)templ[i].ulValueLen) {
  306. if ((j % 8) != 0) {
  307. WOLFSSL_MSG(line);
  308. }
  309. }
  310. else if (j < (int)templ[i].ulValueLen) {
  311. XSNPRINTF(line, sizeof(line), "%27s...", "");
  312. WOLFSSL_MSG(line);
  313. }
  314. break;
  315. case PKCS11_FMT_POINTER:
  316. XSNPRINTF(line, sizeof(line), "%25s: %p %ld", type, templ[i].pValue,
  317. templ[i].ulValueLen);
  318. WOLFSSL_MSG(line);
  319. break;
  320. }
  321. }
  322. }
  323. /*
  324. * Log a PKCS #11 return value with the name of function called.
  325. *
  326. * This is only for debugging purposes. Only the values needed are recognised.
  327. *
  328. * @param [in] op PKCS #11 operation that was attempted.
  329. * @param [in] rv PKCS #11 return value.
  330. */
  331. static void pkcs11_rv(const char* op, CK_RV rv)
  332. {
  333. char line[80];
  334. if (rv == CKR_OK) {
  335. XSNPRINTF(line, 80, "%s: OK", op);
  336. }
  337. else if (rv == CKR_MECHANISM_INVALID) {
  338. XSNPRINTF(line, 80, "%s: MECHANISM_INVALID", op);
  339. }
  340. else if (rv == CKR_SIGNATURE_INVALID) {
  341. XSNPRINTF(line, 80, "%s: SIGNATURE_INVALID", op);
  342. }
  343. else {
  344. XSNPRINTF(line, 80, "%s: %08lxUL (FAILED)", op, rv);
  345. }
  346. WOLFSSL_MSG(line);
  347. }
  348. /*
  349. * Log a value from a PKCS #11 operation.
  350. *
  351. * This is only for debugging purposes.
  352. *
  353. * @param [in] op PKCS #11 operation that was attempted.
  354. * @param [in] val Value to log.
  355. */
  356. static void pkcs11_val(const char* op, CK_ULONG val)
  357. {
  358. char line[80];
  359. XSNPRINTF(line, 80, "%s: %ld", op, val);
  360. WOLFSSL_MSG(line);
  361. }
  362. #endif
  363. /**
  364. * Load library, get function list and initialize PKCS#11.
  365. *
  366. * @param [in] dev Device object.
  367. * @param [in] library Library name including path.
  368. * @return BAD_FUNC_ARG when dev or library are NULL pointers.
  369. * @return BAD_PATH_ERROR when dynamic library cannot be opened.
  370. * @return WC_INIT_E when the initialization PKCS#11 fails.
  371. * @return WC_HW_E when unable to get PKCS#11 function list.
  372. * @return 0 on success.
  373. */
  374. int wc_Pkcs11_Initialize(Pkcs11Dev* dev, const char* library, void* heap)
  375. {
  376. int ret = 0;
  377. void* func;
  378. CK_C_INITIALIZE_ARGS args;
  379. if (dev == NULL || library == NULL)
  380. ret = BAD_FUNC_ARG;
  381. if (ret == 0) {
  382. dev->heap = heap;
  383. dev->dlHandle = dlopen(library, RTLD_NOW | RTLD_LOCAL);
  384. if (dev->dlHandle == NULL) {
  385. WOLFSSL_MSG(dlerror());
  386. ret = BAD_PATH_ERROR;
  387. }
  388. }
  389. if (ret == 0) {
  390. dev->func = NULL;
  391. func = dlsym(dev->dlHandle, "C_GetFunctionList");
  392. if (func == NULL)
  393. ret = WC_HW_E;
  394. }
  395. if (ret == 0) {
  396. if (((CK_C_GetFunctionList)func)(&dev->func) != CKR_OK)
  397. ret = WC_HW_E;
  398. }
  399. if (ret == 0) {
  400. XMEMSET(&args, 0x00, sizeof(args));
  401. args.flags = CKF_OS_LOCKING_OK;
  402. if (dev->func->C_Initialize(&args) != CKR_OK)
  403. ret = WC_INIT_E;
  404. }
  405. if (ret != 0)
  406. wc_Pkcs11_Finalize(dev);
  407. return ret;
  408. }
  409. /**
  410. * Close the Pkcs#11 library.
  411. *
  412. * @param [in] dev Device object.
  413. */
  414. void wc_Pkcs11_Finalize(Pkcs11Dev* dev)
  415. {
  416. if (dev != NULL && dev->dlHandle != NULL) {
  417. if (dev->func != NULL) {
  418. dev->func->C_Finalize(NULL);
  419. dev->func = NULL;
  420. }
  421. dlclose(dev->dlHandle);
  422. dev->dlHandle = NULL;
  423. }
  424. }
  425. /**
  426. * Set up a token for use.
  427. *
  428. * @param [in] token Token object.
  429. * @param [in] dev PKCS#11 device object.
  430. * @param [in] slotId Slot number of the token.<br>
  431. * Passing -1 uses the first available slot.
  432. * @param [in] tokenName Name of token to initialize.
  433. * @param [in] userPin PIN to use to login as user.
  434. * @param [in] userPinSz Number of bytes in PIN.
  435. * @return BAD_FUNC_ARG when token, dev and/or tokenName is NULL.
  436. * @return WC_INIT_E when initializing token fails.
  437. * @return WC_HW_E when another PKCS#11 library call fails.
  438. * @return -1 when no slot available.
  439. * 0 on success.
  440. */
  441. int wc_Pkcs11Token_Init(Pkcs11Token* token, Pkcs11Dev* dev, int slotId,
  442. const char* tokenName, const unsigned char* userPin, int userPinSz)
  443. {
  444. int ret = 0;
  445. CK_RV rv;
  446. CK_SLOT_ID* slot = NULL;
  447. CK_ULONG slotCnt = 0;
  448. if (token == NULL || dev == NULL || tokenName == NULL)
  449. ret = BAD_FUNC_ARG;
  450. if (ret == 0) {
  451. if (slotId < 0) {
  452. /* Use first available slot with a token. */
  453. rv = dev->func->C_GetSlotList(CK_TRUE, NULL, &slotCnt);
  454. #ifdef WOLFSSL_DEBUG_PKCS11
  455. pkcs11_rv("C_GetSlotList", rv);
  456. #endif
  457. if (rv != CKR_OK) {
  458. ret = WC_HW_E;
  459. }
  460. if (ret == 0) {
  461. slot = (CK_SLOT_ID*)XMALLOC(slotCnt * sizeof(*slot), dev->heap,
  462. DYNAMIC_TYPE_TMP_BUFFER);
  463. if (slot == NULL)
  464. ret = MEMORY_E;
  465. }
  466. if (ret == 0) {
  467. rv = dev->func->C_GetSlotList(CK_TRUE, slot, &slotCnt);
  468. #ifdef WOLFSSL_DEBUG_PKCS11
  469. pkcs11_rv("C_GetSlotList", rv);
  470. #endif
  471. if (rv != CKR_OK) {
  472. ret = WC_HW_E;
  473. }
  474. }
  475. if (ret == 0) {
  476. if (slotCnt > 0)
  477. slotId = (int)slot[0];
  478. else
  479. ret = WC_HW_E;
  480. }
  481. }
  482. }
  483. if (ret == 0) {
  484. token->func = dev->func;
  485. token->slotId = (CK_SLOT_ID)slotId;
  486. token->handle = NULL_PTR;
  487. token->userPin = (CK_UTF8CHAR_PTR)userPin;
  488. token->userPinSz = (CK_ULONG)userPinSz;
  489. }
  490. if (slot != NULL)
  491. XFREE(slot, dev->heap, DYNAMIC_TYPE_TMP_BUFFER);
  492. return ret;
  493. }
  494. /**
  495. * Finalize token.
  496. * Closes all sessions on token.
  497. *
  498. * @param [in] token Token object.
  499. */
  500. void wc_Pkcs11Token_Final(Pkcs11Token* token)
  501. {
  502. if (token != NULL && token->func != NULL) {
  503. token->func->C_CloseAllSessions(token->slotId);
  504. token->handle = NULL_PTR;
  505. ForceZero(token->userPin, (word32)token->userPinSz);
  506. }
  507. }
  508. /**
  509. * Open a session on a token.
  510. *
  511. * @param [in] token Token object.
  512. * @param [in] session Session object.
  513. * @param [in] readWrite Boolean indicating to open session for Read/Write.
  514. * @return BAD_FUNC_ARG when token or session is NULL.
  515. * @return WC_HW_E when opening the session fails.
  516. * @return 0 on success.
  517. */
  518. static int Pkcs11OpenSession(Pkcs11Token* token, Pkcs11Session* session,
  519. int readWrite)
  520. {
  521. int ret = 0;
  522. CK_RV rv;
  523. if (token == NULL || session == NULL)
  524. ret = BAD_FUNC_ARG;
  525. if (ret == 0) {
  526. if (token->handle != NULL_PTR)
  527. session->handle = token->handle;
  528. else {
  529. /* Create a new session. */
  530. CK_FLAGS flags = CKF_SERIAL_SESSION;
  531. if (readWrite)
  532. flags |= CKF_RW_SESSION;
  533. rv = token->func->C_OpenSession(token->slotId, flags,
  534. (CK_VOID_PTR)NULL, (CK_NOTIFY)NULL,
  535. &session->handle);
  536. #ifdef WOLFSSL_DEBUG_PKCS11
  537. pkcs11_rv("C_OpenSession", rv);
  538. #endif
  539. if (rv != CKR_OK) {
  540. ret = WC_HW_E;
  541. }
  542. if (ret == 0 && token->userPin != NULL) {
  543. rv = token->func->C_Login(session->handle, CKU_USER,
  544. token->userPin, token->userPinSz);
  545. #ifdef WOLFSSL_DEBUG_PKCS11
  546. pkcs11_rv("C_Login", rv);
  547. #endif
  548. if (rv != CKR_OK) {
  549. ret = WC_HW_E;
  550. }
  551. }
  552. }
  553. }
  554. if (ret == 0) {
  555. session->func = token->func;
  556. session->slotId = token->slotId;
  557. }
  558. return ret;
  559. }
  560. /**
  561. * Close a session on a token.
  562. * Won't close a session created externally.
  563. *
  564. * @param [in] token Token object.
  565. * @param [in] session Session object.
  566. */
  567. static void Pkcs11CloseSession(Pkcs11Token* token, Pkcs11Session* session)
  568. {
  569. if (token != NULL && session != NULL && token->handle != session->handle) {
  570. if (token->userPin != NULL)
  571. session->func->C_Logout(session->handle);
  572. session->func->C_CloseSession(session->handle);
  573. }
  574. }
  575. /**
  576. * Open a session on the token to be used for all operations.
  577. *
  578. * @param [in] token Token object.
  579. * @param [in] readWrite Boolean indicating to open session for Read/Write.
  580. * @return BAD_FUNC_ARG when token is NULL.
  581. * @return WC_HW_E when opening the session fails.
  582. * @return 0 on success.
  583. */
  584. int wc_Pkcs11Token_Open(Pkcs11Token* token, int readWrite)
  585. {
  586. int ret = 0;
  587. Pkcs11Session session;
  588. if (token == NULL)
  589. ret = BAD_FUNC_ARG;
  590. if (ret == 0) {
  591. ret = Pkcs11OpenSession(token, &session, readWrite);
  592. token->handle = session.handle;
  593. }
  594. return ret;
  595. }
  596. /**
  597. * Close the token's session.
  598. * All object, like keys, will be destroyed.
  599. *
  600. * @param [in] token Token object.
  601. */
  602. void wc_Pkcs11Token_Close(Pkcs11Token* token)
  603. {
  604. Pkcs11Session session;
  605. if (token != NULL) {
  606. session.func = token->func;
  607. session.handle = token->handle;
  608. token->handle = NULL_PTR;
  609. Pkcs11CloseSession(token, &session);
  610. }
  611. }
  612. #if (!defined(NO_AES) && (defined(HAVE_AESGCM) || defined(HAVE_AES_CBC))) || \
  613. !defined(NO_HMAC)
  614. /*
  615. * Create a secret key.
  616. *
  617. * @param [out] key Handle to key object.
  618. * @param [in] session Session object.
  619. * @param [in] keyType Type of secret key to create.
  620. * @param [in] data Data of the secret key.
  621. * @param [in] len Length of data in bytes.
  622. * @param [in] id Identifier to set against key.
  623. * @param [in] idLen Length of identifier.
  624. * @param [in] label Label to set against key.
  625. * @param [in] labelLen Length of label.
  626. * @param [in] op Operation to support with key.
  627. * @return WC_HW_E when another PKCS#11 library call fails.
  628. * @return 0 on success.
  629. */
  630. static int Pkcs11CreateSecretKey(CK_OBJECT_HANDLE* key, Pkcs11Session* session,
  631. CK_KEY_TYPE keyType, unsigned char* data,
  632. int len, unsigned char* id, int idLen,
  633. char* label, int labelLen, int op)
  634. {
  635. int ret = 0;
  636. CK_RV rv;
  637. /* Empty entries for optional label/ID. */
  638. CK_ATTRIBUTE keyTemplateEncDec[] = {
  639. { CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
  640. { CKA_KEY_TYPE, &keyType, sizeof(keyType) },
  641. { CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
  642. { CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
  643. { CKA_VALUE, NULL, 0 },
  644. { 0, NULL, 0 },
  645. { 0, NULL, 0 }
  646. };
  647. /* Empty entries for optional label/ID. */
  648. CK_ATTRIBUTE keyTemplateSignVfy[] = {
  649. { CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
  650. { CKA_KEY_TYPE, &keyType, sizeof(keyType) },
  651. { CKA_SIGN, &ckTrue, sizeof(ckTrue) },
  652. { CKA_VERIFY, &ckTrue, sizeof(ckTrue) },
  653. { CKA_VALUE, NULL, 0 },
  654. { 0, NULL, 0 },
  655. { 0, NULL, 0 }
  656. };
  657. CK_ATTRIBUTE* keyTemplate = NULL;
  658. /* 5 mandatory entries + 2 optional. */
  659. int keyTmplCnt = 5;
  660. WOLFSSL_MSG("PKCS#11: Create Secret Key");
  661. if (op == CKA_ENCRYPT || op == CKA_DECRYPT) {
  662. keyTemplate = keyTemplateEncDec;
  663. }
  664. else if (op == CKA_SIGN) {
  665. keyTemplate = keyTemplateSignVfy;
  666. }
  667. else {
  668. WOLFSSL_MSG("PKCS#11: Invalid operation type");
  669. ret = WC_HW_E;
  670. }
  671. if (ret == 0) {
  672. /* Set the secret to store. */
  673. keyTemplate[keyTmplCnt-1].pValue = data;
  674. keyTemplate[keyTmplCnt-1].ulValueLen = (CK_ULONG)len;
  675. if (labelLen > 0) {
  676. keyTemplate[keyTmplCnt].type = CKA_LABEL;
  677. keyTemplate[keyTmplCnt].pValue = label;
  678. keyTemplate[keyTmplCnt].ulValueLen = labelLen;
  679. keyTmplCnt++;
  680. }
  681. if (idLen > 0) {
  682. keyTemplate[keyTmplCnt].type = CKA_ID;
  683. keyTemplate[keyTmplCnt].pValue = id;
  684. keyTemplate[keyTmplCnt].ulValueLen = idLen;
  685. keyTmplCnt++;
  686. }
  687. #ifdef WOLFSSL_DEBUG_PKCS11
  688. WOLFSSL_MSG("Secret Key");
  689. pkcs11_dump_template(keyTemplate, keyTmplCnt);
  690. #endif
  691. /* Create an object containing key data for device to use. */
  692. rv = session->func->C_CreateObject(session->handle, keyTemplate,
  693. keyTmplCnt, key);
  694. #ifdef WOLFSSL_DEBUG_PKCS11
  695. pkcs11_rv("C_CreateObject", rv);
  696. #endif
  697. if (rv != CKR_OK) {
  698. ret = WC_HW_E;
  699. }
  700. }
  701. return ret;
  702. }
  703. #endif
  704. #ifndef NO_RSA
  705. /**
  706. * Create a PKCS#11 object containing the RSA private key data.
  707. *
  708. * @param [out] privateKey Handle to private key object.
  709. * @param [in] session Session object.
  710. * @param [in] rsaKey RSA key with private key data.
  711. * @return WC_HW_E when a PKCS#11 library call fails.
  712. * @return 0 on success.
  713. */
  714. static int Pkcs11CreateRsaPrivateKey(CK_OBJECT_HANDLE* privateKey,
  715. Pkcs11Session* session,
  716. RsaKey* rsaKey, int permanent)
  717. {
  718. int ret = 0;
  719. CK_RV rv;
  720. /* Empty entries for optional label/ID. */
  721. CK_ATTRIBUTE keyTemplate[] = {
  722. { CKA_CLASS, &privKeyClass, sizeof(privKeyClass) },
  723. { CKA_KEY_TYPE, &rsaKeyType, sizeof(rsaKeyType) },
  724. { CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
  725. { CKA_SIGN, &ckTrue, sizeof(ckTrue) },
  726. { CKA_MODULUS, NULL, 0 },
  727. { CKA_PRIVATE_EXPONENT, NULL, 0 },
  728. { CKA_PRIME_1, NULL, 0 },
  729. { CKA_PRIME_2, NULL, 0 },
  730. { CKA_EXPONENT_1, NULL, 0 },
  731. { CKA_EXPONENT_2, NULL, 0 },
  732. { CKA_COEFFICIENT, NULL, 0 },
  733. { CKA_PUBLIC_EXPONENT, NULL, 0 },
  734. { 0, NULL, 0 },
  735. { 0, NULL, 0 }
  736. };
  737. /* Mandatory entries + 2 optional. */
  738. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate) - 2;
  739. /* Set the modulus and private key data. */
  740. keyTemplate[ 4].pValue = rsaKey->n.raw.buf;
  741. keyTemplate[ 4].ulValueLen = rsaKey->n.raw.len;
  742. keyTemplate[ 5].pValue = rsaKey->d.raw.buf;
  743. keyTemplate[ 5].ulValueLen = rsaKey->d.raw.len;
  744. keyTemplate[ 6].pValue = rsaKey->p.raw.buf;
  745. keyTemplate[ 6].ulValueLen = rsaKey->p.raw.len;
  746. keyTemplate[ 7].pValue = rsaKey->q.raw.buf;
  747. keyTemplate[ 7].ulValueLen = rsaKey->q.raw.len;
  748. keyTemplate[ 8].pValue = rsaKey->dP.raw.buf;
  749. keyTemplate[ 8].ulValueLen = rsaKey->dP.raw.len;
  750. keyTemplate[ 9].pValue = rsaKey->dQ.raw.buf;
  751. keyTemplate[ 9].ulValueLen = rsaKey->dQ.raw.len;
  752. keyTemplate[10].pValue = rsaKey->u.raw.buf;
  753. keyTemplate[10].ulValueLen = rsaKey->u.raw.len;
  754. keyTemplate[11].pValue = rsaKey->e.raw.buf;
  755. keyTemplate[11].ulValueLen = rsaKey->e.raw.len;
  756. if (permanent && rsaKey->labelLen > 0) {
  757. keyTemplate[keyTmplCnt].type = CKA_LABEL;
  758. keyTemplate[keyTmplCnt].pValue = rsaKey->label;
  759. keyTemplate[keyTmplCnt].ulValueLen = rsaKey->labelLen;
  760. keyTmplCnt++;
  761. }
  762. if (permanent && rsaKey->idLen > 0) {
  763. keyTemplate[keyTmplCnt].type = CKA_ID;
  764. keyTemplate[keyTmplCnt].pValue = rsaKey->id;
  765. keyTemplate[keyTmplCnt].ulValueLen = rsaKey->idLen;
  766. keyTmplCnt++;
  767. }
  768. #ifdef WOLFSSL_DEBUG_PKCS11
  769. WOLFSSL_MSG("RSA Private Key");
  770. pkcs11_dump_template(keyTemplate, keyTmplCnt);
  771. #endif
  772. rv = session->func->C_CreateObject(session->handle, keyTemplate, keyTmplCnt,
  773. privateKey);
  774. #ifdef WOLFSSL_DEBUG_PKCS11
  775. pkcs11_rv("C_CreateObject", rv);
  776. #endif
  777. if (rv != CKR_OK) {
  778. ret = WC_HW_E;
  779. }
  780. return ret;
  781. }
  782. #endif
  783. #ifdef HAVE_ECC
  784. /**
  785. * Set the ECC parameters into the template.
  786. *
  787. * @param [in] key ECC key.
  788. * @param [in] tmpl PKCS#11 template.
  789. * @param [in] idx Index of template to put parameters into.
  790. * @return NOT_COMPILED_IN when the EC parameters are not known.
  791. * @return 0 on success.
  792. */
  793. static int Pkcs11EccSetParams(ecc_key* key, CK_ATTRIBUTE* tmpl, int idx)
  794. {
  795. int ret = 0;
  796. if (key->dp != NULL && key->dp->oid != NULL) {
  797. unsigned char* derParams = tmpl[idx].pValue;
  798. /* ASN.1 encoding: OBJ + ecc parameters OID */
  799. tmpl[idx].ulValueLen = key->dp->oidSz + 2;
  800. derParams[0] = ASN_OBJECT_ID;
  801. derParams[1] = key->dp->oidSz;
  802. XMEMCPY(derParams + 2, key->dp->oid, key->dp->oidSz);
  803. }
  804. else
  805. ret = NOT_COMPILED_IN;
  806. return ret;
  807. }
  808. /**
  809. * Create a PKCS#11 object containing the ECC public key data.
  810. * Encode the public key as an OCTET_STRING of the encoded point.
  811. *
  812. * @param [out] publicKey Handle to public key object.
  813. * @param [in] session Session object.
  814. * @param [in] public_key ECC public key.
  815. * @param [in] operation Cryptographic operation key is to be used for.
  816. * @return WC_HW_E when a PKCS#11 library call fails.
  817. * @return MEMORY_E when a memory allocation fails.
  818. * @return 0 on success.
  819. */
  820. static int Pkcs11CreateEccPublicKey(CK_OBJECT_HANDLE* publicKey,
  821. Pkcs11Session* session,
  822. ecc_key* public_key,
  823. CK_ATTRIBUTE_TYPE operation)
  824. {
  825. int ret = 0;
  826. int i;
  827. unsigned char* ecPoint = NULL;
  828. word32 len;
  829. CK_RV rv;
  830. CK_UTF8CHAR params[MAX_EC_PARAM_LEN];
  831. /* Empty entries for optional label/ID. */
  832. CK_ATTRIBUTE keyTemplate[] = {
  833. { CKA_CLASS, &pubKeyClass, sizeof(pubKeyClass) },
  834. { CKA_KEY_TYPE, &ecKeyType, sizeof(ecKeyType) },
  835. { operation, &ckTrue, sizeof(ckTrue) },
  836. { CKA_EC_PARAMS, params, 0 },
  837. { CKA_EC_POINT, NULL, 0 },
  838. { 0, NULL, 0 },
  839. { 0, NULL, 0 }
  840. };
  841. /* Mandatory entries + 2 optional. */
  842. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate) - 2;
  843. if (public_key->labelLen > 0) {
  844. keyTemplate[keyTmplCnt].type = CKA_LABEL;
  845. keyTemplate[keyTmplCnt].pValue = public_key->label;
  846. keyTemplate[keyTmplCnt].ulValueLen = public_key->labelLen;
  847. keyTmplCnt++;
  848. }
  849. if (public_key->idLen > 0) {
  850. keyTemplate[keyTmplCnt].type = CKA_ID;
  851. keyTemplate[keyTmplCnt].pValue = public_key->id;
  852. keyTemplate[keyTmplCnt].ulValueLen = public_key->idLen;
  853. keyTmplCnt++;
  854. }
  855. ret = Pkcs11EccSetParams(public_key, keyTemplate, 3);
  856. if (ret == 0) {
  857. /* ASN1 encoded: OCT + uncompressed point */
  858. len = 3 + 1 + 2 * public_key->dp->size;
  859. ecPoint = (unsigned char*)XMALLOC(len, public_key->heap,
  860. DYNAMIC_TYPE_ECC);
  861. if (ecPoint == NULL)
  862. ret = MEMORY_E;
  863. }
  864. if (ret == 0) {
  865. len -= 3;
  866. i = 0;
  867. ecPoint[i++] = ASN_OCTET_STRING;
  868. if (len >= ASN_LONG_LENGTH)
  869. ecPoint[i++] = ASN_LONG_LENGTH | 1;
  870. ecPoint[i++] = len;
  871. if (public_key->type == 0)
  872. public_key->type = ECC_PUBLICKEY;
  873. ret = wc_ecc_export_x963(public_key, ecPoint + i, &len);
  874. }
  875. if (ret == 0) {
  876. keyTemplate[4].pValue = ecPoint;
  877. keyTemplate[4].ulValueLen = len + i;
  878. #ifdef WOLFSSL_DEBUG_PKCS11
  879. WOLFSSL_MSG("Ec Public Key");
  880. pkcs11_dump_template(keyTemplate, keyTmplCnt);
  881. #endif
  882. rv = session->func->C_CreateObject(session->handle, keyTemplate,
  883. keyTmplCnt, publicKey);
  884. #ifdef WOLFSSL_DEBUG_PKCS11
  885. pkcs11_rv("C_CreateObject", rv);
  886. #endif
  887. if (rv != CKR_OK) {
  888. ret = WC_HW_E;
  889. }
  890. }
  891. if (ecPoint != NULL)
  892. XFREE(ecPoint, public_key->heap, DYNAMIC_TYPE_ECC);
  893. return ret;
  894. }
  895. /**
  896. * Create a PKCS#11 object containing the ECC private key data.
  897. *
  898. * @param privateKey [out] Handle to private key object.
  899. * @param session [in] Session object.
  900. * @param private_key [in] ECC private key.
  901. * @param operation [in] Cryptographic operation key is to be used for.
  902. * @return WC_HW_E when a PKCS#11 library call fails.
  903. * @return 0 on success.
  904. */
  905. static int Pkcs11CreateEccPrivateKey(CK_OBJECT_HANDLE* privateKey,
  906. Pkcs11Session* session,
  907. ecc_key* private_key,
  908. CK_ATTRIBUTE_TYPE operation)
  909. {
  910. int ret = 0;
  911. CK_RV rv;
  912. CK_UTF8CHAR params[MAX_EC_PARAM_LEN];
  913. /* Empty entries for optional label/ID. */
  914. CK_ATTRIBUTE keyTemplate[] = {
  915. { CKA_CLASS, &privKeyClass, sizeof(privKeyClass) },
  916. { CKA_KEY_TYPE, &ecKeyType, sizeof(ecKeyType) },
  917. { operation, &ckTrue, sizeof(ckTrue) },
  918. { CKA_EC_PARAMS, params, 0 },
  919. { CKA_VALUE, NULL, 0 },
  920. { 0, NULL, 0 },
  921. { 0, NULL, 0 }
  922. };
  923. /* Mandatory entries + 2 optional. */
  924. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate) - 2;
  925. if (private_key->labelLen > 0) {
  926. keyTemplate[keyTmplCnt].type = CKA_LABEL;
  927. keyTemplate[keyTmplCnt].pValue = private_key->label;
  928. keyTemplate[keyTmplCnt].ulValueLen = private_key->labelLen;
  929. keyTmplCnt++;
  930. }
  931. if (private_key->idLen > 0) {
  932. keyTemplate[keyTmplCnt].type = CKA_ID;
  933. keyTemplate[keyTmplCnt].pValue = private_key->id;
  934. keyTemplate[keyTmplCnt].ulValueLen = private_key->idLen;
  935. keyTmplCnt++;
  936. }
  937. ret = Pkcs11EccSetParams(private_key, keyTemplate, 3);
  938. if (ret == 0) {
  939. keyTemplate[4].pValue = private_key->k.raw.buf;
  940. keyTemplate[4].ulValueLen = private_key->k.raw.len;
  941. #ifdef WOLFSSL_DEBUG_PKCS11
  942. WOLFSSL_MSG("Ec Private Key");
  943. pkcs11_dump_template(keyTemplate, keyTmplCnt);
  944. #endif
  945. rv = session->func->C_CreateObject(session->handle, keyTemplate,
  946. keyTmplCnt, privateKey);
  947. #ifdef WOLFSSL_DEBUG_PKCS11
  948. pkcs11_rv("C_CreateObject", rv);
  949. #endif
  950. if (rv != CKR_OK) {
  951. ret = WC_HW_E;
  952. }
  953. }
  954. return ret;
  955. }
  956. #endif
  957. #if !defined(NO_RSA) || defined(HAVE_ECC) || (!defined(NO_AES) && \
  958. (defined(HAVE_AESGCM) || defined(HAVE_AES_CBC))) || !defined(NO_HMAC)
  959. /**
  960. * Check if mechanism is available in session on token.
  961. *
  962. * @param [in] session Session object.
  963. * @param [in] mech Mechanism to look for.
  964. * @return NOT_COMPILED_IN when mechanism not available.
  965. * @return 0 when mechanism is available.
  966. */
  967. static int Pkcs11MechAvail(Pkcs11Session* session, CK_MECHANISM_TYPE mech)
  968. {
  969. int ret = 0;
  970. CK_RV rv;
  971. CK_MECHANISM_INFO mechInfo;
  972. rv = session->func->C_GetMechanismInfo(session->slotId, mech, &mechInfo);
  973. #ifdef WOLFSSL_DEBUG_PKCS11
  974. pkcs11_rv("C_GetMechanismInfo", rv);
  975. #endif
  976. if (rv != CKR_OK) {
  977. ret = NOT_COMPILED_IN;
  978. }
  979. return ret;
  980. }
  981. #endif
  982. #ifndef NO_HMAC
  983. /**
  984. * Return the mechanism type and key type for the digest type when using HMAC.
  985. *
  986. * @param [in] macType Digest type - e.g. WC_SHA256.
  987. * @param [in] mechType Mechanism type - e.g. CKM_SHA256_HMAC.
  988. * @param [in] keyType Key type - e.g. CKK_SHA256_HMAC.
  989. * @return NOT_COMPILED_IN if the digest algorithm isn't recognised.
  990. * @return 0 otherwise.
  991. */
  992. static int Pkcs11HmacTypes(int macType, int* mechType, int* keyType)
  993. {
  994. int ret = 0;
  995. switch (macType)
  996. {
  997. #ifndef NO_MD5
  998. case WC_MD5:
  999. *mechType = CKM_MD5_HMAC;
  1000. *keyType = CKK_MD5_HMAC;
  1001. break;
  1002. #endif
  1003. #ifndef NO_SHA
  1004. case WC_SHA:
  1005. *mechType = CKM_SHA_1_HMAC;
  1006. *keyType = CKK_SHA_1_HMAC;
  1007. break;
  1008. #endif
  1009. #ifdef WOLFSSL_SHA224
  1010. case WC_SHA224:
  1011. *mechType = CKM_SHA224_HMAC;
  1012. *keyType = CKK_SHA224_HMAC;
  1013. break;
  1014. #endif
  1015. #ifndef NO_SHA256
  1016. case WC_SHA256:
  1017. *mechType = CKM_SHA256_HMAC;
  1018. *keyType = CKK_SHA256_HMAC;
  1019. break;
  1020. #endif
  1021. #ifdef WOLFSSL_SHA384
  1022. case WC_SHA384:
  1023. *mechType = CKM_SHA384_HMAC;
  1024. *keyType = CKK_SHA384_HMAC;
  1025. break;
  1026. #endif
  1027. #ifdef WOLFSSL_SHA512
  1028. case WC_SHA512:
  1029. *mechType = CKM_SHA512_HMAC;
  1030. *keyType = CKK_SHA512_HMAC;
  1031. break;
  1032. #endif
  1033. default:
  1034. ret = NOT_COMPILED_IN;
  1035. break;
  1036. }
  1037. return ret;
  1038. }
  1039. #endif
  1040. /**
  1041. * Store the private key on the token in the session.
  1042. *
  1043. * @param [in] token Token to store private key on.
  1044. * @param [in] type Key type.
  1045. * @param [in] clear Clear out the private data from software key.
  1046. * @param [in] key Key type specific object.
  1047. * @return NOT_COMPILED_IN when mechanism not available.
  1048. * @return 0 on success.
  1049. */
  1050. int wc_Pkcs11StoreKey(Pkcs11Token* token, int type, int clear, void* key)
  1051. {
  1052. int ret = 0;
  1053. Pkcs11Session session;
  1054. CK_OBJECT_HANDLE privKey = NULL_PTR;
  1055. ret = Pkcs11OpenSession(token, &session, 1);
  1056. if (ret == 0) {
  1057. switch (type) {
  1058. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  1059. case PKCS11_KEY_TYPE_AES_GCM: {
  1060. Aes* aes = (Aes*)key;
  1061. ret = Pkcs11MechAvail(&session, CKM_AES_GCM);
  1062. if (ret == 0) {
  1063. ret = Pkcs11CreateSecretKey(&privKey, &session, CKK_AES,
  1064. (unsigned char*)aes->devKey,
  1065. aes->keylen,
  1066. (unsigned char*)aes->id,
  1067. aes->idLen, aes->label,
  1068. aes->labelLen, CKA_ENCRYPT);
  1069. }
  1070. if (ret == 0 && clear)
  1071. ForceZero(aes->devKey, aes->keylen);
  1072. break;
  1073. }
  1074. #endif
  1075. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  1076. case PKCS11_KEY_TYPE_AES_CBC: {
  1077. Aes* aes = (Aes*)key;
  1078. ret = Pkcs11MechAvail(&session, CKM_AES_CBC);
  1079. if (ret == 0) {
  1080. ret = Pkcs11CreateSecretKey(&privKey, &session, CKK_AES,
  1081. (unsigned char*)aes->devKey,
  1082. aes->keylen,
  1083. (unsigned char*)aes->id,
  1084. aes->idLen, aes->label,
  1085. aes->labelLen, CKA_ENCRYPT);
  1086. }
  1087. if (ret == 0 && clear)
  1088. ForceZero(aes->devKey, aes->keylen);
  1089. break;
  1090. }
  1091. #endif
  1092. #ifndef NO_HMAC
  1093. case PKCS11_KEY_TYPE_HMAC: {
  1094. Hmac* hmac = (Hmac*)key;
  1095. int mechType;
  1096. int keyType;
  1097. ret = Pkcs11HmacTypes(hmac->macType, &mechType, &keyType);
  1098. if (ret == NOT_COMPILED_IN)
  1099. break;
  1100. if (ret == 0)
  1101. ret = Pkcs11MechAvail(&session, mechType);
  1102. if (ret == 0) {
  1103. ret = Pkcs11CreateSecretKey(&privKey, &session, keyType,
  1104. (unsigned char*)hmac->keyRaw,
  1105. hmac->keyLen,
  1106. (unsigned char*)hmac->id,
  1107. hmac->idLen, hmac->label,
  1108. hmac->labelLen, CKA_SIGN);
  1109. if (ret == WC_HW_E) {
  1110. ret = Pkcs11CreateSecretKey(&privKey, &session,
  1111. CKK_GENERIC_SECRET,
  1112. (unsigned char*)hmac->keyRaw,
  1113. hmac->keyLen,
  1114. (unsigned char*)hmac->id,
  1115. hmac->idLen, hmac->label,
  1116. hmac->labelLen, CKA_SIGN);
  1117. }
  1118. }
  1119. break;
  1120. }
  1121. #endif
  1122. #ifndef NO_RSA
  1123. case PKCS11_KEY_TYPE_RSA: {
  1124. RsaKey* rsaKey = (RsaKey*)key;
  1125. ret = Pkcs11MechAvail(&session, CKM_RSA_X_509);
  1126. if (ret == 0)
  1127. ret = Pkcs11CreateRsaPrivateKey(&privKey, &session, rsaKey,
  1128. 1);
  1129. if (ret == 0 && clear) {
  1130. mp_forcezero(&rsaKey->u);
  1131. mp_forcezero(&rsaKey->dQ);
  1132. mp_forcezero(&rsaKey->dP);
  1133. mp_forcezero(&rsaKey->q);
  1134. mp_forcezero(&rsaKey->p);
  1135. mp_forcezero(&rsaKey->d);
  1136. }
  1137. break;
  1138. }
  1139. #endif
  1140. #ifdef HAVE_ECC
  1141. case PKCS11_KEY_TYPE_EC: {
  1142. ecc_key* eccKey = (ecc_key*)key;
  1143. int ret2 = NOT_COMPILED_IN;
  1144. #ifndef NO_PKCS11_ECDH
  1145. if ((eccKey->flags & WC_ECC_FLAG_DEC_SIGN) == 0) {
  1146. /* Try ECDH mechanism first. */
  1147. ret = Pkcs11MechAvail(&session, CKM_ECDH1_DERIVE);
  1148. if (ret == 0) {
  1149. ret = Pkcs11CreateEccPrivateKey(&privKey, &session,
  1150. eccKey, CKA_DERIVE);
  1151. }
  1152. }
  1153. #endif
  1154. if (ret == 0 || ret == NOT_COMPILED_IN) {
  1155. /* Try ECDSA mechanism next. */
  1156. ret2 = Pkcs11MechAvail(&session, CKM_ECDSA);
  1157. if (ret2 == 0) {
  1158. ret2 = Pkcs11CreateEccPrivateKey(&privKey, &session,
  1159. eccKey, CKA_SIGN);
  1160. if (ret2 == 0) {
  1161. CK_OBJECT_HANDLE pubKey = NULL_PTR;
  1162. /* Store public key for validation with cert. */
  1163. ret2 = Pkcs11CreateEccPublicKey(&pubKey, &session,
  1164. eccKey, CKA_VERIFY);
  1165. }
  1166. }
  1167. /* OK for this to fail if set for ECDH. */
  1168. if (ret == NOT_COMPILED_IN)
  1169. ret = ret2;
  1170. }
  1171. if (ret == 0 && clear)
  1172. mp_forcezero(&eccKey->k);
  1173. break;
  1174. }
  1175. #endif
  1176. default:
  1177. ret = NOT_COMPILED_IN;
  1178. break;
  1179. }
  1180. Pkcs11CloseSession(token, &session);
  1181. }
  1182. (void)privKey;
  1183. (void)clear;
  1184. (void)key;
  1185. return ret;
  1186. }
  1187. #if !defined(NO_RSA) || defined(HAVE_ECC) || (!defined(NO_AES) && \
  1188. (defined(HAVE_AESGCM) || defined(HAVE_AES_CBC))) || !defined(NO_HMAC)
  1189. /**
  1190. * Find the PKCS#11 object containing key data using template.
  1191. *
  1192. * @param [out] key Handle to key object.
  1193. * @param [in] session Session object.
  1194. * @param [in] keyTemplate PKCS #11 template to use in search.
  1195. * @param [in] keyTmplCnt Count of entries in PKCS #11 template.
  1196. * @param [out] count Number of keys matching template.
  1197. * @return WC_HW_E when a PKCS#11 library call fails.
  1198. * @return 0 on success.
  1199. */
  1200. static int Pkcs11FindKeyByTemplate(CK_OBJECT_HANDLE* key,
  1201. Pkcs11Session* session,
  1202. CK_ATTRIBUTE *keyTemplate,
  1203. CK_ULONG keyTmplCnt,
  1204. CK_ULONG *count)
  1205. {
  1206. int ret = 0;
  1207. CK_RV rv;
  1208. WOLFSSL_MSG("PKCS#11: Find Key By Template");
  1209. #ifdef WOLFSSL_DEBUG_PKCS11
  1210. WOLFSSL_MSG("Find Key");
  1211. pkcs11_dump_template(keyTemplate, keyTmplCnt);
  1212. #endif
  1213. rv = session->func->C_FindObjectsInit(session->handle, keyTemplate,
  1214. keyTmplCnt);
  1215. #ifdef WOLFSSL_DEBUG_PKCS11
  1216. pkcs11_rv("C_FindObjectsInit", rv);
  1217. #endif
  1218. if (rv != CKR_OK) {
  1219. ret = WC_HW_E;
  1220. }
  1221. if (ret == 0) {
  1222. rv = session->func->C_FindObjects(session->handle, key, 1, count);
  1223. #ifdef WOLFSSL_DEBUG_PKCS11
  1224. pkcs11_rv("C_FindObjects", rv);
  1225. pkcs11_val("C_FindObjects Count", *count);
  1226. #endif
  1227. if (rv != CKR_OK) {
  1228. ret = WC_HW_E;
  1229. }
  1230. rv = session->func->C_FindObjectsFinal(session->handle);
  1231. #ifdef WOLFSSL_DEBUG_PKCS11
  1232. pkcs11_rv("C_FindObjectsFinal", rv);
  1233. #endif
  1234. if (rv != CKR_OK) {
  1235. ret = WC_HW_E;
  1236. }
  1237. }
  1238. return ret;
  1239. }
  1240. /**
  1241. * Find the PKCS#11 object containing the private key data by label.
  1242. *
  1243. * @param [out] key Handle to key object.
  1244. * @param [in] keyClass Public or private key class.
  1245. * @param [in] keyType Type of key.
  1246. * @param [in] session Session object.
  1247. * @param [in] id Identifier set against a key.
  1248. * @param [in] idLen Length of identifier.
  1249. * @return WC_HW_E when a PKCS#11 library call fails.
  1250. * @return 0 on success.
  1251. */
  1252. static int Pkcs11FindKeyByLabel(CK_OBJECT_HANDLE* key, CK_OBJECT_CLASS keyClass,
  1253. CK_KEY_TYPE keyType, Pkcs11Session* session,
  1254. char* label, int labelLen)
  1255. {
  1256. int ret = 0;
  1257. CK_ULONG count;
  1258. CK_ATTRIBUTE keyTemplate[] = {
  1259. { CKA_CLASS, &keyClass, sizeof(keyClass) },
  1260. { CKA_KEY_TYPE, &keyType, sizeof(keyType) },
  1261. { CKA_LABEL, label, (CK_ULONG)labelLen }
  1262. };
  1263. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate);
  1264. WOLFSSL_MSG("PKCS#11: Find Key By Label");
  1265. ret = Pkcs11FindKeyByTemplate(key, session, keyTemplate, keyTmplCnt,
  1266. &count);
  1267. if (ret == 0 && count == 0)
  1268. ret = WC_HW_E;
  1269. return ret;
  1270. }
  1271. /**
  1272. * Find the PKCS#11 object containing the private key data by ID.
  1273. *
  1274. * @param [out] key Handle to key object.
  1275. * @param [in] keyClass Public or private key class.
  1276. * @param [in] keyType Type of key.
  1277. * @param [in] session Session object.
  1278. * @param [in] id Identifier set against a key.
  1279. * @param [in] idLen Length of identifier.
  1280. * @return WC_HW_E when a PKCS#11 library call fails.
  1281. * @return 0 on success.
  1282. */
  1283. static int Pkcs11FindKeyById(CK_OBJECT_HANDLE* key, CK_OBJECT_CLASS keyClass,
  1284. CK_KEY_TYPE keyType, Pkcs11Session* session,
  1285. byte* id, int idLen)
  1286. {
  1287. int ret = 0;
  1288. CK_ULONG count;
  1289. CK_ATTRIBUTE keyTemplate[] = {
  1290. { CKA_CLASS, &keyClass, sizeof(keyClass) },
  1291. { CKA_KEY_TYPE, &keyType, sizeof(keyType) },
  1292. { CKA_ID, id, (CK_ULONG)idLen }
  1293. };
  1294. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate);
  1295. WOLFSSL_MSG("PKCS#11: Find Key By Id");
  1296. ret = Pkcs11FindKeyByTemplate(key, session, keyTemplate, keyTmplCnt,
  1297. &count);
  1298. if (ret == 0 && count == 0)
  1299. ret = WC_HW_E;
  1300. return ret;
  1301. }
  1302. #endif
  1303. #ifndef NO_RSA
  1304. /**
  1305. * Find the PKCS#11 object containing the RSA public or private key data with
  1306. * the modulus specified.
  1307. *
  1308. * @param [out] key Handle to key object.
  1309. * @param [in] keyClass Public or private key class.
  1310. * @param [in] session Session object.
  1311. * @param [in] rsaKey RSA key with modulus to search on.
  1312. * @return WC_HW_E when a PKCS#11 library call fails.
  1313. * @return 0 on success.
  1314. */
  1315. static int Pkcs11FindRsaKey(CK_OBJECT_HANDLE* key, CK_OBJECT_CLASS keyClass,
  1316. Pkcs11Session* session, RsaKey* rsaKey)
  1317. {
  1318. CK_ULONG count;
  1319. CK_ATTRIBUTE keyTemplate[] = {
  1320. { CKA_CLASS, &keyClass, sizeof(keyClass) },
  1321. { CKA_KEY_TYPE, &rsaKeyType, sizeof(rsaKeyType) },
  1322. { CKA_MODULUS, NULL, 0 },
  1323. };
  1324. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate);
  1325. /* Set the modulus. */
  1326. keyTemplate[2].pValue = rsaKey->n.raw.buf;
  1327. keyTemplate[2].ulValueLen = rsaKey->n.raw.len;
  1328. return Pkcs11FindKeyByTemplate(key, session, keyTemplate, keyTmplCnt,
  1329. &count);
  1330. }
  1331. /**
  1332. * Exponentiate the input with the public part of the RSA key.
  1333. * Used in public encrypt and decrypt.
  1334. *
  1335. * @param [in] session Session object.
  1336. * @param [in] info Cryptographic operation data.
  1337. * @return WC_HW_E when a PKCS#11 library call fails.
  1338. * 0 on success.
  1339. */
  1340. static int Pkcs11RsaPublic(Pkcs11Session* session, wc_CryptoInfo* info)
  1341. {
  1342. int ret = 0;
  1343. CK_RV rv;
  1344. CK_MECHANISM mech;
  1345. CK_ULONG outLen;
  1346. CK_OBJECT_HANDLE publicKey = NULL_PTR;
  1347. int sessionKey = 0;
  1348. RsaKey* rsaKey = info->pk.rsa.key;
  1349. CK_ATTRIBUTE keyTemplate[] = {
  1350. { CKA_CLASS, &pubKeyClass, sizeof(pubKeyClass) },
  1351. { CKA_KEY_TYPE, &rsaKeyType, sizeof(rsaKeyType) },
  1352. { CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
  1353. { CKA_MODULUS, NULL, 0 },
  1354. { CKA_PUBLIC_EXPONENT, NULL, 0 }
  1355. };
  1356. CK_ULONG keyTmplCnt = sizeof(keyTemplate) / sizeof(*keyTemplate);
  1357. WOLFSSL_MSG("PKCS#11: RSA Public Key Operation");
  1358. if (info->pk.rsa.outLen == NULL) {
  1359. ret = BAD_FUNC_ARG;
  1360. }
  1361. if (ret == 0) {
  1362. if ((sessionKey = !mp_iszero(&rsaKey->e))) {
  1363. /* Set the modulus and public exponent data. */
  1364. keyTemplate[3].pValue = rsaKey->n.raw.buf;
  1365. keyTemplate[3].ulValueLen = rsaKey->n.raw.len;
  1366. keyTemplate[4].pValue = rsaKey->e.raw.buf;
  1367. keyTemplate[4].ulValueLen = rsaKey->e.raw.len;
  1368. #ifdef WOLFSSL_DEBUG_PKCS11
  1369. WOLFSSL_MSG("RSA Public Key");
  1370. pkcs11_dump_template(keyTemplate, keyTmplCnt);
  1371. #endif
  1372. /* Create an object containing public key data for device to use. */
  1373. rv = session->func->C_CreateObject(session->handle, keyTemplate,
  1374. keyTmplCnt, &publicKey);
  1375. #ifdef WOLFSSL_DEBUG_PKCS11
  1376. pkcs11_rv("C_CreateObject", rv);
  1377. #endif
  1378. if (rv != CKR_OK) {
  1379. ret = WC_HW_E;
  1380. }
  1381. }
  1382. else if (info->pk.rsa.key->labelLen > 0) {
  1383. ret = Pkcs11FindKeyByLabel(&publicKey, CKO_PUBLIC_KEY, CKK_RSA,
  1384. session, info->pk.rsa.key->label,
  1385. info->pk.rsa.key->labelLen);
  1386. }
  1387. else {
  1388. ret = Pkcs11FindKeyById(&publicKey, CKO_PUBLIC_KEY, CKK_RSA,
  1389. session, rsaKey->id, rsaKey->idLen);
  1390. }
  1391. }
  1392. if (ret == 0) {
  1393. /* Raw RSA encrypt/decrypt operation. */
  1394. mech.mechanism = CKM_RSA_X_509;
  1395. mech.ulParameterLen = 0;
  1396. mech.pParameter = NULL;
  1397. rv = session->func->C_EncryptInit(session->handle, &mech, publicKey);
  1398. #ifdef WOLFSSL_DEBUG_PKCS11
  1399. pkcs11_rv("C_EncryptInit", rv);
  1400. #endif
  1401. if (rv != CKR_OK) {
  1402. ret = WC_HW_E;
  1403. }
  1404. }
  1405. if (ret == 0) {
  1406. outLen = (CK_ULONG)*info->pk.rsa.outLen;
  1407. rv = session->func->C_Encrypt(session->handle,
  1408. (CK_BYTE_PTR)info->pk.rsa.in, info->pk.rsa.inLen,
  1409. info->pk.rsa.out, &outLen);
  1410. #ifdef WOLFSSL_DEBUG_PKCS11
  1411. pkcs11_rv("C_Encrypt", rv);
  1412. #endif
  1413. if (rv != CKR_OK) {
  1414. ret = WC_HW_E;
  1415. }
  1416. }
  1417. if (ret == 0)
  1418. *info->pk.rsa.outLen = (word32)outLen;
  1419. if (sessionKey)
  1420. session->func->C_DestroyObject(session->handle, publicKey);
  1421. return ret;
  1422. }
  1423. /**
  1424. * Get the RSA public key data from the PKCS#11 object.
  1425. *
  1426. * @param [in] key RSA key to put the data into.
  1427. * @param [in] session Session object.
  1428. * @param [in] pubkey Public key object.
  1429. * @return WC_HW_E when a PKCS#11 library call fails.
  1430. * @return MEMORY_E when a memory allocation fails.
  1431. * @return 0 on success.
  1432. */
  1433. static int Pkcs11GetRsaPublicKey(RsaKey* key, Pkcs11Session* session,
  1434. CK_OBJECT_HANDLE pubKey)
  1435. {
  1436. int ret = 0;
  1437. unsigned char* mod = NULL;
  1438. unsigned char* exp = NULL;
  1439. int modSz, expSz;
  1440. CK_ATTRIBUTE tmpl[] = {
  1441. { CKA_MODULUS, NULL_PTR, 0 },
  1442. { CKA_PUBLIC_EXPONENT, NULL_PTR, 0 }
  1443. };
  1444. CK_ULONG tmplCnt = sizeof(tmpl) / sizeof(*tmpl);
  1445. CK_RV rv;
  1446. #ifdef WOLFSSL_DEBUG_PKCS11
  1447. WOLFSSL_MSG("Get RSA Public Key Length");
  1448. pkcs11_dump_template(tmpl, tmplCnt);
  1449. #endif
  1450. rv = session->func->C_GetAttributeValue(session->handle, pubKey, tmpl,
  1451. tmplCnt);
  1452. #ifdef WOLFSSL_DEBUG_PKCS11
  1453. pkcs11_rv("C_GetAttributeValue", rv);
  1454. #endif
  1455. if (rv != CKR_OK) {
  1456. ret = WC_HW_E;
  1457. }
  1458. #ifdef WOLFSSL_DEBUG_PKCS11
  1459. WOLFSSL_MSG("RSA Public Key Length");
  1460. pkcs11_dump_template(tmpl, tmplCnt);
  1461. #endif
  1462. if (ret == 0) {
  1463. modSz = (int)tmpl[0].ulValueLen;
  1464. expSz = (int)tmpl[1].ulValueLen;
  1465. mod = (unsigned char*)XMALLOC(modSz, key->heap,
  1466. DYNAMIC_TYPE_TMP_BUFFER);
  1467. if (mod == NULL)
  1468. ret = MEMORY_E;
  1469. }
  1470. if (ret == 0) {
  1471. exp = (unsigned char*)XMALLOC(expSz, key->heap,
  1472. DYNAMIC_TYPE_TMP_BUFFER);
  1473. if (exp == NULL)
  1474. ret = MEMORY_E;
  1475. }
  1476. if (ret == 0) {
  1477. tmpl[0].pValue = mod;
  1478. tmpl[1].pValue = exp;
  1479. #ifdef WOLFSSL_DEBUG_PKCS11
  1480. WOLFSSL_MSG("Get RSA Public Key");
  1481. pkcs11_dump_template(tmpl, tmplCnt);
  1482. #endif
  1483. rv = session->func->C_GetAttributeValue(session->handle, pubKey,
  1484. tmpl, tmplCnt);
  1485. #ifdef WOLFSSL_DEBUG_PKCS11
  1486. pkcs11_rv("C_GetAttributeValue", rv);
  1487. #endif
  1488. if (rv != CKR_OK) {
  1489. ret = WC_HW_E;
  1490. }
  1491. #ifdef WOLFSSL_DEBUG_PKCS11
  1492. WOLFSSL_MSG("RSA Public Key");
  1493. pkcs11_dump_template(tmpl, tmplCnt);
  1494. #endif
  1495. }
  1496. if (ret == 0)
  1497. ret = wc_RsaPublicKeyDecodeRaw(mod, modSz, exp, expSz, key);
  1498. if (exp != NULL)
  1499. XFREE(exp, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1500. if (mod != NULL)
  1501. XFREE(mod, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1502. return ret;
  1503. }
  1504. /**
  1505. * Exponentiate the input with the private part of the RSA key.
  1506. * Used in private encrypt and decrypt.
  1507. *
  1508. * @param [in] session Session object.
  1509. * @param [in] info Cryptographic operation data.
  1510. * @param [in] func Function to perform - decrypt or sign.
  1511. * @return WC_HW_E when a PKCS#11 library call fails.
  1512. * @return 0 on success.
  1513. */
  1514. static int Pkcs11RsaPrivate(Pkcs11Session* session, wc_CryptoInfo* info,
  1515. int func)
  1516. {
  1517. int ret = 0;
  1518. CK_RV rv;
  1519. CK_MECHANISM mech;
  1520. CK_ULONG outLen;
  1521. CK_OBJECT_HANDLE privateKey = NULL_PTR;
  1522. int sessionKey = 0;
  1523. WOLFSSL_MSG("PKCS#11: RSA Private Key Operation");
  1524. if (info->pk.rsa.outLen == NULL) {
  1525. ret = BAD_FUNC_ARG;
  1526. }
  1527. if (ret == 0) {
  1528. if ((sessionKey = !mp_iszero(&info->pk.rsa.key->d))) {
  1529. ret = Pkcs11CreateRsaPrivateKey(&privateKey, session,
  1530. info->pk.rsa.key, 0);
  1531. }
  1532. else if (info->pk.rsa.key->labelLen > 0) {
  1533. ret = Pkcs11FindKeyByLabel(&privateKey, CKO_PRIVATE_KEY, CKK_RSA,
  1534. session, info->pk.rsa.key->label,
  1535. info->pk.rsa.key->labelLen);
  1536. }
  1537. else if (info->pk.rsa.key->idLen > 0) {
  1538. ret = Pkcs11FindKeyById(&privateKey, CKO_PRIVATE_KEY, CKK_RSA,
  1539. session, info->pk.rsa.key->id,
  1540. info->pk.rsa.key->idLen);
  1541. }
  1542. else {
  1543. ret = Pkcs11FindRsaKey(&privateKey, CKO_PRIVATE_KEY, session,
  1544. info->pk.rsa.key);
  1545. }
  1546. }
  1547. if ((ret == 0) && (!sessionKey)) {
  1548. ret = Pkcs11GetRsaPublicKey(info->pk.rsa.key, session, privateKey);
  1549. }
  1550. if (ret == 0) {
  1551. /* Raw RSA encrypt/decrypt operation. */
  1552. mech.mechanism = CKM_RSA_X_509;
  1553. mech.ulParameterLen = 0;
  1554. mech.pParameter = NULL;
  1555. if (func == CKF_DECRYPT) {
  1556. rv = session->func->C_DecryptInit(session->handle, &mech,
  1557. privateKey);
  1558. #ifdef WOLFSSL_DEBUG_PKCS11
  1559. pkcs11_rv("C_DecryptInit", rv);
  1560. #endif
  1561. }
  1562. else {
  1563. rv = session->func->C_SignInit(session->handle, &mech, privateKey);
  1564. #ifdef WOLFSSL_DEBUG_PKCS11
  1565. pkcs11_rv("C_SignInit", rv);
  1566. #endif
  1567. }
  1568. if (rv != CKR_OK) {
  1569. ret = WC_HW_E;
  1570. }
  1571. }
  1572. if (ret == 0) {
  1573. if (func == CKF_DECRYPT) {
  1574. #ifdef WOLFSSL_DEBUG_PKCS11
  1575. pkcs11_val("C_Decrypt inLen", info->pk.rsa.inLen);
  1576. pkcs11_val("C_Decrypt outLen", *info->pk.rsa.outLen);
  1577. #endif
  1578. outLen = (CK_ULONG)*info->pk.rsa.outLen;
  1579. rv = session->func->C_Decrypt(session->handle,
  1580. (CK_BYTE_PTR)info->pk.rsa.in, info->pk.rsa.inLen,
  1581. info->pk.rsa.out, &outLen);
  1582. #ifdef WOLFSSL_DEBUG_PKCS11
  1583. pkcs11_rv("C_Decrypt", rv);
  1584. #endif
  1585. }
  1586. else {
  1587. #ifdef WOLFSSL_DEBUG_PKCS11
  1588. pkcs11_val("C_Sign inLen", info->pk.rsa.inLen);
  1589. pkcs11_val("C_Sign outLen", *info->pk.rsa.outLen);
  1590. #endif
  1591. outLen = (CK_ULONG)*info->pk.rsa.outLen;
  1592. rv = session->func->C_Sign(session->handle,
  1593. (CK_BYTE_PTR)info->pk.rsa.in, info->pk.rsa.inLen,
  1594. info->pk.rsa.out, &outLen);
  1595. #ifdef WOLFSSL_DEBUG_PKCS11
  1596. pkcs11_rv("C_Sign", rv);
  1597. #endif
  1598. }
  1599. if (rv != CKR_OK) {
  1600. ret = WC_HW_E;
  1601. }
  1602. }
  1603. if (ret == 0)
  1604. *info->pk.rsa.outLen = (word32)outLen;
  1605. if (sessionKey)
  1606. session->func->C_DestroyObject(session->handle, privateKey);
  1607. return ret;
  1608. }
  1609. /**
  1610. * Perform an RSA operation.
  1611. *
  1612. * @param [in] session Session object.
  1613. * @param [in] info Cryptographic operation data.
  1614. * @return WC_HW_E when a PKCS#11 library call fails.
  1615. * @return 0 on success.
  1616. */
  1617. static int Pkcs11Rsa(Pkcs11Session* session, wc_CryptoInfo* info)
  1618. {
  1619. int ret = 0;
  1620. CK_RV rv;
  1621. CK_MECHANISM_INFO mechInfo;
  1622. /* Check operation is supported. */
  1623. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_RSA_X_509,
  1624. &mechInfo);
  1625. #ifdef WOLFSSL_DEBUG_PKCS11
  1626. pkcs11_rv("C_GetMechanismInfo", rv);
  1627. #endif
  1628. if (rv != CKR_OK) {
  1629. ret = NOT_COMPILED_IN;
  1630. }
  1631. if (ret == 0) {
  1632. if (info->pk.rsa.type == RSA_PUBLIC_ENCRYPT ||
  1633. info->pk.rsa.type == RSA_PUBLIC_DECRYPT) {
  1634. if ((mechInfo.flags & CKF_ENCRYPT) == 0)
  1635. ret = NOT_COMPILED_IN;
  1636. else
  1637. ret = Pkcs11RsaPublic(session, info);
  1638. }
  1639. else if (info->pk.rsa.type == RSA_PRIVATE_ENCRYPT ||
  1640. info->pk.rsa.type == RSA_PRIVATE_DECRYPT) {
  1641. if ((mechInfo.flags & CKF_DECRYPT) == 0 &&
  1642. (mechInfo.flags & CKF_SIGN) == 0) {
  1643. ret = NOT_COMPILED_IN;
  1644. }
  1645. else if ((info->pk.rsa.type == RSA_PRIVATE_ENCRYPT &&
  1646. (mechInfo.flags & CKF_SIGN) != 0) ||
  1647. (info->pk.rsa.type == RSA_PRIVATE_DECRYPT &&
  1648. (mechInfo.flags & CKF_DECRYPT) == 0)) {
  1649. ret = Pkcs11RsaPrivate(session, info, CKF_SIGN);
  1650. }
  1651. else
  1652. ret = Pkcs11RsaPrivate(session, info, CKF_DECRYPT);
  1653. }
  1654. else
  1655. ret = NOT_COMPILED_IN;
  1656. }
  1657. return ret;
  1658. }
  1659. #ifdef WOLFSSL_KEY_GEN
  1660. /**
  1661. * Perform an RSA key generation operation.
  1662. * The private key data stays on the device.
  1663. *
  1664. * @param [in] session Session object.
  1665. * @param [in] info Cryptographic operation data.
  1666. * @return WC_HW_E when a PKCS#11 library call fails.
  1667. * @return 0 on success.
  1668. */
  1669. static int Pkcs11RsaKeyGen(Pkcs11Session* session, wc_CryptoInfo* info)
  1670. {
  1671. int ret = 0;
  1672. RsaKey* key = info->pk.rsakg.key;
  1673. CK_RV rv;
  1674. CK_ULONG bits = info->pk.rsakg.size;
  1675. CK_OBJECT_HANDLE pubKey = NULL_PTR, privKey = NULL_PTR;
  1676. CK_MECHANISM mech;
  1677. static CK_BYTE pub_exp[] = { 0x01, 0x00, 0x01, 0x00 };
  1678. CK_ATTRIBUTE pubKeyTmpl[] = {
  1679. { CKA_MODULUS_BITS, &bits, sizeof(bits) },
  1680. { CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
  1681. { CKA_VERIFY, &ckTrue, sizeof(ckTrue) },
  1682. { CKA_PUBLIC_EXPONENT, &pub_exp, sizeof(pub_exp) }
  1683. };
  1684. CK_ULONG pubTmplCnt = sizeof(pubKeyTmpl)/sizeof(*pubKeyTmpl);
  1685. /* Empty entries for optional label/ID. */
  1686. CK_ATTRIBUTE privKeyTmpl[] = {
  1687. { CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
  1688. { CKA_SIGN, &ckTrue, sizeof(ckTrue) },
  1689. { 0, NULL, 0 },
  1690. { 0, NULL, 0 }
  1691. };
  1692. /* 2 mandatory entries + 2 optional. */
  1693. int privTmplCnt = 2;
  1694. int i;
  1695. ret = Pkcs11MechAvail(session, CKM_RSA_PKCS_KEY_PAIR_GEN);
  1696. if (ret == 0) {
  1697. WOLFSSL_MSG("PKCS#11: RSA Key Generation Operation");
  1698. /* Most commonly used public exponent value (array initialized). */
  1699. if (info->pk.rsakg.e != WC_RSA_EXPONENT) {
  1700. for (i = 0; i < (int)sizeof(pub_exp); i++)
  1701. pub_exp[i] = (info->pk.rsakg.e >> (8 * i)) & 0xff;
  1702. }
  1703. for (i = (int)sizeof(pub_exp) - 1; pub_exp[i] == 0; i--) {
  1704. }
  1705. pubKeyTmpl[3].ulValueLen = i + 1;
  1706. if (key->labelLen != 0) {
  1707. privKeyTmpl[privTmplCnt].type = CKA_LABEL;
  1708. privKeyTmpl[privTmplCnt].pValue = key->label;
  1709. privKeyTmpl[privTmplCnt].ulValueLen = key->labelLen;
  1710. privTmplCnt++;
  1711. }
  1712. if (key->idLen != 0) {
  1713. privKeyTmpl[privTmplCnt].type = CKA_ID;
  1714. privKeyTmpl[privTmplCnt].pValue = key->id;
  1715. privKeyTmpl[privTmplCnt].ulValueLen = key->idLen;
  1716. privTmplCnt++;
  1717. }
  1718. mech.mechanism = CKM_RSA_PKCS_KEY_PAIR_GEN;
  1719. mech.ulParameterLen = 0;
  1720. mech.pParameter = NULL;
  1721. #ifdef WOLFSSL_DEBUG_PKCS11
  1722. WOLFSSL_MSG("Public Key");
  1723. pkcs11_dump_template(pubKeyTmpl, pubTmplCnt);
  1724. WOLFSSL_MSG("Private Key");
  1725. pkcs11_dump_template(privKeyTmpl, privTmplCnt);
  1726. #endif
  1727. rv = session->func->C_GenerateKeyPair(session->handle, &mech,
  1728. pubKeyTmpl, pubTmplCnt,
  1729. privKeyTmpl, privTmplCnt,
  1730. &pubKey, &privKey);
  1731. #ifdef WOLFSSL_DEBUG_PKCS11
  1732. pkcs11_rv("C_GenerateKeyPair", rv);
  1733. #endif
  1734. if (rv != CKR_OK) {
  1735. ret = -1;
  1736. }
  1737. }
  1738. if (ret == 0)
  1739. ret = Pkcs11GetRsaPublicKey(key, session, pubKey);
  1740. if (pubKey != NULL_PTR)
  1741. ret = (int)session->func->C_DestroyObject(session->handle, pubKey);
  1742. if (ret != 0 && privKey != NULL_PTR)
  1743. ret = (int)session->func->C_DestroyObject(session->handle, privKey);
  1744. return ret;
  1745. }
  1746. #endif /* WOLFSSL_KEY_GEN */
  1747. #endif /* !NO_RSA */
  1748. #ifdef HAVE_ECC
  1749. /**
  1750. * Find the PKCS#11 object containing the ECC public or private key data.
  1751. * Search for public key by public point.
  1752. *
  1753. * @param [out] key Handle to key object.
  1754. * @param [in] keyClass Public or private key class.
  1755. * @param [in] session Session object.
  1756. * @param [in] eccKey ECC key with parameters.
  1757. * @return WC_HW_E when a PKCS#11 library call fails.
  1758. * @return MEMORY_E when a memory allocation fails.
  1759. * @return 0 on success.
  1760. */
  1761. static int Pkcs11FindEccKey(CK_OBJECT_HANDLE* key, CK_OBJECT_CLASS keyClass,
  1762. Pkcs11Session* session, ecc_key* eccKey,
  1763. int op)
  1764. {
  1765. int ret = 0;
  1766. int i;
  1767. unsigned char* ecPoint = NULL;
  1768. word32 len = 0;
  1769. CK_RV rv;
  1770. CK_ULONG count;
  1771. CK_UTF8CHAR params[MAX_EC_PARAM_LEN];
  1772. CK_ATTRIBUTE keyTemplate[] = {
  1773. { CKA_CLASS, &keyClass, sizeof(keyClass) },
  1774. { CKA_KEY_TYPE, &ecKeyType, sizeof(ecKeyType) },
  1775. { CKA_EC_PARAMS, params, 0 },
  1776. { op, &ckTrue, sizeof(ckTrue) },
  1777. { CKA_EC_POINT, NULL, 0 },
  1778. };
  1779. CK_ULONG attrCnt = 4;
  1780. ret = Pkcs11EccSetParams(eccKey, keyTemplate, 2);
  1781. if (ret == 0 && keyClass == CKO_PUBLIC_KEY) {
  1782. /* ASN1 encoded: OCT + uncompressed point */
  1783. len = 3 + 1 + 2 * eccKey->dp->size;
  1784. ecPoint = (unsigned char*)XMALLOC(len, eccKey->heap, DYNAMIC_TYPE_ECC);
  1785. if (ecPoint == NULL)
  1786. ret = MEMORY_E;
  1787. }
  1788. if (ret == 0 && keyClass == CKO_PUBLIC_KEY) {
  1789. len -= 3;
  1790. i = 0;
  1791. ecPoint[i++] = ASN_OCTET_STRING;
  1792. if (len >= ASN_LONG_LENGTH)
  1793. ecPoint[i++] = (ASN_LONG_LENGTH | 1);
  1794. ecPoint[i++] = len;
  1795. if (eccKey->type == 0)
  1796. eccKey->type = ECC_PUBLICKEY;
  1797. ret = wc_ecc_export_x963(eccKey, ecPoint + i, &len);
  1798. }
  1799. if (ret == 0 && keyClass == CKO_PUBLIC_KEY) {
  1800. keyTemplate[attrCnt].pValue = ecPoint;
  1801. keyTemplate[attrCnt].ulValueLen = len + i;
  1802. attrCnt++;
  1803. }
  1804. if (ret == 0) {
  1805. #ifdef WOLFSSL_DEBUG_PKCS11
  1806. WOLFSSL_MSG("Find Ec Key");
  1807. pkcs11_dump_template(keyTemplate, attrCnt);
  1808. #endif
  1809. rv = session->func->C_FindObjectsInit(session->handle, keyTemplate,
  1810. attrCnt);
  1811. #ifdef WOLFSSL_DEBUG_PKCS11
  1812. pkcs11_rv("C_FindObjectsInit", rv);
  1813. #endif
  1814. if (rv != CKR_OK) {
  1815. ret = WC_HW_E;
  1816. }
  1817. }
  1818. if (ret == 0) {
  1819. rv = session->func->C_FindObjects(session->handle, key, 1, &count);
  1820. #ifdef WOLFSSL_DEBUG_PKCS11
  1821. pkcs11_rv("C_FindObjects", rv);
  1822. pkcs11_val("C_FindObjects Count", count);
  1823. #endif
  1824. if (rv != CKR_OK) {
  1825. ret = WC_HW_E;
  1826. }
  1827. rv = session->func->C_FindObjectsFinal(session->handle);
  1828. #ifdef WOLFSSL_DEBUG_PKCS11
  1829. pkcs11_rv("C_FindObjectsFinal", rv);
  1830. #endif
  1831. if (rv != CKR_OK) {
  1832. ret = WC_HW_E;
  1833. }
  1834. }
  1835. if (ecPoint != NULL)
  1836. XFREE(ecPoint, eccKey->heap, DYNAMIC_TYPE_ECC);
  1837. return ret;
  1838. }
  1839. /**
  1840. * Gets the public key data from the PKCS#11 object and puts into the ECC key.
  1841. *
  1842. * @param [in] key ECC public key.
  1843. * @param [in] session Session object.
  1844. * @param [in] pubKey ECC public key PKCS#11 object.
  1845. * @return WC_HW_E when a PKCS#11 library call fails.
  1846. * @return MEMORY_E when a memory allocation fails.
  1847. * @return 0 on success.
  1848. */
  1849. static int Pkcs11GetEccPublicKey(ecc_key* key, Pkcs11Session* session,
  1850. CK_OBJECT_HANDLE pubKey)
  1851. {
  1852. int ret = 0;
  1853. word32 i = 0;
  1854. int curveIdx;
  1855. unsigned char* point = NULL;
  1856. int pointSz;
  1857. byte tag;
  1858. CK_RV rv;
  1859. CK_ATTRIBUTE tmpl[] = {
  1860. { CKA_EC_POINT, NULL_PTR, 0 },
  1861. };
  1862. CK_ULONG tmplCnt = sizeof(tmpl) / sizeof(*tmpl);
  1863. rv = session->func->C_GetAttributeValue(session->handle, pubKey, tmpl,
  1864. tmplCnt);
  1865. #ifdef WOLFSSL_DEBUG_PKCS11
  1866. pkcs11_rv("C_GetAttributeValue", rv);
  1867. #endif
  1868. if (rv != CKR_OK) {
  1869. ret = WC_HW_E;
  1870. }
  1871. if (ret == 0) {
  1872. pointSz = (int)tmpl[0].ulValueLen;
  1873. point = (unsigned char*)XMALLOC(pointSz, key->heap, DYNAMIC_TYPE_ECC);
  1874. if (point == NULL)
  1875. ret = MEMORY_E;
  1876. }
  1877. if (ret == 0) {
  1878. tmpl[0].pValue = point;
  1879. #ifdef WOLFSSL_DEBUG_PKCS11
  1880. WOLFSSL_MSG("Get Ec Public Key");
  1881. pkcs11_dump_template(tmpl, tmplCnt);
  1882. #endif
  1883. rv = session->func->C_GetAttributeValue(session->handle, pubKey,
  1884. tmpl, tmplCnt);
  1885. #ifdef WOLFSSL_DEBUG_PKCS11
  1886. pkcs11_rv("C_GetAttributeValue", rv);
  1887. #endif
  1888. if (rv != CKR_OK) {
  1889. ret = WC_HW_E;
  1890. }
  1891. #ifdef WOLFSSL_DEBUG_PKCS11
  1892. WOLFSSL_MSG("Ec Public Key");
  1893. pkcs11_dump_template(tmpl, tmplCnt);
  1894. #endif
  1895. }
  1896. /* Make sure the data is big enough for ASN.1: OCT + uncompressed point */
  1897. if (ret == 0 && pointSz < key->dp->size * 2 + 1 + 2)
  1898. ret = ASN_PARSE_E;
  1899. /* Step over the OCTET_STRING wrapper. */
  1900. if (ret == 0 && GetASNTag(point, &i, &tag, pointSz) != 0)
  1901. ret = ASN_PARSE_E;
  1902. if (ret == 0 && tag != ASN_OCTET_STRING)
  1903. ret = ASN_PARSE_E;
  1904. if (ret == 0 && point[i] >= ASN_LONG_LENGTH) {
  1905. if (point[i++] != (ASN_LONG_LENGTH | 1))
  1906. ret = ASN_PARSE_E;
  1907. else if (pointSz < key->dp->size * 2 + 1 + 3)
  1908. ret = ASN_PARSE_E;
  1909. }
  1910. if (ret == 0 && point[i++] != key->dp->size * 2 + 1)
  1911. ret = ASN_PARSE_E;
  1912. if (ret == 0) {
  1913. curveIdx = wc_ecc_get_curve_idx(key->dp->id);
  1914. ret = wc_ecc_import_point_der(point + i, pointSz - i, curveIdx,
  1915. &key->pubkey);
  1916. }
  1917. /* make sure the ecc_key type has been set */
  1918. if (ret == 0 && key->type == 0) {
  1919. key->type = ECC_PUBLICKEY;
  1920. }
  1921. if (point != NULL)
  1922. XFREE(point, key->heap, DYNAMIC_TYPE_ECC);
  1923. return ret;
  1924. }
  1925. #ifndef NO_PKCS11_EC_KEYGEN
  1926. /**
  1927. * Perform an ECC key generation operation.
  1928. * The private key data stays on the device.
  1929. *
  1930. * @param [in] session Session object.
  1931. * @param [in] info Cryptographic operation data.
  1932. * @return WC_HW_E when a PKCS#11 library call fails.
  1933. * @return 0 on success.
  1934. */
  1935. static int Pkcs11EcKeyGen(Pkcs11Session* session, wc_CryptoInfo* info)
  1936. {
  1937. int ret = 0;
  1938. ecc_key* key = info->pk.eckg.key;
  1939. CK_RV rv;
  1940. CK_OBJECT_HANDLE pubKey = NULL_PTR, privKey = NULL_PTR;
  1941. CK_MECHANISM mech;
  1942. CK_UTF8CHAR params[MAX_EC_PARAM_LEN];
  1943. CK_ATTRIBUTE pubKeyTmpl[] = {
  1944. { CKA_EC_PARAMS, params, 0 },
  1945. { CKA_VERIFY, &ckTrue, sizeof(ckTrue) },
  1946. { CKA_ENCRYPT, &ckTrue, sizeof(ckTrue) },
  1947. };
  1948. int pubTmplCnt = 1;
  1949. /* Empty entries for optional label/ID. */
  1950. CK_ATTRIBUTE privKeyTmplDerive[] = {
  1951. { CKA_DERIVE, &ckTrue, sizeof(ckTrue) },
  1952. { 0, NULL, 0 },
  1953. { 0, NULL, 0 },
  1954. };
  1955. /* Empty entries for optional label/ID. */
  1956. CK_ATTRIBUTE privKeyTmplEncSign[] = {
  1957. { CKA_SIGN, &ckTrue, sizeof(ckTrue) },
  1958. { CKA_DECRYPT, &ckTrue, sizeof(ckTrue) },
  1959. { 0, NULL, 0 },
  1960. { 0, NULL, 0 },
  1961. };
  1962. CK_ATTRIBUTE* privKeyTmpl = privKeyTmplDerive;
  1963. /* Mandatory entries + 2 optional. */
  1964. int privTmplCnt = 1;
  1965. ret = Pkcs11MechAvail(session, CKM_EC_KEY_PAIR_GEN);
  1966. if (ret == 0) {
  1967. WOLFSSL_MSG("PKCS#11: EC Key Generation Operation");
  1968. ret = Pkcs11EccSetParams(key, pubKeyTmpl, 0);
  1969. }
  1970. if (ret == 0) {
  1971. /* Default is to use for derivation. */
  1972. if ((key->flags & WC_ECC_FLAG_DEC_SIGN) == WC_ECC_FLAG_DEC_SIGN) {
  1973. privKeyTmpl = privKeyTmplEncSign;
  1974. privTmplCnt = 2;
  1975. pubTmplCnt = 2;
  1976. }
  1977. if (key->labelLen != 0) {
  1978. privKeyTmpl[privTmplCnt].type = CKA_LABEL;
  1979. privKeyTmpl[privTmplCnt].pValue = key->label;
  1980. privKeyTmpl[privTmplCnt].ulValueLen = key->labelLen;
  1981. privTmplCnt++;
  1982. }
  1983. if (key->idLen != 0) {
  1984. privKeyTmpl[privTmplCnt].type = CKA_ID;
  1985. privKeyTmpl[privTmplCnt].pValue = key->id;
  1986. privKeyTmpl[privTmplCnt].ulValueLen = key->idLen;
  1987. privTmplCnt++;
  1988. }
  1989. mech.mechanism = CKM_EC_KEY_PAIR_GEN;
  1990. mech.ulParameterLen = 0;
  1991. mech.pParameter = NULL;
  1992. #ifdef WOLFSSL_DEBUG_PKCS11
  1993. WOLFSSL_MSG("Ec Private");
  1994. pkcs11_dump_template(privKeyTmpl, privTmplCnt);
  1995. WOLFSSL_MSG("Ec Public");
  1996. pkcs11_dump_template(pubKeyTmpl, pubTmplCnt);
  1997. #endif
  1998. rv = session->func->C_GenerateKeyPair(session->handle, &mech,
  1999. pubKeyTmpl, pubTmplCnt,
  2000. privKeyTmpl, privTmplCnt,
  2001. &pubKey, &privKey);
  2002. #ifdef WOLFSSL_DEBUG_PKCS11
  2003. pkcs11_rv("C_GenerateKeyPair", rv);
  2004. #endif
  2005. if (rv != CKR_OK) {
  2006. ret = -1;
  2007. }
  2008. }
  2009. if (ret == 0)
  2010. ret = Pkcs11GetEccPublicKey(key, session, pubKey);
  2011. if (pubKey != NULL_PTR)
  2012. session->func->C_DestroyObject(session->handle, pubKey);
  2013. if (ret != 0 && privKey != NULL_PTR)
  2014. session->func->C_DestroyObject(session->handle, privKey);
  2015. return ret;
  2016. }
  2017. #endif
  2018. #ifndef NO_PKCS11_ECDH
  2019. /**
  2020. * Extracts the secret key data from the PKCS#11 object.
  2021. *
  2022. * @param [in] session Session object.
  2023. * @param [in] secret PKCS#11 object with the secret key data.
  2024. * @param [in] out Buffer to hold secret data.
  2025. * @param [in,out] outLen On in, length of buffer.
  2026. * On out, the length of data in buffer.
  2027. * @return WC_HW_E when a PKCS#11 library call fails.
  2028. * @return 0 on success.
  2029. */
  2030. static int Pkcs11ExtractSecret(Pkcs11Session* session, CK_OBJECT_HANDLE secret,
  2031. byte* out, word32* outLen)
  2032. {
  2033. int ret = 0;
  2034. CK_ATTRIBUTE tmpl[] = {
  2035. {CKA_VALUE, NULL_PTR, 0}
  2036. };
  2037. CK_ULONG tmplCnt = sizeof(tmpl) / sizeof(*tmpl);
  2038. CK_RV rv;
  2039. #ifdef WOLFSSL_DEBUG_PKCS11
  2040. WOLFSSL_MSG("Get Secret Length");
  2041. pkcs11_dump_template(tmpl, tmplCnt);
  2042. #endif
  2043. rv = session->func->C_GetAttributeValue(session->handle, secret, tmpl,
  2044. tmplCnt);
  2045. #ifdef WOLFSSL_DEBUG_PKCS11
  2046. pkcs11_rv("C_GetAttributeValue", rv);
  2047. #endif
  2048. if (rv != CKR_OK) {
  2049. ret = WC_HW_E;
  2050. }
  2051. #ifdef WOLFSSL_DEBUG_PKCS11
  2052. WOLFSSL_MSG("Secret Length");
  2053. pkcs11_dump_template(tmpl, tmplCnt);
  2054. #endif
  2055. if (ret == 0) {
  2056. if (tmpl[0].ulValueLen > *outLen)
  2057. ret = BUFFER_E;
  2058. }
  2059. if (ret == 0) {
  2060. tmpl[0].pValue = out;
  2061. #ifdef WOLFSSL_DEBUG_PKCS11
  2062. WOLFSSL_MSG("Get Secret");
  2063. pkcs11_dump_template(tmpl, tmplCnt);
  2064. #endif
  2065. rv = session->func->C_GetAttributeValue(session->handle, secret,
  2066. tmpl, tmplCnt);
  2067. #ifdef WOLFSSL_DEBUG_PKCS11
  2068. pkcs11_rv("C_GetAttributeValue", rv);
  2069. #endif
  2070. if (rv != CKR_OK) {
  2071. ret = WC_HW_E;
  2072. }
  2073. #ifdef WOLFSSL_DEBUG_PKCS11
  2074. WOLFSSL_MSG("Secret");
  2075. pkcs11_dump_template(tmpl, tmplCnt);
  2076. #endif
  2077. *outLen = (word32)tmpl[0].ulValueLen;
  2078. }
  2079. return ret;
  2080. }
  2081. /**
  2082. * Performs the ECDH secret generation operation.
  2083. *
  2084. * @param [in] session Session object.
  2085. * @param [in] info Cryptographic operation data.
  2086. * @return WC_HW_E when a PKCS#11 library call fails.
  2087. * 0 on success.
  2088. */
  2089. static int Pkcs11ECDH(Pkcs11Session* session, wc_CryptoInfo* info)
  2090. {
  2091. int ret = 0;
  2092. int sessionKey = 0;
  2093. unsigned char* point = NULL;
  2094. word32 pointLen;
  2095. CK_RV rv;
  2096. CK_KEY_TYPE keyType = CKK_GENERIC_SECRET;
  2097. CK_MECHANISM mech;
  2098. CK_ECDH1_DERIVE_PARAMS params;
  2099. CK_OBJECT_HANDLE privateKey = NULL_PTR;
  2100. CK_OBJECT_HANDLE secret = CK_INVALID_HANDLE;
  2101. CK_ULONG secSz;
  2102. CK_ATTRIBUTE tmpl[] = {
  2103. { CKA_CLASS, &secretKeyClass, sizeof(secretKeyClass) },
  2104. { CKA_KEY_TYPE, &keyType, sizeof(keyType) },
  2105. { CKA_PRIVATE, &ckFalse, sizeof(ckFalse) },
  2106. { CKA_SENSITIVE, &ckFalse, sizeof(ckFalse) },
  2107. { CKA_EXTRACTABLE, &ckTrue, sizeof(ckTrue) },
  2108. { CKA_VALUE_LEN, &secSz, sizeof(secSz) }
  2109. };
  2110. CK_ULONG tmplCnt = sizeof(tmpl) / sizeof(*tmpl);
  2111. ret = Pkcs11MechAvail(session, CKM_ECDH1_DERIVE);
  2112. if (ret == 0 && info->pk.ecdh.outlen == NULL) {
  2113. ret = BAD_FUNC_ARG;
  2114. }
  2115. if (ret == 0) {
  2116. WOLFSSL_MSG("PKCS#11: EC Key Derivation Operation");
  2117. if ((sessionKey = !mp_iszero(&info->pk.ecdh.private_key->k)))
  2118. ret = Pkcs11CreateEccPrivateKey(&privateKey, session,
  2119. info->pk.ecdh.private_key, CKA_DERIVE);
  2120. else if (info->pk.ecdh.private_key->labelLen > 0) {
  2121. ret = Pkcs11FindKeyByLabel(&privateKey, CKO_PRIVATE_KEY, CKK_EC,
  2122. session,
  2123. info->pk.ecdh.private_key->label,
  2124. info->pk.ecdh.private_key->labelLen);
  2125. }
  2126. else if (info->pk.ecdh.private_key->idLen > 0) {
  2127. ret = Pkcs11FindKeyById(&privateKey, CKO_PRIVATE_KEY, CKK_EC,
  2128. session, info->pk.ecdh.private_key->id,
  2129. info->pk.ecdh.private_key->idLen);
  2130. }
  2131. else {
  2132. ret = Pkcs11FindEccKey(&privateKey, CKO_PRIVATE_KEY, session,
  2133. info->pk.ecdh.public_key, CKA_DERIVE);
  2134. }
  2135. }
  2136. if (ret == 0) {
  2137. ret = wc_ecc_export_x963(info->pk.ecdh.public_key, NULL, &pointLen);
  2138. if (ret == LENGTH_ONLY_E) {
  2139. point = (unsigned char*)XMALLOC(pointLen,
  2140. info->pk.ecdh.public_key->heap,
  2141. DYNAMIC_TYPE_ECC_BUFFER);
  2142. ret = wc_ecc_export_x963(info->pk.ecdh.public_key, point,
  2143. &pointLen);
  2144. }
  2145. }
  2146. if (ret == 0) {
  2147. secSz = *info->pk.ecdh.outlen;
  2148. if (secSz > (CK_ULONG)info->pk.ecdh.private_key->dp->size)
  2149. secSz = info->pk.ecdh.private_key->dp->size;
  2150. params.kdf = CKD_NULL;
  2151. params.pSharedData = NULL;
  2152. params.ulSharedDataLen = 0;
  2153. params.pPublicData = point;
  2154. params.ulPublicDataLen = pointLen;
  2155. mech.mechanism = CKM_ECDH1_DERIVE;
  2156. mech.ulParameterLen = sizeof(params);
  2157. mech.pParameter = &params;
  2158. #ifdef WOLFSSL_DEBUG_PKCS11
  2159. WOLFSSL_MSG("ECDH key");
  2160. pkcs11_dump_template(tmpl, tmplCnt);
  2161. #endif
  2162. rv = session->func->C_DeriveKey(session->handle, &mech, privateKey,
  2163. tmpl, tmplCnt, &secret);
  2164. #ifdef WOLFSSL_DEBUG_PKCS11
  2165. pkcs11_rv("C_DeriveKey", rv);
  2166. #endif
  2167. if (rv != CKR_OK) {
  2168. ret = WC_HW_E;
  2169. }
  2170. }
  2171. if (ret == 0) {
  2172. ret = Pkcs11ExtractSecret(session, secret, info->pk.ecdh.out,
  2173. info->pk.ecdh.outlen);
  2174. }
  2175. if (sessionKey)
  2176. session->func->C_DestroyObject(session->handle, privateKey);
  2177. if (point != NULL)
  2178. XFREE(point, info->pk.ecdh.public_key->heap, DYNAMIC_TYPE_ECC_BUFFER);
  2179. return ret;
  2180. }
  2181. #endif
  2182. /**
  2183. * Encode, in place, the ECDSA signature.
  2184. * Two fixed width values into ASN.1 DER encoded SEQ { INT, INT }
  2185. *
  2186. * @param [in,out] sig Signature data.
  2187. * @param [in] sz Size of original signature data.
  2188. * @return Length of the ASN.1 DER encoded signature.
  2189. */
  2190. static word32 Pkcs11ECDSASig_Encode(byte* sig, word32 sz)
  2191. {
  2192. word32 rHigh, sHigh, seqLen;
  2193. word32 rStart = 0, sStart = 0;
  2194. word32 sigSz, rSz, rLen, sSz, sLen;
  2195. word32 i;
  2196. /* Find first byte of data in r and s. */
  2197. while (rStart < sz - 1 && sig[rStart] == 0x00)
  2198. rStart++;
  2199. while (sStart < sz - 1 && sig[sz + sStart] == 0x00)
  2200. sStart++;
  2201. /* Check if 0 needs to be prepended to make integer a positive number. */
  2202. rHigh = sig[rStart] >> 7;
  2203. sHigh = sig[sz + sStart] >> 7;
  2204. /* Calculate length of integer to put into ASN.1 encoding. */
  2205. rLen = sz - rStart;
  2206. sLen = sz - sStart;
  2207. /* r and s: INT (2 bytes) + [ 0x00 ] + integer */
  2208. rSz = 2 + rHigh + rLen;
  2209. sSz = 2 + sHigh + sLen;
  2210. /* Calculate the complete ASN.1 DER encoded size. */
  2211. sigSz = rSz + sSz;
  2212. if (sigSz >= ASN_LONG_LENGTH)
  2213. seqLen = 3;
  2214. else
  2215. seqLen = 2;
  2216. /* Move s and then r integers into their final places. */
  2217. XMEMMOVE(sig + seqLen + rSz + (sSz - sLen), sig + sz + sStart, sLen);
  2218. XMEMMOVE(sig + seqLen + (rSz - rLen), sig + rStart, rLen);
  2219. /* Put the ASN.1 DER encoding around data. */
  2220. i = 0;
  2221. sig[i++] = ASN_CONSTRUCTED | ASN_SEQUENCE;
  2222. if (seqLen == 3)
  2223. sig[i++] = ASN_LONG_LENGTH | 0x01;
  2224. sig[i++] = sigSz;
  2225. sig[i++] = ASN_INTEGER;
  2226. sig[i++] = rHigh + (sz - rStart);
  2227. if (rHigh)
  2228. sig[i++] = 0x00;
  2229. i += sz - rStart;
  2230. sig[i++] = ASN_INTEGER;
  2231. sig[i++] = sHigh + (sz - sStart);
  2232. if (sHigh)
  2233. sig[i] = 0x00;
  2234. return seqLen + sigSz;
  2235. }
  2236. /**
  2237. * Decode the ECDSA signature.
  2238. * ASN.1 DER encode SEQ { INT, INT } converted to two fixed with values.
  2239. *
  2240. * @param [in] in ASN.1 DER encoded signature.
  2241. * @param [in] inSz Size of ASN.1 signature.
  2242. * @param [in] sig Output buffer.
  2243. * @param [in] sz Size of output buffer.
  2244. * @return ASN_PARSE_E when the ASN.1 encoding is invalid.
  2245. * @return 0 on success.
  2246. */
  2247. static int Pkcs11ECDSASig_Decode(const byte* in, word32 inSz, byte* sig,
  2248. word32 sz)
  2249. {
  2250. int ret = 0;
  2251. word32 i = 0;
  2252. byte tag;
  2253. int len, seqLen = 2;
  2254. /* Make sure zeros in place when decoding short integers. */
  2255. XMEMSET(sig, 0, sz * 2);
  2256. /* Check min data for: SEQ + INT. */
  2257. if (inSz < 5)
  2258. ret = ASN_PARSE_E;
  2259. /* Check SEQ */
  2260. if (ret == 0 && in[i++] != (ASN_CONSTRUCTED | ASN_SEQUENCE))
  2261. ret = ASN_PARSE_E;
  2262. if (ret == 0 && in[i] >= ASN_LONG_LENGTH) {
  2263. if (in[i] != (ASN_LONG_LENGTH | 0x01))
  2264. ret = ASN_PARSE_E;
  2265. else {
  2266. i++;
  2267. seqLen++;
  2268. }
  2269. }
  2270. if (ret == 0 && in[i++] != inSz - seqLen)
  2271. ret = ASN_PARSE_E;
  2272. /* Check INT */
  2273. if (ret == 0 && GetASNTag(in, &i, &tag, inSz) != 0)
  2274. ret = ASN_PARSE_E;
  2275. if (ret == 0 && tag != ASN_INTEGER)
  2276. ret = ASN_PARSE_E;
  2277. if (ret == 0 && (len = in[i++]) > sz + 1)
  2278. ret = ASN_PARSE_E;
  2279. /* Check there is space for INT data */
  2280. if (ret == 0 && i + len > inSz)
  2281. ret = ASN_PARSE_E;
  2282. if (ret == 0) {
  2283. /* Skip leading zero */
  2284. if (in[i] == 0x00) {
  2285. i++;
  2286. len--;
  2287. }
  2288. /* Copy r into sig. */
  2289. XMEMCPY(sig + sz - len, in + i, len);
  2290. i += len;
  2291. }
  2292. /* Check min data for: INT. */
  2293. if (ret == 0 && i + 2 > inSz)
  2294. ret = ASN_PARSE_E;
  2295. /* Check INT */
  2296. if (ret == 0 && GetASNTag(in, &i, &tag, inSz) != 0)
  2297. ret = ASN_PARSE_E;
  2298. if (ret == 0 && tag != ASN_INTEGER)
  2299. ret = ASN_PARSE_E;
  2300. if (ret == 0 && (len = in[i++]) > sz + 1)
  2301. ret = ASN_PARSE_E;
  2302. /* Check there is space for INT data */
  2303. if (ret == 0 && i + len > inSz)
  2304. ret = ASN_PARSE_E;
  2305. if (ret == 0) {
  2306. /* Skip leading zero */
  2307. if (in[i] == 0x00) {
  2308. i++;
  2309. len--;
  2310. }
  2311. /* Copy s into sig. */
  2312. XMEMCPY(sig + sz + sz - len, in + i, len);
  2313. }
  2314. return ret;
  2315. }
  2316. /**
  2317. * Get the parameters from the private key on the device.
  2318. *
  2319. * @param [in] session Session object.
  2320. * @param [in] privKey PKCS #11 object handle of private key..
  2321. * @param [in] key Ecc key to set parameters against.
  2322. * @return WC_HW_E when a PKCS#11 library call fails.
  2323. * @return 0 on success.
  2324. */
  2325. static int Pkcs11GetEccParams(Pkcs11Session* session, CK_OBJECT_HANDLE privKey,
  2326. ecc_key* key)
  2327. {
  2328. int ret = 0;
  2329. int curveId;
  2330. CK_RV rv;
  2331. byte oid[16];
  2332. CK_ATTRIBUTE template[] = {
  2333. { CKA_EC_PARAMS, (CK_VOID_PTR)oid, sizeof(oid) }
  2334. };
  2335. #ifdef WOLFSSL_DEBUG_PKCS11
  2336. WOLFSSL_MSG("Get Ec Params");
  2337. pkcs11_dump_template(template, 1);
  2338. #endif
  2339. rv = session->func->C_GetAttributeValue(session->handle, privKey, template,
  2340. 1);
  2341. #ifdef WOLFSSL_DEBUG_PKCS11
  2342. pkcs11_rv("C_GetAttributeValue", rv);
  2343. #endif
  2344. if (rv != CKR_OK) {
  2345. ret = WC_HW_E;
  2346. }
  2347. #ifdef WOLFSSL_DEBUG_PKCS11
  2348. WOLFSSL_MSG("Ec Params");
  2349. pkcs11_dump_template(template, 1);
  2350. #endif
  2351. if (ret == 0) {
  2352. /* PKCS #11 wraps the OID in ASN.1 */
  2353. curveId = wc_ecc_get_curve_id_from_oid(oid + 2,
  2354. (word32)template[0].ulValueLen - 2);
  2355. if (curveId == ECC_CURVE_INVALID)
  2356. ret = WC_HW_E;
  2357. }
  2358. if (ret == 0)
  2359. ret = wc_ecc_set_curve(key, 0, curveId);
  2360. return ret;
  2361. }
  2362. /**
  2363. * Performs the ECDSA signing operation.
  2364. *
  2365. * @param session [in] Session object.
  2366. * @param info [in] Cryptographic operation data.
  2367. * @return WC_HW_E when a PKCS#11 library call fails.
  2368. * @return 0 on success.
  2369. */
  2370. static int Pkcs11ECDSA_Sign(Pkcs11Session* session, wc_CryptoInfo* info)
  2371. {
  2372. int ret = 0;
  2373. int sessionKey = 0;
  2374. word32 sz;
  2375. CK_RV rv;
  2376. CK_ULONG outLen;
  2377. CK_MECHANISM mech;
  2378. CK_MECHANISM_INFO mechInfo;
  2379. CK_OBJECT_HANDLE privateKey = NULL_PTR;
  2380. /* Check operation is supported. */
  2381. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_ECDSA,
  2382. &mechInfo);
  2383. #ifdef WOLFSSL_DEBUG_PKCS11
  2384. pkcs11_rv("C_GetMechanismInfo", rv);
  2385. #endif
  2386. if (rv != CKR_OK || (mechInfo.flags & CKF_SIGN) == 0)
  2387. ret = NOT_COMPILED_IN;
  2388. if (ret == 0 && info->pk.eccsign.outlen == NULL) {
  2389. ret = BAD_FUNC_ARG;
  2390. }
  2391. if (ret == 0) {
  2392. WOLFSSL_MSG("PKCS#11: EC Signing Operation");
  2393. if ((sessionKey = !mp_iszero(&info->pk.eccsign.key->k)))
  2394. ret = Pkcs11CreateEccPrivateKey(&privateKey, session,
  2395. info->pk.eccsign.key, CKA_SIGN);
  2396. else if (info->pk.eccsign.key->labelLen > 0) {
  2397. ret = Pkcs11FindKeyByLabel(&privateKey, CKO_PRIVATE_KEY, CKK_EC,
  2398. session, info->pk.eccsign.key->label,
  2399. info->pk.eccsign.key->labelLen);
  2400. if (ret == 0 && info->pk.eccsign.key->dp == NULL) {
  2401. ret = Pkcs11GetEccParams(session, privateKey,
  2402. info->pk.eccsign.key);
  2403. }
  2404. }
  2405. else if (info->pk.eccsign.key->idLen > 0) {
  2406. ret = Pkcs11FindKeyById(&privateKey, CKO_PRIVATE_KEY, CKK_EC,
  2407. session, info->pk.eccsign.key->id,
  2408. info->pk.eccsign.key->idLen);
  2409. if (ret == 0 && info->pk.eccsign.key->dp == NULL) {
  2410. ret = Pkcs11GetEccParams(session, privateKey,
  2411. info->pk.eccsign.key);
  2412. }
  2413. }
  2414. else {
  2415. ret = Pkcs11FindEccKey(&privateKey, CKO_PRIVATE_KEY, session,
  2416. info->pk.eccsign.key, CKA_SIGN);
  2417. }
  2418. }
  2419. if (ret == 0) {
  2420. sz = info->pk.eccsign.key->dp->size;
  2421. /* Maximum encoded size is two ordinates + 8 bytes of ASN.1. */
  2422. if (*info->pk.eccsign.outlen < (word32)wc_ecc_sig_size_calc(sz))
  2423. ret = BUFFER_E;
  2424. }
  2425. if (ret == 0) {
  2426. mech.mechanism = CKM_ECDSA;
  2427. mech.ulParameterLen = 0;
  2428. mech.pParameter = NULL;
  2429. rv = session->func->C_SignInit(session->handle, &mech, privateKey);
  2430. #ifdef WOLFSSL_DEBUG_PKCS11
  2431. pkcs11_rv("C_SignInit", rv);
  2432. #endif
  2433. if (rv != CKR_OK) {
  2434. ret = WC_HW_E;
  2435. }
  2436. }
  2437. if (ret == 0) {
  2438. outLen = *info->pk.eccsign.outlen;
  2439. rv = session->func->C_Sign(session->handle,
  2440. (CK_BYTE_PTR)info->pk.eccsign.in,
  2441. info->pk.eccsign.inlen, info->pk.eccsign.out,
  2442. &outLen);
  2443. #ifdef WOLFSSL_DEBUG_PKCS11
  2444. pkcs11_rv("C_Sign", rv);
  2445. #endif
  2446. if (rv != CKR_OK) {
  2447. ret = WC_HW_E;
  2448. }
  2449. }
  2450. if (ret == 0) {
  2451. *info->pk.eccsign.outlen = Pkcs11ECDSASig_Encode(info->pk.eccsign.out,
  2452. sz);
  2453. }
  2454. if (sessionKey)
  2455. session->func->C_DestroyObject(session->handle, privateKey);
  2456. return ret;
  2457. }
  2458. /**
  2459. * Performs the ECDSA verification operation.
  2460. *
  2461. * @param [in] session Session object.
  2462. * @param [in] info Cryptographic operation data.
  2463. * @return WC_HW_E when a PKCS#11 library call fails.
  2464. * @return MEMORY_E when a memory allocation fails.
  2465. * @return 0 on success.
  2466. */
  2467. static int Pkcs11ECDSA_Verify(Pkcs11Session* session, wc_CryptoInfo* info)
  2468. {
  2469. int ret = 0;
  2470. CK_RV rv;
  2471. CK_MECHANISM mech;
  2472. CK_MECHANISM_INFO mechInfo;
  2473. CK_OBJECT_HANDLE publicKey = NULL_PTR;
  2474. unsigned char* sig = NULL;
  2475. word32 sz = info->pk.eccverify.key->dp->size;
  2476. /* Check operation is supported. */
  2477. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_ECDSA,
  2478. &mechInfo);
  2479. #ifdef WOLFSSL_DEBUG_PKCS11
  2480. pkcs11_rv("C_GetMechanismInfo", rv);
  2481. #endif
  2482. if (rv != CKR_OK || (mechInfo.flags & CKF_VERIFY) == 0)
  2483. ret = NOT_COMPILED_IN;
  2484. if (ret == 0 && info->pk.eccverify.res == NULL) {
  2485. ret = BAD_FUNC_ARG;
  2486. }
  2487. if (ret == 0) {
  2488. WOLFSSL_MSG("PKCS#11: EC Verification Operation");
  2489. ret = Pkcs11CreateEccPublicKey(&publicKey, session,
  2490. info->pk.eccverify.key, CKA_VERIFY);
  2491. }
  2492. if (ret == 0) {
  2493. sig = (unsigned char *)XMALLOC(sz * 2, info->pk.eccverify.key->heap,
  2494. DYNAMIC_TYPE_TMP_BUFFER);
  2495. if (sig == NULL)
  2496. ret = MEMORY_E;
  2497. }
  2498. if (ret == 0) {
  2499. ret = Pkcs11ECDSASig_Decode(info->pk.eccverify.sig,
  2500. info->pk.eccverify.siglen, sig, sz);
  2501. }
  2502. if (ret == 0) {
  2503. mech.mechanism = CKM_ECDSA;
  2504. mech.ulParameterLen = 0;
  2505. mech.pParameter = NULL;
  2506. rv = session->func->C_VerifyInit(session->handle, &mech, publicKey);
  2507. #ifdef WOLFSSL_DEBUG_PKCS11
  2508. pkcs11_rv("C_VerifyInit", rv);
  2509. #endif
  2510. if (rv != CKR_OK) {
  2511. ret = WC_HW_E;
  2512. }
  2513. }
  2514. if (ret == 0) {
  2515. *info->pk.eccverify.res = 0;
  2516. rv = session->func->C_Verify(session->handle,
  2517. (CK_BYTE_PTR)info->pk.eccverify.hash,
  2518. info->pk.eccverify.hashlen,
  2519. (CK_BYTE_PTR)sig, sz * 2);
  2520. #ifdef WOLFSSL_DEBUG_PKCS11
  2521. pkcs11_rv("C_Verify", rv);
  2522. #endif
  2523. if (rv == CKR_SIGNATURE_INVALID) {
  2524. }
  2525. else if (rv != CKR_OK)
  2526. ret = WC_HW_E;
  2527. else
  2528. *info->pk.eccverify.res = 1;
  2529. }
  2530. if (publicKey != NULL_PTR)
  2531. session->func->C_DestroyObject(session->handle, publicKey);
  2532. if (sig != NULL)
  2533. XFREE(sig, info->pk.eccverify.key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  2534. return ret;
  2535. }
  2536. #endif
  2537. #ifndef NO_RSA
  2538. /**
  2539. * Check the private RSA key matches the public key.
  2540. *
  2541. * @param [in] priv RSA private key.
  2542. * @param [in] publicKey Encoded RSA public key.
  2543. * @param [in] pubKeySize Length of encoded RSA public key.
  2544. * @return MEMORY_E when a memory allocation fails.
  2545. * @return MP_CMP_E when the public parts are different.
  2546. * @return 0 on success.
  2547. */
  2548. static int wc_Pkcs11CheckPrivKey_Rsa(RsaKey* priv,
  2549. const unsigned char* publicKey, word32 pubKeySize)
  2550. {
  2551. int ret = 0;
  2552. #ifdef WOLFSSL_SMALL_STACK
  2553. RsaKey* pub = NULL;
  2554. #else
  2555. RsaKey pub[1];
  2556. #endif
  2557. word32 keyIdx = 0;
  2558. #ifdef WOLFSSL_SMALL_STACK
  2559. pub = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  2560. if (pub == NULL) {
  2561. ret = MEMORY_E;
  2562. }
  2563. #endif
  2564. if ((ret == 0) && (ret = wc_InitRsaKey(pub, NULL)) == 0) {
  2565. if (ret == 0) {
  2566. ret = wc_RsaPublicKeyDecode(publicKey, &keyIdx, pub, pubKeySize);
  2567. }
  2568. if (ret == 0) {
  2569. /* both keys extracted successfully now check n and e
  2570. * values are the same. This is dereferencing RsaKey */
  2571. if (mp_cmp(&(priv->n), &(pub->n)) != MP_EQ ||
  2572. mp_cmp(&(priv->e), &(pub->e)) != MP_EQ) {
  2573. ret = MP_CMP_E;
  2574. }
  2575. else
  2576. ret = 0;
  2577. }
  2578. wc_FreeRsaKey(pub);
  2579. }
  2580. #ifdef WOLFSSL_SMALL_STACK
  2581. if (pub != NULL) {
  2582. XFREE(pub, NULL, DYNAMIC_TYPE_RSA);
  2583. }
  2584. #endif
  2585. return ret;
  2586. }
  2587. /**
  2588. * Checks the RSA private key matches the RSA public key.
  2589. *
  2590. * @param [in] session Session object.
  2591. * @param [in] info Cryptographic operation data.
  2592. * @return WC_HW_E when a PKCS#11 library call fails.
  2593. * @return MEMORY_E when a memory allocation fails.
  2594. * @return MEMORY_E when a memory allocation fails.
  2595. * @return MP_CMP_E when the public parts are different.
  2596. * @return 0 on success.
  2597. */
  2598. static int Pkcs11RsaCheckPrivKey(Pkcs11Session* session, wc_CryptoInfo* info)
  2599. {
  2600. int ret = 0;
  2601. CK_OBJECT_HANDLE privateKey;
  2602. RsaKey* priv = info->pk.rsa_check.key;
  2603. if (mp_iszero(&priv->n) || mp_iszero(&priv->e)) {
  2604. /* Get the RSA private key object. */
  2605. if (priv->labelLen > 0) {
  2606. ret = Pkcs11FindKeyByLabel(&privateKey, CKO_PRIVATE_KEY,
  2607. CKK_RSA, session, priv->label,
  2608. priv->labelLen);
  2609. }
  2610. else if (info->pk.rsa.key->idLen > 0) {
  2611. ret = Pkcs11FindKeyById(&privateKey, CKO_PRIVATE_KEY, CKK_RSA,
  2612. session, priv->id, priv->idLen);
  2613. }
  2614. else {
  2615. ret = Pkcs11FindRsaKey(&privateKey, CKO_PRIVATE_KEY, session, priv);
  2616. }
  2617. if (ret == 0) {
  2618. /* Extract the public key components. */
  2619. ret = Pkcs11GetRsaPublicKey(priv, session, privateKey);
  2620. }
  2621. }
  2622. if (ret == 0) {
  2623. /* Compare the extracted public parts with the public key. */
  2624. ret = wc_Pkcs11CheckPrivKey_Rsa(priv, info->pk.rsa_check.pubKey,
  2625. info->pk.rsa_check.pubKeySz);
  2626. }
  2627. return ret;
  2628. }
  2629. #endif
  2630. #ifdef HAVE_ECC
  2631. /**
  2632. * Check the private ECC key matches the public key.
  2633. * Do this by looking up the public key data from the associated public key.
  2634. * The public key object handle is passed in for the private key.
  2635. *
  2636. * @param [in] privateKey Handle to private key object.
  2637. * @param [in] publicKey Encoded EC public key.
  2638. * @param [in] pubKeySize Length of encoded EC public key.
  2639. * @return MEMORY_E when a memory allocation fails.
  2640. * @return MP_CMP_E when the public parts are different.
  2641. * @return 0 on success.
  2642. */
  2643. static int wc_Pkcs11CheckPrivKey_Ecc(ecc_key* priv,
  2644. const unsigned char* publicKey, word32 pubKeySize)
  2645. {
  2646. int ret = 0;
  2647. #ifdef WOLFSSL_SMALL_STACK
  2648. ecc_key* pub = NULL;
  2649. #else
  2650. ecc_key pub[1];
  2651. #endif
  2652. word32 keyIdx = 0;
  2653. #ifdef WOLFSSL_SMALL_STACK
  2654. pub = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_ECC);
  2655. if (pub == NULL) {
  2656. ret = MEMORY_E;
  2657. }
  2658. #endif
  2659. if ((ret == 0) && (ret = wc_ecc_init(pub)) == 0) {
  2660. ret = wc_EccPublicKeyDecode(publicKey, &keyIdx, pub, pubKeySize);
  2661. if (ret == 0) {
  2662. /* both keys extracted successfully now check curve and
  2663. * pubkey. */
  2664. if ((pub->idx != priv->idx) || (wc_ecc_cmp_point(&priv->pubkey,
  2665. &pub->pubkey) != MP_EQ)) {
  2666. ret = MP_CMP_E;
  2667. }
  2668. else {
  2669. ret = 0;
  2670. }
  2671. }
  2672. wc_ecc_free(pub);
  2673. }
  2674. #ifdef WOLFSSL_SMALL_STACK
  2675. if (pub != NULL) {
  2676. XFREE(pub, NULL, DYNAMIC_TYPE_ECC);
  2677. }
  2678. #endif
  2679. return ret;
  2680. }
  2681. /**
  2682. * Checks the ECC private key matches the ECC public key.
  2683. *
  2684. * @param [in] session Session object.
  2685. * @param [in] info Cryptographic operation data.
  2686. * @return WC_HW_E when a PKCS#11 library call fails.
  2687. * @return MEMORY_E when a memory allocation fails.
  2688. * @return MEMORY_E when a memory allocation fails.
  2689. * @return MP_CMP_E when the public parts are different.
  2690. * @return 0 on success.
  2691. */
  2692. static int Pkcs11EccCheckPrivKey(Pkcs11Session* session, wc_CryptoInfo* info)
  2693. {
  2694. int ret = 0;
  2695. ecc_key* priv = info->pk.ecc_check.key;
  2696. CK_OBJECT_HANDLE privateKey;
  2697. if (mp_iszero(priv->pubkey.x) || mp_iszero(priv->pubkey.y)) {
  2698. /* Get the public key object as the private key object doesn't have
  2699. * the public point stored in it.
  2700. */
  2701. if (priv->labelLen > 0) {
  2702. ret = Pkcs11FindKeyByLabel(&privateKey, CKO_PUBLIC_KEY, CKK_EC,
  2703. session, priv->label, priv->labelLen);
  2704. }
  2705. else if (priv->idLen > 0) {
  2706. ret = Pkcs11FindKeyById(&privateKey, CKO_PUBLIC_KEY, CKK_EC,
  2707. session, priv->id, priv->idLen);
  2708. }
  2709. else {
  2710. ret = Pkcs11FindEccKey(&privateKey, CKO_PUBLIC_KEY, session, priv,
  2711. CKA_SIGN);
  2712. }
  2713. if (ret == 0 && priv->dp == NULL) {
  2714. /* Extract the group id. */
  2715. ret = Pkcs11GetEccParams(session, privateKey, priv);
  2716. }
  2717. if (ret == 0) {
  2718. /* Extract the public point. */
  2719. ret = Pkcs11GetEccPublicKey(priv, session, privateKey);
  2720. }
  2721. }
  2722. if (ret == 0) {
  2723. /* Compare the extracted public parts with the public key. */
  2724. ret = wc_Pkcs11CheckPrivKey_Ecc(priv, info->pk.ecc_check.pubKey,
  2725. info->pk.ecc_check.pubKeySz);
  2726. }
  2727. return ret;
  2728. }
  2729. #endif
  2730. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  2731. /**
  2732. * Performs the AES-GCM encryption operation.
  2733. *
  2734. * @param [in] session Session object.
  2735. * @param [in] info Cryptographic operation data.
  2736. * @return WC_HW_E when a PKCS#11 library call fails.
  2737. * @return MEMORY_E when a memory allocation fails.
  2738. * @return 0 on success.
  2739. */
  2740. static int Pkcs11AesGcmEncrypt(Pkcs11Session* session, wc_CryptoInfo* info)
  2741. {
  2742. int ret = 0;
  2743. CK_RV rv;
  2744. Aes* aes = info->cipher.aesgcm_enc.aes;
  2745. CK_GCM_PARAMS params;
  2746. CK_MECHANISM_INFO mechInfo;
  2747. CK_OBJECT_HANDLE key = NULL_PTR;
  2748. CK_MECHANISM mech;
  2749. CK_ULONG outLen;
  2750. /* Check operation is supported. */
  2751. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_AES_GCM,
  2752. &mechInfo);
  2753. #ifdef WOLFSSL_DEBUG_PKCS11
  2754. pkcs11_rv("C_GetMechanismInfo", rv);
  2755. #endif
  2756. if (rv != CKR_OK || (mechInfo.flags & CKF_ENCRYPT) == 0)
  2757. ret = NOT_COMPILED_IN;
  2758. if (ret == 0) {
  2759. WOLFSSL_MSG("PKCS#11: AES-GCM Encryption Operation");
  2760. /* Create a private key object or find by label or id. */
  2761. if (aes->idLen == 0 && aes->labelLen == 0) {
  2762. ret = Pkcs11CreateSecretKey(&key, session, CKK_AES,
  2763. (unsigned char*)aes->devKey,
  2764. aes->keylen, NULL, 0, NULL, 0,
  2765. CKA_ENCRYPT);
  2766. }
  2767. else if (aes->labelLen != 0) {
  2768. ret = Pkcs11FindKeyByLabel(&key, CKO_SECRET_KEY, CKK_AES, session,
  2769. aes->label, aes->labelLen);
  2770. }
  2771. else {
  2772. ret = Pkcs11FindKeyById(&key, CKO_SECRET_KEY, CKK_AES, session,
  2773. aes->id, aes->idLen);
  2774. }
  2775. }
  2776. if (ret == 0) {
  2777. params.pIv = (CK_BYTE_PTR)info->cipher.aesgcm_enc.iv;
  2778. params.ulIvLen = info->cipher.aesgcm_enc.ivSz;
  2779. params.pAAD = (CK_BYTE_PTR)info->cipher.aesgcm_enc.authIn;
  2780. params.ulAADLen = info->cipher.aesgcm_enc.authInSz;
  2781. params.ulTagBits = info->cipher.aesgcm_enc.authTagSz * 8;
  2782. mech.mechanism = CKM_AES_GCM;
  2783. mech.ulParameterLen = sizeof(params);
  2784. mech.pParameter = &params;
  2785. rv = session->func->C_EncryptInit(session->handle, &mech, key);
  2786. #ifdef WOLFSSL_DEBUG_PKCS11
  2787. pkcs11_rv("C_EncryptInit", rv);
  2788. #endif
  2789. if (rv != CKR_OK) {
  2790. ret = WC_HW_E;
  2791. }
  2792. }
  2793. if (ret == 0) {
  2794. outLen = info->cipher.aesgcm_enc.sz;
  2795. rv = session->func->C_EncryptUpdate(session->handle,
  2796. (CK_BYTE_PTR)info->cipher.aesgcm_enc.in,
  2797. info->cipher.aesgcm_enc.sz,
  2798. info->cipher.aesgcm_enc.out,
  2799. &outLen);
  2800. #ifdef WOLFSSL_DEBUG_PKCS11
  2801. pkcs11_rv("C_EncryptUpdate", rv);
  2802. #endif
  2803. if (rv != CKR_OK) {
  2804. ret = WC_HW_E;
  2805. }
  2806. }
  2807. if (ret == 0) {
  2808. /* Authentication tag comes out in final block. */
  2809. outLen = info->cipher.aesgcm_enc.authTagSz;
  2810. rv = session->func->C_EncryptFinal(session->handle,
  2811. info->cipher.aesgcm_enc.authTag,
  2812. &outLen);
  2813. #ifdef WOLFSSL_DEBUG_PKCS11
  2814. pkcs11_rv("C_EncryptFinal", rv);
  2815. #endif
  2816. if (rv != CKR_OK) {
  2817. ret = WC_HW_E;
  2818. }
  2819. }
  2820. if (aes->idLen == 0 && aes->labelLen == 0 && key != NULL_PTR)
  2821. session->func->C_DestroyObject(session->handle, key);
  2822. return ret;
  2823. }
  2824. /**
  2825. * Performs the AES-GCM decryption operation.
  2826. *
  2827. * @param [in] session Session object.
  2828. * @param [in] info Cryptographic operation data.
  2829. * @return WC_HW_E when a PKCS#11 library call fails.
  2830. * @return MEMORY_E when a memory allocation fails.
  2831. * @return 0 on success.
  2832. */
  2833. static int Pkcs11AesGcmDecrypt(Pkcs11Session* session, wc_CryptoInfo* info)
  2834. {
  2835. int ret = 0;
  2836. CK_RV rv;
  2837. Aes* aes = info->cipher.aesgcm_enc.aes;
  2838. CK_GCM_PARAMS params;
  2839. CK_MECHANISM_INFO mechInfo;
  2840. CK_OBJECT_HANDLE key = NULL_PTR;
  2841. CK_MECHANISM mech;
  2842. CK_ULONG outLen;
  2843. word32 len;
  2844. /* Check operation is supported. */
  2845. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_AES_GCM,
  2846. &mechInfo);
  2847. #ifdef WOLFSSL_DEBUG_PKCS11
  2848. pkcs11_rv("C_GetMechanismInfo", rv);
  2849. #endif
  2850. if (rv != CKR_OK || (mechInfo.flags & CKF_DECRYPT) == 0)
  2851. ret = NOT_COMPILED_IN;
  2852. if (ret == 0) {
  2853. WOLFSSL_MSG("PKCS#11: AES-GCM Decryption Operation");
  2854. /* Create a private key object or find by id. */
  2855. if (aes->idLen == 0 && aes->labelLen == 0) {
  2856. ret = Pkcs11CreateSecretKey(&key, session, CKK_AES,
  2857. (unsigned char*)aes->devKey,
  2858. aes->keylen, NULL, 0, NULL, 0,
  2859. CKA_DECRYPT);
  2860. }
  2861. else if (aes->labelLen != 0) {
  2862. ret = Pkcs11FindKeyByLabel(&key, CKO_SECRET_KEY, CKK_AES, session,
  2863. aes->label, aes->labelLen);
  2864. }
  2865. else {
  2866. ret = Pkcs11FindKeyById(&key, CKO_SECRET_KEY, CKK_AES, session,
  2867. aes->id, aes->idLen);
  2868. }
  2869. }
  2870. if (ret == 0) {
  2871. params.pIv = (CK_BYTE_PTR)info->cipher.aesgcm_dec.iv;
  2872. params.ulIvLen = info->cipher.aesgcm_dec.ivSz;
  2873. params.pAAD = (CK_BYTE_PTR)info->cipher.aesgcm_dec.authIn;
  2874. params.ulAADLen = info->cipher.aesgcm_dec.authInSz;
  2875. params.ulTagBits = info->cipher.aesgcm_dec.authTagSz * 8;
  2876. mech.mechanism = CKM_AES_GCM;
  2877. mech.ulParameterLen = sizeof(params);
  2878. mech.pParameter = &params;
  2879. rv = session->func->C_DecryptInit(session->handle, &mech, key);
  2880. #ifdef WOLFSSL_DEBUG_PKCS11
  2881. pkcs11_rv("C_DecryptInit", rv);
  2882. #endif
  2883. if (rv != CKR_OK) {
  2884. ret = WC_HW_E;
  2885. }
  2886. }
  2887. if (ret == 0) {
  2888. outLen = len = info->cipher.aesgcm_dec.sz;
  2889. rv = session->func->C_DecryptUpdate(session->handle,
  2890. (CK_BYTE_PTR)info->cipher.aesgcm_dec.in,
  2891. info->cipher.aesgcm_dec.sz,
  2892. info->cipher.aesgcm_dec.out,
  2893. &outLen);
  2894. #ifdef WOLFSSL_DEBUG_PKCS11
  2895. pkcs11_rv("C_DecryptUpdate", rv);
  2896. #endif
  2897. if (rv != CKR_OK) {
  2898. ret = WC_HW_E;
  2899. }
  2900. }
  2901. if (ret == 0) {
  2902. /* Put authentication tag in as encrypted data. */
  2903. outLen = len = (len + info->cipher.aesgcm_dec.authTagSz -
  2904. (word32)outLen);
  2905. rv = session->func->C_DecryptUpdate(session->handle,
  2906. (CK_BYTE_PTR)info->cipher.aesgcm_dec.authTag,
  2907. info->cipher.aesgcm_dec.authTagSz,
  2908. info->cipher.aesgcm_dec.out,
  2909. &outLen);
  2910. #ifdef WOLFSSL_DEBUG_PKCS11
  2911. pkcs11_rv("C_DecryptUpdate", rv);
  2912. #endif
  2913. if (rv != CKR_OK) {
  2914. ret = WC_HW_E;
  2915. }
  2916. }
  2917. if (ret == 0) {
  2918. outLen = len = (len - (word32)outLen);
  2919. /* Decrypted data comes out now. */
  2920. rv = session->func->C_DecryptFinal(session->handle,
  2921. info->cipher.aesgcm_dec.out,
  2922. &outLen);
  2923. #ifdef WOLFSSL_DEBUG_PKCS11
  2924. pkcs11_rv("C_DecryptFinal", rv);
  2925. #endif
  2926. if (rv != CKR_OK) {
  2927. ret = WC_HW_E;
  2928. }
  2929. }
  2930. if (aes->idLen == 0 && aes->labelLen == 0 && key != NULL_PTR)
  2931. session->func->C_DestroyObject(session->handle, key);
  2932. return ret;
  2933. }
  2934. #endif
  2935. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  2936. /**
  2937. * Performs the AES-CBC encryption operation.
  2938. *
  2939. * @param [in] session Session object.
  2940. * @param [in] info Cryptographic operation data.
  2941. * @return WC_HW_E when a PKCS#11 library call fails.
  2942. * @return MEMORY_E when a memory allocation fails.
  2943. * @return 0 on success.
  2944. */
  2945. static int Pkcs11AesCbcEncrypt(Pkcs11Session* session, wc_CryptoInfo* info)
  2946. {
  2947. int ret = 0;
  2948. CK_RV rv;
  2949. Aes* aes = info->cipher.aescbc.aes;
  2950. CK_MECHANISM_INFO mechInfo;
  2951. CK_OBJECT_HANDLE key = NULL_PTR;
  2952. CK_MECHANISM mech;
  2953. CK_ULONG outLen;
  2954. /* Check operation is supported. */
  2955. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_AES_CBC,
  2956. &mechInfo);
  2957. #ifdef WOLFSSL_DEBUG_PKCS11
  2958. pkcs11_rv("C_GetMechanismInfo", rv);
  2959. #endif
  2960. if (rv != CKR_OK || (mechInfo.flags & CKF_ENCRYPT) == 0)
  2961. ret = NOT_COMPILED_IN;
  2962. if (ret == 0) {
  2963. WOLFSSL_MSG("PKCS#11: AES-CBC Encryption Operation");
  2964. /* Create a private key object or find by id. */
  2965. if (aes->idLen == 0 && aes->labelLen == 0) {
  2966. ret = Pkcs11CreateSecretKey(&key, session, CKK_AES,
  2967. (unsigned char*)aes->devKey,
  2968. aes->keylen, NULL, 0, NULL, 0,
  2969. CKA_ENCRYPT);
  2970. }
  2971. else if (aes->labelLen != 0) {
  2972. ret = Pkcs11FindKeyByLabel(&key, CKO_SECRET_KEY, CKK_AES, session,
  2973. aes->label, aes->labelLen);
  2974. }
  2975. else {
  2976. ret = Pkcs11FindKeyById(&key, CKO_SECRET_KEY, CKK_AES, session,
  2977. aes->id, aes->idLen);
  2978. }
  2979. }
  2980. if (ret == 0) {
  2981. mech.mechanism = CKM_AES_CBC;
  2982. mech.ulParameterLen = AES_BLOCK_SIZE;
  2983. mech.pParameter = (CK_BYTE_PTR)info->cipher.aescbc.aes->reg;
  2984. rv = session->func->C_EncryptInit(session->handle, &mech, key);
  2985. #ifdef WOLFSSL_DEBUG_PKCS11
  2986. pkcs11_rv("C_EncryptInit", rv);
  2987. #endif
  2988. if (rv != CKR_OK) {
  2989. ret = WC_HW_E;
  2990. }
  2991. }
  2992. if (ret == 0) {
  2993. outLen = info->cipher.aescbc.sz;
  2994. rv = session->func->C_Encrypt(session->handle,
  2995. (CK_BYTE_PTR)info->cipher.aescbc.in,
  2996. info->cipher.aescbc.sz,
  2997. info->cipher.aescbc.out,
  2998. &outLen);
  2999. #ifdef WOLFSSL_DEBUG_PKCS11
  3000. pkcs11_rv("C_Encrypt", rv);
  3001. #endif
  3002. if (rv != CKR_OK) {
  3003. ret = WC_HW_E;
  3004. }
  3005. }
  3006. if (aes->idLen == 0 && aes->labelLen == 0 && key != NULL_PTR)
  3007. session->func->C_DestroyObject(session->handle, key);
  3008. return ret;
  3009. }
  3010. /**
  3011. * Performs the AES-CBC decryption operation.
  3012. *
  3013. * @param [in] session Session object.
  3014. * @param [in] info Cryptographic operation data.
  3015. * @return WC_HW_E when a PKCS#11 library call fails.
  3016. * @return MEMORY_E when a memory allocation fails.
  3017. * @return 0 on success.
  3018. */
  3019. static int Pkcs11AesCbcDecrypt(Pkcs11Session* session, wc_CryptoInfo* info)
  3020. {
  3021. int ret = 0;
  3022. CK_RV rv;
  3023. Aes* aes = info->cipher.aescbc.aes;
  3024. CK_MECHANISM_INFO mechInfo;
  3025. CK_OBJECT_HANDLE key = NULL_PTR;
  3026. CK_MECHANISM mech;
  3027. CK_ULONG outLen;
  3028. /* Check operation is supported. */
  3029. rv = session->func->C_GetMechanismInfo(session->slotId, CKM_AES_CBC,
  3030. &mechInfo);
  3031. #ifdef WOLFSSL_DEBUG_PKCS11
  3032. pkcs11_rv("C_GetMechanismInfo", rv);
  3033. #endif
  3034. if (rv != CKR_OK || (mechInfo.flags & CKF_DECRYPT) == 0)
  3035. ret = NOT_COMPILED_IN;
  3036. if (ret == 0) {
  3037. WOLFSSL_MSG("PKCS#11: AES-CBC Decryption Operation");
  3038. /* Create a private key object or find by id. */
  3039. if (aes->idLen == 0 && aes->labelLen == 0) {
  3040. ret = Pkcs11CreateSecretKey(&key, session, CKK_AES,
  3041. (unsigned char*)aes->devKey,
  3042. aes->keylen, NULL, 0, NULL, 0,
  3043. CKA_DECRYPT);
  3044. }
  3045. else if (aes->labelLen != 0) {
  3046. ret = Pkcs11FindKeyByLabel(&key, CKO_SECRET_KEY, CKK_AES, session,
  3047. aes->label, aes->labelLen);
  3048. }
  3049. else {
  3050. ret = Pkcs11FindKeyById(&key, CKO_SECRET_KEY, CKK_AES, session,
  3051. aes->id, aes->idLen);
  3052. }
  3053. }
  3054. if (ret == 0) {
  3055. mech.mechanism = CKM_AES_CBC;
  3056. mech.ulParameterLen = AES_BLOCK_SIZE;
  3057. mech.pParameter = (CK_BYTE_PTR)info->cipher.aescbc.aes->reg;
  3058. rv = session->func->C_DecryptInit(session->handle, &mech, key);
  3059. #ifdef WOLFSSL_DEBUG_PKCS11
  3060. pkcs11_rv("C_DecryptInit", rv);
  3061. #endif
  3062. if (rv != CKR_OK) {
  3063. ret = WC_HW_E;
  3064. }
  3065. }
  3066. if (ret == 0) {
  3067. outLen = info->cipher.aescbc.sz;
  3068. rv = session->func->C_DecryptUpdate(session->handle,
  3069. (CK_BYTE_PTR)info->cipher.aescbc.in,
  3070. info->cipher.aescbc.sz,
  3071. info->cipher.aescbc.out,
  3072. &outLen);
  3073. #ifdef WOLFSSL_DEBUG_PKCS11
  3074. pkcs11_rv("C_DecryptUpdate", rv);
  3075. #endif
  3076. if (rv != CKR_OK) {
  3077. ret = WC_HW_E;
  3078. }
  3079. }
  3080. if (aes->idLen == 0 && aes->labelLen == 0 && key != NULL_PTR)
  3081. session->func->C_DestroyObject(session->handle, key);
  3082. return ret;
  3083. }
  3084. #endif
  3085. #ifndef NO_HMAC
  3086. /**
  3087. * Updates or calculates the HMAC of the data.
  3088. *
  3089. * @param [in] session Session object.
  3090. * @param [in] info Cryptographic operation data.
  3091. * @return WC_HW_E when a PKCS#11 library call fails.
  3092. * @return 0 on success.
  3093. */
  3094. static int Pkcs11Hmac(Pkcs11Session* session, wc_CryptoInfo* info)
  3095. {
  3096. int ret = 0;
  3097. CK_RV rv;
  3098. Hmac* hmac = info->hmac.hmac;
  3099. CK_MECHANISM_INFO mechInfo;
  3100. CK_OBJECT_HANDLE key = NULL_PTR;
  3101. CK_MECHANISM mech;
  3102. CK_ULONG outLen;
  3103. int mechType;
  3104. int keyType;
  3105. if (hmac->innerHashKeyed == WC_HMAC_INNER_HASH_KEYED_SW)
  3106. ret = NOT_COMPILED_IN;
  3107. if (ret == 0)
  3108. ret = Pkcs11HmacTypes(info->hmac.macType, &mechType, &keyType);
  3109. if (ret == 0) {
  3110. /* Check operation is supported. */
  3111. rv = session->func->C_GetMechanismInfo(session->slotId, mechType,
  3112. &mechInfo);
  3113. #ifdef WOLFSSL_DEBUG_PKCS11
  3114. pkcs11_rv("C_GetMechanismInfo", rv);
  3115. #endif
  3116. if (rv != CKR_OK || (mechInfo.flags & CKF_SIGN) == 0)
  3117. ret = NOT_COMPILED_IN;
  3118. }
  3119. /* Check whether key been used to initialized. */
  3120. if (ret == 0 && !hmac->innerHashKeyed) {
  3121. WOLFSSL_MSG("PKCS#11: HMAC Init");
  3122. /* Check device supports key length. */
  3123. if (mechInfo.ulMaxKeySize > 0 &&
  3124. (hmac->keyLen < mechInfo.ulMinKeySize ||
  3125. hmac->keyLen > mechInfo.ulMaxKeySize)) {
  3126. WOLFSSL_MSG("PKCS#11: Key Length not supported");
  3127. ret = NOT_COMPILED_IN;
  3128. }
  3129. /* Create a private key object or find by id. */
  3130. if (ret == 0 && hmac->idLen == 0 && hmac->labelLen == 0) {
  3131. ret = Pkcs11CreateSecretKey(&key, session, keyType,
  3132. (unsigned char*)hmac->keyRaw, hmac->keyLen,
  3133. NULL, 0, NULL, 0, CKA_SIGN);
  3134. if (ret == WC_HW_E) {
  3135. ret = Pkcs11CreateSecretKey(&key, session, CKK_GENERIC_SECRET,
  3136. (unsigned char*)hmac->keyRaw, hmac->keyLen,
  3137. NULL, 0, NULL, 0, CKA_SIGN);
  3138. }
  3139. }
  3140. else if (ret == 0 && hmac->labelLen != 0) {
  3141. ret = Pkcs11FindKeyByLabel(&key, CKO_SECRET_KEY, keyType, session,
  3142. hmac->label, hmac->labelLen);
  3143. if (ret == WC_HW_E) {
  3144. ret = Pkcs11FindKeyByLabel(&key, CKO_SECRET_KEY,
  3145. CKK_GENERIC_SECRET, session,
  3146. hmac->label, hmac->labelLen);
  3147. }
  3148. }
  3149. else if (ret == 0) {
  3150. ret = Pkcs11FindKeyById(&key, CKO_SECRET_KEY, keyType, session,
  3151. hmac->id, hmac->idLen);
  3152. if (ret == WC_HW_E) {
  3153. ret = Pkcs11FindKeyById(&key, CKO_SECRET_KEY,
  3154. CKK_GENERIC_SECRET, session, hmac->id,
  3155. hmac->idLen);
  3156. }
  3157. }
  3158. /* Initialize HMAC operation */
  3159. if (ret == 0) {
  3160. mech.mechanism = mechType;
  3161. mech.ulParameterLen = 0;
  3162. mech.pParameter = NULL;
  3163. rv = session->func->C_SignInit(session->handle, &mech, key);
  3164. #ifdef WOLFSSL_DEBUG_PKCS11
  3165. pkcs11_rv("C_SignInit", rv);
  3166. #endif
  3167. if (rv != CKR_OK) {
  3168. ret = WC_HW_E;
  3169. }
  3170. }
  3171. /* Don't initialize HMAC again if this succeeded */
  3172. if (ret == 0)
  3173. hmac->innerHashKeyed = WC_HMAC_INNER_HASH_KEYED_DEV;
  3174. }
  3175. /* Update the HMAC if input data passed in. */
  3176. if (ret == 0 && info->hmac.inSz > 0) {
  3177. WOLFSSL_MSG("PKCS#11: HMAC Update");
  3178. rv = session->func->C_SignUpdate(session->handle,
  3179. (CK_BYTE_PTR)info->hmac.in,
  3180. info->hmac.inSz);
  3181. #ifdef WOLFSSL_DEBUG_PKCS11
  3182. pkcs11_rv("C_SignUpdate", rv);
  3183. #endif
  3184. /* Some algorithm implementations only support C_Sign. */
  3185. if (rv == CKR_MECHANISM_INVALID) {
  3186. WOLFSSL_MSG("PKCS#11: HMAC Update/Final not supported");
  3187. ret = NOT_COMPILED_IN;
  3188. /* Allow software implementation to set key. */
  3189. hmac->innerHashKeyed = 0;
  3190. }
  3191. else if (rv != CKR_OK)
  3192. ret = WC_HW_E;
  3193. }
  3194. /* Calculate the HMAC result if output buffer specified. */
  3195. if (ret == 0 && info->hmac.digest != NULL) {
  3196. WOLFSSL_MSG("PKCS#11: HMAC Final");
  3197. outLen = WC_MAX_DIGEST_SIZE;
  3198. rv = session->func->C_SignFinal(session->handle,
  3199. (CK_BYTE_PTR)info->hmac.digest,
  3200. &outLen);
  3201. #ifdef WOLFSSL_DEBUG_PKCS11
  3202. pkcs11_rv("C_SignFinal", rv);
  3203. #endif
  3204. /* Some algorithm implementations only support C_Sign. */
  3205. if (rv != CKR_OK) {
  3206. ret = WC_HW_E;
  3207. }
  3208. else
  3209. hmac->innerHashKeyed = 0;
  3210. }
  3211. if (hmac->idLen == 0 && hmac->labelLen == 0 && key != NULL_PTR)
  3212. session->func->C_DestroyObject(session->handle, key);
  3213. return ret;
  3214. }
  3215. #endif
  3216. #ifndef WC_NO_RNG
  3217. #ifndef HAVE_HASHDRBG
  3218. /**
  3219. * Performs random number generation.
  3220. *
  3221. * @param [in] session Session object.
  3222. * @param [in] info Cryptographic operation data.
  3223. * @return WC_HW_E when a PKCS#11 library call fails.
  3224. * @return 0 on success.
  3225. */
  3226. static int Pkcs11RandomBlock(Pkcs11Session* session, wc_CryptoInfo* info)
  3227. {
  3228. int ret = 0;
  3229. CK_RV rv;
  3230. rv = session->func->C_GenerateRandom(session->handle, info->rng.out,
  3231. info->rng.sz);
  3232. #ifdef WOLFSSL_DEBUG_PKCS11
  3233. pkcs11_rv("C_GenerateRandom", rv);
  3234. #endif
  3235. if (rv != CKR_OK) {
  3236. ret = WC_HW_E;
  3237. }
  3238. return ret;
  3239. }
  3240. #endif
  3241. /**
  3242. * Generates entropy (seed) data.
  3243. *
  3244. * @param [in] session Session object.
  3245. * @param [in] info Cryptographic operation data.
  3246. * @return WC_HW_E when a PKCS#11 library call fails.
  3247. * @return 0 on success.
  3248. */
  3249. static int Pkcs11RandomSeed(Pkcs11Session* session, wc_CryptoInfo* info)
  3250. {
  3251. int ret = 0;
  3252. CK_RV rv;
  3253. rv = session->func->C_GenerateRandom(session->handle, info->seed.seed,
  3254. info->seed.sz);
  3255. #ifdef WOLFSSL_DEBUG_PKCS11
  3256. pkcs11_rv("C_GenerateRandom", rv);
  3257. #endif
  3258. if (rv != CKR_OK) {
  3259. ret = WC_HW_E;
  3260. }
  3261. return ret;
  3262. }
  3263. #endif
  3264. /**
  3265. * Perform a cryptographic operation using PKCS#11 device.
  3266. *
  3267. * @param [in] devId Device identifier.
  3268. * @param [in] info Cryptographic operation data.
  3269. * @param [in] ctx Context data for device - the token object.
  3270. * @return WC_HW_E when a PKCS#11 library call fails.
  3271. * @return 0 on success.
  3272. */
  3273. int wc_Pkcs11_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
  3274. {
  3275. int ret = 0;
  3276. Pkcs11Token* token = (Pkcs11Token*)ctx;
  3277. Pkcs11Session session;
  3278. int readWrite = 0;
  3279. if (devId <= INVALID_DEVID || info == NULL || ctx == NULL)
  3280. ret = BAD_FUNC_ARG;
  3281. /* Open and close a session around each operation as the operation may not
  3282. * be compiled in.
  3283. */
  3284. if (ret == 0) {
  3285. if (info->algo_type == WC_ALGO_TYPE_PK) {
  3286. #if !defined(NO_RSA) || defined(HAVE_ECC)
  3287. switch (info->pk.type) {
  3288. #ifndef NO_RSA
  3289. case WC_PK_TYPE_RSA:
  3290. ret = Pkcs11OpenSession(token, &session, readWrite);
  3291. if (ret == 0) {
  3292. ret = Pkcs11Rsa(&session, info);
  3293. Pkcs11CloseSession(token, &session);
  3294. }
  3295. break;
  3296. #ifdef WOLFSSL_KEY_GEN
  3297. case WC_PK_TYPE_RSA_KEYGEN:
  3298. ret = Pkcs11OpenSession(token, &session, readWrite);
  3299. if (ret == 0) {
  3300. ret = Pkcs11RsaKeyGen(&session, info);
  3301. Pkcs11CloseSession(token, &session);
  3302. }
  3303. break;
  3304. #endif
  3305. case WC_PK_TYPE_RSA_CHECK_PRIV_KEY:
  3306. ret = Pkcs11OpenSession(token, &session, readWrite);
  3307. if (ret == 0) {
  3308. ret = Pkcs11RsaCheckPrivKey(&session, info);
  3309. Pkcs11CloseSession(token, &session);
  3310. }
  3311. break;
  3312. #endif
  3313. #ifdef HAVE_ECC
  3314. #ifndef NO_PKCS11_EC_KEYGEN
  3315. case WC_PK_TYPE_EC_KEYGEN:
  3316. ret = Pkcs11OpenSession(token, &session, readWrite);
  3317. if (ret == 0) {
  3318. ret = Pkcs11EcKeyGen(&session, info);
  3319. Pkcs11CloseSession(token, &session);
  3320. }
  3321. break;
  3322. #endif
  3323. #ifndef NO_PKCS11_ECDH
  3324. case WC_PK_TYPE_ECDH:
  3325. ret = Pkcs11OpenSession(token, &session, readWrite);
  3326. if (ret == 0) {
  3327. ret = Pkcs11ECDH(&session, info);
  3328. Pkcs11CloseSession(token, &session);
  3329. }
  3330. break;
  3331. #endif
  3332. case WC_PK_TYPE_ECDSA_SIGN:
  3333. ret = Pkcs11OpenSession(token, &session, readWrite);
  3334. if (ret == 0) {
  3335. ret = Pkcs11ECDSA_Sign(&session, info);
  3336. Pkcs11CloseSession(token, &session);
  3337. }
  3338. break;
  3339. case WC_PK_TYPE_ECDSA_VERIFY:
  3340. ret = Pkcs11OpenSession(token, &session, readWrite);
  3341. if (ret == 0) {
  3342. ret = Pkcs11ECDSA_Verify(&session, info);
  3343. Pkcs11CloseSession(token, &session);
  3344. }
  3345. break;
  3346. case WC_PK_TYPE_EC_CHECK_PRIV_KEY:
  3347. ret = Pkcs11OpenSession(token, &session, readWrite);
  3348. if (ret == 0) {
  3349. ret = Pkcs11EccCheckPrivKey(&session, info);
  3350. Pkcs11CloseSession(token, &session);
  3351. }
  3352. break;
  3353. #endif
  3354. default:
  3355. ret = NOT_COMPILED_IN;
  3356. break;
  3357. }
  3358. #else
  3359. ret = NOT_COMPILED_IN;
  3360. #endif /* !NO_RSA || HAVE_ECC */
  3361. }
  3362. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  3363. #ifndef NO_AES
  3364. switch (info->cipher.type) {
  3365. #ifdef HAVE_AESGCM
  3366. case WC_CIPHER_AES_GCM:
  3367. if (info->cipher.enc) {
  3368. ret = Pkcs11OpenSession(token, &session, readWrite);
  3369. if (ret == 0) {
  3370. ret = Pkcs11AesGcmEncrypt(&session, info);
  3371. Pkcs11CloseSession(token, &session);
  3372. }
  3373. }
  3374. else {
  3375. ret = Pkcs11OpenSession(token, &session, readWrite);
  3376. if (ret == 0) {
  3377. ret = Pkcs11AesGcmDecrypt(&session, info);
  3378. Pkcs11CloseSession(token, &session);
  3379. }
  3380. }
  3381. break;
  3382. #endif
  3383. #ifdef HAVE_AES_CBC
  3384. case WC_CIPHER_AES_CBC:
  3385. if (info->cipher.enc) {
  3386. ret = Pkcs11OpenSession(token, &session, readWrite);
  3387. if (ret == 0) {
  3388. ret = Pkcs11AesCbcEncrypt(&session, info);
  3389. Pkcs11CloseSession(token, &session);
  3390. }
  3391. }
  3392. else {
  3393. ret = Pkcs11OpenSession(token, &session, readWrite);
  3394. if (ret == 0) {
  3395. ret = Pkcs11AesCbcDecrypt(&session, info);
  3396. Pkcs11CloseSession(token, &session);
  3397. }
  3398. }
  3399. break;
  3400. #endif
  3401. }
  3402. #else
  3403. ret = NOT_COMPILED_IN;
  3404. #endif
  3405. }
  3406. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  3407. #ifndef NO_HMAC
  3408. ret = Pkcs11OpenSession(token, &session, readWrite);
  3409. if (ret == 0) {
  3410. ret = Pkcs11Hmac(&session, info);
  3411. Pkcs11CloseSession(token, &session);
  3412. }
  3413. #else
  3414. ret = NOT_COMPILED_IN;
  3415. #endif
  3416. }
  3417. else if (info->algo_type == WC_ALGO_TYPE_RNG) {
  3418. #if !defined(WC_NO_RNG) && !defined(HAVE_HASHDRBG)
  3419. ret = Pkcs11OpenSession(token, &session, readWrite);
  3420. if (ret == 0) {
  3421. ret = Pkcs11RandomBlock(&session, info);
  3422. Pkcs11CloseSession(token, &session);
  3423. }
  3424. #else
  3425. ret = NOT_COMPILED_IN;
  3426. #endif
  3427. }
  3428. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  3429. #ifndef WC_NO_RNG
  3430. ret = Pkcs11OpenSession(token, &session, readWrite);
  3431. if (ret == 0) {
  3432. ret = Pkcs11RandomSeed(&session, info);
  3433. Pkcs11CloseSession(token, &session);
  3434. }
  3435. #else
  3436. ret = NOT_COMPILED_IN;
  3437. #endif
  3438. }
  3439. else
  3440. ret = NOT_COMPILED_IN;
  3441. }
  3442. return ret;
  3443. }
  3444. #endif /* HAVE_PKCS11 */