scram.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  1. /* SCRAM-SHA-1/SHA-2 SASL plugin
  2. * Alexey Melnikov
  3. */
  4. /*
  5. * Copyright (c) 2009-2016 Carnegie Mellon University. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The name "Carnegie Mellon University" must not be used to
  20. * endorse or promote products derived from this software without
  21. * prior written permission. For permission or any other legal
  22. * details, please contact
  23. * Carnegie Mellon University
  24. * Center for Technology Transfer and Enterprise Creation
  25. * 4615 Forbes Avenue
  26. * Suite 302
  27. * Pittsburgh, PA 15213
  28. * (412) 268-7393, fax: (412) 268-7395
  29. * innovation@andrew.cmu.edu
  30. *
  31. * 4. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by Computing Services
  34. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  35. *
  36. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  37. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  38. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  39. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  40. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  41. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  42. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  43. */
  44. #include <config.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #ifndef macintosh
  49. #include <sys/stat.h>
  50. #endif
  51. #include <fcntl.h>
  52. #include <errno.h>
  53. #include <sasl.h>
  54. #include <saslplug.h>
  55. #include <saslutil.h>
  56. #include "plugin_common.h"
  57. #ifdef macintosh
  58. #error #include <sasl_scram_plugin_decl.h>
  59. #endif
  60. #include <openssl/sha.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/hmac.h>
  63. /***************************** Common Section *****************************/
  64. #define NONCE_SIZE (32) /* arbitrary */
  65. #define SALT_SIZE (16) /* arbitrary */
  66. /* TODO: make this a configurable option? */
  67. #define DEFAULT_ITERATION_COUNTER 4096
  68. #define MIN_ITERATION_COUNTER 4096
  69. #define MAX_ITERATION_COUNTER 0x10000
  70. /* maximum length of the iteration_counter (as a string). Assume it is 32bits */
  71. #define ITERATION_COUNTER_BUF_LEN 20
  72. #define BASE64_LEN(size) (((size) / 3 * 4) + (((size) % 3) ? 4 : 0))
  73. #define MAX_CLIENTIN_LEN 2048
  74. #define MAX_SERVERIN_LEN 2048
  75. #define STRINGIZE(x) #x
  76. #define MAX_CLIENTIN_LEN_STR STRINGIZE((MAX_CLIENTIN_LEN))
  77. #define MAX_SERVERIN_LEN_STR STRINGIZE((MAX_SERVERIN_LEN))
  78. #define CLIENT_KEY_CONSTANT "Client Key"
  79. #define SERVER_KEY_CONSTANT "Server Key"
  80. #define CLIENT_KEY_CONSTANT_LEN sizeof(CLIENT_KEY_CONSTANT)-1
  81. #define SERVER_KEY_CONSTANT_LEN sizeof(SERVER_KEY_CONSTANT)-1
  82. #define SCRAM_CB_FLAG_MASK 0x0F
  83. #define SCRAM_CB_FLAG_N 0x00
  84. #define SCRAM_CB_FLAG_P 0x01
  85. #define SCRAM_CB_FLAG_Y 0x02
  86. #ifdef SCRAM_DEBUG
  87. #define PRINT_HASH(func,hash,size) print_hash(func,hash,size)
  88. #else
  89. #define PRINT_HASH(func,hash,size)
  90. #endif
  91. /* NB: A temporary mapping for "internal errors". It would be better to add
  92. a new SASL error code for that */
  93. #define SASL_SCRAM_INTERNAL SASL_NOMEM
  94. /* Holds the core salt to avoid regenerating salt each auth. */
  95. static unsigned char g_salt_key[SALT_SIZE];
  96. /* Note that currently only SHA-* variants are supported! */
  97. static const char *
  98. scram_sasl_mech_name(size_t hash_size)
  99. {
  100. switch (hash_size) {
  101. case 64:
  102. return "SCRAM-SHA-512";
  103. case 48:
  104. return "SCRAM-SHA-384";
  105. case 32:
  106. return "SCRAM-SHA-256";
  107. case 28:
  108. return "SCRAM-SHA-224";
  109. case 20:
  110. return "SCRAM-SHA-1";
  111. }
  112. return NULL;
  113. }
  114. /* Convert saslname = 1*(value-safe-char / "=2C" / "=3D") in place.
  115. Returns SASL_FAIL if the encoding is invalid, otherwise SASL_OK */
  116. static int
  117. decode_saslname (char *buf)
  118. {
  119. char * inp;
  120. char * outp;
  121. inp = outp = buf;
  122. while (*inp) {
  123. if (*inp == '=') {
  124. inp++;
  125. if (*inp == '\0') {
  126. return SASL_FAIL;
  127. }
  128. if (inp[0] == '2' && inp[1] == 'C') {
  129. *outp = ',';
  130. inp += 2;
  131. } else if (inp[0] == '3' && inp[1] == 'D') {
  132. *outp = '=';
  133. inp += 2;
  134. } else {
  135. return SASL_FAIL;
  136. }
  137. } else {
  138. *outp = *inp;
  139. inp++;
  140. }
  141. outp++;
  142. }
  143. *outp = '\0';
  144. return SASL_OK;
  145. }
  146. /* Convert a username to saslname = 1*(value-safe-char / "=2C" / "=3D")
  147. and return an allocated copy.
  148. "freeme" contains pointer to the allocated output, or NULL,
  149. if encoded_saslname just points to saslname.
  150. Returns SASL_NOMEM if can't allocate memory for the output, otherwise SASL_OK */
  151. static int
  152. encode_saslname (const char *saslname,
  153. const char **encoded_saslname,
  154. char **freeme)
  155. {
  156. const char * inp;
  157. char * outp;
  158. int special_chars = 0;
  159. /* Found out if anything needs encoding */
  160. for (inp = saslname; *inp; inp++) {
  161. if (*inp == ',' || *inp == '=') {
  162. special_chars++;
  163. }
  164. }
  165. if (special_chars == 0) {
  166. *encoded_saslname = saslname;
  167. *freeme = NULL;
  168. return SASL_OK;
  169. }
  170. outp = malloc(strlen(saslname) + special_chars * 2 + 1);
  171. *encoded_saslname = outp;
  172. *freeme = outp;
  173. if (outp == NULL) {
  174. return SASL_NOMEM;
  175. }
  176. for (inp = saslname; *inp; inp++) {
  177. switch (*inp) {
  178. case ',':
  179. *outp++ = '=';
  180. *outp++ = '2';
  181. *outp++ = 'C';
  182. break;
  183. case '=':
  184. *outp++ = '=';
  185. *outp++ = '3';
  186. *outp++ = 'D';
  187. break;
  188. default:
  189. *outp++ = *inp;
  190. }
  191. }
  192. *outp = '\0';
  193. return SASL_OK;
  194. }
  195. static char *
  196. create_nonce(const sasl_utils_t * utils,
  197. char *buffer,
  198. size_t buflen) /* Including the terminating NUL */
  199. {
  200. char *intbuf;
  201. unsigned int estimated;
  202. if ((buflen - 1) % 4 != 0) {
  203. /* NB: the algorithm below doesn't work for such length.
  204. It needs to be adjusted to allocate + 4 bytes,
  205. encode the last 4 bytes to a separate buffer and
  206. then copy the necessary number of bytes to the end of the output */
  207. return NULL;
  208. }
  209. estimated = (unsigned int)((buflen - 1) / 4 * 3);
  210. intbuf = (char *) utils->malloc(estimated + 1);
  211. if (intbuf == NULL) {
  212. return NULL;
  213. }
  214. utils->rand(utils->rpool, intbuf, estimated);
  215. /* base 64 encode it so it has valid chars */
  216. if (utils->encode64(intbuf,
  217. estimated,
  218. buffer,
  219. (unsigned int)buflen,
  220. NULL) != SASL_OK) {
  221. utils->free(intbuf);
  222. return NULL;
  223. }
  224. utils->free(intbuf);
  225. buffer[buflen-1] = '\0';
  226. return buffer;
  227. }
  228. #ifdef SCRAM_DEBUG
  229. /* Useful for debugging interop issues */
  230. static void
  231. print_hash (const char * func, const char * hash, size_t hash_size)
  232. {
  233. int i;
  234. printf (" HASH in %s:", func);
  235. for (i = 0; i < hash_size; i++) {
  236. printf (" %.2X", (unsigned char)hash[i]);
  237. }
  238. printf ("\n");
  239. }
  240. #endif
  241. /* The result variable need to point to a buffer big enough for the [SHA-*] hash */
  242. static void
  243. Hi (const sasl_utils_t * utils,
  244. const EVP_MD *md,
  245. const char * str,
  246. size_t str_len,
  247. const char * salt,
  248. size_t salt_len,
  249. unsigned int iteration_count,
  250. char * result)
  251. {
  252. char * initial_key = NULL;
  253. unsigned int i;
  254. char * temp_result;
  255. unsigned int hash_len = 0;
  256. size_t k, hash_size = EVP_MD_size(md);
  257. initial_key = utils->malloc(salt_len + 4);
  258. memcpy (initial_key, salt, salt_len);
  259. initial_key[salt_len] = 0;
  260. initial_key[salt_len+1] = 0;
  261. initial_key[salt_len+2] = 0;
  262. initial_key[salt_len+3] = 1;
  263. temp_result = utils->malloc(hash_size);
  264. /* U1 := HMAC(str, salt || INT(1)) */
  265. if (HMAC(md,
  266. (const unsigned char *) str,
  267. (int)str_len,
  268. (const unsigned char *) initial_key,
  269. (int)salt_len + 4,
  270. (unsigned char *)result,
  271. &hash_len) == NULL) {
  272. }
  273. memcpy(temp_result, result, hash_size);
  274. PRINT_HASH ("first HMAC in Hi()", temp_result, hash_size);
  275. /* On each loop iteration j "temp_result" contains Uj,
  276. while "result" contains "U1 XOR ... XOR Uj" */
  277. for (i = 2; i <= iteration_count; i++) {
  278. if (HMAC(md,
  279. (const unsigned char *) str,
  280. (int)str_len,
  281. (const unsigned char *) temp_result,
  282. hash_size,
  283. (unsigned char *)temp_result,
  284. &hash_len) == NULL) {
  285. }
  286. PRINT_HASH ("Hi() HMAC inside loop", temp_result, hash_size);
  287. for (k = 0; k < hash_size; k++) {
  288. result[k] ^= temp_result[k];
  289. }
  290. PRINT_HASH ("Hi() - accumulated result inside loop", result, hash_size);
  291. }
  292. utils->free(initial_key);
  293. utils->free(temp_result);
  294. }
  295. /**
  296. * User salt is Hi(username,salt_key);
  297. * This is fixed per reboot, to allow caching of SCRAM
  298. * SaltedPassword.
  299. */
  300. static unsigned char *
  301. scram_server_user_salt(const sasl_utils_t * utils,
  302. const EVP_MD *md,
  303. const char * username,
  304. size_t * p_salt_len)
  305. {
  306. size_t hash_size = EVP_MD_size(md);
  307. char * result = utils->malloc(hash_size);
  308. Hi(utils, md, username, strlen(username), (const char *) g_salt_key, SALT_SIZE,
  309. 20 /* iterations */, result);
  310. *p_salt_len = hash_size;
  311. return (unsigned char *) result;
  312. }
  313. static int
  314. GenerateScramSecrets (const sasl_utils_t * utils,
  315. const EVP_MD *md,
  316. const char * password,
  317. size_t password_len,
  318. char * salt,
  319. size_t salt_len,
  320. unsigned int iteration_count,
  321. char * StoredKey,
  322. char * ServerKey,
  323. char ** error_text)
  324. {
  325. char SaltedPassword[EVP_MAX_MD_SIZE];
  326. char ClientKey[EVP_MAX_MD_SIZE];
  327. sasl_secret_t *sec = NULL;
  328. unsigned int hash_len = 0;
  329. int result;
  330. size_t hash_size = EVP_MD_size(md);
  331. *error_text = NULL;
  332. if (password_len == 0) {
  333. *error_text = "empty secret";
  334. result = SASL_FAIL;
  335. goto cleanup;
  336. }
  337. sec = utils->malloc(sizeof(sasl_secret_t) + password_len);
  338. if (sec == NULL) {
  339. result = SASL_NOMEM;
  340. goto cleanup;
  341. }
  342. sec->len = (unsigned) password_len;
  343. strncpy((char *)sec->data, password, password_len + 1);
  344. /* SaltedPassword := Hi(password, salt) */
  345. Hi (utils,
  346. md,
  347. (const char *) sec->data,
  348. sec->len,
  349. salt,
  350. salt_len,
  351. iteration_count,
  352. SaltedPassword);
  353. /* ClientKey := HMAC(SaltedPassword, "Client Key") */
  354. if (HMAC(md,
  355. (const unsigned char *) SaltedPassword,
  356. hash_size,
  357. (const unsigned char *) CLIENT_KEY_CONSTANT,
  358. CLIENT_KEY_CONSTANT_LEN,
  359. (unsigned char *)ClientKey,
  360. &hash_len) == NULL) {
  361. *error_text = "HMAC call failed";
  362. result = SASL_SCRAM_INTERNAL;
  363. goto cleanup;
  364. }
  365. /* StoredKey := H(ClientKey) */
  366. if (EVP_Digest((const unsigned char *) ClientKey, hash_size,
  367. (unsigned char *) StoredKey, NULL, md, NULL) == 0) {
  368. *error_text = "Digest call failed";
  369. result = SASL_SCRAM_INTERNAL;
  370. goto cleanup;
  371. }
  372. /* ServerKey := HMAC(SaltedPassword, "Server Key") */
  373. if (HMAC(md,
  374. (const unsigned char *) SaltedPassword,
  375. hash_size,
  376. (const unsigned char *) SERVER_KEY_CONSTANT,
  377. SERVER_KEY_CONSTANT_LEN,
  378. (unsigned char *)ServerKey,
  379. &hash_len) == NULL) {
  380. *error_text = "HMAC call failed";
  381. result = SASL_SCRAM_INTERNAL;
  382. goto cleanup;
  383. }
  384. result = SASL_OK;
  385. cleanup:
  386. if (sec) {
  387. _plug_free_secret(utils, &sec);
  388. }
  389. return result;
  390. }
  391. /***************************** Server Section *****************************/
  392. typedef struct server_context {
  393. int state;
  394. const EVP_MD *md; /* underlying MDA */
  395. char * authentication_id;
  396. char * authorization_id;
  397. char * out_buf;
  398. unsigned out_buf_len;
  399. char * auth_message;
  400. size_t auth_message_len;
  401. char * nonce;
  402. /* in binary form */
  403. char * salt;
  404. size_t salt_len;
  405. unsigned int iteration_count;
  406. char StoredKey[EVP_MAX_MD_SIZE + 1];
  407. char ServerKey[EVP_MAX_MD_SIZE + 1];
  408. int cb_flags;
  409. char *cbindingname;
  410. char *gs2_header;
  411. size_t gs2_header_length;
  412. } server_context_t;
  413. static int
  414. scram_server_mech_new(void *glob_context,
  415. sasl_server_params_t *sparams,
  416. const char *challenge __attribute__((unused)),
  417. unsigned challen __attribute__((unused)),
  418. void **conn_context)
  419. {
  420. server_context_t *text;
  421. /* holds state are in */
  422. text = sparams->utils->malloc(sizeof(server_context_t));
  423. if (text == NULL) {
  424. MEMERROR( sparams->utils );
  425. return SASL_NOMEM;
  426. }
  427. memset(text, 0, sizeof(server_context_t));
  428. /* text->state = 0; */
  429. text->md = EVP_get_digestbyname((const char *) glob_context);
  430. *conn_context = text;
  431. return SASL_OK;
  432. }
  433. static int
  434. scram_server_mech_step1(server_context_t *text,
  435. sasl_server_params_t *sparams,
  436. const char *clientin,
  437. unsigned clientinlen,
  438. const char **serverout,
  439. unsigned *serveroutlen,
  440. sasl_out_params_t *oparams __attribute__((unused)))
  441. {
  442. char * authorization_id;
  443. char * authentication_id;
  444. char * p;
  445. char * nonce;
  446. size_t client_nonce_len;
  447. char * base64_salt = NULL;
  448. size_t base64len;
  449. size_t estimated_challenge_len;
  450. size_t pure_scram_length;
  451. char * inbuf = NULL;
  452. const char *password_request[] = { SASL_AUX_PASSWORD,
  453. "*authPassword",
  454. NULL };
  455. int canon_flags;
  456. struct propval auxprop_values[3];
  457. int result;
  458. size_t hash_size = EVP_MD_size(text->md);
  459. const char *scram_sasl_mech = scram_sasl_mech_name(hash_size);
  460. if (clientinlen == 0) {
  461. sparams->utils->seterror(sparams->utils->conn, 0,
  462. "%s input expected", scram_sasl_mech);
  463. return SASL_BADPROT;
  464. }
  465. /* Expecting: 'gs2-cbind-flag "," [ authzid ] "," [reserved-mext ","]
  466. username "," nonce ["," extensions]' */
  467. if (clientinlen < 10) {
  468. sparams->utils->seterror(sparams->utils->conn, 0,
  469. "Invalid %s input", scram_sasl_mech);
  470. return SASL_BADPROT;
  471. }
  472. inbuf = sparams->utils->malloc (clientinlen + 1);
  473. if (inbuf == NULL) {
  474. MEMERROR( sparams->utils );
  475. return SASL_NOMEM;
  476. }
  477. memcpy(inbuf, clientin, clientinlen);
  478. inbuf[clientinlen] = 0;
  479. if (strlen(inbuf) != clientinlen) {
  480. sparams->utils->seterror(sparams->utils->conn, 0,
  481. "NULs found in %s input", scram_sasl_mech);
  482. result = SASL_BADPROT;
  483. goto cleanup;
  484. }
  485. p = inbuf;
  486. /* gs2-cbind-flag = "p=" cb-name / "n" / "y"
  487. ;; "n" -> client doesn't support channel binding
  488. ;; "y" -> client does support channel binding
  489. ;; but thinks the server does not.
  490. ;; "p" -> client requires channel binding.
  491. ;; The selected channel binding follows "p=". */
  492. switch (p[0]) {
  493. case 'p':
  494. if (p[1] != '=') {
  495. sparams->utils->seterror(sparams->utils->conn, 0,
  496. "The initial 'p' needs to be followed by '=' in %s input",
  497. scram_sasl_mech);
  498. result = SASL_BADPROT;
  499. goto cleanup;
  500. }
  501. p++;
  502. text->cbindingname = p + 1;
  503. p = strchr (p, ',');
  504. if (p == NULL) {
  505. text->cbindingname = NULL;
  506. sparams->utils->seterror(sparams->utils->conn, 0,
  507. "Channel binding name must be terminated by a comma in %s input",
  508. scram_sasl_mech);
  509. result = SASL_BADPROT;
  510. goto cleanup;
  511. }
  512. *p = '\0';
  513. _plug_strdup(sparams->utils, text->cbindingname, &text->cbindingname, NULL);
  514. *p = ',';
  515. text->cb_flags = SCRAM_CB_FLAG_P;
  516. break;
  517. case 'n':
  518. text->cb_flags = SCRAM_CB_FLAG_N;
  519. /* We always have at least 10 bytes, so this is safe */
  520. p++;
  521. break;
  522. case 'y':
  523. text->cb_flags = SCRAM_CB_FLAG_Y;
  524. /* We always have at least 10 bytes, so this is safe */
  525. p++;
  526. break;
  527. default:
  528. sparams->utils->seterror(sparams->utils->conn, 0,
  529. "The initial %s client response needs to start with 'y', 'n' or 'p'",
  530. scram_sasl_mech);
  531. result = SASL_BADPROT;
  532. goto cleanup;
  533. }
  534. if (p[0] != ',') {
  535. sparams->utils->seterror(sparams->utils->conn, 0,
  536. "',' expected in %s input", scram_sasl_mech);
  537. result = SASL_BADPROT;
  538. goto cleanup;
  539. }
  540. p++;
  541. if (p[0] == 'a' && p[1] == '=') {
  542. authorization_id = p + 2;
  543. p = strchr (authorization_id, ',');
  544. if (p == NULL) {
  545. sparams->utils->seterror(sparams->utils->conn, 0,
  546. "At least nonce is expected in %s input",
  547. scram_sasl_mech);
  548. result = SASL_BADPROT;
  549. goto cleanup;
  550. }
  551. /* End of the GS2 header */
  552. p[0] = '\0';
  553. /* The GS2 header length DOES include the terminating comma */
  554. text->gs2_header_length = p - inbuf + 1;
  555. p++;
  556. /* Make a read-write copy we can modify */
  557. _plug_strdup(sparams->utils, authorization_id, &text->authorization_id, NULL);
  558. if (decode_saslname(text->authorization_id) != SASL_OK) {
  559. sparams->utils->seterror(sparams->utils->conn, 0,
  560. "Invalid authorization identity encoding in %s input",
  561. scram_sasl_mech);
  562. result = SASL_BADPROT;
  563. goto cleanup;
  564. }
  565. } else if (p[0] != ',') {
  566. sparams->utils->seterror(sparams->utils->conn, 0,
  567. "',' expected in %s input", scram_sasl_mech);
  568. result = SASL_BADPROT;
  569. goto cleanup;
  570. } else {
  571. /* End of the GS2 header */
  572. p[0] = '\0';
  573. /* The GS2 header length DOES include the terminating comma */
  574. text->gs2_header_length = p - inbuf + 1;
  575. p++;
  576. }
  577. text->gs2_header = sparams->utils->malloc (text->gs2_header_length + 1);
  578. if (text->gs2_header == NULL) {
  579. MEMERROR( sparams->utils );
  580. result = SASL_NOMEM;
  581. goto cleanup;
  582. }
  583. memcpy(text->gs2_header, inbuf, text->gs2_header_length - 1);
  584. /* Remember the comma */
  585. text->gs2_header[text->gs2_header_length - 1] = ',';
  586. text->gs2_header[text->gs2_header_length] = 0;
  587. if (p[1] != '=') {
  588. sparams->utils->seterror(sparams->utils->conn, 0,
  589. "Invalid %s input", scram_sasl_mech);
  590. result = SASL_BADPROT;
  591. goto cleanup;
  592. }
  593. if (p[0] == 'm') {
  594. sparams->utils->seterror(sparams->utils->conn, 0,
  595. "Unsupported mandatory extension to %s",
  596. scram_sasl_mech);
  597. result = SASL_BADPROT;
  598. goto cleanup;
  599. }
  600. if (p[0] != 'n') {
  601. sparams->utils->seterror(sparams->utils->conn, 0,
  602. "Username (n=) expected in %s input",
  603. scram_sasl_mech);
  604. result = SASL_BADPROT;
  605. goto cleanup;
  606. }
  607. authentication_id = p + 2;
  608. p = strchr (authentication_id, ',');
  609. /* MUST be followed by a nonce */
  610. if (p == NULL) {
  611. sparams->utils->seterror(sparams->utils->conn, 0,
  612. "Nonce expected after the username in %s input",
  613. scram_sasl_mech);
  614. result = SASL_BADPROT;
  615. goto cleanup;
  616. }
  617. *p = '\0';
  618. p++;
  619. if (decode_saslname(authentication_id) != SASL_OK) {
  620. sparams->utils->seterror(sparams->utils->conn, 0,
  621. "Invalid username encoding in %s input",
  622. scram_sasl_mech);
  623. result = SASL_BADPROT;
  624. goto cleanup;
  625. }
  626. _plug_strdup(sparams->utils, authentication_id, &text->authentication_id, NULL);
  627. if (strncmp(p, "r=", 2) != 0) {
  628. sparams->utils->seterror(sparams->utils->conn, 0,
  629. "Nonce expected after the username in %s input",
  630. scram_sasl_mech);
  631. result = SASL_BADPROT;
  632. goto cleanup;
  633. }
  634. p += 2;
  635. nonce = p;
  636. p = strchr (nonce, ',');
  637. if (p == NULL) {
  638. p = nonce + strlen(nonce);
  639. } else {
  640. *p = '\0';
  641. }
  642. /* Generate server nonce, by appending some random stuff to the client nonce */
  643. client_nonce_len = strlen(nonce);
  644. text->nonce = sparams->utils->malloc (client_nonce_len + NONCE_SIZE + 1);
  645. if (text->nonce == NULL) {
  646. MEMERROR( sparams->utils );
  647. result = SASL_NOMEM;
  648. goto cleanup;
  649. }
  650. strcpy (text->nonce, nonce);
  651. if (create_nonce(sparams->utils,
  652. text->nonce + client_nonce_len,
  653. NONCE_SIZE + 1) == NULL) {
  654. MEMERROR( sparams->utils );
  655. result = SASL_NOMEM;
  656. goto cleanup;
  657. }
  658. /* Now we fetch user's password and calculate our secret */
  659. result = sparams->utils->prop_request(sparams->propctx, password_request);
  660. if (result != SASL_OK) {
  661. goto cleanup;
  662. }
  663. /* this will trigger the getting of the aux properties */
  664. canon_flags = SASL_CU_AUTHID;
  665. if (text->authorization_id == NULL || *text->authorization_id == '\0') {
  666. canon_flags |= SASL_CU_AUTHZID;
  667. }
  668. result = sparams->canon_user(sparams->utils->conn,
  669. text->authentication_id,
  670. 0,
  671. canon_flags,
  672. oparams);
  673. if (result != SASL_OK) {
  674. SETERROR(sparams->utils, "unable to canonify user and get auxprops");
  675. goto cleanup;
  676. }
  677. if (text->authorization_id != NULL && *text->authorization_id != '\0') {
  678. result = sparams->canon_user(sparams->utils->conn,
  679. text->authorization_id,
  680. 0,
  681. SASL_CU_AUTHZID,
  682. oparams);
  683. }
  684. if (result != SASL_OK) {
  685. SETERROR(sparams->utils, "unable to canonify authorization ID");
  686. goto cleanup;
  687. }
  688. result = sparams->utils->prop_getnames(sparams->propctx,
  689. password_request,
  690. auxprop_values);
  691. if (result < 0 ||
  692. ((!auxprop_values[0].name || !auxprop_values[0].values) &&
  693. (!auxprop_values[1].name || !auxprop_values[1].values))) {
  694. /* We didn't find this username */
  695. sparams->utils->seterror(sparams->utils->conn,0,
  696. "no secret in database");
  697. result = sparams->transition ? SASL_TRANS : SASL_NOUSER;
  698. goto cleanup;
  699. }
  700. if (auxprop_values[0].name && auxprop_values[0].values) {
  701. char * error_text = NULL;
  702. char * s_iteration_count;
  703. char * end;
  704. text->salt = (char *) scram_server_user_salt(sparams->utils, text->md, text->authentication_id, &text->salt_len);
  705. sparams->utils->getopt(sparams->utils->getopt_context,
  706. /* Different SCRAM hashes can have different strengh */
  707. scram_sasl_mech,
  708. "scram_iteration_counter",
  709. (const char **) &s_iteration_count,
  710. NULL);
  711. if (s_iteration_count != NULL) {
  712. errno = 0;
  713. text->iteration_count = strtoul(s_iteration_count, &end, 10);
  714. if (s_iteration_count == end || *end != '\0' || errno != 0) {
  715. sparams->utils->log(NULL,
  716. SASL_LOG_DEBUG,
  717. "Invalid iteration-count in scram_iteration_count SASL option: not a number. Using the default instead.");
  718. s_iteration_count = NULL;
  719. }
  720. }
  721. if (s_iteration_count == NULL) {
  722. text->iteration_count = DEFAULT_ITERATION_COUNTER;
  723. }
  724. result = GenerateScramSecrets (sparams->utils,
  725. text->md,
  726. auxprop_values[0].values[0],
  727. strlen(auxprop_values[0].values[0]),
  728. text->salt,
  729. text->salt_len,
  730. text->iteration_count,
  731. text->StoredKey,
  732. text->ServerKey,
  733. &error_text);
  734. if (result != SASL_OK) {
  735. if (error_text != NULL) {
  736. sparams->utils->seterror(sparams->utils->conn, 0, "%s",
  737. error_text);
  738. }
  739. goto cleanup;
  740. }
  741. } else if (auxprop_values[1].name && auxprop_values[1].values) {
  742. char s_iteration_count[ITERATION_COUNTER_BUF_LEN+1];
  743. size_t base64_salt_len;
  744. unsigned int exact_key_len;
  745. const char * scram_hash;
  746. const char * p_field;
  747. char * end;
  748. int i;
  749. size_t scram_sasl_mech_len = strlen(scram_sasl_mech);
  750. result = SASL_SCRAM_INTERNAL;
  751. for (i = 0; auxprop_values[1].values[i] != NULL; i++) {
  752. scram_hash = auxprop_values[1].values[i];
  753. /* Skip the leading spaces */
  754. while (*scram_hash == ' ') {
  755. scram_hash++;
  756. }
  757. if (strncmp(scram_hash, scram_sasl_mech, scram_sasl_mech_len) != 0) {
  758. continue;
  759. }
  760. scram_hash += scram_sasl_mech_len;
  761. /* Skip spaces */
  762. while (*scram_hash == ' ') {
  763. scram_hash++;
  764. }
  765. if (*scram_hash != '$') {
  766. /* syntax error, ignore the value */
  767. continue;
  768. }
  769. scram_hash++;
  770. /* Skip spaces */
  771. while (*scram_hash == ' ') {
  772. scram_hash++;
  773. }
  774. p_field = strchr(scram_hash, ':');
  775. if (p_field == NULL || p_field == scram_hash) {
  776. /* syntax error, ignore the value */
  777. continue;
  778. }
  779. if ((p_field - scram_hash) > ITERATION_COUNTER_BUF_LEN) {
  780. /* The iteration counter is too big for us */
  781. sparams->utils->seterror(sparams->utils->conn, 0,
  782. "Invalid iteration-count in %s input: the value is too big",
  783. scram_sasl_mech);
  784. continue;
  785. }
  786. memcpy(s_iteration_count, scram_hash, p_field - scram_hash);
  787. s_iteration_count[p_field - scram_hash] = '\0';
  788. errno = 0;
  789. text->iteration_count = strtoul(s_iteration_count, &end, 10);
  790. if (s_iteration_count == end || *end != '\0' || errno != 0) {
  791. sparams->utils->seterror(sparams->utils->conn, 0,
  792. "Invalid iteration-count in %s input: not a number",
  793. scram_sasl_mech);
  794. continue;
  795. }
  796. scram_hash = p_field + 1;
  797. p_field = scram_hash + strcspn(scram_hash, "$ ");
  798. if (p_field == scram_hash || *p_field == '\0') {
  799. /* syntax error, ignore the value */
  800. continue;
  801. }
  802. base64_salt_len = p_field - scram_hash;
  803. text->salt = (char *) sparams->utils->malloc(base64_salt_len);
  804. if (sparams->utils->decode64(scram_hash,
  805. (unsigned int)base64_salt_len,
  806. text->salt,
  807. (unsigned int)base64_salt_len,
  808. (unsigned int *) &text->salt_len) != SASL_OK) {
  809. sparams->utils->seterror(sparams->utils->conn, 0,
  810. "Invalid base64 encoding of the salt in %s stored value",
  811. scram_sasl_mech);
  812. continue;
  813. }
  814. scram_hash = p_field;
  815. /* Skip spaces */
  816. while (*scram_hash == ' ') {
  817. scram_hash++;
  818. }
  819. if (*scram_hash != '$') {
  820. /* syntax error, ignore the value */
  821. sparams->utils->free(text->salt);
  822. text->salt = NULL;
  823. continue;
  824. }
  825. scram_hash++;
  826. /* Skip spaces */
  827. while (*scram_hash == ' ') {
  828. scram_hash++;
  829. }
  830. p_field = strchr(scram_hash, ':');
  831. if (p_field == NULL || p_field == scram_hash) {
  832. /* syntax error, ignore the value */
  833. sparams->utils->free(text->salt);
  834. text->salt = NULL;
  835. continue;
  836. }
  837. if (sparams->utils->decode64(scram_hash,
  838. (unsigned int)(p_field - scram_hash),
  839. text->StoredKey,
  840. hash_size + 1,
  841. &exact_key_len) != SASL_OK) {
  842. sparams->utils->seterror(sparams->utils->conn, 0,
  843. "Invalid base64 encoding of StoredKey in %s per-user storage",
  844. scram_sasl_mech);
  845. sparams->utils->free(text->salt);
  846. text->salt = NULL;
  847. continue;
  848. }
  849. if (exact_key_len != hash_size) {
  850. sparams->utils->seterror(sparams->utils->conn, 0,
  851. "Invalid StoredKey in %s per-user storage",
  852. scram_sasl_mech);
  853. sparams->utils->free(text->salt);
  854. text->salt = NULL;
  855. continue;
  856. }
  857. scram_hash = p_field + 1;
  858. p_field = strchr(scram_hash, ' ');
  859. if (p_field == NULL) {
  860. p_field = scram_hash + strlen(scram_hash);
  861. }
  862. if (sparams->utils->decode64(scram_hash,
  863. (unsigned int)(p_field - scram_hash),
  864. text->ServerKey,
  865. hash_size + 1,
  866. &exact_key_len) != SASL_OK) {
  867. sparams->utils->seterror(sparams->utils->conn, 0,
  868. "Invalid base64 encoding of ServerKey in %s per-user storage",
  869. scram_sasl_mech);
  870. sparams->utils->free(text->salt);
  871. text->salt = NULL;
  872. continue;
  873. }
  874. if (exact_key_len != hash_size) {
  875. sparams->utils->seterror(sparams->utils->conn, 0,
  876. "Invalid ServerKey in %s per-user storage", scram_sasl_mech);
  877. sparams->utils->free(text->salt);
  878. text->salt = NULL;
  879. continue;
  880. }
  881. result = SASL_OK;
  882. break;
  883. }
  884. if (result != SASL_OK) {
  885. sparams->utils->seterror(sparams->utils->conn,
  886. 0, "No valid %s secret found",
  887. scram_sasl_mech);
  888. goto cleanup;
  889. }
  890. } else {
  891. sparams->utils->seterror(sparams->utils->conn,
  892. 0,
  893. "Have neither type of secret");
  894. return SASL_FAIL;
  895. }
  896. /* erase the plaintext password */
  897. sparams->utils->prop_erase(sparams->propctx, password_request[0]);
  898. /* base 64 encode it so it has valid chars */
  899. base64len = (text->salt_len / 3 * 4) + ((text->salt_len % 3) ? 4 : 0);
  900. base64_salt = (char *) sparams->utils->malloc(base64len + 1);
  901. if (base64_salt == NULL) {
  902. MEMERROR( sparams->utils );
  903. result = SASL_NOMEM;
  904. goto cleanup;
  905. }
  906. /*
  907. * Returns SASL_OK on success, SASL_BUFOVER if result won't fit
  908. */
  909. if (sparams->utils->encode64(text->salt,
  910. (unsigned int)text->salt_len,
  911. base64_salt,
  912. (unsigned int)base64len + 1,
  913. NULL) != SASL_OK) {
  914. MEMERROR( sparams->utils );
  915. result = SASL_NOMEM;
  916. goto cleanup;
  917. }
  918. base64_salt[base64len] = '\0';
  919. /* Now we generate server challenge */
  920. estimated_challenge_len = client_nonce_len + NONCE_SIZE +
  921. base64len +
  922. ITERATION_COUNTER_BUF_LEN +
  923. strlen("r=,s=,i=");
  924. result = _plug_buf_alloc(sparams->utils,
  925. &(text->out_buf),
  926. &(text->out_buf_len),
  927. (unsigned) estimated_challenge_len + 1);
  928. if (result != SASL_OK) {
  929. MEMERROR( sparams->utils );
  930. result = SASL_NOMEM;
  931. goto cleanup;
  932. }
  933. sprintf(text->out_buf,
  934. "r=%s,s=%s,i=%u",
  935. text->nonce,
  936. base64_salt,
  937. text->iteration_count);
  938. /* Save the (client response, ",", server challenge, ",").
  939. Note, we skip the GS2 prefix here */
  940. pure_scram_length = clientinlen - text->gs2_header_length;
  941. text->auth_message_len = pure_scram_length + 1 + estimated_challenge_len + 1;
  942. text->auth_message = sparams->utils->malloc (text->auth_message_len + 1);
  943. if (text->auth_message == NULL) {
  944. MEMERROR( sparams->utils );
  945. result = SASL_NOMEM;
  946. goto cleanup;
  947. }
  948. memcpy(text->auth_message, clientin + text->gs2_header_length, pure_scram_length);
  949. text->auth_message[pure_scram_length] = ',';
  950. strcpy (text->auth_message + pure_scram_length + 1, text->out_buf);
  951. strcat (text->auth_message + pure_scram_length + 1, ",");
  952. /* Now remember the exact length, not the estimated one */
  953. text->auth_message_len = strlen(text->auth_message);
  954. *serverout = text->out_buf;
  955. *serveroutlen = (unsigned) strlen(text->out_buf);
  956. result = SASL_CONTINUE;
  957. text->state = 2;
  958. cleanup:
  959. if (inbuf != NULL) {
  960. sparams->utils->free(inbuf);
  961. }
  962. if (base64_salt != NULL) {
  963. sparams->utils->free(base64_salt);
  964. }
  965. return result;
  966. }
  967. static int
  968. scram_server_mech_step2(server_context_t *text,
  969. sasl_server_params_t *sparams,
  970. const char *clientin,
  971. unsigned clientinlen,
  972. const char **serverout,
  973. unsigned *serveroutlen,
  974. sasl_out_params_t *oparams)
  975. {
  976. char *channel_binding = NULL;
  977. size_t channel_binding_len = 0;
  978. char *binary_channel_binding = NULL;
  979. unsigned binary_channel_binding_len = 0;
  980. char *client_proof = NULL;
  981. char *inbuf = NULL;
  982. char *p;
  983. int result = SASL_FAIL;
  984. size_t proof_offset;
  985. char * full_auth_message;
  986. char ReceivedClientKey[EVP_MAX_MD_SIZE];
  987. char DecodedClientProof[EVP_MAX_MD_SIZE + 1];
  988. char CalculatedStoredKey[EVP_MAX_MD_SIZE];
  989. char ClientSignature[EVP_MAX_MD_SIZE];
  990. char ServerSignature[EVP_MAX_MD_SIZE];
  991. char * nonce;
  992. size_t client_proof_len;
  993. size_t server_proof_len;
  994. unsigned exact_client_proof_len;
  995. unsigned int hash_len = 0;
  996. size_t k, hash_size = EVP_MD_size(text->md);
  997. const char *scram_sasl_mech = scram_sasl_mech_name(hash_size);
  998. if (clientinlen == 0) {
  999. sparams->utils->seterror(sparams->utils->conn, 0,
  1000. "%s input expected", scram_sasl_mech);
  1001. return SASL_BADPROT;
  1002. }
  1003. if (clientinlen < 3 || clientin[1] != '=') {
  1004. sparams->utils->seterror(sparams->utils->conn, 0,
  1005. "Invalid %s input", scram_sasl_mech);
  1006. return SASL_BADPROT;
  1007. }
  1008. inbuf = sparams->utils->malloc (clientinlen + 1);
  1009. if (inbuf == NULL) {
  1010. MEMERROR( sparams->utils );
  1011. return SASL_NOMEM;
  1012. }
  1013. memcpy(inbuf, clientin, clientinlen);
  1014. inbuf[clientinlen] = 0;
  1015. if (strlen(inbuf) != clientinlen) {
  1016. sparams->utils->seterror(sparams->utils->conn, 0,
  1017. "NULs found in %s input", scram_sasl_mech);
  1018. result = SASL_BADPROT;
  1019. goto cleanup;
  1020. }
  1021. /* Expecting: channel-binding "," nonce ["," extensions] "," proof */
  1022. p = inbuf;
  1023. if (strncmp(p, "c=", 2) != 0) {
  1024. sparams->utils->seterror(sparams->utils->conn, 0,
  1025. "Channel binding expected in %s input",
  1026. scram_sasl_mech);
  1027. result = SASL_BADPROT;
  1028. goto cleanup;
  1029. }
  1030. channel_binding = p + 2;
  1031. p = strchr (channel_binding, ',');
  1032. if (p == NULL) {
  1033. sparams->utils->seterror(sparams->utils->conn, 0,
  1034. "At least nonce is expected in %s input",
  1035. scram_sasl_mech);
  1036. result = SASL_BADPROT;
  1037. goto cleanup;
  1038. }
  1039. *p = '\0';
  1040. p++;
  1041. channel_binding_len = strlen(channel_binding);
  1042. /* We can calculate the exact length, but the decoded (binary) data
  1043. is always shorter than its base64 version. */
  1044. binary_channel_binding = (char *) sparams->utils->malloc(channel_binding_len + 1);
  1045. if (sparams->utils->decode64(channel_binding,
  1046. (unsigned int)channel_binding_len,
  1047. binary_channel_binding,
  1048. (unsigned int)channel_binding_len,
  1049. &binary_channel_binding_len) != SASL_OK) {
  1050. sparams->utils->seterror(sparams->utils->conn, 0,
  1051. "Invalid base64 encoding of the channel bindings in %s",
  1052. scram_sasl_mech);
  1053. result = SASL_BADPROT;
  1054. goto cleanup;
  1055. }
  1056. if (binary_channel_binding_len < text->gs2_header_length ||
  1057. strncmp(binary_channel_binding, text->gs2_header, text->gs2_header_length) != 0) {
  1058. sparams->utils->seterror (sparams->utils->conn,
  1059. 0,
  1060. "Channel bindings prefix doesn't match the one received in the GS2 header of %s. Expected \"%s\"",
  1061. scram_sasl_mech, text->gs2_header);
  1062. result = SASL_BADPROT;
  1063. goto cleanup;
  1064. }
  1065. switch (text->cb_flags & SCRAM_CB_FLAG_MASK) {
  1066. case SCRAM_CB_FLAG_P:
  1067. binary_channel_binding_len -= (unsigned)text->gs2_header_length;
  1068. if (binary_channel_binding_len == 0) {
  1069. sparams->utils->seterror(sparams->utils->conn, 0,
  1070. "Channel bindings data expected in %s",
  1071. scram_sasl_mech);
  1072. result = SASL_BADPROT;
  1073. goto cleanup;
  1074. }
  1075. if (sparams->cbinding == NULL) {
  1076. sparams->utils->seterror (sparams->utils->conn,
  1077. 0,
  1078. "Server does not support channel binding type received in %s. Received: %s",
  1079. scram_sasl_mech,
  1080. text->cbindingname);
  1081. result = SASL_BADPROT;
  1082. goto cleanup;
  1083. }
  1084. if (strcmp(sparams->cbinding->name, text->cbindingname) != 0) {
  1085. sparams->utils->seterror (sparams->utils->conn,
  1086. 0,
  1087. "Unsupported channel bindings type received in %s. Expected: %s, received: %s",
  1088. scram_sasl_mech,
  1089. sparams->cbinding->name,
  1090. text->cbindingname);
  1091. result = SASL_BADPROT;
  1092. goto cleanup;
  1093. }
  1094. if (binary_channel_binding_len != sparams->cbinding->len) {
  1095. sparams->utils->seterror (sparams->utils->conn,
  1096. 0,
  1097. "Unsupported channel bindings length received in %s. Expected length: %lu, received: %d",
  1098. scram_sasl_mech,
  1099. sparams->cbinding->len,
  1100. binary_channel_binding_len);
  1101. result = SASL_BADPROT;
  1102. goto cleanup;
  1103. }
  1104. if (memcmp(binary_channel_binding + text->gs2_header_length,
  1105. sparams->cbinding->data,
  1106. binary_channel_binding_len) != 0) {
  1107. sparams->utils->seterror(sparams->utils->conn, 0,
  1108. "Channel bindings mismatch in %s",
  1109. scram_sasl_mech);
  1110. result = SASL_BADPROT;
  1111. goto cleanup;
  1112. }
  1113. break;
  1114. }
  1115. if (strncmp(p, "r=", 2) != 0) {
  1116. sparams->utils->seterror(sparams->utils->conn, 0,
  1117. "Nonce expected in %s input",
  1118. scram_sasl_mech);
  1119. result = SASL_BADPROT;
  1120. goto cleanup;
  1121. }
  1122. nonce = p + 2;
  1123. p = strchr (nonce, ',');
  1124. if (p == NULL) {
  1125. sparams->utils->seterror(sparams->utils->conn, 0,
  1126. "At least proof is expected in %s input",
  1127. scram_sasl_mech);
  1128. result = SASL_BADPROT;
  1129. goto cleanup;
  1130. }
  1131. *p = '\0';
  1132. p++;
  1133. if (strcmp(nonce, text->nonce) != 0) {
  1134. sparams->utils->seterror(sparams->utils->conn, 0,
  1135. "Nonce mismatch %s input",
  1136. scram_sasl_mech);
  1137. result = SASL_BADPROT;
  1138. goto cleanup;
  1139. }
  1140. while (p[0] != '\0') {
  1141. if (strncmp(p, "p=", 2) == 0) {
  1142. client_proof = p + 2;
  1143. proof_offset = p - inbuf - 1;
  1144. break;
  1145. }
  1146. p = strchr (p, ',');
  1147. if (p == NULL) {
  1148. break;
  1149. }
  1150. p++;
  1151. }
  1152. if (client_proof == NULL) {
  1153. sparams->utils->seterror(sparams->utils->conn, 0,
  1154. "Client proof is expected in %s input",
  1155. scram_sasl_mech);
  1156. result = SASL_BADPROT;
  1157. goto cleanup;
  1158. }
  1159. /* Check that no extension data exists after the proof */
  1160. p = strchr (client_proof, ',');
  1161. if (p != NULL) {
  1162. sparams->utils->seterror(sparams->utils->conn, 0,
  1163. "No extension data is allowed after the client proof in %s input",
  1164. scram_sasl_mech);
  1165. result = SASL_BADPROT;
  1166. goto cleanup;
  1167. }
  1168. if (strlen(client_proof) != (hash_size / 3 * 4 + (hash_size % 3 ? 4 : 0))) {
  1169. sparams->utils->seterror(sparams->utils->conn, 0,
  1170. "Invalid client proof length in %s input",
  1171. scram_sasl_mech);
  1172. result = SASL_BADPROT;
  1173. goto cleanup;
  1174. }
  1175. /* Construct the full AuthMessage */
  1176. full_auth_message = sparams->utils->realloc(text->auth_message,
  1177. text->auth_message_len + proof_offset + 1);
  1178. if (full_auth_message == NULL) {
  1179. MEMERROR( sparams->utils );
  1180. result = SASL_NOMEM;
  1181. goto cleanup;
  1182. }
  1183. text->auth_message = full_auth_message;
  1184. memcpy(text->auth_message + text->auth_message_len, clientin, proof_offset);
  1185. text->auth_message_len += proof_offset;
  1186. text->auth_message[text->auth_message_len] = '\0';
  1187. /* ClientSignature := HMAC(StoredKey, AuthMessage) */
  1188. if (HMAC(text->md,
  1189. (const unsigned char *) text->StoredKey,
  1190. hash_size,
  1191. (const unsigned char *)text->auth_message,
  1192. (int)text->auth_message_len,
  1193. (unsigned char *)ClientSignature,
  1194. &hash_len) == NULL) {
  1195. sparams->utils->seterror(sparams->utils->conn, 0,
  1196. "HMAC-%s call failed", scram_sasl_mech+6);
  1197. result = SASL_SCRAM_INTERNAL;
  1198. goto cleanup;
  1199. }
  1200. client_proof_len = strlen(client_proof);
  1201. if (sparams->utils->decode64(client_proof,
  1202. (unsigned int)client_proof_len,
  1203. DecodedClientProof,
  1204. hash_size + 1,
  1205. &exact_client_proof_len) != SASL_OK) {
  1206. sparams->utils->seterror(sparams->utils->conn, 0,
  1207. "Invalid base64 encoding of the client proof in %s input",
  1208. scram_sasl_mech);
  1209. result = SASL_BADPROT;
  1210. goto cleanup;
  1211. }
  1212. if (exact_client_proof_len != hash_size) {
  1213. sparams->utils->seterror(sparams->utils->conn, 0,
  1214. "Invalid client proof (truncated) in %s input",
  1215. scram_sasl_mech);
  1216. result = SASL_BADPROT;
  1217. goto cleanup;
  1218. }
  1219. for (k = 0; k < hash_size; k++) {
  1220. ReceivedClientKey[k] = DecodedClientProof[k] ^ ClientSignature[k];
  1221. }
  1222. /* StoredKey := H(ClientKey) */
  1223. if (EVP_Digest((const unsigned char *) ReceivedClientKey, hash_size,
  1224. (unsigned char *) CalculatedStoredKey, NULL, text->md, NULL) == 0) {
  1225. sparams->utils->seterror(sparams->utils->conn,0,
  1226. "%s call failed", scram_sasl_mech+6);
  1227. result = SASL_SCRAM_INTERNAL;
  1228. goto cleanup;
  1229. }
  1230. for (k = 0; k < hash_size; k++) {
  1231. if (CalculatedStoredKey[k] != text->StoredKey[k]) {
  1232. SETERROR(sparams->utils, "StoredKey mismatch");
  1233. result = SASL_BADAUTH;
  1234. goto cleanup;
  1235. }
  1236. }
  1237. /* ServerSignature := HMAC(ServerKey, AuthMessage) */
  1238. if (HMAC(text->md,
  1239. (const unsigned char *) text->ServerKey,
  1240. hash_size,
  1241. (unsigned char *) text->auth_message,
  1242. (int)text->auth_message_len,
  1243. (unsigned char *)ServerSignature,
  1244. &hash_len) == NULL) {
  1245. sparams->utils->seterror(sparams->utils->conn,0,
  1246. "HMAC-%s call failed", scram_sasl_mech+6);
  1247. result = SASL_SCRAM_INTERNAL;
  1248. goto cleanup;
  1249. }
  1250. server_proof_len = (hash_size / 3 * 4 + (hash_size % 3 ? 4 : 0));
  1251. result = _plug_buf_alloc(sparams->utils,
  1252. &(text->out_buf),
  1253. &(text->out_buf_len),
  1254. (unsigned) server_proof_len + strlen("v=") + 1);
  1255. if (result != SASL_OK) {
  1256. MEMERROR( sparams->utils );
  1257. result = SASL_NOMEM;
  1258. goto cleanup;
  1259. }
  1260. text->out_buf[0] = 'v';
  1261. text->out_buf[1] = '=';
  1262. if (sparams->utils->encode64(ServerSignature,
  1263. hash_size,
  1264. text->out_buf+2,
  1265. (unsigned int)server_proof_len + 1,
  1266. NULL) != SASL_OK) {
  1267. SETERROR(sparams->utils, "Internal error");
  1268. /* This is not quite right, but better than alternatives */
  1269. result = SASL_NOMEM;
  1270. goto cleanup;
  1271. }
  1272. text->out_buf[server_proof_len + 2] = '\0';
  1273. *serverout = text->out_buf;
  1274. *serveroutlen = (unsigned) strlen(text->out_buf);
  1275. /* set oparams */
  1276. switch (text->cb_flags & SCRAM_CB_FLAG_MASK) {
  1277. case SCRAM_CB_FLAG_N:
  1278. oparams->cbindingdisp = SASL_CB_DISP_NONE;
  1279. break;
  1280. case SCRAM_CB_FLAG_P:
  1281. oparams->cbindingdisp = SASL_CB_DISP_USED;
  1282. oparams->cbindingname = text->cbindingname;
  1283. break;
  1284. case SCRAM_CB_FLAG_Y:
  1285. oparams->cbindingdisp = SASL_CB_DISP_WANT;
  1286. break;
  1287. }
  1288. oparams->doneflag = 1;
  1289. oparams->mech_ssf = 0;
  1290. oparams->maxoutbuf = 0;
  1291. oparams->encode_context = NULL;
  1292. oparams->encode = NULL;
  1293. oparams->decode_context = NULL;
  1294. oparams->decode = NULL;
  1295. oparams->param_version = 0;
  1296. result = SASL_OK;
  1297. cleanup:
  1298. if (inbuf != NULL) {
  1299. sparams->utils->free(inbuf);
  1300. }
  1301. if (binary_channel_binding != NULL) {
  1302. sparams->utils->free(binary_channel_binding);
  1303. }
  1304. return result;
  1305. }
  1306. static int scram_server_mech_step(void *conn_context,
  1307. sasl_server_params_t *sparams,
  1308. const char *clientin,
  1309. unsigned clientinlen,
  1310. const char **serverout,
  1311. unsigned *serveroutlen,
  1312. sasl_out_params_t *oparams)
  1313. {
  1314. server_context_t *text = (server_context_t *) conn_context;
  1315. const char *scram_sasl_mech = NULL;
  1316. *serverout = NULL;
  1317. *serveroutlen = 0;
  1318. if (text == NULL) {
  1319. return SASL_BADPROT;
  1320. }
  1321. scram_sasl_mech = scram_sasl_mech_name(EVP_MD_size(text->md));
  1322. /* this should be well more than is ever needed */
  1323. if (clientinlen > MAX_CLIENTIN_LEN) {
  1324. sparams->utils->seterror(sparams->utils->conn, 0,
  1325. "%s input longer than "
  1326. STRINGIZE((MAX_CLIENTIN_LEN)) " bytes",
  1327. scram_sasl_mech);
  1328. return SASL_BADPROT;
  1329. }
  1330. switch (text->state) {
  1331. case 0:
  1332. text->state++;
  1333. /* Assume the protocol doesn't support initial client response */
  1334. if (clientinlen == 0) {
  1335. return SASL_CONTINUE;
  1336. }
  1337. /* fall through */
  1338. case 1:
  1339. return scram_server_mech_step1(text,
  1340. sparams,
  1341. clientin,
  1342. clientinlen,
  1343. serverout,
  1344. serveroutlen,
  1345. oparams);
  1346. case 2:
  1347. text->state++;
  1348. return scram_server_mech_step2(text,
  1349. sparams,
  1350. clientin,
  1351. clientinlen,
  1352. serverout,
  1353. serveroutlen,
  1354. oparams);
  1355. default: /* should never get here */
  1356. sparams->utils->log(NULL, SASL_LOG_ERR,
  1357. "Invalid %s server step %d\n",
  1358. scram_sasl_mech, text->state);
  1359. return SASL_FAIL;
  1360. }
  1361. return SASL_FAIL; /* should never get here */
  1362. }
  1363. static int scram_setpass(void *glob_context,
  1364. sasl_server_params_t *sparams,
  1365. const char *userstr,
  1366. const char *pass,
  1367. unsigned passlen,
  1368. const char *oldpass __attribute__((unused)),
  1369. unsigned oldpasslen __attribute__((unused)),
  1370. unsigned flags)
  1371. {
  1372. int r;
  1373. char *user = NULL;
  1374. char *user_only = NULL;
  1375. char *realm = NULL;
  1376. sasl_secret_t *sec = NULL;
  1377. struct propctx *propctx = NULL;
  1378. const char *store_request[] = { "authPassword",
  1379. NULL };
  1380. const char *generate_scram_secret;
  1381. const EVP_MD *md = EVP_get_digestbyname((const char *) glob_context);
  1382. size_t hash_size = EVP_MD_size(md);
  1383. const char *scram_sasl_mech = scram_sasl_mech_name(hash_size);
  1384. /* Do we have a backend that can store properties? */
  1385. if (!sparams->utils->auxprop_store ||
  1386. sparams->utils->auxprop_store(NULL, NULL, NULL) != SASL_OK) {
  1387. sparams->utils->seterror(sparams->utils->conn, 0,
  1388. "%s: auxprop backend can't store properties",
  1389. scram_sasl_mech);
  1390. return SASL_NOMECH;
  1391. }
  1392. sparams->utils->getopt(sparams->utils->getopt_context,
  1393. /* This affects all SCRAM plugins, not just SCRAM-SHA-1 */
  1394. "SCRAM",
  1395. "scram_secret_generate",
  1396. &generate_scram_secret,
  1397. NULL);
  1398. /* NOTE: The default (when this option is not set) is NOT to generate authPassword secret */
  1399. if (!(generate_scram_secret &&
  1400. (generate_scram_secret[0] == '1' || generate_scram_secret[0] == 'y' ||
  1401. (generate_scram_secret[0] == 'o' && generate_scram_secret[1] == 'n') ||
  1402. generate_scram_secret[0] == 't'))) {
  1403. /* Pretend that everything is Ok, no need to generate noise in the logs */
  1404. return SASL_OK;
  1405. }
  1406. r = _plug_parseuser(sparams->utils,
  1407. &user_only,
  1408. &realm,
  1409. sparams->user_realm,
  1410. sparams->serverFQDN,
  1411. userstr);
  1412. if (r) {
  1413. sparams->utils->seterror(sparams->utils->conn, 0,
  1414. "%s: Error parsing user", scram_sasl_mech);
  1415. return r;
  1416. }
  1417. r = _plug_make_fulluser(sparams->utils, &user, user_only, realm);
  1418. if (r) {
  1419. goto cleanup;
  1420. }
  1421. if ((flags & SASL_SET_DISABLE) || pass == NULL) {
  1422. sec = NULL;
  1423. } else {
  1424. char * error_text = NULL;
  1425. char salt[SALT_SIZE + 1];
  1426. char base64_salt[BASE64_LEN(SALT_SIZE) + 1];
  1427. /* size_t salt_len = SALT_SIZE; */
  1428. char StoredKey[EVP_MAX_MD_SIZE + 1];
  1429. char ServerKey[EVP_MAX_MD_SIZE + 1];
  1430. char base64_StoredKey[BASE64_LEN(EVP_MAX_MD_SIZE) + 1];
  1431. char base64_ServerKey[BASE64_LEN(EVP_MAX_MD_SIZE) + 1];
  1432. size_t secret_len;
  1433. unsigned int iteration_count = DEFAULT_ITERATION_COUNTER;
  1434. char * s_iteration_count;
  1435. char * end;
  1436. sparams->utils->getopt(sparams->utils->getopt_context,
  1437. /* Different SCRAM hashes can have different strengh */
  1438. scram_sasl_mech,
  1439. "scram_iteration_counter",
  1440. (const char **) &s_iteration_count,
  1441. NULL);
  1442. if (s_iteration_count != NULL) {
  1443. errno = 0;
  1444. iteration_count = strtoul(s_iteration_count, &end, 10);
  1445. if (s_iteration_count == end || *end != '\0' || errno != 0) {
  1446. sparams->utils->log(NULL,
  1447. SASL_LOG_DEBUG,
  1448. "Invalid iteration-count in scram_iteration_count SASL option: not a number. Using the default instead.");
  1449. s_iteration_count = NULL;
  1450. }
  1451. }
  1452. if (s_iteration_count == NULL) {
  1453. iteration_count = DEFAULT_ITERATION_COUNTER;
  1454. }
  1455. sparams->utils->rand(sparams->utils->rpool, salt, SALT_SIZE);
  1456. r = GenerateScramSecrets (sparams->utils,
  1457. md,
  1458. pass,
  1459. passlen,
  1460. salt,
  1461. SALT_SIZE,
  1462. iteration_count,
  1463. StoredKey,
  1464. ServerKey,
  1465. &error_text);
  1466. if (r != SASL_OK) {
  1467. if (error_text != NULL) {
  1468. sparams->utils->seterror(sparams->utils->conn, 0, "%s",
  1469. error_text);
  1470. }
  1471. goto cleanup;
  1472. }
  1473. /* Returns SASL_OK on success, SASL_BUFOVER if result won't fit */
  1474. if (sparams->utils->encode64(salt,
  1475. SALT_SIZE,
  1476. base64_salt,
  1477. BASE64_LEN(SALT_SIZE) + 1,
  1478. NULL) != SASL_OK) {
  1479. MEMERROR( sparams->utils );
  1480. r = SASL_NOMEM;
  1481. goto cleanup;
  1482. }
  1483. base64_salt[BASE64_LEN(SALT_SIZE)] = '\0';
  1484. /* Returns SASL_OK on success, SASL_BUFOVER if result won't fit */
  1485. if (sparams->utils->encode64(StoredKey,
  1486. hash_size,
  1487. base64_StoredKey,
  1488. BASE64_LEN(hash_size) + 1,
  1489. NULL) != SASL_OK) {
  1490. MEMERROR( sparams->utils );
  1491. r = SASL_NOMEM;
  1492. goto cleanup;
  1493. }
  1494. base64_StoredKey[BASE64_LEN(hash_size)] = '\0';
  1495. /* Returns SASL_OK on success, SASL_BUFOVER if result won't fit */
  1496. if (sparams->utils->encode64(ServerKey,
  1497. hash_size,
  1498. base64_ServerKey,
  1499. BASE64_LEN(hash_size) + 1,
  1500. NULL) != SASL_OK) {
  1501. MEMERROR( sparams->utils );
  1502. r = SASL_NOMEM;
  1503. goto cleanup;
  1504. }
  1505. base64_ServerKey[BASE64_LEN(hash_size)] = '\0';
  1506. secret_len = strlen(scram_sasl_mech) + strlen("$:$:") +
  1507. ITERATION_COUNTER_BUF_LEN +
  1508. sizeof(base64_salt) +
  1509. sizeof(base64_StoredKey) +
  1510. sizeof(base64_ServerKey);
  1511. sec = sparams->utils->malloc(sizeof(sasl_secret_t) + secret_len);
  1512. if (sec == NULL) {
  1513. MEMERROR( sparams->utils );
  1514. r = SASL_NOMEM;
  1515. goto cleanup;
  1516. }
  1517. sprintf((char *) sec->data,
  1518. "%s$%u:%s$%s:%s",
  1519. scram_sasl_mech,
  1520. iteration_count,
  1521. base64_salt,
  1522. base64_StoredKey,
  1523. base64_ServerKey);
  1524. sec->len = (unsigned int) strlen((const char *) sec->data);
  1525. }
  1526. /* do the store */
  1527. propctx = sparams->utils->prop_new(0);
  1528. if (!propctx) {
  1529. r = SASL_FAIL;
  1530. }
  1531. if (!r) {
  1532. r = sparams->utils->prop_request(propctx, store_request);
  1533. }
  1534. if (!r) {
  1535. r = sparams->utils->prop_set(propctx,
  1536. "authPassword",
  1537. (const char *) (sec ? sec->data : NULL),
  1538. (sec ? sec->len : 0));
  1539. }
  1540. if (!r) {
  1541. r = sparams->utils->auxprop_store(sparams->utils->conn, propctx, user);
  1542. }
  1543. if (propctx) {
  1544. sparams->utils->prop_dispose(&propctx);
  1545. }
  1546. if (r) {
  1547. sparams->utils->seterror(sparams->utils->conn, 0,
  1548. "Error putting %s secret",
  1549. scram_sasl_mech);
  1550. goto cleanup;
  1551. }
  1552. sparams->utils->log(NULL, SASL_LOG_DEBUG, "Setpass for %s successful\n",
  1553. scram_sasl_mech);
  1554. cleanup:
  1555. if (user) _plug_free_string(sparams->utils, &user);
  1556. if (user_only) _plug_free_string(sparams->utils, &user_only);
  1557. if (realm) _plug_free_string(sparams->utils, &realm);
  1558. if (sec) _plug_free_secret(sparams->utils, &sec);
  1559. return r;
  1560. }
  1561. static void scram_server_mech_dispose(void *conn_context,
  1562. const sasl_utils_t *utils)
  1563. {
  1564. server_context_t *text = (server_context_t *) conn_context;
  1565. if (!text) return;
  1566. if (text->authentication_id) _plug_free_string(utils,&(text->authentication_id));
  1567. if (text->authorization_id) _plug_free_string(utils,&(text->authorization_id));
  1568. if (text->out_buf) _plug_free_string(utils,&(text->out_buf));
  1569. if (text->auth_message) _plug_free_string(utils,&(text->auth_message));
  1570. if (text->nonce) _plug_free_string(utils,&(text->nonce));
  1571. if (text->salt) utils->free(text->salt);
  1572. if (text->cbindingname != NULL) {
  1573. utils->free(text->cbindingname);
  1574. text->cbindingname = NULL;
  1575. }
  1576. if (text->gs2_header != NULL) {
  1577. utils->free(text->gs2_header);
  1578. text->gs2_header = NULL;
  1579. }
  1580. utils->free(text);
  1581. }
  1582. static sasl_server_plug_t scram_server_plugins[] =
  1583. {
  1584. #ifdef HAVE_SHA512
  1585. {
  1586. "SCRAM-SHA-512", /* mech_name */
  1587. 0, /* max_ssf */
  1588. SASL_SET_HASH_STRENGTH_BITS(512) |
  1589. SASL_SEC_NOPLAINTEXT
  1590. | SASL_SEC_NOACTIVE
  1591. | SASL_SEC_NOANONYMOUS
  1592. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  1593. SASL_FEAT_ALLOWS_PROXY
  1594. | SASL_FEAT_SUPPORTS_HTTP
  1595. | SASL_FEAT_CHANNEL_BINDING, /* features */
  1596. "SHA512", /* glob_context */
  1597. &scram_server_mech_new, /* mech_new */
  1598. &scram_server_mech_step, /* mech_step */
  1599. &scram_server_mech_dispose, /* mech_dispose */
  1600. NULL, /* mech_free */
  1601. &scram_setpass, /* setpass */
  1602. NULL, /* user_query */
  1603. NULL, /* idle */
  1604. NULL, /* mech avail */
  1605. NULL /* spare */
  1606. },
  1607. {
  1608. "SCRAM-SHA-384", /* mech_name */
  1609. 0, /* max_ssf */
  1610. SASL_SET_HASH_STRENGTH_BITS(384) |
  1611. SASL_SEC_NOPLAINTEXT
  1612. | SASL_SEC_NOACTIVE
  1613. | SASL_SEC_NOANONYMOUS
  1614. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  1615. SASL_FEAT_ALLOWS_PROXY
  1616. | SASL_FEAT_SUPPORTS_HTTP
  1617. | SASL_FEAT_CHANNEL_BINDING, /* features */
  1618. "SHA384", /* glob_context */
  1619. &scram_server_mech_new, /* mech_new */
  1620. &scram_server_mech_step, /* mech_step */
  1621. &scram_server_mech_dispose, /* mech_dispose */
  1622. NULL, /* mech_free */
  1623. &scram_setpass, /* setpass */
  1624. NULL, /* user_query */
  1625. NULL, /* idle */
  1626. NULL, /* mech avail */
  1627. NULL /* spare */
  1628. },
  1629. {
  1630. "SCRAM-SHA-256", /* mech_name */
  1631. 0, /* max_ssf */
  1632. SASL_SET_HASH_STRENGTH_BITS(256) |
  1633. SASL_SEC_NOPLAINTEXT
  1634. | SASL_SEC_NOACTIVE
  1635. | SASL_SEC_NOANONYMOUS
  1636. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  1637. SASL_FEAT_ALLOWS_PROXY
  1638. | SASL_FEAT_SUPPORTS_HTTP
  1639. | SASL_FEAT_CHANNEL_BINDING, /* features */
  1640. "SHA256", /* glob_context */
  1641. &scram_server_mech_new, /* mech_new */
  1642. &scram_server_mech_step, /* mech_step */
  1643. &scram_server_mech_dispose, /* mech_dispose */
  1644. NULL, /* mech_free */
  1645. &scram_setpass, /* setpass */
  1646. NULL, /* user_query */
  1647. NULL, /* idle */
  1648. NULL, /* mech avail */
  1649. NULL /* spare */
  1650. },
  1651. {
  1652. "SCRAM-SHA-224", /* mech_name */
  1653. 0, /* max_ssf */
  1654. SASL_SET_HASH_STRENGTH_BITS(224) |
  1655. SASL_SEC_NOPLAINTEXT
  1656. | SASL_SEC_NOACTIVE
  1657. | SASL_SEC_NOANONYMOUS
  1658. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  1659. SASL_FEAT_ALLOWS_PROXY
  1660. | SASL_FEAT_SUPPORTS_HTTP
  1661. | SASL_FEAT_CHANNEL_BINDING, /* features */
  1662. "SHA224", /* glob_context */
  1663. &scram_server_mech_new, /* mech_new */
  1664. &scram_server_mech_step, /* mech_step */
  1665. &scram_server_mech_dispose, /* mech_dispose */
  1666. NULL, /* mech_free */
  1667. &scram_setpass, /* setpass */
  1668. NULL, /* user_query */
  1669. NULL, /* idle */
  1670. NULL, /* mech avail */
  1671. NULL /* spare */
  1672. },
  1673. #endif
  1674. {
  1675. "SCRAM-SHA-1", /* mech_name */
  1676. 0, /* max_ssf */
  1677. SASL_SET_HASH_STRENGTH_BITS(160) |
  1678. SASL_SEC_NOPLAINTEXT
  1679. | SASL_SEC_NOACTIVE
  1680. | SASL_SEC_NOANONYMOUS
  1681. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  1682. SASL_FEAT_ALLOWS_PROXY
  1683. | SASL_FEAT_SUPPORTS_HTTP
  1684. | SASL_FEAT_CHANNEL_BINDING, /* features */
  1685. "SHA1", /* glob_context */
  1686. &scram_server_mech_new, /* mech_new */
  1687. &scram_server_mech_step, /* mech_step */
  1688. &scram_server_mech_dispose, /* mech_dispose */
  1689. NULL, /* mech_free */
  1690. &scram_setpass, /* setpass */
  1691. NULL, /* user_query */
  1692. NULL, /* idle */
  1693. NULL, /* mech avail */
  1694. NULL /* spare */
  1695. }
  1696. };
  1697. int scram_server_plug_init(const sasl_utils_t *utils,
  1698. int maxversion,
  1699. int *out_version,
  1700. sasl_server_plug_t **pluglist,
  1701. int *plugcount)
  1702. {
  1703. if (maxversion < SASL_SERVER_PLUG_VERSION) {
  1704. SETERROR( utils, "SCRAM-SHA-* version mismatch");
  1705. return SASL_BADVERS;
  1706. }
  1707. *out_version = SASL_SERVER_PLUG_VERSION;
  1708. *pluglist = scram_server_plugins;
  1709. #ifdef HAVE_SHA512
  1710. *plugcount = 5;
  1711. #else
  1712. *plugcount = 1;
  1713. #endif
  1714. utils->rand(utils->rpool, (char *)g_salt_key, SALT_SIZE);
  1715. return SASL_OK;
  1716. }
  1717. /***************************** Client Section *****************************/
  1718. typedef struct client_context {
  1719. int state;
  1720. const EVP_MD *md; /* underlying MDA */
  1721. sasl_secret_t *password; /* user password */
  1722. unsigned int free_password; /* set if we need to free the password */
  1723. char * gs2_header;
  1724. size_t gs2_header_length;
  1725. char * out_buf;
  1726. unsigned out_buf_len;
  1727. char * auth_message;
  1728. size_t auth_message_len;
  1729. char * nonce;
  1730. /* in binary form */
  1731. char * salt;
  1732. size_t salt_len;
  1733. unsigned int iteration_count;
  1734. char SaltedPassword[EVP_MAX_MD_SIZE];
  1735. int cb_flags;
  1736. } client_context_t;
  1737. static int scram_client_mech_new(void *glob_context,
  1738. sasl_client_params_t *params,
  1739. void **conn_context)
  1740. {
  1741. client_context_t *text;
  1742. /* holds state are in */
  1743. text = params->utils->malloc(sizeof(client_context_t));
  1744. if (text == NULL) {
  1745. MEMERROR(params->utils);
  1746. return SASL_NOMEM;
  1747. }
  1748. memset(text, 0, sizeof(client_context_t));
  1749. text->md = EVP_get_digestbyname((const char *) glob_context);
  1750. *conn_context = text;
  1751. return SASL_OK;
  1752. }
  1753. static int
  1754. scram_client_mech_step1(client_context_t *text,
  1755. sasl_client_params_t *params,
  1756. const char *serverin __attribute__((unused)),
  1757. unsigned serverinlen __attribute__((unused)),
  1758. sasl_interact_t **prompt_need,
  1759. const char **clientout,
  1760. unsigned *clientoutlen,
  1761. sasl_out_params_t *oparams)
  1762. {
  1763. const char *authid = NULL;
  1764. const char *userid = NULL;
  1765. int user_result = SASL_OK;
  1766. int auth_result = SASL_OK;
  1767. int pass_result = SASL_OK;
  1768. int result;
  1769. size_t maxsize;
  1770. char * encoded_authcid;
  1771. char * freeme = NULL;
  1772. char * freeme2 = NULL;
  1773. char channel_binding_state = 'n';
  1774. const char * channel_binding_name = NULL;
  1775. char * encoded_authorization_id = NULL;
  1776. const char *scram_sasl_mech = scram_sasl_mech_name(EVP_MD_size(text->md));
  1777. /* check if sec layer strong enough */
  1778. if (params->props.min_ssf > params->external_ssf) {
  1779. params->utils->seterror(params->utils->conn, 0,
  1780. "SSF requested of %s plugin",
  1781. scram_sasl_mech);
  1782. return SASL_TOOWEAK;
  1783. }
  1784. /* try to get the userid */
  1785. if (oparams->authid == NULL) {
  1786. auth_result=_plug_get_authid(params->utils, &authid, prompt_need);
  1787. if ((auth_result != SASL_OK) && (auth_result != SASL_INTERACT))
  1788. return auth_result;
  1789. }
  1790. /* try to get the userid */
  1791. if (oparams->user == NULL) {
  1792. user_result = _plug_get_userid(params->utils, &userid, prompt_need);
  1793. if ((user_result != SASL_OK) && (user_result != SASL_INTERACT)) {
  1794. return user_result;
  1795. }
  1796. }
  1797. /* try to get the password */
  1798. if (text->password == NULL) {
  1799. pass_result = _plug_get_password(params->utils,
  1800. &text->password,
  1801. &text->free_password,
  1802. prompt_need);
  1803. if ((pass_result != SASL_OK) && (pass_result != SASL_INTERACT)) {
  1804. return pass_result;
  1805. }
  1806. }
  1807. /* free prompts we got */
  1808. if (prompt_need && *prompt_need) {
  1809. params->utils->free(*prompt_need);
  1810. *prompt_need = NULL;
  1811. }
  1812. /* if there are prompts not filled in */
  1813. if ((auth_result == SASL_INTERACT) ||
  1814. (user_result == SASL_INTERACT) ||
  1815. (pass_result == SASL_INTERACT)) {
  1816. /* make the prompt list */
  1817. result =
  1818. _plug_make_prompts(params->utils,
  1819. prompt_need,
  1820. user_result == SASL_INTERACT ?
  1821. "Please enter your authorization name" : NULL,
  1822. NULL,
  1823. auth_result == SASL_INTERACT ?
  1824. "Please enter your authentication name" : NULL,
  1825. NULL,
  1826. pass_result == SASL_INTERACT ?
  1827. "Please enter your password" : NULL,
  1828. NULL,
  1829. NULL,
  1830. NULL,
  1831. NULL,
  1832. NULL,
  1833. NULL,
  1834. NULL);
  1835. if (result != SASL_OK) {
  1836. goto cleanup;
  1837. }
  1838. return SASL_INTERACT;
  1839. }
  1840. if (!text->password) {
  1841. PARAMERROR(params->utils);
  1842. return SASL_BADPARAM;
  1843. }
  1844. if (oparams->authid == NULL) {
  1845. if (!userid || !*userid) {
  1846. result = params->canon_user(params->utils->conn,
  1847. authid,
  1848. 0,
  1849. SASL_CU_AUTHID | SASL_CU_AUTHZID,
  1850. oparams);
  1851. }
  1852. else {
  1853. result = params->canon_user(params->utils->conn,
  1854. authid,
  1855. 0,
  1856. SASL_CU_AUTHID,
  1857. oparams);
  1858. if (result != SASL_OK) {
  1859. goto cleanup;
  1860. }
  1861. result = params->canon_user(params->utils->conn,
  1862. userid,
  1863. 0,
  1864. SASL_CU_AUTHZID,
  1865. oparams);
  1866. }
  1867. if (result != SASL_OK) {
  1868. goto cleanup;
  1869. }
  1870. }
  1871. switch (params->cbindingdisp) {
  1872. case SASL_CB_DISP_NONE:
  1873. text->cb_flags = SCRAM_CB_FLAG_N;
  1874. channel_binding_state = 'n';
  1875. break;
  1876. case SASL_CB_DISP_USED:
  1877. if (!SASL_CB_PRESENT(params)) {
  1878. result = SASL_BADPARAM;
  1879. goto cleanup;
  1880. }
  1881. channel_binding_name = params->cbinding->name;
  1882. text->cb_flags = SCRAM_CB_FLAG_P;
  1883. channel_binding_state = 'p';
  1884. break;
  1885. case SASL_CB_DISP_WANT:
  1886. text->cb_flags = SCRAM_CB_FLAG_Y;
  1887. channel_binding_state = 'y';
  1888. break;
  1889. }
  1890. text->nonce = params->utils->malloc (NONCE_SIZE + 1);
  1891. if (text->nonce == NULL) {
  1892. MEMERROR( params->utils );
  1893. result = SASL_NOMEM;
  1894. goto cleanup;
  1895. }
  1896. if (create_nonce(params->utils,
  1897. text->nonce,
  1898. NONCE_SIZE + 1) == NULL) {
  1899. MEMERROR( params->utils );
  1900. result = SASL_NOMEM;
  1901. goto cleanup;
  1902. }
  1903. if (userid != NULL && *userid != '\0') {
  1904. result = encode_saslname (oparams->user,
  1905. (const char **) &encoded_authorization_id,
  1906. &freeme2);
  1907. if (result != SASL_OK) {
  1908. MEMERROR( params->utils );
  1909. result = SASL_NOMEM;
  1910. goto cleanup;
  1911. }
  1912. }
  1913. result = encode_saslname (oparams->authid,
  1914. (const char **) &encoded_authcid,
  1915. &freeme);
  1916. if (result != SASL_OK) {
  1917. MEMERROR( params->utils );
  1918. result = SASL_NOMEM;
  1919. goto cleanup;
  1920. }
  1921. maxsize = strlen("p=,a=,n=,r=") +
  1922. ((channel_binding_name != NULL) ? strlen(channel_binding_name) : 0) +
  1923. ((encoded_authorization_id != NULL) ? strlen(encoded_authorization_id) : 0) +
  1924. strlen(encoded_authcid) +
  1925. strlen(text->nonce);
  1926. result = _plug_buf_alloc(params->utils,
  1927. &(text->out_buf),
  1928. &(text->out_buf_len),
  1929. (unsigned) maxsize + 1);
  1930. if (result != SASL_OK) {
  1931. MEMERROR( params->utils );
  1932. result = SASL_NOMEM;
  1933. goto cleanup;
  1934. }
  1935. snprintf(text->out_buf,
  1936. maxsize + 1,
  1937. "%c%s%s,%s%s,",
  1938. channel_binding_state,
  1939. (channel_binding_name != NULL) ? "=" : "",
  1940. (channel_binding_name != NULL) ? channel_binding_name : "",
  1941. (encoded_authorization_id != NULL) ? "a=" : "",
  1942. (encoded_authorization_id != NULL) ? encoded_authorization_id : "");
  1943. text->gs2_header_length = strlen(text->out_buf);
  1944. _plug_strdup(params->utils, text->out_buf, &text->gs2_header, NULL);
  1945. sprintf(text->out_buf + text->gs2_header_length,
  1946. "n=%s,r=%s",
  1947. encoded_authcid,
  1948. text->nonce);
  1949. /* Save the copy of the client-first-message */
  1950. /* Need to skip the GS2 prefix here */
  1951. _plug_strdup(params->utils,
  1952. text->out_buf + text->gs2_header_length,
  1953. &text->auth_message,
  1954. NULL);
  1955. if (text->auth_message == NULL) {
  1956. MEMERROR( params->utils );
  1957. result = SASL_NOMEM;
  1958. goto cleanup;
  1959. }
  1960. text->auth_message_len = strlen(text->auth_message);
  1961. *clientout = text->out_buf;
  1962. *clientoutlen = (unsigned) strlen(*clientout);
  1963. result = SASL_CONTINUE;
  1964. cleanup:
  1965. if (freeme != NULL) _plug_free_string(params->utils, &freeme);
  1966. if (freeme2 != NULL) _plug_free_string(params->utils, &freeme2);
  1967. return result;
  1968. }
  1969. static int
  1970. scram_client_mech_step2(client_context_t *text,
  1971. sasl_client_params_t *params,
  1972. const char *serverin,
  1973. unsigned serverinlen,
  1974. sasl_interact_t **prompt_need __attribute__((unused)),
  1975. const char **clientout,
  1976. unsigned *clientoutlen,
  1977. sasl_out_params_t *oparams __attribute__((unused)))
  1978. {
  1979. char * p;
  1980. char * nonce;
  1981. size_t server_nonce_len;
  1982. char * base64_salt = NULL;
  1983. size_t base64_salt_len;
  1984. unsigned exact_salt_len;
  1985. char * counter;
  1986. char * end;
  1987. char * inbuf = NULL;
  1988. size_t estimated_response_len;
  1989. size_t length_no_proof;
  1990. char * full_auth_message;
  1991. size_t cb_bin_length;
  1992. size_t channel_binding_data_len = 0;
  1993. size_t cb_encoded_length;
  1994. const char * channel_binding_data = NULL;
  1995. char * cb_encoded = NULL;
  1996. char * cb_bin = NULL;
  1997. int result;
  1998. char ClientKey[EVP_MAX_MD_SIZE];
  1999. char StoredKey[EVP_MAX_MD_SIZE];
  2000. char ClientSignature[EVP_MAX_MD_SIZE];
  2001. char ClientProof[EVP_MAX_MD_SIZE];
  2002. char * client_proof = NULL;
  2003. size_t client_proof_len;
  2004. unsigned int hash_len = 0;
  2005. size_t k, hash_size = EVP_MD_size(text->md);
  2006. const char *scram_sasl_mech = scram_sasl_mech_name(hash_size);
  2007. if (serverinlen == 0) {
  2008. params->utils->seterror(params->utils->conn, 0,
  2009. "%s input expected", scram_sasl_mech);
  2010. return SASL_BADPROT;
  2011. }
  2012. /* [reserved-mext ","] nonce "," salt "," iteration-count ["," extensions] */
  2013. if (serverinlen < 3 || serverin[1] != '=') {
  2014. params->utils->seterror(params->utils->conn, 0,
  2015. "Invalid %s input", scram_sasl_mech);
  2016. return SASL_BADPROT;
  2017. }
  2018. if (serverin[0] == 'm') {
  2019. params->utils->seterror(params->utils->conn, 0,
  2020. "Unsupported mandatory extension to %s",
  2021. scram_sasl_mech);
  2022. return SASL_BADPROT;
  2023. }
  2024. if (serverin[0] != 'r') {
  2025. params->utils->seterror(params->utils->conn, 0,
  2026. "Nonce (r=) expected in %s input",
  2027. scram_sasl_mech);
  2028. return SASL_BADPROT;
  2029. }
  2030. inbuf = params->utils->malloc (serverinlen + 1);
  2031. if (inbuf == NULL) {
  2032. MEMERROR( params->utils );
  2033. return SASL_NOMEM;
  2034. }
  2035. memcpy(inbuf, serverin, serverinlen);
  2036. inbuf[serverinlen] = 0;
  2037. if (strlen(inbuf) != serverinlen) {
  2038. params->utils->seterror(params->utils->conn, 0,
  2039. "NULs found in %s input", scram_sasl_mech);
  2040. result = SASL_BADPROT;
  2041. goto cleanup;
  2042. }
  2043. nonce = inbuf + 2;
  2044. p = strchr (nonce, ',');
  2045. /* MUST be followed by a salt */
  2046. if (p == NULL) {
  2047. params->utils->seterror(params->utils->conn, 0,
  2048. "Salt expected after the nonce in %s input",
  2049. scram_sasl_mech);
  2050. result = SASL_BADPROT;
  2051. goto cleanup;
  2052. }
  2053. *p = '\0';
  2054. p++;
  2055. if (strncmp(p, "s=", 2) != 0) {
  2056. params->utils->seterror(params->utils->conn, 0,
  2057. "Salt expected after the nonce in %s input",
  2058. scram_sasl_mech);
  2059. result = SASL_BADPROT;
  2060. goto cleanup;
  2061. }
  2062. p += 2;
  2063. base64_salt = p;
  2064. p = strchr (base64_salt, ',');
  2065. /* MUST be followed by an iteration-count */
  2066. if (p == NULL) {
  2067. params->utils->seterror(params->utils->conn, 0,
  2068. "iteration-count expected after the salt in %s input",
  2069. scram_sasl_mech);
  2070. result = SASL_BADPROT;
  2071. goto cleanup;
  2072. }
  2073. *p = '\0';
  2074. p++;
  2075. if (strncmp(p, "i=", 2) != 0) {
  2076. params->utils->seterror(params->utils->conn, 0,
  2077. "iteration-count expected after the salt in %s input",
  2078. scram_sasl_mech);
  2079. result = SASL_BADPROT;
  2080. goto cleanup;
  2081. }
  2082. p += 2;
  2083. counter = p;
  2084. p = strchr (counter, ',');
  2085. if (p == NULL) {
  2086. p = counter + strlen(counter);
  2087. } else {
  2088. *p = '\0';
  2089. }
  2090. errno = 0;
  2091. text->iteration_count = strtoul(counter, &end, 10);
  2092. if (counter == end || *end != '\0' || errno != 0) {
  2093. params->utils->seterror(params->utils->conn, 0,
  2094. "Invalid iteration-count in %s input",
  2095. scram_sasl_mech);
  2096. result = SASL_BADPROT;
  2097. goto cleanup;
  2098. }
  2099. if (text->iteration_count < MIN_ITERATION_COUNTER) {
  2100. }
  2101. if (text->iteration_count > MAX_ITERATION_COUNTER) {
  2102. SETERROR(params->utils, "iteration-count is too big, refusing to compute");
  2103. result = SASL_BADPROT;
  2104. goto cleanup;
  2105. }
  2106. /* The client MUST verify that the initial part of the nonce
  2107. used in subsequent messages is the same as the nonce it
  2108. initially specified. */
  2109. server_nonce_len = strlen(nonce);
  2110. if (server_nonce_len <= NONCE_SIZE ||
  2111. strncmp(nonce, text->nonce, NONCE_SIZE) != 0) {
  2112. SETERROR(params->utils, "The nonce received from the server doesn't start from the nonce sent by the client");
  2113. result = SASL_BADPROT;
  2114. goto cleanup;
  2115. }
  2116. /* Now we can forget about our nonce */
  2117. params->utils->free(text->nonce);
  2118. _plug_strdup(params->utils, nonce, &text->nonce, NULL);
  2119. if (text->nonce == NULL) {
  2120. MEMERROR( params->utils );
  2121. result = SASL_NOMEM;
  2122. goto cleanup;
  2123. }
  2124. /* base64 decode salt */
  2125. base64_salt_len = strlen(base64_salt);
  2126. if (base64_salt_len == 0) {
  2127. SETERROR(params->utils, "The salt can't be empty");
  2128. result = SASL_BADPROT;
  2129. goto cleanup;
  2130. }
  2131. if (base64_salt_len % 4 != 0) {
  2132. SETERROR(params->utils, "Invalid base64 encoding of the salt");
  2133. result = SASL_BADPROT;
  2134. goto cleanup;
  2135. }
  2136. text->salt_len = base64_salt_len / 4 * 3;
  2137. text->salt = (char *) params->utils->malloc(text->salt_len + 1);
  2138. if (text->salt == NULL) {
  2139. MEMERROR( params->utils );
  2140. result = SASL_NOMEM;
  2141. goto cleanup;
  2142. }
  2143. if (params->utils->decode64(base64_salt,
  2144. (unsigned int)base64_salt_len,
  2145. text->salt,
  2146. (unsigned int)text->salt_len + 1,
  2147. &exact_salt_len) != SASL_OK) {
  2148. params->utils->seterror(params->utils->conn, 0,
  2149. "Invalid base64 encoding of the salt in %s input",
  2150. scram_sasl_mech);
  2151. result = SASL_BADPROT;
  2152. goto cleanup;
  2153. }
  2154. text->salt_len = exact_salt_len;
  2155. /* Now we generate client response */
  2156. if (text->gs2_header[0] == 'p') {
  2157. if (params->cbinding == NULL) {
  2158. result = SASL_FAIL;
  2159. goto cleanup;
  2160. }
  2161. channel_binding_data = (const char *) params->cbinding->data;
  2162. channel_binding_data_len = params->cbinding->len;
  2163. }
  2164. cb_bin_length = text->gs2_header_length +
  2165. ((channel_binding_data != NULL) ? channel_binding_data_len : 0);
  2166. cb_encoded_length = (cb_bin_length / 3 * 4) + ((cb_bin_length % 3) ? 4 : 0);
  2167. if (channel_binding_data != NULL) {
  2168. cb_bin = (char *) params->utils->malloc(cb_bin_length + 1);
  2169. if (cb_bin == NULL) {
  2170. MEMERROR( params->utils );
  2171. result = SASL_NOMEM;
  2172. goto cleanup;
  2173. }
  2174. memcpy(cb_bin, text->gs2_header, text->gs2_header_length);
  2175. memcpy(cb_bin + text->gs2_header_length, channel_binding_data, channel_binding_data_len);
  2176. }
  2177. cb_encoded = (char *) params->utils->malloc(cb_encoded_length + 1);
  2178. if (cb_encoded == NULL) {
  2179. MEMERROR( params->utils );
  2180. result = SASL_NOMEM;
  2181. goto cleanup;
  2182. }
  2183. /*
  2184. * Returns SASL_OK on success, SASL_BUFOVER if result won't fit
  2185. */
  2186. if (params->utils->encode64((cb_bin != NULL) ? cb_bin : text->gs2_header,
  2187. (unsigned int)cb_bin_length,
  2188. cb_encoded,
  2189. (unsigned int)cb_encoded_length + 1,
  2190. NULL) != SASL_OK) {
  2191. MEMERROR( params->utils );
  2192. result = SASL_NOMEM;
  2193. goto cleanup;
  2194. }
  2195. cb_encoded[cb_encoded_length] = '\0';
  2196. client_proof_len = hash_size / 3 * 4 + ((hash_size % 3) ? 4 : 0);
  2197. estimated_response_len = strlen(cb_encoded)+
  2198. strlen(text->nonce)+
  2199. client_proof_len +
  2200. strlen("c=,r=,p=");
  2201. result = _plug_buf_alloc(params->utils,
  2202. &(text->out_buf),
  2203. &(text->out_buf_len),
  2204. (unsigned) estimated_response_len + 1);
  2205. if (result != SASL_OK) {
  2206. MEMERROR( params->utils );
  2207. result = SASL_NOMEM;
  2208. goto cleanup;
  2209. }
  2210. /* channel-binding "," nonce ["," extensions] */
  2211. sprintf(text->out_buf,
  2212. "c=%s,r=%s",
  2213. cb_encoded,
  2214. text->nonce);
  2215. length_no_proof = strlen(text->out_buf);
  2216. /* Build AuthMessage */
  2217. full_auth_message = params->utils->realloc(text->auth_message,
  2218. text->auth_message_len + 1 +
  2219. serverinlen + 1 +
  2220. length_no_proof + 1);
  2221. if (full_auth_message == NULL) {
  2222. MEMERROR( params->utils );
  2223. result = SASL_NOMEM;
  2224. goto cleanup;
  2225. }
  2226. text->auth_message = full_auth_message;
  2227. text->auth_message[text->auth_message_len] = ',';
  2228. memcpy(text->auth_message + text->auth_message_len + 1, serverin, serverinlen);
  2229. text->auth_message[text->auth_message_len + 1 + serverinlen] = ',';
  2230. memcpy(text->auth_message + text->auth_message_len + 1 + serverinlen + 1,
  2231. text->out_buf,
  2232. length_no_proof);
  2233. text->auth_message_len += serverinlen + 2 + length_no_proof;
  2234. text->auth_message[text->auth_message_len] = '\0';
  2235. /* Calculate ClientProof */
  2236. /* SaltedPassword := Hi(password, salt) */
  2237. Hi (params->utils,
  2238. text->md,
  2239. (const char *) text->password->data,
  2240. text->password->len,
  2241. text->salt,
  2242. text->salt_len,
  2243. text->iteration_count,
  2244. text->SaltedPassword);
  2245. PRINT_HASH ("SaltedPassword", text->SaltedPassword, hash_size);
  2246. /* ClientKey := HMAC(SaltedPassword, "Client Key") */
  2247. if (HMAC(text->md,
  2248. (const unsigned char *) text->SaltedPassword,
  2249. hash_size,
  2250. (const unsigned char *) CLIENT_KEY_CONSTANT,
  2251. CLIENT_KEY_CONSTANT_LEN,
  2252. (unsigned char *)ClientKey,
  2253. &hash_len) == NULL) {
  2254. params->utils->seterror(params->utils->conn,0,
  2255. "HMAC-%s call failed", scram_sasl_mech+6);
  2256. result = SASL_SCRAM_INTERNAL;
  2257. goto cleanup;
  2258. }
  2259. PRINT_HASH ("ClientKey", ClientKey, hash_size);
  2260. /* StoredKey := H(ClientKey) */
  2261. if (EVP_Digest((const unsigned char *) ClientKey, hash_size,
  2262. (unsigned char *) StoredKey, NULL, text->md, NULL) == 0) {
  2263. params->utils->seterror(params->utils->conn,0,
  2264. "%s call failed", scram_sasl_mech+6);
  2265. result = SASL_SCRAM_INTERNAL;
  2266. goto cleanup;
  2267. }
  2268. PRINT_HASH ("StoredKey", StoredKey, hash_size);
  2269. /* ClientSignature := HMAC(StoredKey, AuthMessage) */
  2270. if (HMAC(text->md,
  2271. (const unsigned char *)StoredKey,
  2272. hash_size,
  2273. (const unsigned char *) text->auth_message,
  2274. (int)text->auth_message_len,
  2275. (unsigned char *)ClientSignature,
  2276. &hash_len) == NULL) {
  2277. params->utils->seterror(params->utils->conn,0,
  2278. "HMAC-%s call failed", scram_sasl_mech+6);
  2279. result = SASL_SCRAM_INTERNAL;
  2280. goto cleanup;
  2281. }
  2282. PRINT_HASH ("ClientSignature", ClientSignature, hash_size);
  2283. /* ClientProof := ClientKey XOR ClientSignature */
  2284. for (k = 0; k < hash_size; k++) {
  2285. ClientProof[k] = ClientKey[k] ^ ClientSignature[k];
  2286. }
  2287. PRINT_HASH ("ClientProof", ClientProof, hash_size);
  2288. /* base64-encode ClientProof */
  2289. client_proof = (char *) params->utils->malloc(client_proof_len + 1);
  2290. if (client_proof == NULL) {
  2291. MEMERROR( params->utils );
  2292. result = SASL_NOMEM;
  2293. goto cleanup;
  2294. }
  2295. result = params->utils->encode64(ClientProof,
  2296. hash_size,
  2297. client_proof,
  2298. (unsigned int)client_proof_len + 1,
  2299. NULL);
  2300. if (result != SASL_OK) {
  2301. goto cleanup;
  2302. }
  2303. client_proof[client_proof_len] = '\0';
  2304. sprintf(text->out_buf + length_no_proof,
  2305. ",p=%s",
  2306. client_proof);
  2307. *clientout = text->out_buf;
  2308. *clientoutlen = (unsigned) strlen(text->out_buf);
  2309. result = SASL_CONTINUE;
  2310. cleanup:
  2311. if (inbuf != NULL) {
  2312. params->utils->free(inbuf);
  2313. }
  2314. if (client_proof != NULL) {
  2315. params->utils->free(client_proof);
  2316. }
  2317. if (cb_encoded != NULL) {
  2318. params->utils->free(cb_encoded);
  2319. }
  2320. if (cb_bin != NULL) {
  2321. params->utils->free(cb_bin);
  2322. }
  2323. return result;
  2324. }
  2325. static int
  2326. scram_client_mech_step3(client_context_t *text,
  2327. sasl_client_params_t *params,
  2328. const char *serverin,
  2329. unsigned serverinlen,
  2330. sasl_interact_t **prompt_need __attribute__((unused)),
  2331. const char **clientout __attribute__((unused)),
  2332. unsigned *clientoutlen __attribute__((unused)),
  2333. sasl_out_params_t *oparams)
  2334. {
  2335. char * p;
  2336. int result;
  2337. size_t server_proof_len;
  2338. unsigned exact_server_proof_len;
  2339. char DecodedServerProof[EVP_MAX_MD_SIZE + 1];
  2340. char ServerKey[EVP_MAX_MD_SIZE];
  2341. char ServerSignature[EVP_MAX_MD_SIZE];
  2342. unsigned int hash_len = 0;
  2343. size_t k, hash_size = EVP_MD_size(text->md);
  2344. const char *scram_sasl_mech = scram_sasl_mech_name(hash_size);
  2345. if (serverinlen < 3) {
  2346. params->utils->seterror(params->utils->conn, 0,
  2347. "Invalid %s input expected",
  2348. scram_sasl_mech);
  2349. return SASL_BADPROT;
  2350. }
  2351. /* Expecting: 'verifier ["," extensions]' */
  2352. if (strncmp(serverin, "v=", 2) != 0) {
  2353. params->utils->seterror(params->utils->conn, 0,
  2354. "ServerSignature expected in %s input",
  2355. scram_sasl_mech);
  2356. return SASL_BADPROT;
  2357. }
  2358. /* Use memchr instead of the original strchr as there is no guarantee that
  2359. the input data is NUL terminated */
  2360. p = memchr (serverin + 2, ',', serverinlen - 2);
  2361. if (p != NULL) {
  2362. server_proof_len = p - (serverin + 2) - 1;
  2363. } else {
  2364. server_proof_len = serverinlen - 2;
  2365. }
  2366. if (params->utils->decode64(serverin + 2, /* ServerProof */
  2367. (unsigned int)server_proof_len,
  2368. DecodedServerProof,
  2369. hash_size + 1,
  2370. &exact_server_proof_len) != SASL_OK) {
  2371. params->utils->seterror(params->utils->conn, 0,
  2372. "Invalid base64 encoding of the server proof in %s input",
  2373. scram_sasl_mech);
  2374. result = SASL_BADPROT;
  2375. goto cleanup;
  2376. }
  2377. if (exact_server_proof_len != hash_size) {
  2378. params->utils->seterror(params->utils->conn, 0,
  2379. "Invalid server proof (truncated) in %s input",
  2380. scram_sasl_mech);
  2381. result = SASL_BADPROT;
  2382. goto cleanup;
  2383. }
  2384. /* ServerKey := HMAC(SaltedPassword, "Server Key") */
  2385. if (HMAC(text->md,
  2386. (const unsigned char *)text->SaltedPassword,
  2387. hash_size,
  2388. (const unsigned char *) SERVER_KEY_CONSTANT,
  2389. SERVER_KEY_CONSTANT_LEN,
  2390. (unsigned char *)ServerKey,
  2391. &hash_len) == NULL) {
  2392. params->utils->seterror(params->utils->conn,0,
  2393. "HMAC-%s call failed", scram_sasl_mech+6);
  2394. result = SASL_SCRAM_INTERNAL;
  2395. goto cleanup;
  2396. }
  2397. /* ServerSignature := HMAC(ServerKey, AuthMessage) */
  2398. if (HMAC(text->md,
  2399. (const unsigned char *)ServerKey,
  2400. hash_size,
  2401. (const unsigned char *) text->auth_message,
  2402. (int)text->auth_message_len,
  2403. (unsigned char *)ServerSignature,
  2404. &hash_len) == NULL) {
  2405. params->utils->seterror(params->utils->conn,0,
  2406. "HMAC-%s call failed", scram_sasl_mech+6);
  2407. result = SASL_SCRAM_INTERNAL;
  2408. goto cleanup;
  2409. }
  2410. for (k = 0; k < hash_size; k++) {
  2411. if (DecodedServerProof[k] != ServerSignature[k]) {
  2412. SETERROR(params->utils, "ServerSignature mismatch");
  2413. result = SASL_BADAUTH;
  2414. goto cleanup;
  2415. }
  2416. }
  2417. /* set oparams */
  2418. oparams->doneflag = 1;
  2419. oparams->mech_ssf = 0;
  2420. oparams->maxoutbuf = 0;
  2421. oparams->encode_context = NULL;
  2422. oparams->encode = NULL;
  2423. oparams->decode_context = NULL;
  2424. oparams->decode = NULL;
  2425. oparams->param_version = 0;
  2426. result = SASL_OK;
  2427. cleanup:
  2428. return result;
  2429. }
  2430. static int scram_client_mech_step(void *conn_context,
  2431. sasl_client_params_t *params,
  2432. const char *serverin,
  2433. unsigned serverinlen,
  2434. sasl_interact_t **prompt_need,
  2435. const char **clientout,
  2436. unsigned *clientoutlen,
  2437. sasl_out_params_t *oparams)
  2438. {
  2439. int result = SASL_FAIL;
  2440. client_context_t *text = (client_context_t *) conn_context;
  2441. const char *scram_sasl_mech = scram_sasl_mech_name(EVP_MD_size(text->md));
  2442. *clientout = NULL;
  2443. *clientoutlen = 0;
  2444. /* this should be well more than is ever needed */
  2445. if (serverinlen > MAX_SERVERIN_LEN) {
  2446. params->utils->seterror(params->utils->conn, 0,
  2447. "%s input longer than " STRINGIZE((MAX_SERVERIN_LEN)) " bytes",
  2448. scram_sasl_mech);
  2449. return SASL_BADPROT;
  2450. }
  2451. switch (text->state) {
  2452. case 0:
  2453. result = scram_client_mech_step1(text,
  2454. params,
  2455. serverin,
  2456. serverinlen,
  2457. prompt_need,
  2458. clientout,
  2459. clientoutlen,
  2460. oparams);
  2461. break;
  2462. case 1:
  2463. result = scram_client_mech_step2(text,
  2464. params,
  2465. serverin,
  2466. serverinlen,
  2467. prompt_need,
  2468. clientout,
  2469. clientoutlen,
  2470. oparams);
  2471. break;
  2472. case 2:
  2473. result = scram_client_mech_step3(text,
  2474. params,
  2475. serverin,
  2476. serverinlen,
  2477. prompt_need,
  2478. clientout,
  2479. clientoutlen,
  2480. oparams);
  2481. break;
  2482. default: /* should never get here */
  2483. params->utils->log(NULL, SASL_LOG_ERR,
  2484. "Invalid %s client step %d\n",
  2485. scram_sasl_mech, text->state);
  2486. return SASL_FAIL;
  2487. }
  2488. if (result != SASL_INTERACT) {
  2489. text->state++;
  2490. }
  2491. return result;
  2492. }
  2493. static void scram_client_mech_dispose(void *conn_context,
  2494. const sasl_utils_t *utils)
  2495. {
  2496. client_context_t *text = (client_context_t *) conn_context;
  2497. if (!text) return;
  2498. /* get rid of all sensitive info */
  2499. if (text->free_password) {
  2500. _plug_free_secret(utils, &text->password);
  2501. text->free_password = 0;
  2502. }
  2503. if (text->gs2_header) {
  2504. utils->free(text->gs2_header);
  2505. text->gs2_header = NULL;
  2506. }
  2507. if (text->out_buf) {
  2508. utils->free(text->out_buf);
  2509. text->out_buf = NULL;
  2510. }
  2511. if (text->auth_message) _plug_free_string(utils,&(text->auth_message));
  2512. if (text->nonce) _plug_free_string(utils,&(text->nonce));
  2513. if (text->salt) utils->free(text->salt);
  2514. utils->free(text);
  2515. }
  2516. static sasl_client_plug_t scram_client_plugins[] =
  2517. {
  2518. #ifdef HAVE_SHA512
  2519. {
  2520. "SCRAM-SHA-512", /* mech_name */
  2521. 0, /* max_ssf */
  2522. SASL_SET_HASH_STRENGTH_BITS(512) |
  2523. SASL_SEC_NOPLAINTEXT
  2524. | SASL_SEC_NOANONYMOUS
  2525. | SASL_SEC_NOACTIVE
  2526. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  2527. SASL_FEAT_ALLOWS_PROXY
  2528. | SASL_FEAT_SUPPORTS_HTTP
  2529. | SASL_FEAT_CHANNEL_BINDING, /* features */
  2530. NULL, /* required_prompts */
  2531. "SHA512", /* glob_context */
  2532. &scram_client_mech_new, /* mech_new */
  2533. &scram_client_mech_step, /* mech_step */
  2534. &scram_client_mech_dispose, /* mech_dispose */
  2535. NULL, /* mech_free */
  2536. NULL, /* idle */
  2537. NULL, /* spare */
  2538. NULL /* spare */
  2539. },
  2540. {
  2541. "SCRAM-SHA-384", /* mech_name */
  2542. 0, /* max_ssf */
  2543. SASL_SET_HASH_STRENGTH_BITS(384) |
  2544. SASL_SEC_NOPLAINTEXT
  2545. | SASL_SEC_NOANONYMOUS
  2546. | SASL_SEC_NOACTIVE
  2547. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  2548. SASL_FEAT_ALLOWS_PROXY
  2549. | SASL_FEAT_SUPPORTS_HTTP
  2550. | SASL_FEAT_CHANNEL_BINDING, /* features */
  2551. NULL, /* required_prompts */
  2552. "SHA384", /* glob_context */
  2553. &scram_client_mech_new, /* mech_new */
  2554. &scram_client_mech_step, /* mech_step */
  2555. &scram_client_mech_dispose, /* mech_dispose */
  2556. NULL, /* mech_free */
  2557. NULL, /* idle */
  2558. NULL, /* spare */
  2559. NULL /* spare */
  2560. },
  2561. {
  2562. "SCRAM-SHA-256", /* mech_name */
  2563. 0, /* max_ssf */
  2564. SASL_SET_HASH_STRENGTH_BITS(256) |
  2565. SASL_SEC_NOPLAINTEXT
  2566. | SASL_SEC_NOANONYMOUS
  2567. | SASL_SEC_NOACTIVE
  2568. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  2569. SASL_FEAT_ALLOWS_PROXY
  2570. | SASL_FEAT_SUPPORTS_HTTP
  2571. | SASL_FEAT_CHANNEL_BINDING, /* features */
  2572. NULL, /* required_prompts */
  2573. "SHA256", /* glob_context */
  2574. &scram_client_mech_new, /* mech_new */
  2575. &scram_client_mech_step, /* mech_step */
  2576. &scram_client_mech_dispose, /* mech_dispose */
  2577. NULL, /* mech_free */
  2578. NULL, /* idle */
  2579. NULL, /* spare */
  2580. NULL /* spare */
  2581. },
  2582. {
  2583. "SCRAM-SHA-224", /* mech_name */
  2584. 0, /* max_ssf */
  2585. SASL_SET_HASH_STRENGTH_BITS(224) |
  2586. SASL_SEC_NOPLAINTEXT
  2587. | SASL_SEC_NOANONYMOUS
  2588. | SASL_SEC_NOACTIVE
  2589. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  2590. SASL_FEAT_ALLOWS_PROXY
  2591. | SASL_FEAT_SUPPORTS_HTTP
  2592. | SASL_FEAT_CHANNEL_BINDING, /* features */
  2593. NULL, /* required_prompts */
  2594. "SHA224", /* glob_context */
  2595. &scram_client_mech_new, /* mech_new */
  2596. &scram_client_mech_step, /* mech_step */
  2597. &scram_client_mech_dispose, /* mech_dispose */
  2598. NULL, /* mech_free */
  2599. NULL, /* idle */
  2600. NULL, /* spare */
  2601. NULL /* spare */
  2602. },
  2603. #endif
  2604. {
  2605. "SCRAM-SHA-1", /* mech_name */
  2606. 0, /* max_ssf */
  2607. SASL_SET_HASH_STRENGTH_BITS(160) |
  2608. SASL_SEC_NOPLAINTEXT
  2609. | SASL_SEC_NOANONYMOUS
  2610. | SASL_SEC_NOACTIVE
  2611. | SASL_SEC_MUTUAL_AUTH, /* security_flags */
  2612. SASL_FEAT_ALLOWS_PROXY
  2613. | SASL_FEAT_SUPPORTS_HTTP
  2614. | SASL_FEAT_CHANNEL_BINDING, /* features */
  2615. NULL, /* required_prompts */
  2616. "SHA1", /* glob_context */
  2617. &scram_client_mech_new, /* mech_new */
  2618. &scram_client_mech_step, /* mech_step */
  2619. &scram_client_mech_dispose, /* mech_dispose */
  2620. NULL, /* mech_free */
  2621. NULL, /* idle */
  2622. NULL, /* spare */
  2623. NULL /* spare */
  2624. }
  2625. };
  2626. int scram_client_plug_init(const sasl_utils_t *utils,
  2627. int maxversion,
  2628. int *out_version,
  2629. sasl_client_plug_t **pluglist,
  2630. int *plugcount)
  2631. {
  2632. if (maxversion < SASL_CLIENT_PLUG_VERSION) {
  2633. SETERROR( utils, "SCRAM-SHA-* version mismatch");
  2634. return SASL_BADVERS;
  2635. }
  2636. *out_version = SASL_CLIENT_PLUG_VERSION;
  2637. *pluglist = scram_client_plugins;
  2638. #ifdef HAVE_SHA512
  2639. *plugcount = 5;
  2640. #else
  2641. *plugcount = 1;
  2642. #endif
  2643. return SASL_OK;
  2644. }