tls1.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  1. /*
  2. * Copyright (c) 2007-2017, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * Common ssl/tlsv1 code to both the client and server implementations.
  32. */
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <stdarg.h>
  37. #include "os_port.h"
  38. #include "ssl.h"
  39. /* The session expiry time */
  40. #define SSL_EXPIRY_TIME (CONFIG_SSL_EXPIRY_TIME*3600)
  41. static const uint8_t g_hello_request[] = { HS_HELLO_REQUEST, 0, 0, 0 };
  42. static const uint8_t g_chg_cipher_spec_pkt[] = { 1 };
  43. static const char * server_finished = "server finished";
  44. static const char * client_finished = "client finished";
  45. static int do_handshake(SSL *ssl, uint8_t *buf, int read_len);
  46. static int set_key_block(SSL *ssl, int is_write);
  47. static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len);
  48. static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt);
  49. static int send_raw_packet(SSL *ssl, uint8_t protocol);
  50. static int check_certificate_chain(SSL *ssl);
  51. /**
  52. * The server will pick the cipher based on the order that the order that the
  53. * ciphers are listed. This order is defined at compile time.
  54. */
  55. #ifndef CONFIG_SSL_SKELETON_MODE
  56. static void session_free(SSL_SESSION *ssl_sessions[], int sess_index);
  57. #endif
  58. const uint8_t ssl_prot_prefs[NUM_PROTOCOLS] =
  59. #ifdef CONFIG_SSL_PROT_LOW /* low security, fast speed */
  60. { SSL_AES128_SHA, SSL_AES128_SHA256, SSL_AES256_SHA, SSL_AES256_SHA256 };
  61. #elif CONFIG_SSL_PROT_MEDIUM /* medium security, medium speed */
  62. { SSL_AES128_SHA256, SSL_AES256_SHA256, SSL_AES256_SHA, SSL_AES128_SHA };
  63. #else /* CONFIG_SSL_PROT_HIGH */ /* high security, low speed */
  64. { SSL_AES256_SHA256, SSL_AES128_SHA256, SSL_AES256_SHA, SSL_AES128_SHA };
  65. #endif
  66. /**
  67. * The cipher map containing all the essentials for each cipher.
  68. */
  69. static const cipher_info_t cipher_info[NUM_PROTOCOLS] =
  70. {
  71. { /* AES128-SHA */
  72. SSL_AES128_SHA, /* AES128-SHA */
  73. 16, /* key size */
  74. 16, /* iv size */
  75. 16, /* block padding size */
  76. SHA1_SIZE, /* digest size */
  77. 2*(SHA1_SIZE+16+16), /* key block size */
  78. hmac_sha1, /* hmac algorithm */
  79. (crypt_func)AES_cbc_encrypt, /* encrypt */
  80. (crypt_func)AES_cbc_decrypt /* decrypt */
  81. },
  82. { /* AES256-SHA */
  83. SSL_AES256_SHA, /* AES256-SHA */
  84. 32, /* key size */
  85. 16, /* iv size */
  86. 16, /* block padding size */
  87. SHA1_SIZE, /* digest size */
  88. 2*(SHA1_SIZE+32+16), /* key block size */
  89. hmac_sha1, /* hmac algorithm */
  90. (crypt_func)AES_cbc_encrypt, /* encrypt */
  91. (crypt_func)AES_cbc_decrypt /* decrypt */
  92. },
  93. { /* AES128-SHA256 */
  94. SSL_AES128_SHA256, /* AES128-SHA256 */
  95. 16, /* key size */
  96. 16, /* iv size */
  97. 16, /* block padding size */
  98. SHA256_SIZE, /* digest size */
  99. 2*(SHA256_SIZE+32+16), /* key block size */
  100. hmac_sha256, /* hmac algorithm */
  101. (crypt_func)AES_cbc_encrypt, /* encrypt */
  102. (crypt_func)AES_cbc_decrypt /* decrypt */
  103. },
  104. { /* AES256-SHA256 */
  105. SSL_AES256_SHA256, /* AES256-SHA256 */
  106. 32, /* key size */
  107. 16, /* iv size */
  108. 16, /* block padding size */
  109. SHA256_SIZE, /* digest size */
  110. 2*(SHA256_SIZE+32+16), /* key block size */
  111. hmac_sha256, /* hmac algorithm */
  112. (crypt_func)AES_cbc_encrypt, /* encrypt */
  113. (crypt_func)AES_cbc_decrypt /* decrypt */
  114. }
  115. };
  116. static void prf(SSL *ssl, const uint8_t *sec, int sec_len,
  117. uint8_t *seed, int seed_len,
  118. uint8_t *out, int olen);
  119. static const cipher_info_t *get_cipher_info(uint8_t cipher);
  120. static void increment_read_sequence(SSL *ssl);
  121. static void increment_write_sequence(SSL *ssl);
  122. static void add_hmac_digest(SSL *ssl, int snd, uint8_t *hmac_header,
  123. const uint8_t *buf, int buf_len, uint8_t *hmac_buf);
  124. /* win32 VC6.0 doesn't have variadic macros */
  125. #if defined(WIN32) && !defined(CONFIG_SSL_FULL_MODE)
  126. void DISPLAY_BYTES(SSL *ssl, const char *format,
  127. const uint8_t *data, int size, ...) {}
  128. #endif
  129. /**
  130. * Establish a new client/server context.
  131. */
  132. EXP_FUNC SSL_CTX *LIB_CALLTYPE ssl_ctx_new_with_load_params(
  133. uint32_t options, int num_sessions,
  134. const char *ssl_private_key,
  135. size_t ssl_private_key_len,
  136. const char *ssl_private_key_password,
  137. const char *ssl_x509_cert,
  138. size_t ssl_x509_cert_len)
  139. {
  140. SSL_CTX *ssl_ctx = (SSL_CTX *)calloc(1, sizeof (SSL_CTX));
  141. ssl_ctx->options = options;
  142. RNG_initialize();
  143. if (load_key_certs_with_params(ssl_ctx, ssl_private_key, ssl_private_key_len,
  144. ssl_private_key_password, ssl_x509_cert, ssl_x509_cert_len) < 0)
  145. {
  146. free(ssl_ctx); /* can't load our key/certificate pair, so die */
  147. return NULL;
  148. }
  149. #ifndef CONFIG_SSL_SKELETON_MODE
  150. ssl_ctx->num_sessions = num_sessions;
  151. #endif
  152. SSL_CTX_MUTEX_INIT(ssl_ctx->mutex);
  153. #ifndef CONFIG_SSL_SKELETON_MODE
  154. if (num_sessions)
  155. {
  156. ssl_ctx->ssl_sessions = (SSL_SESSION **)
  157. calloc(1, num_sessions*sizeof(SSL_SESSION *));
  158. }
  159. #endif
  160. return ssl_ctx;
  161. }
  162. EXP_FUNC SSL_CTX *LIB_CALLTYPE ssl_ctx_new(uint32_t options, int num_sessions)
  163. {
  164. return ssl_ctx_new_with_load_params(options, num_sessions, 0, 0, 0, 0, 0);
  165. }
  166. /*
  167. * Remove a client/server context.
  168. */
  169. EXP_FUNC void LIB_CALLTYPE ssl_ctx_free(SSL_CTX *ssl_ctx)
  170. {
  171. SSL *ssl;
  172. int i;
  173. if (ssl_ctx == NULL)
  174. return;
  175. ssl = ssl_ctx->head;
  176. /* clear out all the ssl entries */
  177. while (ssl)
  178. {
  179. SSL *next = ssl->next;
  180. ssl_free(ssl);
  181. ssl = next;
  182. }
  183. #ifndef CONFIG_SSL_SKELETON_MODE
  184. /* clear out all the sessions */
  185. for (i = 0; i < ssl_ctx->num_sessions; i++)
  186. session_free(ssl_ctx->ssl_sessions, i);
  187. free(ssl_ctx->ssl_sessions);
  188. #endif
  189. i = 0;
  190. while (i < CONFIG_SSL_MAX_CERTS && ssl_ctx->certs[i].buf)
  191. {
  192. free(ssl_ctx->certs[i].buf);
  193. ssl_ctx->certs[i++].buf = NULL;
  194. }
  195. #ifdef CONFIG_SSL_CERT_VERIFICATION
  196. remove_ca_certs(ssl_ctx->ca_cert_ctx);
  197. #endif
  198. ssl_ctx->chain_length = 0;
  199. SSL_CTX_MUTEX_DESTROY(ssl_ctx->mutex);
  200. RSA_free(ssl_ctx->rsa_ctx);
  201. RNG_terminate();
  202. free(ssl_ctx);
  203. }
  204. /*
  205. * Free any used resources used by this connection.
  206. */
  207. EXP_FUNC void LIB_CALLTYPE ssl_free(SSL *ssl)
  208. {
  209. SSL_CTX *ssl_ctx;
  210. if (ssl == NULL) /* just ignore null pointers */
  211. return;
  212. /* only notify if we weren't notified first */
  213. /* spec says we must notify when we are dying */
  214. if (!IS_SET_SSL_FLAG(SSL_SENT_CLOSE_NOTIFY))
  215. send_alert(ssl, SSL_ALERT_CLOSE_NOTIFY);
  216. ssl_ctx = ssl->ssl_ctx;
  217. SSL_CTX_LOCK(ssl_ctx->mutex);
  218. /* adjust the server SSL list */
  219. if (ssl->prev)
  220. ssl->prev->next = ssl->next;
  221. else
  222. ssl_ctx->head = ssl->next;
  223. if (ssl->next)
  224. ssl->next->prev = ssl->prev;
  225. else
  226. ssl_ctx->tail = ssl->prev;
  227. SSL_CTX_UNLOCK(ssl_ctx->mutex);
  228. /* may already be free - but be sure */
  229. free(ssl->encrypt_ctx);
  230. ssl->encrypt_ctx = NULL;
  231. free(ssl->decrypt_ctx);
  232. ssl->decrypt_ctx = NULL;
  233. disposable_free(ssl);
  234. #ifdef CONFIG_SSL_CERT_VERIFICATION
  235. x509_free(ssl->x509_ctx);
  236. #endif
  237. //ssl_ext_free(ssl->extensions);
  238. ssl->extensions = NULL;
  239. free(ssl);
  240. }
  241. /*
  242. * Read the SSL connection and send any alerts for various errors.
  243. */
  244. EXP_FUNC int LIB_CALLTYPE ssl_read(SSL *ssl, uint8_t **in_data, int in_num)
  245. {
  246. int ret = basic_read(ssl, in_data, in_num);
  247. /* check for return code so we can send an alert */
  248. if (ret < SSL_OK && ret != SSL_CLOSE_NOTIFY)
  249. {
  250. if (ret != SSL_ERROR_CONN_LOST)
  251. {
  252. send_alert(ssl, ret);
  253. #ifndef CONFIG_SSL_SKELETON_MODE
  254. /* something nasty happened, so get rid of this session */
  255. kill_ssl_session(ssl->ssl_ctx->ssl_sessions, ssl);
  256. #endif
  257. }
  258. }
  259. return ret;
  260. }
  261. /*
  262. * Write application data to the client
  263. */
  264. EXP_FUNC int LIB_CALLTYPE ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
  265. {
  266. int n = out_len, nw, i, tot = 0;
  267. /* maximum size of a TLS packet is around 16kB, so fragment */
  268. do
  269. {
  270. nw = n;
  271. if (nw > RT_MAX_PLAIN_LENGTH) /* fragment if necessary */
  272. nw = RT_MAX_PLAIN_LENGTH;
  273. if ((i = send_packet(ssl, PT_APP_PROTOCOL_DATA,
  274. &out_data[tot], nw)) <= 0)
  275. {
  276. out_len = i; /* an error */
  277. break;
  278. }
  279. tot += i;
  280. n -= i;
  281. } while (n > 0);
  282. return out_len;
  283. }
  284. /**
  285. * Add a certificate to the certificate chain.
  286. */
  287. int add_cert(SSL_CTX *ssl_ctx, const uint8_t *buf, int len)
  288. {
  289. int ret = SSL_ERROR_NO_CERT_DEFINED, i = 0;
  290. SSL_CERT *ssl_cert;
  291. X509_CTX *cert = NULL;
  292. int offset;
  293. while (i < CONFIG_SSL_MAX_CERTS && ssl_ctx->certs[i].buf)
  294. i++;
  295. if (i == CONFIG_SSL_MAX_CERTS) /* too many certs */
  296. {
  297. #ifdef CONFIG_SSL_FULL_MODE
  298. printf("Error: maximum number of certs added (%d) - change of "
  299. "compile-time configuration required\n",
  300. CONFIG_SSL_MAX_CERTS);
  301. #endif
  302. goto error;
  303. }
  304. if ((ret = x509_new(buf, &offset, &cert)))
  305. goto error;
  306. #if defined (CONFIG_SSL_FULL_MODE)
  307. if (ssl_ctx->options & SSL_DISPLAY_CERTS)
  308. x509_print(cert, NULL);
  309. #endif
  310. ssl_cert = &ssl_ctx->certs[i];
  311. ssl_cert->size = len;
  312. ssl_cert->buf = (uint8_t *)malloc(len);
  313. switch (cert->sig_type)
  314. {
  315. case SIG_TYPE_SHA1:
  316. ssl_cert->hash_alg = SIG_ALG_SHA1;
  317. break;
  318. case SIG_TYPE_SHA256:
  319. ssl_cert->hash_alg = SIG_ALG_SHA256;
  320. break;
  321. case SIG_TYPE_SHA384:
  322. ssl_cert->hash_alg = SIG_ALG_SHA384;
  323. break;
  324. case SIG_TYPE_SHA512:
  325. ssl_cert->hash_alg = SIG_ALG_SHA512;
  326. break;
  327. }
  328. memcpy(ssl_cert->buf, buf, len);
  329. ssl_ctx->chain_length++;
  330. len -= offset;
  331. ret = SSL_OK; /* ok so far */
  332. /* recurse? */
  333. if (len > 0)
  334. {
  335. ret = add_cert(ssl_ctx, &buf[offset], len);
  336. }
  337. error:
  338. x509_free(cert); /* don't need anymore */
  339. return ret;
  340. }
  341. #ifdef CONFIG_SSL_CERT_VERIFICATION
  342. /**
  343. * Add a certificate authority.
  344. */
  345. int add_cert_auth(SSL_CTX *ssl_ctx, const uint8_t *buf, int len)
  346. {
  347. int ret = X509_OK; /* ignore errors for now */
  348. int i = 0;
  349. CA_CERT_CTX *ca_cert_ctx;
  350. if (ssl_ctx->ca_cert_ctx == NULL)
  351. ssl_ctx->ca_cert_ctx = (CA_CERT_CTX *)calloc(1, sizeof(CA_CERT_CTX));
  352. ca_cert_ctx = ssl_ctx->ca_cert_ctx;
  353. while (i < CONFIG_X509_MAX_CA_CERTS && ca_cert_ctx->cert[i])
  354. i++;
  355. while (len > 0)
  356. {
  357. int offset;
  358. if (i >= CONFIG_X509_MAX_CA_CERTS)
  359. {
  360. #ifdef CONFIG_SSL_FULL_MODE
  361. printf("Error: maximum number of CA certs added (%d) - change of "
  362. "compile-time configuration required\n",
  363. CONFIG_X509_MAX_CA_CERTS);
  364. #endif
  365. ret = X509_MAX_CERTS;
  366. break;
  367. }
  368. /* ignore the return code */
  369. if (x509_new(buf, &offset, &ca_cert_ctx->cert[i]) == X509_OK)
  370. {
  371. #if defined (CONFIG_SSL_FULL_MODE)
  372. if (ssl_ctx->options & SSL_DISPLAY_CERTS)
  373. x509_print(ca_cert_ctx->cert[i], NULL);
  374. #endif
  375. }
  376. i++;
  377. len -= offset;
  378. }
  379. return ret;
  380. }
  381. /*
  382. * Retrieve an X.509 distinguished name component
  383. */
  384. EXP_FUNC const char * LIB_CALLTYPE ssl_get_cert_dn(const SSL *ssl, int component)
  385. {
  386. if (ssl->x509_ctx == NULL)
  387. return NULL;
  388. switch (component)
  389. {
  390. case SSL_X509_CERT_COMMON_NAME:
  391. return ssl->x509_ctx->cert_dn[X509_COMMON_NAME];
  392. case SSL_X509_CERT_ORGANIZATION:
  393. return ssl->x509_ctx->cert_dn[X509_ORGANIZATION];
  394. case SSL_X509_CERT_ORGANIZATIONAL_NAME:
  395. return ssl->x509_ctx->cert_dn[X509_ORGANIZATIONAL_UNIT];
  396. case SSL_X509_CERT_LOCATION:
  397. return ssl->x509_ctx->cert_dn[X509_LOCATION];
  398. case SSL_X509_CERT_COUNTRY:
  399. return ssl->x509_ctx->cert_dn[X509_COUNTRY];
  400. case SSL_X509_CERT_STATE:
  401. return ssl->x509_ctx->cert_dn[X509_STATE];
  402. case SSL_X509_CA_CERT_COMMON_NAME:
  403. return ssl->x509_ctx->ca_cert_dn[X509_COMMON_NAME];
  404. case SSL_X509_CA_CERT_ORGANIZATION:
  405. return ssl->x509_ctx->ca_cert_dn[X509_ORGANIZATION];
  406. case SSL_X509_CA_CERT_ORGANIZATIONAL_NAME:
  407. return ssl->x509_ctx->ca_cert_dn[X509_ORGANIZATIONAL_UNIT];
  408. case SSL_X509_CA_CERT_LOCATION:
  409. return ssl->x509_ctx->ca_cert_dn[X509_LOCATION];
  410. case SSL_X509_CA_CERT_COUNTRY:
  411. return ssl->x509_ctx->ca_cert_dn[X509_COUNTRY];
  412. case SSL_X509_CA_CERT_STATE:
  413. return ssl->x509_ctx->ca_cert_dn[X509_STATE];
  414. default:
  415. return NULL;
  416. }
  417. }
  418. /*
  419. * Retrieve a "Subject Alternative Name" from a v3 certificate
  420. */
  421. EXP_FUNC const char * LIB_CALLTYPE ssl_get_cert_subject_alt_dnsname(const SSL *ssl,
  422. int dnsindex)
  423. {
  424. int i;
  425. if (ssl->x509_ctx == NULL || ssl->x509_ctx->subject_alt_dnsnames == NULL)
  426. return NULL;
  427. for (i = 0; i < dnsindex; ++i)
  428. {
  429. if (ssl->x509_ctx->subject_alt_dnsnames[i] == NULL)
  430. return NULL;
  431. }
  432. return ssl->x509_ctx->subject_alt_dnsnames[dnsindex];
  433. }
  434. #endif /* CONFIG_SSL_CERT_VERIFICATION */
  435. /*
  436. * Find an ssl object based on the client's file descriptor.
  437. */
  438. EXP_FUNC SSL * LIB_CALLTYPE ssl_find(SSL_CTX *ssl_ctx, int client_fd)
  439. {
  440. SSL *ssl;
  441. SSL_CTX_LOCK(ssl_ctx->mutex);
  442. ssl = ssl_ctx->head;
  443. /* search through all the ssl entries */
  444. while (ssl)
  445. {
  446. if (ssl->client_fd == client_fd)
  447. {
  448. SSL_CTX_UNLOCK(ssl_ctx->mutex);
  449. return ssl;
  450. }
  451. ssl = ssl->next;
  452. }
  453. SSL_CTX_UNLOCK(ssl_ctx->mutex);
  454. return NULL;
  455. }
  456. /*
  457. * Force the client to perform its handshake again.
  458. */
  459. EXP_FUNC int LIB_CALLTYPE ssl_renegotiate(SSL *ssl)
  460. {
  461. int ret = SSL_OK;
  462. disposable_new(ssl);
  463. #ifdef CONFIG_SSL_ENABLE_CLIENT
  464. if (IS_SET_SSL_FLAG(SSL_IS_CLIENT))
  465. {
  466. ret = do_client_connect(ssl);
  467. }
  468. else
  469. #endif
  470. {
  471. send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
  472. g_hello_request, sizeof(g_hello_request));
  473. SET_SSL_FLAG(SSL_NEED_RECORD);
  474. }
  475. return ret;
  476. }
  477. /**
  478. * @brief Get what we need for key info.
  479. * @param cipher [in] The cipher information we are after
  480. * @param key_size [out] The key size for the cipher
  481. * @param iv_size [out] The iv size for the cipher
  482. * @return The amount of key information we need.
  483. */
  484. static const cipher_info_t *get_cipher_info(uint8_t cipher)
  485. {
  486. int i;
  487. for (i = 0; i < NUM_PROTOCOLS; i++)
  488. {
  489. if (cipher_info[i].cipher == cipher)
  490. {
  491. return &cipher_info[i];
  492. }
  493. }
  494. return NULL; /* error */
  495. }
  496. /*
  497. * Get a new ssl context for a new connection.
  498. */
  499. SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
  500. {
  501. SSL *ssl = (SSL *)calloc(1, sizeof(SSL));
  502. ssl->ssl_ctx = ssl_ctx;
  503. ssl->need_bytes = SSL_RECORD_SIZE; /* need a record */
  504. ssl->client_fd = client_fd;
  505. ssl->flag = SSL_NEED_RECORD;
  506. ssl->bm_data = ssl->bm_all_data+BM_RECORD_OFFSET; /* space at the start */
  507. ssl->hs_status = SSL_NOT_OK; /* not connected */
  508. #ifdef CONFIG_ENABLE_VERIFICATION
  509. ssl->ca_cert_ctx = ssl_ctx->ca_cert_ctx;
  510. #endif
  511. disposable_new(ssl);
  512. /* a bit hacky but saves a few bytes of memory */
  513. ssl->flag |= ssl_ctx->options;
  514. SSL_CTX_LOCK(ssl_ctx->mutex);
  515. if (ssl_ctx->head == NULL)
  516. {
  517. ssl_ctx->head = ssl;
  518. ssl_ctx->tail = ssl;
  519. }
  520. else
  521. {
  522. ssl->prev = ssl_ctx->tail;
  523. ssl_ctx->tail->next = ssl;
  524. ssl_ctx->tail = ssl;
  525. }
  526. SSL_CTX_UNLOCK(ssl_ctx->mutex);
  527. return ssl;
  528. }
  529. /*
  530. * Add a private key to a context.
  531. */
  532. int add_private_key(SSL_CTX *ssl_ctx, SSLObjLoader *ssl_obj)
  533. {
  534. int ret = SSL_OK;
  535. /* get the private key details */
  536. if (asn1_get_private_key(ssl_obj->buf, ssl_obj->len, &ssl_ctx->rsa_ctx))
  537. {
  538. ret = SSL_ERROR_INVALID_KEY;
  539. goto error;
  540. }
  541. error:
  542. return ret;
  543. }
  544. /**
  545. * Increment the read sequence number (as a 64 bit endian indepenent #)
  546. */
  547. static void increment_read_sequence(SSL *ssl)
  548. {
  549. int i;
  550. for (i = 7; i >= 0; i--)
  551. {
  552. if (++ssl->read_sequence[i])
  553. break;
  554. }
  555. }
  556. /**
  557. * Increment the read sequence number (as a 64 bit endian indepenent #)
  558. */
  559. static void increment_write_sequence(SSL *ssl)
  560. {
  561. int i;
  562. for (i = 7; i >= 0; i--)
  563. {
  564. if (++ssl->write_sequence[i])
  565. break;
  566. }
  567. }
  568. /**
  569. * Work out the HMAC digest in a packet.
  570. */
  571. static void add_hmac_digest(SSL *ssl, int mode, uint8_t *hmac_header,
  572. const uint8_t *buf, int buf_len, uint8_t *hmac_buf)
  573. {
  574. int hmac_len = buf_len + 8 + SSL_RECORD_SIZE;
  575. uint8_t *t_buf = (uint8_t *)alloca(hmac_len);
  576. memcpy(t_buf, (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE) ?
  577. ssl->write_sequence : ssl->read_sequence, 8);
  578. memcpy(&t_buf[8], hmac_header, SSL_RECORD_SIZE);
  579. memcpy(&t_buf[8+SSL_RECORD_SIZE], buf, buf_len);
  580. ssl->cipher_info->hmac(t_buf, hmac_len,
  581. (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ) ?
  582. ssl->server_mac : ssl->client_mac,
  583. ssl->cipher_info->digest_size, hmac_buf);
  584. #if 0
  585. print_blob("record", hmac_header, SSL_RECORD_SIZE);
  586. print_blob("buf", buf, buf_len);
  587. if (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE)
  588. {
  589. print_blob("write seq", ssl->write_sequence, 8);
  590. }
  591. else
  592. {
  593. print_blob("read seq", ssl->read_sequence, 8);
  594. }
  595. if (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ)
  596. {
  597. print_blob("server mac",
  598. ssl->server_mac, ssl->cipher_info->digest_size);
  599. }
  600. else
  601. {
  602. print_blob("client mac",
  603. ssl->client_mac, ssl->cipher_info->digest_size);
  604. }
  605. print_blob("hmac", hmac_buf, ssl->cipher_info->digest_size);
  606. #endif
  607. }
  608. /**
  609. * Verify that the digest of a packet is correct.
  610. */
  611. static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len)
  612. {
  613. uint8_t hmac_buf[SHA256_SIZE]; // size of largest digest
  614. int hmac_offset;
  615. int last_blk_size = buf[read_len-1], i;
  616. hmac_offset = read_len-last_blk_size-ssl->cipher_info->digest_size-1;
  617. /* guard against a timing attack - make sure we do the digest */
  618. if (hmac_offset < 0)
  619. {
  620. hmac_offset = 0;
  621. }
  622. else
  623. {
  624. /* already looked at last byte */
  625. for (i = 1; i < last_blk_size; i++)
  626. {
  627. if (buf[read_len-i] != last_blk_size)
  628. {
  629. hmac_offset = 0;
  630. break;
  631. }
  632. }
  633. }
  634. /* sanity check the offset */
  635. ssl->hmac_header[3] = hmac_offset >> 8; /* insert size */
  636. ssl->hmac_header[4] = hmac_offset & 0xff;
  637. add_hmac_digest(ssl, mode, ssl->hmac_header, buf, hmac_offset, hmac_buf);
  638. if (memcmp(hmac_buf, &buf[hmac_offset], ssl->cipher_info->digest_size))
  639. {
  640. return SSL_ERROR_INVALID_HMAC;
  641. }
  642. return hmac_offset;
  643. }
  644. /**
  645. * Add a packet to the end of our sent and received packets, so that we may use
  646. * it to calculate the hash at the end.
  647. */
  648. void add_packet(SSL *ssl, const uint8_t *pkt, int len)
  649. {
  650. // TLS1.2+
  651. if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2 || ssl->version == 0)
  652. {
  653. SHA256_Update(&ssl->dc->sha256_ctx, pkt, len);
  654. }
  655. if (ssl->version < SSL_PROTOCOL_VERSION_TLS1_2 ||
  656. ssl->next_state == HS_SERVER_HELLO ||
  657. ssl->next_state == 0)
  658. {
  659. MD5_Update(&ssl->dc->md5_ctx, pkt, len);
  660. SHA1_Update(&ssl->dc->sha1_ctx, pkt, len);
  661. }
  662. }
  663. /**
  664. * Work out the MD5 PRF.
  665. */
  666. static void p_hash_md5(const uint8_t *sec, int sec_len,
  667. uint8_t *seed, int seed_len, uint8_t *out, int olen)
  668. {
  669. uint8_t a1[MD5_SIZE+77];
  670. /* A(1) */
  671. hmac_md5(seed, seed_len, sec, sec_len, a1);
  672. memcpy(&a1[MD5_SIZE], seed, seed_len);
  673. hmac_md5(a1, MD5_SIZE+seed_len, sec, sec_len, out);
  674. while (olen > MD5_SIZE)
  675. {
  676. uint8_t a2[MD5_SIZE];
  677. out += MD5_SIZE;
  678. olen -= MD5_SIZE;
  679. /* A(N) */
  680. hmac_md5(a1, MD5_SIZE, sec, sec_len, a2);
  681. memcpy(a1, a2, MD5_SIZE);
  682. /* work out the actual hash */
  683. hmac_md5(a1, MD5_SIZE+seed_len, sec, sec_len, out);
  684. }
  685. }
  686. /**
  687. * Work out the SHA1 PRF.
  688. */
  689. static void p_hash_sha1(const uint8_t *sec, int sec_len,
  690. uint8_t *seed, int seed_len, uint8_t *out, int olen)
  691. {
  692. uint8_t a1[SHA1_SIZE+77];
  693. /* A(1) */
  694. hmac_sha1(seed, seed_len, sec, sec_len, a1);
  695. memcpy(&a1[SHA1_SIZE], seed, seed_len);
  696. hmac_sha1(a1, SHA1_SIZE+seed_len, sec, sec_len, out);
  697. while (olen > SHA1_SIZE)
  698. {
  699. uint8_t a2[SHA1_SIZE];
  700. out += SHA1_SIZE;
  701. olen -= SHA1_SIZE;
  702. /* A(N) */
  703. hmac_sha1(a1, SHA1_SIZE, sec, sec_len, a2);
  704. memcpy(a1, a2, SHA1_SIZE);
  705. /* work out the actual hash */
  706. hmac_sha1(a1, SHA1_SIZE+seed_len, sec, sec_len, out);
  707. }
  708. }
  709. /**
  710. * Work out the SHA256 PRF.
  711. */
  712. static void p_hash_sha256(const uint8_t *sec, int sec_len,
  713. uint8_t *seed, int seed_len, uint8_t *out, int olen)
  714. {
  715. uint8_t a1[SHA256_SIZE+77];
  716. /* A(1) */
  717. hmac_sha256(seed, seed_len, sec, sec_len, a1);
  718. memcpy(&a1[SHA256_SIZE], seed, seed_len);
  719. hmac_sha256(a1, SHA256_SIZE+seed_len, sec, sec_len, out);
  720. while (olen > SHA256_SIZE)
  721. {
  722. uint8_t a2[SHA256_SIZE];
  723. out += SHA256_SIZE;
  724. olen -= SHA256_SIZE;
  725. // A(N)
  726. hmac_sha256(a1, SHA256_SIZE, sec, sec_len, a2);
  727. memcpy(a1, a2, SHA256_SIZE);
  728. // work out the actual hash
  729. hmac_sha256(a1, SHA256_SIZE+seed_len, sec, sec_len, out);
  730. }
  731. }
  732. /**
  733. * Work out the PRF.
  734. */
  735. static void prf(SSL *ssl, const uint8_t *sec, int sec_len,
  736. uint8_t *seed, int seed_len,
  737. uint8_t *out, int olen)
  738. {
  739. if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
  740. {
  741. p_hash_sha256(sec, sec_len, seed, seed_len, out, olen);
  742. }
  743. else // TLS1.0/1.1
  744. {
  745. int len, i;
  746. const uint8_t *S1, *S2;
  747. uint8_t xbuf[2*(SHA256_SIZE+32+16) + MD5_SIZE]; /* max keyblock */
  748. uint8_t ybuf[2*(SHA256_SIZE+32+16) + SHA1_SIZE]; /* max keyblock */
  749. len = sec_len/2;
  750. S1 = sec;
  751. S2 = &sec[len];
  752. len += (sec_len & 1); /* add for odd, make longer */
  753. p_hash_md5(S1, len, seed, seed_len, xbuf, olen);
  754. p_hash_sha1(S2, len, seed, seed_len, ybuf, olen);
  755. for (i = 0; i < olen; i++)
  756. out[i] = xbuf[i] ^ ybuf[i];
  757. }
  758. }
  759. /**
  760. * Generate a master secret based on the client/server random data and the
  761. * premaster secret.
  762. */
  763. void generate_master_secret(SSL *ssl, const uint8_t *premaster_secret)
  764. {
  765. uint8_t buf[77];
  766. //print_blob("premaster secret", premaster_secret, 48);
  767. strcpy((char *)buf, "master secret");
  768. memcpy(&buf[13], ssl->dc->client_random, SSL_RANDOM_SIZE);
  769. memcpy(&buf[45], ssl->dc->server_random, SSL_RANDOM_SIZE);
  770. prf(ssl, premaster_secret, SSL_SECRET_SIZE, buf, 77, ssl->dc->master_secret,
  771. SSL_SECRET_SIZE);
  772. #if 0
  773. print_blob("client random", ssl->dc->client_random, 32);
  774. print_blob("server random", ssl->dc->server_random, 32);
  775. print_blob("master secret", ssl->dc->master_secret, 48);
  776. #endif
  777. }
  778. /**
  779. * Generate a 'random' blob of data used for the generation of keys.
  780. */
  781. static void generate_key_block(SSL *ssl,
  782. uint8_t *client_random, uint8_t *server_random,
  783. uint8_t *master_secret, uint8_t *key_block, int key_block_size)
  784. {
  785. uint8_t buf[77];
  786. strcpy((char *)buf, "key expansion");
  787. memcpy(&buf[13], server_random, SSL_RANDOM_SIZE);
  788. memcpy(&buf[45], client_random, SSL_RANDOM_SIZE);
  789. prf(ssl, master_secret, SSL_SECRET_SIZE, buf, 77,
  790. key_block, key_block_size);
  791. }
  792. /**
  793. * Calculate the digest used in the finished message. This function also
  794. * doubles up as a certificate verify function.
  795. */
  796. int finished_digest(SSL *ssl, const char *label, uint8_t *digest)
  797. {
  798. uint8_t mac_buf[SHA1_SIZE+MD5_SIZE+15];
  799. uint8_t *q = mac_buf;
  800. int dgst_len;
  801. if (label)
  802. {
  803. strcpy((char *)q, label);
  804. q += strlen(label);
  805. }
  806. if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2) // TLS1.2+
  807. {
  808. SHA256_CTX sha256_ctx = ssl->dc->sha256_ctx; // interim copy
  809. SHA256_Final(q, &sha256_ctx);
  810. q += SHA256_SIZE;
  811. dgst_len = (int)(q-mac_buf);
  812. }
  813. else // TLS1.0/1.1
  814. {
  815. MD5_CTX md5_ctx = ssl->dc->md5_ctx; // interim copy
  816. SHA1_CTX sha1_ctx = ssl->dc->sha1_ctx;
  817. MD5_Final(q, &md5_ctx);
  818. q += MD5_SIZE;
  819. SHA1_Final(q, &sha1_ctx);
  820. q += SHA1_SIZE;
  821. dgst_len = (int)(q-mac_buf);
  822. }
  823. if (label)
  824. {
  825. prf(ssl, ssl->dc->master_secret, SSL_SECRET_SIZE,
  826. mac_buf, dgst_len, digest, SSL_FINISHED_HASH_SIZE);
  827. }
  828. else /* for use in a certificate verify */
  829. {
  830. memcpy(digest, mac_buf, dgst_len);
  831. }
  832. #if 0
  833. printf("label: %s\n", label);
  834. print_blob("mac_buf", mac_buf, dgst_len);
  835. print_blob("finished digest", digest, SSL_FINISHED_HASH_SIZE);
  836. #endif
  837. return dgst_len;
  838. }
  839. /**
  840. * Retrieve (and initialise) the context of a cipher.
  841. */
  842. static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt)
  843. {
  844. switch (ssl->cipher)
  845. {
  846. case SSL_AES128_SHA:
  847. case SSL_AES128_SHA256:
  848. {
  849. AES_CTX *aes_ctx = (AES_CTX *)malloc(sizeof(AES_CTX));
  850. AES_set_key(aes_ctx, key, iv, AES_MODE_128);
  851. if (is_decrypt)
  852. {
  853. AES_convert_key(aes_ctx);
  854. }
  855. return (void *)aes_ctx;
  856. }
  857. case SSL_AES256_SHA:
  858. case SSL_AES256_SHA256:
  859. {
  860. AES_CTX *aes_ctx = (AES_CTX *)malloc(sizeof(AES_CTX));
  861. AES_set_key(aes_ctx, key, iv, AES_MODE_256);
  862. if (is_decrypt)
  863. {
  864. AES_convert_key(aes_ctx);
  865. }
  866. return (void *)aes_ctx;
  867. }
  868. }
  869. return NULL; /* its all gone wrong */
  870. }
  871. /**
  872. * Send a packet over the socket.
  873. */
  874. static int send_raw_packet(SSL *ssl, uint8_t protocol)
  875. {
  876. uint8_t *rec_buf = ssl->bm_all_data;
  877. int pkt_size = SSL_RECORD_SIZE+ssl->bm_index;
  878. int sent = 0;
  879. int ret = SSL_OK;
  880. rec_buf[0] = protocol;
  881. rec_buf[1] = 0x03; /* version = 3.1 or higher */
  882. rec_buf[2] = ssl->version & 0x0f;
  883. rec_buf[3] = ssl->bm_index >> 8;
  884. rec_buf[4] = ssl->bm_index & 0xff;
  885. DISPLAY_BYTES(ssl, "sending %d bytes", ssl->bm_all_data,
  886. pkt_size, pkt_size);
  887. while (sent < pkt_size)
  888. {
  889. ret = SOCKET_WRITE(ssl->client_fd,
  890. &ssl->bm_all_data[sent], pkt_size-sent);
  891. if (ret >= 0)
  892. sent += ret;
  893. else
  894. {
  895. #ifdef WIN32
  896. if (GetLastError() != WSAEWOULDBLOCK)
  897. #else
  898. if (errno != EAGAIN && errno != EWOULDBLOCK)
  899. #endif
  900. return SSL_ERROR_CONN_LOST;
  901. }
  902. /* keep going until the write buffer has some space */
  903. if (sent != pkt_size)
  904. {
  905. fd_set wfds;
  906. FD_ZERO(&wfds);
  907. FD_SET(ssl->client_fd, &wfds);
  908. /* block and wait for it */
  909. if (select(ssl->client_fd + 1, NULL, &wfds, NULL, NULL) < 0)
  910. return SSL_ERROR_CONN_LOST;
  911. }
  912. }
  913. SET_SSL_FLAG(SSL_NEED_RECORD); /* reset for next time */
  914. ssl->bm_index = 0;
  915. if (protocol != PT_APP_PROTOCOL_DATA)
  916. {
  917. /* always return SSL_OK during handshake */
  918. ret = SSL_OK;
  919. }
  920. return ret;
  921. }
  922. /**
  923. * Send an encrypted packet with padding bytes if necessary.
  924. */
  925. int send_packet(SSL *ssl, uint8_t protocol, const uint8_t *in, int length)
  926. {
  927. int ret, msg_length = 0;
  928. /* if our state is bad, don't bother */
  929. if (ssl->hs_status == SSL_ERROR_DEAD)
  930. return SSL_ERROR_CONN_LOST;
  931. if (IS_SET_SSL_FLAG(SSL_SENT_CLOSE_NOTIFY))
  932. return SSL_CLOSE_NOTIFY;
  933. if (in) /* has the buffer already been initialised? */
  934. {
  935. memcpy(ssl->bm_data, in, length);
  936. }
  937. msg_length += length;
  938. if (IS_SET_SSL_FLAG(SSL_TX_ENCRYPTED))
  939. {
  940. int mode = IS_SET_SSL_FLAG(SSL_IS_CLIENT) ?
  941. SSL_CLIENT_WRITE : SSL_SERVER_WRITE;
  942. uint8_t hmac_header[SSL_RECORD_SIZE] =
  943. {
  944. protocol,
  945. 0x03, /* version = 3.1 or higher */
  946. ssl->version & 0x0f,
  947. msg_length >> 8,
  948. msg_length & 0xff
  949. };
  950. if (protocol == PT_HANDSHAKE_PROTOCOL)
  951. {
  952. DISPLAY_STATE(ssl, 1, ssl->bm_data[0], 0);
  953. if (ssl->bm_data[0] != HS_HELLO_REQUEST)
  954. {
  955. add_packet(ssl, ssl->bm_data, msg_length);
  956. }
  957. }
  958. /* add the packet digest */
  959. add_hmac_digest(ssl, mode, hmac_header, ssl->bm_data, msg_length,
  960. &ssl->bm_data[msg_length]);
  961. msg_length += ssl->cipher_info->digest_size;
  962. /* add padding */
  963. {
  964. int last_blk_size = msg_length%ssl->cipher_info->padding_size;
  965. int pad_bytes = ssl->cipher_info->padding_size - last_blk_size;
  966. /* ensure we always have at least 1 padding byte */
  967. if (pad_bytes == 0)
  968. pad_bytes += ssl->cipher_info->padding_size;
  969. memset(&ssl->bm_data[msg_length], pad_bytes-1, pad_bytes);
  970. msg_length += pad_bytes;
  971. }
  972. DISPLAY_BYTES(ssl, "unencrypted write", ssl->bm_data, msg_length);
  973. increment_write_sequence(ssl);
  974. /* add the explicit IV for TLS1.1 */
  975. if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_1)
  976. {
  977. uint8_t iv_size = ssl->cipher_info->iv_size;
  978. uint8_t *t_buf = alloca(msg_length + iv_size);
  979. memcpy(t_buf + iv_size, ssl->bm_data, msg_length);
  980. if (get_random(iv_size, t_buf) < 0)
  981. return SSL_NOT_OK;
  982. msg_length += iv_size;
  983. memcpy(ssl->bm_data, t_buf, msg_length);
  984. }
  985. /* now encrypt the packet */
  986. ssl->cipher_info->encrypt(ssl->encrypt_ctx, ssl->bm_data,
  987. ssl->bm_data, msg_length);
  988. }
  989. else if (protocol == PT_HANDSHAKE_PROTOCOL)
  990. {
  991. DISPLAY_STATE(ssl, 1, ssl->bm_data[0], 0);
  992. if (ssl->bm_data[0] != HS_HELLO_REQUEST)
  993. {
  994. add_packet(ssl, ssl->bm_data, length);
  995. }
  996. }
  997. ssl->bm_index = msg_length;
  998. if ((ret = send_raw_packet(ssl, protocol)) <= 0)
  999. return ret;
  1000. return length; /* just return what we wanted to send */
  1001. }
  1002. /**
  1003. * Work out the cipher keys we are going to use for this session based on the
  1004. * master secret.
  1005. */
  1006. static int set_key_block(SSL *ssl, int is_write)
  1007. {
  1008. const cipher_info_t *ciph_info = get_cipher_info(ssl->cipher);
  1009. uint8_t *q;
  1010. uint8_t client_key[32], server_key[32]; /* big enough for AES256 */
  1011. uint8_t client_iv[16], server_iv[16]; /* big enough for AES128/256 */
  1012. int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
  1013. if (ciph_info == NULL)
  1014. return -1;
  1015. /* only do once in a handshake */
  1016. if (!ssl->dc->key_block_generated)
  1017. {
  1018. generate_key_block(ssl, ssl->dc->client_random, ssl->dc->server_random,
  1019. ssl->dc->master_secret, ssl->dc->key_block,
  1020. ciph_info->key_block_size);
  1021. #if 0
  1022. print_blob("master", ssl->dc->master_secret, SSL_SECRET_SIZE);
  1023. print_blob("keyblock", ssl->dc->key_block, ciph_info->key_block_size);
  1024. print_blob("client random", ssl->dc->client_random, 32);
  1025. print_blob("server random", ssl->dc->server_random, 32);
  1026. #endif
  1027. ssl->dc->key_block_generated = 1;
  1028. }
  1029. q = ssl->dc->key_block;
  1030. if ((is_client && is_write) || (!is_client && !is_write))
  1031. {
  1032. memcpy(ssl->client_mac, q, ciph_info->digest_size);
  1033. }
  1034. q += ciph_info->digest_size;
  1035. if ((!is_client && is_write) || (is_client && !is_write))
  1036. {
  1037. memcpy(ssl->server_mac, q, ciph_info->digest_size);
  1038. }
  1039. q += ciph_info->digest_size;
  1040. memcpy(client_key, q, ciph_info->key_size);
  1041. q += ciph_info->key_size;
  1042. memcpy(server_key, q, ciph_info->key_size);
  1043. q += ciph_info->key_size;
  1044. memcpy(client_iv, q, ciph_info->iv_size);
  1045. q += ciph_info->iv_size;
  1046. memcpy(server_iv, q, ciph_info->iv_size);
  1047. q += ciph_info->iv_size;
  1048. #if 0
  1049. print_blob("client key", client_key, ciph_info->key_size);
  1050. print_blob("server key", server_key, ciph_info->key_size);
  1051. print_blob("client iv", client_iv, ciph_info->iv_size);
  1052. print_blob("server iv", server_iv, ciph_info->iv_size);
  1053. #endif
  1054. free(is_write ? ssl->encrypt_ctx : ssl->decrypt_ctx);
  1055. /* now initialise the ciphers */
  1056. if (is_client)
  1057. {
  1058. finished_digest(ssl, server_finished, ssl->dc->final_finish_mac);
  1059. if (is_write)
  1060. ssl->encrypt_ctx = crypt_new(ssl, client_key, client_iv, 0);
  1061. else
  1062. ssl->decrypt_ctx = crypt_new(ssl, server_key, server_iv, 1);
  1063. }
  1064. else
  1065. {
  1066. finished_digest(ssl, client_finished, ssl->dc->final_finish_mac);
  1067. if (is_write)
  1068. ssl->encrypt_ctx = crypt_new(ssl, server_key, server_iv, 0);
  1069. else
  1070. ssl->decrypt_ctx = crypt_new(ssl, client_key, client_iv, 1);
  1071. }
  1072. ssl->cipher_info = ciph_info;
  1073. return 0;
  1074. }
  1075. /**
  1076. * Read the SSL connection.
  1077. * if the third paramenter "in_num" is 0 read as much as possible
  1078. */
  1079. int basic_read(SSL *ssl, uint8_t **in_data, int in_num)
  1080. {
  1081. int ret = SSL_OK;
  1082. if((in_num > 0) && ssl->read_data_in_buffer_bytes) {
  1083. int ret_val = 0;
  1084. *in_data = ssl->bm_read_data + ssl->read_data_in_buffer_start;
  1085. if(in_num >= ssl->read_data_in_buffer_bytes) {
  1086. ret_val = ssl->read_data_in_buffer_bytes;
  1087. ssl->read_data_in_buffer_start = ssl->read_data_in_buffer_bytes = 0;
  1088. } else { //in_num < ssl->data_in_buffer_bytes
  1089. ret_val = in_num;
  1090. ssl->read_data_in_buffer_start += in_num;
  1091. ssl->read_data_in_buffer_bytes -= in_num;
  1092. }
  1093. return ret_val;
  1094. }
  1095. int read_len, is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
  1096. uint8_t *buf = ssl->bm_data;
  1097. if (IS_SET_SSL_FLAG(SSL_SENT_CLOSE_NOTIFY))
  1098. return SSL_CLOSE_NOTIFY;
  1099. read_len = SOCKET_READ(ssl->client_fd, &buf[ssl->bm_read_index],
  1100. ssl->need_bytes-ssl->got_bytes);
  1101. if (read_len < 0)
  1102. {
  1103. #ifdef WIN32
  1104. if (GetLastError() == WSAEWOULDBLOCK)
  1105. #else
  1106. if (errno == EAGAIN || errno == EWOULDBLOCK)
  1107. #endif
  1108. return 0;
  1109. }
  1110. /* connection has gone, so die */
  1111. if (read_len <= 0)
  1112. {
  1113. ret = SSL_ERROR_CONN_LOST;
  1114. ssl->hs_status = SSL_ERROR_DEAD; /* make sure it stays dead */
  1115. goto error;
  1116. }
  1117. DISPLAY_BYTES(ssl, "received %d bytes",
  1118. &ssl->bm_data[ssl->bm_read_index], read_len, read_len);
  1119. ssl->got_bytes += read_len;
  1120. ssl->bm_read_index += read_len;
  1121. /* haven't quite got what we want, so try again later */
  1122. if (ssl->got_bytes < ssl->need_bytes)
  1123. return SSL_OK;
  1124. read_len = ssl->got_bytes;
  1125. ssl->got_bytes = 0;
  1126. if (IS_SET_SSL_FLAG(SSL_NEED_RECORD))
  1127. {
  1128. /* check for sslv2 "client hello" */
  1129. if ((buf[0] & 0x80) && (buf[2] == 1))
  1130. {
  1131. #ifdef CONFIG_SSL_FULL_MODE
  1132. printf("Error: no SSLv23 handshaking allowed\n");
  1133. #endif
  1134. ret = SSL_ERROR_NOT_SUPPORTED;
  1135. goto error; /* not an error - just get out of here */
  1136. }
  1137. ssl->need_bytes = (buf[3] << 8) + buf[4];
  1138. /* do we violate the spec with the message size? */
  1139. if (ssl->need_bytes > RT_MAX_PLAIN_LENGTH+RT_EXTRA-BM_RECORD_OFFSET)
  1140. {
  1141. ret = SSL_ERROR_RECORD_OVERFLOW;
  1142. goto error;
  1143. }
  1144. CLR_SSL_FLAG(SSL_NEED_RECORD);
  1145. memcpy(ssl->hmac_header, buf, 3); /* store for hmac */
  1146. ssl->record_type = buf[0];
  1147. goto error; /* no error, we're done */
  1148. }
  1149. /* for next time - just do it now in case of an error */
  1150. SET_SSL_FLAG(SSL_NEED_RECORD);
  1151. ssl->need_bytes = SSL_RECORD_SIZE;
  1152. /* decrypt if we need to */
  1153. if (IS_SET_SSL_FLAG(SSL_RX_ENCRYPTED))
  1154. {
  1155. ssl->cipher_info->decrypt(ssl->decrypt_ctx, buf, buf, read_len);
  1156. if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_1)
  1157. {
  1158. buf += ssl->cipher_info->iv_size;
  1159. read_len -= ssl->cipher_info->iv_size;
  1160. }
  1161. read_len = verify_digest(ssl,
  1162. is_client ? SSL_CLIENT_READ : SSL_SERVER_READ, buf, read_len);
  1163. /* does the hmac work? */
  1164. if (read_len < 0)
  1165. {
  1166. ret = read_len;
  1167. goto error;
  1168. }
  1169. DISPLAY_BYTES(ssl, "decrypted", buf, read_len);
  1170. increment_read_sequence(ssl);
  1171. }
  1172. /* The main part of the SSL packet */
  1173. switch (ssl->record_type)
  1174. {
  1175. case PT_HANDSHAKE_PROTOCOL:
  1176. if (ssl->dc != NULL)
  1177. {
  1178. ssl->dc->bm_proc_index = 0;
  1179. ret = do_handshake(ssl, buf, read_len);
  1180. }
  1181. else /* no client renegotiation allowed */
  1182. {
  1183. ret = SSL_ERROR_NO_CLIENT_RENOG;
  1184. goto error;
  1185. }
  1186. break;
  1187. case PT_CHANGE_CIPHER_SPEC:
  1188. if (ssl->next_state != HS_FINISHED)
  1189. {
  1190. ret = SSL_ERROR_INVALID_HANDSHAKE;
  1191. goto error;
  1192. }
  1193. if (set_key_block(ssl, 0) < 0)
  1194. {
  1195. ret = SSL_ERROR_INVALID_HANDSHAKE;
  1196. goto error;
  1197. }
  1198. /* all encrypted from now on */
  1199. SET_SSL_FLAG(SSL_RX_ENCRYPTED);
  1200. memset(ssl->read_sequence, 0, 8);
  1201. break;
  1202. case PT_APP_PROTOCOL_DATA:
  1203. if (in_data && ssl->hs_status == SSL_OK)
  1204. {
  1205. *in_data = buf; /* point to the work buffer */
  1206. (*in_data)[read_len] = 0; /* null terminate just in case */
  1207. if((in_num > 0) && (read_len > in_num)) {
  1208. ret = in_num;
  1209. ssl->read_data_in_buffer_start = 0;
  1210. ssl->read_data_in_buffer_bytes = read_len - in_num;
  1211. memcpy(ssl->bm_read_data, buf+in_num, ssl->read_data_in_buffer_bytes);
  1212. }
  1213. ret = read_len;
  1214. }
  1215. else
  1216. ret = SSL_ERROR_INVALID_PROT_MSG;
  1217. break;
  1218. case PT_ALERT_PROTOCOL:
  1219. /* return the alert # with alert bit set */
  1220. if(buf[0] == SSL_ALERT_TYPE_WARNING &&
  1221. buf[1] == SSL_ALERT_CLOSE_NOTIFY)
  1222. {
  1223. ret = SSL_CLOSE_NOTIFY;
  1224. send_alert(ssl, SSL_ALERT_CLOSE_NOTIFY);
  1225. SET_SSL_FLAG(SSL_SENT_CLOSE_NOTIFY);
  1226. }
  1227. else
  1228. {
  1229. ret = -buf[1];
  1230. DISPLAY_ALERT(ssl, buf[1]);
  1231. }
  1232. break;
  1233. default:
  1234. ret = SSL_ERROR_INVALID_PROT_MSG;
  1235. break;
  1236. }
  1237. error:
  1238. ssl->bm_read_index = 0; /* reset to go again */
  1239. if (ret < SSL_OK && in_data)/* if all wrong, then clear this buffer ptr */
  1240. *in_data = NULL;
  1241. return ret;
  1242. }
  1243. /**
  1244. * Do some basic checking of data and then perform the appropriate handshaking.
  1245. */
  1246. static int do_handshake(SSL *ssl, uint8_t *buf, int read_len)
  1247. {
  1248. int hs_len = (buf[2]<<8) + buf[3];
  1249. uint8_t handshake_type = buf[0];
  1250. int ret = SSL_OK;
  1251. int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
  1252. /* some integrity checking on the handshake */
  1253. PARANOIA_CHECK(read_len-SSL_HS_HDR_SIZE, hs_len);
  1254. if (handshake_type != ssl->next_state)
  1255. {
  1256. /* handle a special case on the client */
  1257. if (!is_client || handshake_type != HS_CERT_REQ ||
  1258. ssl->next_state != HS_SERVER_HELLO_DONE)
  1259. {
  1260. ret = SSL_ERROR_INVALID_HANDSHAKE;
  1261. goto error;
  1262. }
  1263. }
  1264. hs_len += SSL_HS_HDR_SIZE; /* adjust for when adding packets */
  1265. ssl->bm_index = hs_len; /* store the size and check later */
  1266. DISPLAY_STATE(ssl, 0, handshake_type, 0);
  1267. if (handshake_type != HS_CERT_VERIFY && handshake_type != HS_HELLO_REQUEST)
  1268. add_packet(ssl, buf, hs_len);
  1269. #if defined(CONFIG_SSL_ENABLE_CLIENT)
  1270. ret = is_client ?
  1271. do_clnt_handshake(ssl, handshake_type, buf, hs_len) :
  1272. do_svr_handshake(ssl, handshake_type, buf, hs_len);
  1273. #else
  1274. ret = do_svr_handshake(ssl, handshake_type, buf, hs_len);
  1275. #endif
  1276. /* just use recursion to get the rest */
  1277. if (hs_len < read_len && ret == SSL_OK)
  1278. ret = do_handshake(ssl, &buf[hs_len], read_len-hs_len);
  1279. error:
  1280. return ret;
  1281. }
  1282. /**
  1283. * Sends the change cipher spec message. We have just read a finished message
  1284. * from the client.
  1285. */
  1286. int send_change_cipher_spec(SSL *ssl)
  1287. {
  1288. int ret = send_packet(ssl, PT_CHANGE_CIPHER_SPEC,
  1289. g_chg_cipher_spec_pkt, sizeof(g_chg_cipher_spec_pkt));
  1290. if (ret >= 0 && set_key_block(ssl, 1) < 0)
  1291. ret = SSL_ERROR_INVALID_HANDSHAKE;
  1292. if (ssl->cipher_info)
  1293. SET_SSL_FLAG(SSL_TX_ENCRYPTED);
  1294. memset(ssl->write_sequence, 0, 8);
  1295. return ret;
  1296. }
  1297. /**
  1298. * Send a "finished" message
  1299. */
  1300. int send_finished(SSL *ssl)
  1301. {
  1302. uint8_t buf[SHA1_SIZE+MD5_SIZE+15+4] = {
  1303. HS_FINISHED, 0, 0, SSL_FINISHED_HASH_SIZE };
  1304. /* now add the finished digest mac (12 bytes) */
  1305. finished_digest(ssl,
  1306. IS_SET_SSL_FLAG(SSL_IS_CLIENT) ?
  1307. client_finished : server_finished, &buf[4]);
  1308. #ifndef CONFIG_SSL_SKELETON_MODE
  1309. /* store in the session cache */
  1310. if (!IS_SET_SSL_FLAG(SSL_SESSION_RESUME) && ssl->ssl_ctx->num_sessions)
  1311. {
  1312. memcpy(ssl->session->master_secret,
  1313. ssl->dc->master_secret, SSL_SECRET_SIZE);
  1314. }
  1315. #endif
  1316. return send_packet(ssl, PT_HANDSHAKE_PROTOCOL,
  1317. buf, SSL_FINISHED_HASH_SIZE+4);
  1318. }
  1319. /**
  1320. * Send an alert message.
  1321. * Return 1 if the alert was an "error".
  1322. */
  1323. int send_alert(SSL *ssl, int error_code)
  1324. {
  1325. int alert_num = 0;
  1326. int is_warning = 0;
  1327. uint8_t buf[2];
  1328. /* Don't bother we're already dead */
  1329. if (ssl->hs_status == SSL_ERROR_DEAD)
  1330. {
  1331. return SSL_ERROR_CONN_LOST;
  1332. }
  1333. #ifdef CONFIG_SSL_FULL_MODE
  1334. if (IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
  1335. ssl_display_error(error_code);
  1336. #endif
  1337. switch (error_code)
  1338. {
  1339. case SSL_ALERT_CLOSE_NOTIFY:
  1340. is_warning = 1;
  1341. alert_num = SSL_ALERT_CLOSE_NOTIFY;
  1342. break;
  1343. case SSL_ERROR_CONN_LOST: /* don't send alert just yet */
  1344. is_warning = 1;
  1345. break;
  1346. case SSL_ERROR_NO_CIPHER:
  1347. alert_num = SSL_ALERT_HANDSHAKE_FAILURE;
  1348. break;
  1349. case SSL_ERROR_INVALID_HMAC:
  1350. alert_num = SSL_ALERT_BAD_RECORD_MAC;
  1351. break;
  1352. case SSL_ERROR_FINISHED_INVALID:
  1353. case SSL_ERROR_INVALID_KEY:
  1354. alert_num = SSL_ALERT_DECRYPT_ERROR;
  1355. break;
  1356. case SSL_ERROR_INVALID_VERSION:
  1357. alert_num = SSL_ALERT_INVALID_VERSION;
  1358. break;
  1359. case SSL_ERROR_INVALID_SESSION:
  1360. alert_num = SSL_ALERT_ILLEGAL_PARAMETER;
  1361. break;
  1362. case SSL_ERROR_NO_CLIENT_RENOG:
  1363. alert_num = SSL_ALERT_NO_RENEGOTIATION;
  1364. break;
  1365. case SSL_ERROR_RECORD_OVERFLOW:
  1366. alert_num = SSL_ALERT_RECORD_OVERFLOW;
  1367. break;
  1368. case SSL_X509_ERROR(X509_VFY_ERROR_EXPIRED):
  1369. case SSL_X509_ERROR(X509_VFY_ERROR_NOT_YET_VALID):
  1370. alert_num = SSL_ALERT_CERTIFICATE_EXPIRED;
  1371. break;
  1372. case SSL_X509_ERROR(X509_VFY_ERROR_NO_TRUSTED_CERT):
  1373. alert_num = SSL_ALERT_UNKNOWN_CA;
  1374. break;
  1375. case SSL_X509_ERROR(X509_VFY_ERROR_UNSUPPORTED_DIGEST):
  1376. case SSL_ERROR_INVALID_CERT_HASH_ALG:
  1377. alert_num = SSL_ALERT_UNSUPPORTED_CERTIFICATE;
  1378. break;
  1379. case SSL_ERROR_BAD_CERTIFICATE:
  1380. case SSL_X509_ERROR(X509_VFY_ERROR_BAD_SIGNATURE):
  1381. alert_num = SSL_ALERT_BAD_CERTIFICATE;
  1382. break;
  1383. case SSL_ERROR_INVALID_HANDSHAKE:
  1384. case SSL_ERROR_INVALID_PROT_MSG:
  1385. default:
  1386. /* a catch-all for anything bad */
  1387. alert_num = (error_code <= SSL_X509_OFFSET) ?
  1388. SSL_ALERT_CERTIFICATE_UNKNOWN: SSL_ALERT_UNEXPECTED_MESSAGE;
  1389. break;
  1390. }
  1391. buf[0] = is_warning ? 1 : 2;
  1392. buf[1] = alert_num;
  1393. send_packet(ssl, PT_ALERT_PROTOCOL, buf, sizeof(buf));
  1394. DISPLAY_ALERT(ssl, alert_num);
  1395. return is_warning ? 0 : 1;
  1396. }
  1397. /**
  1398. * Process a client finished message.
  1399. */
  1400. int process_finished(SSL *ssl, uint8_t *buf, int hs_len)
  1401. {
  1402. int ret = SSL_OK;
  1403. int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
  1404. int resume = IS_SET_SSL_FLAG(SSL_SESSION_RESUME);
  1405. PARANOIA_CHECK(ssl->bm_index, SSL_FINISHED_HASH_SIZE+4);
  1406. /* check that we all work before we continue */
  1407. if (memcmp(ssl->dc->final_finish_mac, &buf[4], SSL_FINISHED_HASH_SIZE))
  1408. return SSL_ERROR_FINISHED_INVALID;
  1409. if ((!is_client && !resume) || (is_client && resume))
  1410. {
  1411. if ((ret = send_change_cipher_spec(ssl)) == SSL_OK)
  1412. ret = send_finished(ssl);
  1413. }
  1414. /* if we ever renegotiate */
  1415. ssl->next_state = is_client ? HS_HELLO_REQUEST : HS_CLIENT_HELLO;
  1416. ssl->hs_status = ret; /* set the final handshake status */
  1417. error:
  1418. return ret;
  1419. }
  1420. /**
  1421. * Send a certificate.
  1422. */
  1423. int send_certificate(SSL *ssl)
  1424. {
  1425. int ret = SSL_OK;
  1426. int i = 0;
  1427. uint8_t *buf = ssl->bm_data;
  1428. int offset = 7;
  1429. int chain_length;
  1430. buf[0] = HS_CERTIFICATE;
  1431. buf[1] = 0;
  1432. buf[4] = 0;
  1433. /* spec says we must check if the hash/sig algorithm is OK */
  1434. if (ssl->version >= SSL_PROTOCOL_VERSION_TLS1_2 &&
  1435. ((ret = check_certificate_chain(ssl)) != SSL_OK))
  1436. {
  1437. ret = SSL_ERROR_INVALID_CERT_HASH_ALG;
  1438. goto error;
  1439. }
  1440. while (i < ssl->ssl_ctx->chain_length)
  1441. {
  1442. SSL_CERT *cert = &ssl->ssl_ctx->certs[i];
  1443. buf[offset++] = 0;
  1444. buf[offset++] = cert->size >> 8; /* cert 1 length */
  1445. buf[offset++] = cert->size & 0xff;
  1446. memcpy(&buf[offset], cert->buf, cert->size);
  1447. offset += cert->size;
  1448. i++;
  1449. }
  1450. chain_length = offset - 7;
  1451. buf[5] = chain_length >> 8; /* cert chain length */
  1452. buf[6] = chain_length & 0xff;
  1453. chain_length += 3;
  1454. buf[2] = chain_length >> 8; /* handshake length */
  1455. buf[3] = chain_length & 0xff;
  1456. ssl->bm_index = offset;
  1457. ret = send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);
  1458. error:
  1459. return ret;
  1460. }
  1461. /**
  1462. * Create a blob of memory that we'll get rid of once the handshake is
  1463. * complete.
  1464. */
  1465. void disposable_new(SSL *ssl)
  1466. {
  1467. if (ssl->dc == NULL)
  1468. {
  1469. ssl->dc = (DISPOSABLE_CTX *)calloc(1, sizeof(DISPOSABLE_CTX));
  1470. SHA256_Init(&ssl->dc->sha256_ctx);
  1471. MD5_Init(&ssl->dc->md5_ctx);
  1472. SHA1_Init(&ssl->dc->sha1_ctx);
  1473. }
  1474. }
  1475. /**
  1476. * Remove the temporary blob of memory.
  1477. */
  1478. void disposable_free(SSL *ssl)
  1479. {
  1480. if (ssl->dc)
  1481. {
  1482. memset(ssl->dc, 0, sizeof(DISPOSABLE_CTX));
  1483. free(ssl->dc);
  1484. ssl->dc = NULL;
  1485. }
  1486. }
  1487. #ifndef CONFIG_SSL_SKELETON_MODE /* no session resumption in this mode */
  1488. /**
  1489. * Find if an existing session has the same session id. If so, use the
  1490. * master secret from this session for session resumption.
  1491. */
  1492. SSL_SESSION *ssl_session_update(int max_sessions, SSL_SESSION *ssl_sessions[],
  1493. SSL *ssl, const uint8_t *session_id)
  1494. {
  1495. time_t tm = time(NULL);
  1496. time_t oldest_sess_time = tm;
  1497. SSL_SESSION *oldest_sess = NULL;
  1498. int i;
  1499. /* no sessions? Then bail */
  1500. if (max_sessions == 0)
  1501. return NULL;
  1502. SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
  1503. if (session_id)
  1504. {
  1505. for (i = 0; i < max_sessions; i++)
  1506. {
  1507. if (ssl_sessions[i])
  1508. {
  1509. /* kill off any expired sessions (including those in
  1510. the future) */
  1511. if ((tm > ssl_sessions[i]->conn_time + SSL_EXPIRY_TIME) ||
  1512. (tm < ssl_sessions[i]->conn_time))
  1513. {
  1514. session_free(ssl_sessions, i);
  1515. continue;
  1516. }
  1517. /* if the session id matches, it must still be less than
  1518. the expiry time */
  1519. if (memcmp(ssl_sessions[i]->session_id, session_id,
  1520. SSL_SESSION_ID_SIZE) == 0)
  1521. {
  1522. ssl->session_index = i;
  1523. memcpy(ssl->dc->master_secret,
  1524. ssl_sessions[i]->master_secret, SSL_SECRET_SIZE);
  1525. SET_SSL_FLAG(SSL_SESSION_RESUME);
  1526. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  1527. return ssl_sessions[i]; /* a session was found */
  1528. }
  1529. }
  1530. }
  1531. }
  1532. /* If we've got here, no matching session was found - so create one */
  1533. for (i = 0; i < max_sessions; i++)
  1534. {
  1535. if (ssl_sessions[i] == NULL)
  1536. {
  1537. /* perfect, this will do */
  1538. ssl_sessions[i] = (SSL_SESSION *)calloc(1, sizeof(SSL_SESSION));
  1539. ssl_sessions[i]->conn_time = tm;
  1540. ssl->session_index = i;
  1541. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  1542. return ssl_sessions[i]; /* return the session object */
  1543. }
  1544. else if (ssl_sessions[i]->conn_time <= oldest_sess_time)
  1545. {
  1546. /* find the oldest session */
  1547. oldest_sess_time = ssl_sessions[i]->conn_time;
  1548. oldest_sess = ssl_sessions[i];
  1549. ssl->session_index = i;
  1550. }
  1551. }
  1552. /* ok, we've used up all of our sessions. So blow the oldest session away */
  1553. oldest_sess->conn_time = tm;
  1554. memset(oldest_sess->session_id, 0, SSL_SESSION_ID_SIZE);
  1555. memset(oldest_sess->master_secret, 0, SSL_SECRET_SIZE);
  1556. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  1557. return oldest_sess;
  1558. }
  1559. /**
  1560. * Free an existing session.
  1561. */
  1562. static void session_free(SSL_SESSION *ssl_sessions[], int sess_index)
  1563. {
  1564. if (ssl_sessions[sess_index])
  1565. {
  1566. free(ssl_sessions[sess_index]);
  1567. ssl_sessions[sess_index] = NULL;
  1568. }
  1569. }
  1570. /**
  1571. * This ssl object doesn't want this session anymore.
  1572. */
  1573. void kill_ssl_session(SSL_SESSION **ssl_sessions, SSL *ssl)
  1574. {
  1575. SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
  1576. if (ssl->ssl_ctx->num_sessions)
  1577. {
  1578. session_free(ssl_sessions, ssl->session_index);
  1579. ssl->session = NULL;
  1580. }
  1581. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  1582. }
  1583. #endif /* CONFIG_SSL_SKELETON_MODE */
  1584. /*
  1585. * Get the session id for a handshake. This will be a 32 byte sequence.
  1586. */
  1587. EXP_FUNC const uint8_t * LIB_CALLTYPE ssl_get_session_id(const SSL *ssl)
  1588. {
  1589. return ssl->session_id;
  1590. }
  1591. /*
  1592. * Get the session id size for a handshake.
  1593. */
  1594. EXP_FUNC uint8_t LIB_CALLTYPE ssl_get_session_id_size(const SSL *ssl)
  1595. {
  1596. return ssl->sess_id_size;
  1597. }
  1598. /*
  1599. * Return the cipher id (in the SSL form).
  1600. */
  1601. EXP_FUNC uint8_t LIB_CALLTYPE ssl_get_cipher_id(const SSL *ssl)
  1602. {
  1603. return ssl->cipher;
  1604. }
  1605. /*
  1606. * Return the status of the handshake.
  1607. */
  1608. EXP_FUNC int LIB_CALLTYPE ssl_handshake_status(const SSL *ssl)
  1609. {
  1610. return ssl->hs_status;
  1611. }
  1612. /*
  1613. * Retrieve various parameters about the SSL engine.
  1614. */
  1615. EXP_FUNC int LIB_CALLTYPE ssl_get_config(int offset)
  1616. {
  1617. switch (offset)
  1618. {
  1619. /* return the appropriate build mode */
  1620. case SSL_BUILD_MODE:
  1621. #if defined(CONFIG_SSL_FULL_MODE)
  1622. return SSL_BUILD_FULL_MODE;
  1623. #elif defined(CONFIG_SSL_ENABLE_CLIENT)
  1624. return SSL_BUILD_ENABLE_CLIENT;
  1625. #elif defined(CONFIG_ENABLE_VERIFICATION)
  1626. return SSL_BUILD_ENABLE_VERIFICATION;
  1627. #elif defined(CONFIG_SSL_SERVER_ONLY )
  1628. return SSL_BUILD_SERVER_ONLY;
  1629. #else
  1630. return SSL_BUILD_SKELETON_MODE;
  1631. #endif
  1632. case SSL_MAX_CERT_CFG_OFFSET:
  1633. return CONFIG_SSL_MAX_CERTS;
  1634. #ifdef CONFIG_SSL_CERT_VERIFICATION
  1635. case SSL_MAX_CA_CERT_CFG_OFFSET:
  1636. return CONFIG_X509_MAX_CA_CERTS;
  1637. #endif
  1638. #ifdef CONFIG_SSL_HAS_PEM
  1639. case SSL_HAS_PEM:
  1640. return 1;
  1641. #endif
  1642. default:
  1643. return 0;
  1644. }
  1645. }
  1646. /**
  1647. * Check the certificate chain to see if the certs are supported
  1648. */
  1649. static int check_certificate_chain(SSL *ssl)
  1650. {
  1651. int i = 0;
  1652. int ret = SSL_OK;
  1653. while (i < ssl->ssl_ctx->chain_length)
  1654. {
  1655. int j = 0;
  1656. uint8_t found = 0;
  1657. SSL_CERT *cert = &ssl->ssl_ctx->certs[i];
  1658. while (j < ssl->num_sig_algs)
  1659. {
  1660. if (ssl->sig_algs[j++] == cert->hash_alg)
  1661. {
  1662. found = 1;
  1663. break;
  1664. }
  1665. }
  1666. if (!found)
  1667. {
  1668. ret = SSL_ERROR_INVALID_CERT_HASH_ALG;
  1669. goto error;
  1670. }
  1671. i++;
  1672. }
  1673. error:
  1674. return ret;
  1675. }
  1676. #ifdef CONFIG_SSL_CERT_VERIFICATION
  1677. /**
  1678. * Authenticate a received certificate.
  1679. */
  1680. EXP_FUNC int LIB_CALLTYPE ssl_verify_cert(const SSL *ssl)
  1681. {
  1682. int ret;
  1683. int pathLenConstraint = 0;
  1684. SSL_CTX_LOCK(ssl->ssl_ctx->mutex);
  1685. ret = x509_verify(ssl->ssl_ctx->ca_cert_ctx, ssl->x509_ctx,
  1686. &pathLenConstraint);
  1687. SSL_CTX_UNLOCK(ssl->ssl_ctx->mutex);
  1688. if (ret) /* modify into an SSL error type */
  1689. {
  1690. ret = SSL_X509_ERROR(ret);
  1691. }
  1692. return ret;
  1693. }
  1694. /**
  1695. * Process a certificate message.
  1696. */
  1697. int process_certificate(SSL *ssl, X509_CTX **x509_ctx)
  1698. {
  1699. int ret = SSL_OK;
  1700. uint8_t *buf = &ssl->bm_data[ssl->dc->bm_proc_index];
  1701. int pkt_size = ssl->bm_index;
  1702. int cert_size, offset = 5, offset_start;
  1703. int total_cert_len = (buf[offset]<<8) + buf[offset+1];
  1704. int is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
  1705. X509_CTX *chain = 0;
  1706. X509_CTX **certs = 0;
  1707. int *cert_used = 0;
  1708. int num_certs = 0;
  1709. int i = 0;
  1710. offset += 2;
  1711. PARANOIA_CHECK(pkt_size, total_cert_len + offset);
  1712. // record the start point for the second pass
  1713. offset_start = offset;
  1714. // first pass - count the certificates
  1715. while (offset < total_cert_len)
  1716. {
  1717. offset++; /* skip empty char */
  1718. cert_size = (buf[offset]<<8) + buf[offset+1];
  1719. offset += 2;
  1720. offset += cert_size;
  1721. num_certs++;
  1722. }
  1723. PARANOIA_CHECK(pkt_size, offset);
  1724. certs = (X509_CTX**) calloc(num_certs, sizeof(void*));
  1725. cert_used = (int*) calloc(num_certs, sizeof(int));
  1726. num_certs = 0;
  1727. // restore the offset from the saved value
  1728. offset = offset_start;
  1729. // second pass - load the certificates
  1730. while (offset < total_cert_len)
  1731. {
  1732. offset++; /* skip empty char */
  1733. cert_size = (buf[offset]<<8) + buf[offset+1];
  1734. offset += 2;
  1735. if (x509_new(&buf[offset], NULL, certs+num_certs))
  1736. {
  1737. ret = SSL_ERROR_BAD_CERTIFICATE;
  1738. goto error;
  1739. }
  1740. #if defined (CONFIG_SSL_FULL_MODE)
  1741. if (ssl->ssl_ctx->options & SSL_DISPLAY_CERTS)
  1742. x509_print(certs[num_certs], NULL);
  1743. #endif
  1744. num_certs++;
  1745. offset += cert_size;
  1746. }
  1747. PARANOIA_CHECK(pkt_size, offset);
  1748. // third pass - link certs together, assume server cert is the first
  1749. *x509_ctx = certs[0];
  1750. chain = certs[0];
  1751. cert_used[0] = 1;
  1752. // repeat until the end of the chain is found
  1753. while (1)
  1754. {
  1755. // look for CA cert
  1756. for( i = 1; i < num_certs; i++ )
  1757. {
  1758. if (certs[i] == chain)
  1759. continue;
  1760. if (cert_used[i])
  1761. continue; // don't allow loops
  1762. if (asn1_compare_dn(chain->ca_cert_dn, certs[i]->cert_dn) == 0)
  1763. {
  1764. // CA cert found, add it to the chain
  1765. cert_used[i] = 1;
  1766. chain->next = certs[i];
  1767. chain = certs[i];
  1768. break;
  1769. }
  1770. }
  1771. // no CA cert found, reached the end of the chain
  1772. if (i >= num_certs)
  1773. break;
  1774. }
  1775. // clean up any certs that aren't part of the chain
  1776. for (i = 1; i < num_certs; i++)
  1777. {
  1778. if (cert_used[i] == 0)
  1779. x509_free(certs[i]);
  1780. }
  1781. /* if we are client we can do the verify now or later */
  1782. if (is_client && !IS_SET_SSL_FLAG(SSL_SERVER_VERIFY_LATER))
  1783. {
  1784. ret = ssl_verify_cert(ssl);
  1785. }
  1786. ssl->next_state = is_client ? HS_SERVER_HELLO_DONE : HS_CLIENT_KEY_XCHG;
  1787. ssl->dc->bm_proc_index += offset;
  1788. error:
  1789. // clean up arrays
  1790. if (certs)
  1791. free(certs);
  1792. if (cert_used)
  1793. free(cert_used);
  1794. return ret;
  1795. }
  1796. #endif /* CONFIG_SSL_CERT_VERIFICATION */
  1797. /**
  1798. * Debugging routine to display SSL handshaking stuff.
  1799. */
  1800. #ifdef CONFIG_SSL_FULL_MODE
  1801. /**
  1802. * Debugging routine to display SSL states.
  1803. */
  1804. void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok)
  1805. {
  1806. const char *str;
  1807. if (!IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
  1808. return;
  1809. printf(not_ok ? "Error - invalid State:\t" : "State:\t");
  1810. printf(is_send ? "sending " : "receiving ");
  1811. switch (state)
  1812. {
  1813. case HS_HELLO_REQUEST:
  1814. str = "Hello Request (0)";
  1815. break;
  1816. case HS_CLIENT_HELLO:
  1817. str = "Client Hello (1)";
  1818. break;
  1819. case HS_SERVER_HELLO:
  1820. str = "Server Hello (2)";
  1821. break;
  1822. case HS_CERTIFICATE:
  1823. str = "Certificate (11)";
  1824. break;
  1825. case HS_SERVER_KEY_XCHG:
  1826. str = "Certificate Request (12)";
  1827. break;
  1828. case HS_CERT_REQ:
  1829. str = "Certificate Request (13)";
  1830. break;
  1831. case HS_SERVER_HELLO_DONE:
  1832. str = "Server Hello Done (14)";
  1833. break;
  1834. case HS_CERT_VERIFY:
  1835. str = "Certificate Verify (15)";
  1836. break;
  1837. case HS_CLIENT_KEY_XCHG:
  1838. str = "Client Key Exchange (16)";
  1839. break;
  1840. case HS_FINISHED:
  1841. str = "Finished (16)";
  1842. break;
  1843. default:
  1844. str = "Error (Unknown)";
  1845. break;
  1846. }
  1847. printf("%s\n", str);
  1848. TTY_FLUSH();
  1849. }
  1850. /**
  1851. * Debugging routine to display RSA objects
  1852. */
  1853. void DISPLAY_RSA(SSL *ssl, const RSA_CTX *rsa_ctx)
  1854. {
  1855. if (!IS_SET_SSL_FLAG(SSL_DISPLAY_RSA))
  1856. return;
  1857. RSA_print(rsa_ctx);
  1858. TTY_FLUSH();
  1859. }
  1860. /**
  1861. * Debugging routine to display SSL handshaking bytes.
  1862. */
  1863. void DISPLAY_BYTES(SSL *ssl, const char *format,
  1864. const uint8_t *data, int size, ...)
  1865. {
  1866. va_list(ap);
  1867. if (!IS_SET_SSL_FLAG(SSL_DISPLAY_BYTES))
  1868. return;
  1869. va_start(ap, size);
  1870. print_blob(format, data, size, va_arg(ap, char *));
  1871. va_end(ap);
  1872. TTY_FLUSH();
  1873. }
  1874. /**
  1875. * Debugging routine to display SSL handshaking errors.
  1876. */
  1877. EXP_FUNC const char* LIB_CALLTYPE ssl_get_error(int error_code, char *buf, size_t bufsize)
  1878. {
  1879. if (error_code == SSL_OK){
  1880. snprintf(buf, bufsize, "Error: SSL_OK");
  1881. return buf;
  1882. }
  1883. /* X509 error? */
  1884. if (error_code < SSL_X509_OFFSET)
  1885. {
  1886. snprintf(buf, bufsize, "Error: %s", x509_display_error(error_code - SSL_X509_OFFSET));
  1887. return buf;
  1888. }
  1889. /* SSL alert error code */
  1890. if (error_code > SSL_ERROR_CONN_LOST)
  1891. {
  1892. snprintf(buf, bufsize, "Error: SSL error %d", -error_code);
  1893. return buf;
  1894. }
  1895. switch (error_code)
  1896. {
  1897. case SSL_ERROR_DEAD:
  1898. snprintf(buf, bufsize, "Error: connection dead");
  1899. break;
  1900. case SSL_ERROR_RECORD_OVERFLOW:
  1901. snprintf(buf, bufsize, "Error: record overflow");
  1902. break;
  1903. case SSL_ERROR_INVALID_HANDSHAKE:
  1904. snprintf(buf, bufsize, "Error: invalid handshake");
  1905. break;
  1906. case SSL_ERROR_INVALID_PROT_MSG:
  1907. snprintf(buf, bufsize, "Error: invalid protocol message");
  1908. break;
  1909. case SSL_ERROR_INVALID_HMAC:
  1910. snprintf(buf, bufsize, "Error: invalid mac");
  1911. break;
  1912. case SSL_ERROR_INVALID_VERSION:
  1913. snprintf(buf, bufsize, "Error: invalid version");
  1914. break;
  1915. case SSL_ERROR_INVALID_SESSION:
  1916. snprintf(buf, bufsize, "Error: invalid session");
  1917. break;
  1918. case SSL_ERROR_NO_CIPHER:
  1919. snprintf(buf, bufsize, "Error: no cipher");
  1920. break;
  1921. case SSL_ERROR_INVALID_CERT_HASH_ALG:
  1922. snprintf(buf, bufsize, "Error: invalid cert hash algorithm");
  1923. break;
  1924. case SSL_ERROR_CONN_LOST:
  1925. snprintf(buf, bufsize, "Error: connection lost");
  1926. break;
  1927. case SSL_ERROR_BAD_CERTIFICATE:
  1928. snprintf(buf, bufsize, "Error: bad certificate");
  1929. break;
  1930. case SSL_ERROR_INVALID_KEY:
  1931. snprintf(buf, bufsize, "Error: invalid key");
  1932. break;
  1933. case SSL_ERROR_FINISHED_INVALID:
  1934. snprintf(buf, bufsize, "Error: finished invalid");
  1935. break;
  1936. case SSL_ERROR_NO_CERT_DEFINED:
  1937. snprintf(buf, bufsize, "Error: no certificate defined");
  1938. break;
  1939. case SSL_ERROR_NO_CLIENT_RENOG:
  1940. snprintf(buf, bufsize, "Error: client renegotiation not supported");
  1941. break;
  1942. case SSL_ERROR_NOT_SUPPORTED:
  1943. snprintf(buf, bufsize, "Error: Option not supported");
  1944. break;
  1945. default:
  1946. snprintf(buf, bufsize, "Error: undefined as yet - %d", error_code);
  1947. break;
  1948. }
  1949. return buf;
  1950. }
  1951. /**
  1952. * Debugging routine to display SSL handshaking errors.
  1953. */
  1954. EXP_FUNC void LIB_CALLTYPE ssl_display_error(int error_code)
  1955. {
  1956. char buf[254];
  1957. ssl_get_error(error_code, buf, sizeof(buf));
  1958. printf("%s\n", buf);
  1959. TTY_FLUSH();
  1960. }
  1961. /**
  1962. * Debugging routine to display alerts.
  1963. */
  1964. void DISPLAY_ALERT(SSL *ssl, int alert)
  1965. {
  1966. if (!IS_SET_SSL_FLAG(SSL_DISPLAY_STATES))
  1967. return;
  1968. printf("Alert: ");
  1969. switch (alert)
  1970. {
  1971. case SSL_ALERT_CLOSE_NOTIFY:
  1972. printf("close notify");
  1973. break;
  1974. case SSL_ALERT_UNEXPECTED_MESSAGE:
  1975. printf("unexpected message");
  1976. break;
  1977. case SSL_ALERT_BAD_RECORD_MAC:
  1978. printf("bad record mac");
  1979. break;
  1980. case SSL_ALERT_RECORD_OVERFLOW:
  1981. printf("record overlow");
  1982. break;
  1983. case SSL_ALERT_HANDSHAKE_FAILURE:
  1984. printf("handshake failure");
  1985. break;
  1986. case SSL_ALERT_BAD_CERTIFICATE:
  1987. printf("bad certificate");
  1988. break;
  1989. case SSL_ALERT_UNSUPPORTED_CERTIFICATE:
  1990. printf("unsupported certificate");
  1991. break;
  1992. case SSL_ALERT_CERTIFICATE_EXPIRED:
  1993. printf("certificate expired");
  1994. break;
  1995. case SSL_ALERT_CERTIFICATE_UNKNOWN:
  1996. printf("certificate unknown");
  1997. break;
  1998. case SSL_ALERT_ILLEGAL_PARAMETER:
  1999. printf("illegal parameter");
  2000. break;
  2001. case SSL_ALERT_UNKNOWN_CA:
  2002. printf("unknown ca");
  2003. break;
  2004. case SSL_ALERT_DECODE_ERROR:
  2005. printf("decode error");
  2006. break;
  2007. case SSL_ALERT_DECRYPT_ERROR:
  2008. printf("decrypt error");
  2009. break;
  2010. case SSL_ALERT_INVALID_VERSION:
  2011. printf("invalid version");
  2012. break;
  2013. case SSL_ALERT_NO_RENEGOTIATION:
  2014. printf("no renegotiation");
  2015. break;
  2016. default:
  2017. printf("alert - (unknown %d)", alert);
  2018. break;
  2019. }
  2020. printf("\n");
  2021. TTY_FLUSH();
  2022. }
  2023. #endif /* CONFIG_SSL_FULL_MODE */
  2024. /**
  2025. * Return the version of this library.
  2026. */
  2027. EXP_FUNC const char * LIB_CALLTYPE ssl_version()
  2028. {
  2029. static const char * axtls_version = AXTLS_VERSION;
  2030. return axtls_version;
  2031. }
  2032. /**
  2033. * Enable the various language bindings to work regardless of the
  2034. * configuration - they just return an error statement and a bad return code.
  2035. */
  2036. #if !defined(CONFIG_SSL_FULL_MODE)
  2037. EXP_FUNC void LIB_CALLTYPE ssl_display_error(int error_code) {}
  2038. #endif
  2039. #ifdef CONFIG_BINDINGS
  2040. #if !defined(CONFIG_SSL_ENABLE_CLIENT)
  2041. EXP_FUNC SSL * LIB_CALLTYPE ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
  2042. uint8_t *session_id, uint8_t sess_id_size)
  2043. {
  2044. printf("%s", unsupported_str);
  2045. return NULL;
  2046. }
  2047. #endif
  2048. #if !defined(CONFIG_SSL_CERT_VERIFICATION)
  2049. EXP_FUNC int LIB_CALLTYPE ssl_verify_cert(const SSL *ssl)
  2050. {
  2051. printf("%s", unsupported_str);
  2052. return -1;
  2053. }
  2054. EXP_FUNC const char * LIB_CALLTYPE ssl_get_cert_dn(const SSL *ssl, int component)
  2055. {
  2056. printf("%s", unsupported_str);
  2057. return NULL;
  2058. }
  2059. EXP_FUNC const char * LIB_CALLTYPE ssl_get_cert_subject_alt_dnsname(const SSL *ssl, int index)
  2060. {
  2061. printf("%s", unsupported_str);
  2062. return NULL;
  2063. }
  2064. #endif /* CONFIG_SSL_CERT_VERIFICATION */
  2065. #endif /* CONFIG_BINDINGS */