s3_srvr.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. /* ssl/s3_srvr.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. #define REUSE_CIPHER_BUG
  125. #define NETSCAPE_HANG_BUG
  126. #include <stdio.h>
  127. #include "ssl_locl.h"
  128. #include "kssl_lcl.h"
  129. #include <openssl/buffer.h>
  130. #include <openssl/rand.h>
  131. #include <openssl/objects.h>
  132. #include <openssl/evp.h>
  133. #include <openssl/x509.h>
  134. #include <openssl/dh.h>
  135. #ifndef OPENSSL_NO_KRB5
  136. #include <openssl/krb5_asn.h>
  137. #endif
  138. #include <openssl/md5.h>
  139. static SSL_METHOD *ssl3_get_server_method(int ver);
  140. static int ssl3_get_client_hello(SSL *s);
  141. static int ssl3_check_client_hello(SSL *s);
  142. static int ssl3_send_server_hello(SSL *s);
  143. static int ssl3_send_server_key_exchange(SSL *s);
  144. static int ssl3_send_certificate_request(SSL *s);
  145. static int ssl3_send_server_done(SSL *s);
  146. static int ssl3_get_client_key_exchange(SSL *s);
  147. static int ssl3_get_client_certificate(SSL *s);
  148. static int ssl3_get_cert_verify(SSL *s);
  149. static int ssl3_send_hello_request(SSL *s);
  150. #ifndef OPENSSL_NO_ECDH
  151. static int nid2curve_id(int nid);
  152. #endif
  153. static SSL_METHOD *ssl3_get_server_method(int ver)
  154. {
  155. if (ver == SSL3_VERSION)
  156. return(SSLv3_server_method());
  157. else
  158. return(NULL);
  159. }
  160. SSL_METHOD *SSLv3_server_method(void)
  161. {
  162. static int init=1;
  163. static SSL_METHOD SSLv3_server_data;
  164. if (init)
  165. {
  166. CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
  167. if (init)
  168. {
  169. memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
  170. sizeof(SSL_METHOD));
  171. SSLv3_server_data.ssl_accept=ssl3_accept;
  172. SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
  173. init=0;
  174. }
  175. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
  176. }
  177. return(&SSLv3_server_data);
  178. }
  179. int ssl3_accept(SSL *s)
  180. {
  181. BUF_MEM *buf;
  182. unsigned long l,Time=time(NULL);
  183. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  184. long num1;
  185. int ret= -1;
  186. int new_state,state,skip=0;
  187. RAND_add(&Time,sizeof(Time),0);
  188. ERR_clear_error();
  189. clear_sys_error();
  190. if (s->info_callback != NULL)
  191. cb=s->info_callback;
  192. else if (s->ctx->info_callback != NULL)
  193. cb=s->ctx->info_callback;
  194. /* init things to blank */
  195. s->in_handshake++;
  196. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  197. if (s->cert == NULL)
  198. {
  199. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
  200. return(-1);
  201. }
  202. for (;;)
  203. {
  204. state=s->state;
  205. switch (s->state)
  206. {
  207. case SSL_ST_RENEGOTIATE:
  208. s->new_session=1;
  209. /* s->state=SSL_ST_ACCEPT; */
  210. case SSL_ST_BEFORE:
  211. case SSL_ST_ACCEPT:
  212. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  213. case SSL_ST_OK|SSL_ST_ACCEPT:
  214. s->server=1;
  215. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  216. if ((s->version>>8) != 3)
  217. {
  218. SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
  219. return -1;
  220. }
  221. s->type=SSL_ST_ACCEPT;
  222. if (s->init_buf == NULL)
  223. {
  224. if ((buf=BUF_MEM_new()) == NULL)
  225. {
  226. ret= -1;
  227. goto end;
  228. }
  229. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  230. {
  231. ret= -1;
  232. goto end;
  233. }
  234. s->init_buf=buf;
  235. }
  236. if (!ssl3_setup_buffers(s))
  237. {
  238. ret= -1;
  239. goto end;
  240. }
  241. s->init_num=0;
  242. if (s->state != SSL_ST_RENEGOTIATE)
  243. {
  244. /* Ok, we now need to push on a buffering BIO so that
  245. * the output is sent in a way that TCP likes :-)
  246. */
  247. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  248. ssl3_init_finished_mac(s);
  249. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  250. s->ctx->stats.sess_accept++;
  251. }
  252. else
  253. {
  254. /* s->state == SSL_ST_RENEGOTIATE,
  255. * we will just send a HelloRequest */
  256. s->ctx->stats.sess_accept_renegotiate++;
  257. s->state=SSL3_ST_SW_HELLO_REQ_A;
  258. }
  259. break;
  260. case SSL3_ST_SW_HELLO_REQ_A:
  261. case SSL3_ST_SW_HELLO_REQ_B:
  262. s->shutdown=0;
  263. ret=ssl3_send_hello_request(s);
  264. if (ret <= 0) goto end;
  265. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  266. s->state=SSL3_ST_SW_FLUSH;
  267. s->init_num=0;
  268. ssl3_init_finished_mac(s);
  269. break;
  270. case SSL3_ST_SW_HELLO_REQ_C:
  271. s->state=SSL_ST_OK;
  272. break;
  273. case SSL3_ST_SR_CLNT_HELLO_A:
  274. case SSL3_ST_SR_CLNT_HELLO_B:
  275. case SSL3_ST_SR_CLNT_HELLO_C:
  276. s->shutdown=0;
  277. ret=ssl3_get_client_hello(s);
  278. if (ret <= 0) goto end;
  279. s->new_session = 2;
  280. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  281. s->init_num=0;
  282. break;
  283. case SSL3_ST_SW_SRVR_HELLO_A:
  284. case SSL3_ST_SW_SRVR_HELLO_B:
  285. ret=ssl3_send_server_hello(s);
  286. if (ret <= 0) goto end;
  287. if (s->hit)
  288. s->state=SSL3_ST_SW_CHANGE_A;
  289. else
  290. s->state=SSL3_ST_SW_CERT_A;
  291. s->init_num=0;
  292. break;
  293. case SSL3_ST_SW_CERT_A:
  294. case SSL3_ST_SW_CERT_B:
  295. /* Check if it is anon DH or anon ECDH */
  296. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  297. {
  298. ret=ssl3_send_server_certificate(s);
  299. if (ret <= 0) goto end;
  300. }
  301. else
  302. skip=1;
  303. s->state=SSL3_ST_SW_KEY_EXCH_A;
  304. s->init_num=0;
  305. break;
  306. case SSL3_ST_SW_KEY_EXCH_A:
  307. case SSL3_ST_SW_KEY_EXCH_B:
  308. l=s->s3->tmp.new_cipher->algorithms;
  309. /* clear this, it may get reset by
  310. * send_server_key_exchange */
  311. if ((s->options & SSL_OP_EPHEMERAL_RSA)
  312. #ifndef OPENSSL_NO_KRB5
  313. && !(l & SSL_KRB5)
  314. #endif /* OPENSSL_NO_KRB5 */
  315. )
  316. /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
  317. * even when forbidden by protocol specs
  318. * (handshake may fail as clients are not required to
  319. * be able to handle this) */
  320. s->s3->tmp.use_rsa_tmp=1;
  321. else
  322. s->s3->tmp.use_rsa_tmp=0;
  323. /* only send if a DH key exchange, fortezza or
  324. * RSA but we have a sign only certificate
  325. *
  326. * For ECC ciphersuites, we send a serverKeyExchange
  327. * message only if the cipher suite is either
  328. * ECDH-anon or ECDHE. In other cases, the
  329. * server certificate contains the server's
  330. * public key for key exchange.
  331. */
  332. if (s->s3->tmp.use_rsa_tmp
  333. || (l & SSL_kECDHE)
  334. || (l & (SSL_DH|SSL_kFZA))
  335. || ((l & SSL_kRSA)
  336. && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
  337. || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
  338. && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
  339. )
  340. )
  341. )
  342. )
  343. {
  344. ret=ssl3_send_server_key_exchange(s);
  345. if (ret <= 0) goto end;
  346. }
  347. else
  348. skip=1;
  349. s->state=SSL3_ST_SW_CERT_REQ_A;
  350. s->init_num=0;
  351. break;
  352. case SSL3_ST_SW_CERT_REQ_A:
  353. case SSL3_ST_SW_CERT_REQ_B:
  354. if (/* don't request cert unless asked for it: */
  355. !(s->verify_mode & SSL_VERIFY_PEER) ||
  356. /* if SSL_VERIFY_CLIENT_ONCE is set,
  357. * don't request cert during re-negotiation: */
  358. ((s->session->peer != NULL) &&
  359. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  360. /* never request cert in anonymous ciphersuites
  361. * (see section "Certificate request" in SSL 3 drafts
  362. * and in RFC 2246): */
  363. ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
  364. /* ... except when the application insists on verification
  365. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  366. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  367. /* never request cert in Kerberos ciphersuites */
  368. (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
  369. {
  370. /* no cert request */
  371. skip=1;
  372. s->s3->tmp.cert_request=0;
  373. s->state=SSL3_ST_SW_SRVR_DONE_A;
  374. }
  375. else
  376. {
  377. s->s3->tmp.cert_request=1;
  378. ret=ssl3_send_certificate_request(s);
  379. if (ret <= 0) goto end;
  380. #ifndef NETSCAPE_HANG_BUG
  381. s->state=SSL3_ST_SW_SRVR_DONE_A;
  382. #else
  383. s->state=SSL3_ST_SW_FLUSH;
  384. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  385. #endif
  386. s->init_num=0;
  387. }
  388. break;
  389. case SSL3_ST_SW_SRVR_DONE_A:
  390. case SSL3_ST_SW_SRVR_DONE_B:
  391. ret=ssl3_send_server_done(s);
  392. if (ret <= 0) goto end;
  393. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  394. s->state=SSL3_ST_SW_FLUSH;
  395. s->init_num=0;
  396. break;
  397. case SSL3_ST_SW_FLUSH:
  398. /* number of bytes to be flushed */
  399. num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
  400. if (num1 > 0)
  401. {
  402. s->rwstate=SSL_WRITING;
  403. num1=BIO_flush(s->wbio);
  404. if (num1 <= 0) { ret= -1; goto end; }
  405. s->rwstate=SSL_NOTHING;
  406. }
  407. s->state=s->s3->tmp.next_state;
  408. break;
  409. case SSL3_ST_SR_CERT_A:
  410. case SSL3_ST_SR_CERT_B:
  411. /* Check for second client hello (MS SGC) */
  412. ret = ssl3_check_client_hello(s);
  413. if (ret <= 0)
  414. goto end;
  415. if (ret == 2)
  416. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  417. else {
  418. if (s->s3->tmp.cert_request)
  419. {
  420. ret=ssl3_get_client_certificate(s);
  421. if (ret <= 0) goto end;
  422. }
  423. s->init_num=0;
  424. s->state=SSL3_ST_SR_KEY_EXCH_A;
  425. }
  426. break;
  427. case SSL3_ST_SR_KEY_EXCH_A:
  428. case SSL3_ST_SR_KEY_EXCH_B:
  429. ret=ssl3_get_client_key_exchange(s);
  430. if (ret <= 0)
  431. goto end;
  432. if (ret == 2)
  433. {
  434. /* For the ECDH ciphersuites when
  435. * the client sends its ECDH pub key in
  436. * a certificate, the CertificateVerify
  437. * message is not sent.
  438. */
  439. s->state=SSL3_ST_SR_FINISHED_A;
  440. s->init_num = 0;
  441. }
  442. else
  443. {
  444. s->state=SSL3_ST_SR_CERT_VRFY_A;
  445. s->init_num=0;
  446. /* We need to get hashes here so if there is
  447. * a client cert, it can be verified
  448. */
  449. s->method->ssl3_enc->cert_verify_mac(s,
  450. &(s->s3->finish_dgst1),
  451. &(s->s3->tmp.cert_verify_md[0]));
  452. s->method->ssl3_enc->cert_verify_mac(s,
  453. &(s->s3->finish_dgst2),
  454. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
  455. }
  456. break;
  457. case SSL3_ST_SR_CERT_VRFY_A:
  458. case SSL3_ST_SR_CERT_VRFY_B:
  459. /* we should decide if we expected this one */
  460. ret=ssl3_get_cert_verify(s);
  461. if (ret <= 0) goto end;
  462. s->state=SSL3_ST_SR_FINISHED_A;
  463. s->init_num=0;
  464. break;
  465. case SSL3_ST_SR_FINISHED_A:
  466. case SSL3_ST_SR_FINISHED_B:
  467. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  468. SSL3_ST_SR_FINISHED_B);
  469. if (ret <= 0) goto end;
  470. if (s->hit)
  471. s->state=SSL_ST_OK;
  472. else
  473. s->state=SSL3_ST_SW_CHANGE_A;
  474. s->init_num=0;
  475. break;
  476. case SSL3_ST_SW_CHANGE_A:
  477. case SSL3_ST_SW_CHANGE_B:
  478. s->session->cipher=s->s3->tmp.new_cipher;
  479. if (!s->method->ssl3_enc->setup_key_block(s))
  480. { ret= -1; goto end; }
  481. ret=ssl3_send_change_cipher_spec(s,
  482. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  483. if (ret <= 0) goto end;
  484. s->state=SSL3_ST_SW_FINISHED_A;
  485. s->init_num=0;
  486. if (!s->method->ssl3_enc->change_cipher_state(s,
  487. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  488. {
  489. ret= -1;
  490. goto end;
  491. }
  492. break;
  493. case SSL3_ST_SW_FINISHED_A:
  494. case SSL3_ST_SW_FINISHED_B:
  495. ret=ssl3_send_finished(s,
  496. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  497. s->method->ssl3_enc->server_finished_label,
  498. s->method->ssl3_enc->server_finished_label_len);
  499. if (ret <= 0) goto end;
  500. s->state=SSL3_ST_SW_FLUSH;
  501. if (s->hit)
  502. s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
  503. else
  504. s->s3->tmp.next_state=SSL_ST_OK;
  505. s->init_num=0;
  506. break;
  507. case SSL_ST_OK:
  508. /* clean a few things up */
  509. ssl3_cleanup_key_block(s);
  510. BUF_MEM_free(s->init_buf);
  511. s->init_buf=NULL;
  512. /* remove buffering on output */
  513. ssl_free_wbio_buffer(s);
  514. s->init_num=0;
  515. if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
  516. {
  517. /* actually not necessarily a 'new' session unless
  518. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
  519. s->new_session=0;
  520. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  521. s->ctx->stats.sess_accept_good++;
  522. /* s->server=1; */
  523. s->handshake_func=ssl3_accept;
  524. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  525. }
  526. ret = 1;
  527. goto end;
  528. /* break; */
  529. default:
  530. SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
  531. ret= -1;
  532. goto end;
  533. /* break; */
  534. }
  535. if (!s->s3->tmp.reuse_message && !skip)
  536. {
  537. if (s->debug)
  538. {
  539. if ((ret=BIO_flush(s->wbio)) <= 0)
  540. goto end;
  541. }
  542. if ((cb != NULL) && (s->state != state))
  543. {
  544. new_state=s->state;
  545. s->state=state;
  546. cb(s,SSL_CB_ACCEPT_LOOP,1);
  547. s->state=new_state;
  548. }
  549. }
  550. skip=0;
  551. }
  552. end:
  553. /* BIO_flush(s->wbio); */
  554. s->in_handshake--;
  555. if (cb != NULL)
  556. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  557. return(ret);
  558. }
  559. static int ssl3_send_hello_request(SSL *s)
  560. {
  561. unsigned char *p;
  562. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  563. {
  564. p=(unsigned char *)s->init_buf->data;
  565. *(p++)=SSL3_MT_HELLO_REQUEST;
  566. *(p++)=0;
  567. *(p++)=0;
  568. *(p++)=0;
  569. s->state=SSL3_ST_SW_HELLO_REQ_B;
  570. /* number of bytes to write */
  571. s->init_num=4;
  572. s->init_off=0;
  573. }
  574. /* SSL3_ST_SW_HELLO_REQ_B */
  575. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  576. }
  577. static int ssl3_check_client_hello(SSL *s)
  578. {
  579. int ok;
  580. long n;
  581. /* this function is called when we really expect a Certificate message,
  582. * so permit appropriate message length */
  583. n=ssl3_get_message(s,
  584. SSL3_ST_SR_CERT_A,
  585. SSL3_ST_SR_CERT_B,
  586. -1,
  587. s->max_cert_list,
  588. &ok);
  589. if (!ok) return((int)n);
  590. s->s3->tmp.reuse_message = 1;
  591. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
  592. {
  593. /* Throw away what we have done so far in the current handshake,
  594. * which will now be aborted. (A full SSL_clear would be too much.)
  595. * I hope that tmp.dh is the only thing that may need to be cleared
  596. * when a handshake is not completed ... */
  597. #ifndef OPENSSL_NO_DH
  598. if (s->s3->tmp.dh != NULL)
  599. {
  600. DH_free(s->s3->tmp.dh);
  601. s->s3->tmp.dh = NULL;
  602. }
  603. #endif
  604. return 2;
  605. }
  606. return 1;
  607. }
  608. static int ssl3_get_client_hello(SSL *s)
  609. {
  610. int i,j,ok,al,ret= -1;
  611. long n;
  612. unsigned long id;
  613. unsigned char *p,*d,*q;
  614. SSL_CIPHER *c;
  615. SSL_COMP *comp=NULL;
  616. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  617. /* We do this so that we will respond with our native type.
  618. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  619. * This down switching should be handled by a different method.
  620. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  621. * TLSv1.
  622. */
  623. if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
  624. {
  625. s->first_packet=1;
  626. s->state=SSL3_ST_SR_CLNT_HELLO_B;
  627. }
  628. n=ssl3_get_message(s,
  629. SSL3_ST_SR_CLNT_HELLO_B,
  630. SSL3_ST_SR_CLNT_HELLO_C,
  631. SSL3_MT_CLIENT_HELLO,
  632. SSL3_RT_MAX_PLAIN_LENGTH,
  633. &ok);
  634. if (!ok) return((int)n);
  635. d=p=(unsigned char *)s->init_msg;
  636. /* use version from inside client hello, not from record header
  637. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  638. s->client_version=(((int)p[0])<<8)|(int)p[1];
  639. p+=2;
  640. if (s->client_version < s->version)
  641. {
  642. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
  643. if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
  644. {
  645. /* similar to ssl3_get_record, send alert using remote version number */
  646. s->version = s->client_version;
  647. }
  648. al = SSL_AD_PROTOCOL_VERSION;
  649. goto f_err;
  650. }
  651. /* load the client random */
  652. memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
  653. p+=SSL3_RANDOM_SIZE;
  654. /* get the session-id */
  655. j= *(p++);
  656. s->hit=0;
  657. /* Versions before 0.9.7 always allow session reuse during renegotiation
  658. * (i.e. when s->new_session is true), option
  659. * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
  660. * Maybe this optional behaviour should always have been the default,
  661. * but we cannot safely change the default behaviour (or new applications
  662. * might be written that become totally unsecure when compiled with
  663. * an earlier library version)
  664. */
  665. if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
  666. {
  667. if (!ssl_get_new_session(s,1))
  668. goto err;
  669. }
  670. else
  671. {
  672. i=ssl_get_prev_session(s,p,j);
  673. if (i == 1)
  674. { /* previous session */
  675. s->hit=1;
  676. }
  677. else if (i == -1)
  678. goto err;
  679. else /* i == 0 */
  680. {
  681. if (!ssl_get_new_session(s,1))
  682. goto err;
  683. }
  684. }
  685. p+=j;
  686. n2s(p,i);
  687. if ((i == 0) && (j != 0))
  688. {
  689. /* we need a cipher if we are not resuming a session */
  690. al=SSL_AD_ILLEGAL_PARAMETER;
  691. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
  692. goto f_err;
  693. }
  694. if ((p+i) >= (d+n))
  695. {
  696. /* not enough data */
  697. al=SSL_AD_DECODE_ERROR;
  698. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  699. goto f_err;
  700. }
  701. if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
  702. == NULL))
  703. {
  704. goto err;
  705. }
  706. p+=i;
  707. /* If it is a hit, check that the cipher is in the list */
  708. if ((s->hit) && (i > 0))
  709. {
  710. j=0;
  711. id=s->session->cipher->id;
  712. #ifdef CIPHER_DEBUG
  713. printf("client sent %d ciphers\n",sk_num(ciphers));
  714. #endif
  715. for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
  716. {
  717. c=sk_SSL_CIPHER_value(ciphers,i);
  718. #ifdef CIPHER_DEBUG
  719. printf("client [%2d of %2d]:%s\n",
  720. i,sk_num(ciphers),SSL_CIPHER_get_name(c));
  721. #endif
  722. if (c->id == id)
  723. {
  724. j=1;
  725. break;
  726. }
  727. }
  728. if (j == 0)
  729. {
  730. if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
  731. {
  732. /* Very bad for multi-threading.... */
  733. s->session->cipher=sk_SSL_CIPHER_value(ciphers,
  734. 0);
  735. }
  736. else
  737. {
  738. /* we need to have the cipher in the cipher
  739. * list if we are asked to reuse it */
  740. al=SSL_AD_ILLEGAL_PARAMETER;
  741. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
  742. goto f_err;
  743. }
  744. }
  745. }
  746. /* compression */
  747. i= *(p++);
  748. if ((p+i) > (d+n))
  749. {
  750. /* not enough data */
  751. al=SSL_AD_DECODE_ERROR;
  752. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  753. goto f_err;
  754. }
  755. q=p;
  756. for (j=0; j<i; j++)
  757. {
  758. if (p[j] == 0) break;
  759. }
  760. p+=i;
  761. if (j >= i)
  762. {
  763. /* no compress */
  764. al=SSL_AD_DECODE_ERROR;
  765. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
  766. goto f_err;
  767. }
  768. /* Worst case, we will use the NULL compression, but if we have other
  769. * options, we will now look for them. We have i-1 compression
  770. * algorithms from the client, starting at q. */
  771. s->s3->tmp.new_compression=NULL;
  772. if (s->ctx->comp_methods != NULL)
  773. { /* See if we have a match */
  774. int m,nn,o,v,done=0;
  775. nn=sk_SSL_COMP_num(s->ctx->comp_methods);
  776. for (m=0; m<nn; m++)
  777. {
  778. comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
  779. v=comp->id;
  780. for (o=0; o<i; o++)
  781. {
  782. if (v == q[o])
  783. {
  784. done=1;
  785. break;
  786. }
  787. }
  788. if (done) break;
  789. }
  790. if (done)
  791. s->s3->tmp.new_compression=comp;
  792. else
  793. comp=NULL;
  794. }
  795. /* TLS does not mind if there is extra stuff */
  796. #if 0 /* SSL 3.0 does not mind either, so we should disable this test
  797. * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
  798. * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
  799. if (s->version == SSL3_VERSION)
  800. {
  801. if (p < (d+n))
  802. {
  803. /* wrong number of bytes,
  804. * there could be more to follow */
  805. al=SSL_AD_DECODE_ERROR;
  806. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
  807. goto f_err;
  808. }
  809. }
  810. #endif
  811. /* Given s->session->ciphers and SSL_get_ciphers, we must
  812. * pick a cipher */
  813. if (!s->hit)
  814. {
  815. s->session->compress_meth=(comp == NULL)?0:comp->id;
  816. if (s->session->ciphers != NULL)
  817. sk_SSL_CIPHER_free(s->session->ciphers);
  818. s->session->ciphers=ciphers;
  819. if (ciphers == NULL)
  820. {
  821. al=SSL_AD_ILLEGAL_PARAMETER;
  822. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
  823. goto f_err;
  824. }
  825. ciphers=NULL;
  826. c=ssl3_choose_cipher(s,s->session->ciphers,
  827. SSL_get_ciphers(s));
  828. if (c == NULL)
  829. {
  830. al=SSL_AD_HANDSHAKE_FAILURE;
  831. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  832. goto f_err;
  833. }
  834. s->s3->tmp.new_cipher=c;
  835. }
  836. else
  837. {
  838. /* Session-id reuse */
  839. #ifdef REUSE_CIPHER_BUG
  840. STACK_OF(SSL_CIPHER) *sk;
  841. SSL_CIPHER *nc=NULL;
  842. SSL_CIPHER *ec=NULL;
  843. if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
  844. {
  845. sk=s->session->ciphers;
  846. for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
  847. {
  848. c=sk_SSL_CIPHER_value(sk,i);
  849. if (c->algorithms & SSL_eNULL)
  850. nc=c;
  851. if (SSL_C_IS_EXPORT(c))
  852. ec=c;
  853. }
  854. if (nc != NULL)
  855. s->s3->tmp.new_cipher=nc;
  856. else if (ec != NULL)
  857. s->s3->tmp.new_cipher=ec;
  858. else
  859. s->s3->tmp.new_cipher=s->session->cipher;
  860. }
  861. else
  862. #endif
  863. s->s3->tmp.new_cipher=s->session->cipher;
  864. }
  865. /* we now have the following setup.
  866. * client_random
  867. * cipher_list - our prefered list of ciphers
  868. * ciphers - the clients prefered list of ciphers
  869. * compression - basically ignored right now
  870. * ssl version is set - sslv3
  871. * s->session - The ssl session has been setup.
  872. * s->hit - session reuse flag
  873. * s->tmp.new_cipher - the new cipher to use.
  874. */
  875. ret=1;
  876. if (0)
  877. {
  878. f_err:
  879. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  880. }
  881. err:
  882. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  883. return(ret);
  884. }
  885. static int ssl3_send_server_hello(SSL *s)
  886. {
  887. unsigned char *buf;
  888. unsigned char *p,*d;
  889. int i,sl;
  890. unsigned long l,Time;
  891. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  892. {
  893. buf=(unsigned char *)s->init_buf->data;
  894. p=s->s3->server_random;
  895. Time=time(NULL); /* Time */
  896. l2n(Time,p);
  897. RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
  898. /* Do the message type and length last */
  899. d=p= &(buf[4]);
  900. *(p++)=s->version>>8;
  901. *(p++)=s->version&0xff;
  902. /* Random stuff */
  903. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  904. p+=SSL3_RANDOM_SIZE;
  905. /* now in theory we have 3 options to sending back the
  906. * session id. If it is a re-use, we send back the
  907. * old session-id, if it is a new session, we send
  908. * back the new session-id or we send back a 0 length
  909. * session-id if we want it to be single use.
  910. * Currently I will not implement the '0' length session-id
  911. * 12-Jan-98 - I'll now support the '0' length stuff.
  912. */
  913. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
  914. s->session->session_id_length=0;
  915. sl=s->session->session_id_length;
  916. if (sl > (int)sizeof(s->session->session_id))
  917. {
  918. SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  919. return -1;
  920. }
  921. *(p++)=sl;
  922. memcpy(p,s->session->session_id,sl);
  923. p+=sl;
  924. /* put the cipher */
  925. i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
  926. p+=i;
  927. /* put the compression method */
  928. if (s->s3->tmp.new_compression == NULL)
  929. *(p++)=0;
  930. else
  931. *(p++)=s->s3->tmp.new_compression->id;
  932. /* do the header */
  933. l=(p-d);
  934. d=buf;
  935. *(d++)=SSL3_MT_SERVER_HELLO;
  936. l2n3(l,d);
  937. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  938. /* number of bytes to write */
  939. s->init_num=p-buf;
  940. s->init_off=0;
  941. }
  942. /* SSL3_ST_CW_CLNT_HELLO_B */
  943. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  944. }
  945. static int ssl3_send_server_done(SSL *s)
  946. {
  947. unsigned char *p;
  948. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  949. {
  950. p=(unsigned char *)s->init_buf->data;
  951. /* do the header */
  952. *(p++)=SSL3_MT_SERVER_DONE;
  953. *(p++)=0;
  954. *(p++)=0;
  955. *(p++)=0;
  956. s->state=SSL3_ST_SW_SRVR_DONE_B;
  957. /* number of bytes to write */
  958. s->init_num=4;
  959. s->init_off=0;
  960. }
  961. /* SSL3_ST_CW_CLNT_HELLO_B */
  962. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  963. }
  964. static int ssl3_send_server_key_exchange(SSL *s)
  965. {
  966. #ifndef OPENSSL_NO_RSA
  967. unsigned char *q;
  968. int j,num;
  969. RSA *rsa;
  970. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  971. unsigned int u;
  972. #endif
  973. #ifndef OPENSSL_NO_DH
  974. DH *dh=NULL,*dhp;
  975. #endif
  976. #ifndef OPENSSL_NO_ECDH
  977. EC_KEY *ecdh=NULL, *ecdhp;
  978. unsigned char *encodedPoint = NULL;
  979. int encodedlen = 0;
  980. int curve_id = 0;
  981. BN_CTX *bn_ctx = NULL;
  982. #endif
  983. EVP_PKEY *pkey;
  984. unsigned char *p,*d;
  985. int al,i;
  986. unsigned long type;
  987. int n;
  988. CERT *cert;
  989. BIGNUM *r[4];
  990. int nr[4],kn;
  991. BUF_MEM *buf;
  992. EVP_MD_CTX md_ctx;
  993. EVP_MD_CTX_init(&md_ctx);
  994. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  995. {
  996. type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
  997. cert=s->cert;
  998. buf=s->init_buf;
  999. r[0]=r[1]=r[2]=r[3]=NULL;
  1000. n=0;
  1001. #ifndef OPENSSL_NO_RSA
  1002. if (type & SSL_kRSA)
  1003. {
  1004. rsa=cert->rsa_tmp;
  1005. if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
  1006. {
  1007. rsa=s->cert->rsa_tmp_cb(s,
  1008. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1009. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1010. if(rsa == NULL)
  1011. {
  1012. al=SSL_AD_HANDSHAKE_FAILURE;
  1013. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
  1014. goto f_err;
  1015. }
  1016. RSA_up_ref(rsa);
  1017. cert->rsa_tmp=rsa;
  1018. }
  1019. if (rsa == NULL)
  1020. {
  1021. al=SSL_AD_HANDSHAKE_FAILURE;
  1022. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
  1023. goto f_err;
  1024. }
  1025. r[0]=rsa->n;
  1026. r[1]=rsa->e;
  1027. s->s3->tmp.use_rsa_tmp=1;
  1028. }
  1029. else
  1030. #endif
  1031. #ifndef OPENSSL_NO_DH
  1032. if (type & SSL_kEDH)
  1033. {
  1034. dhp=cert->dh_tmp;
  1035. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1036. dhp=s->cert->dh_tmp_cb(s,
  1037. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1038. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1039. if (dhp == NULL)
  1040. {
  1041. al=SSL_AD_HANDSHAKE_FAILURE;
  1042. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1043. goto f_err;
  1044. }
  1045. if (s->s3->tmp.dh != NULL)
  1046. {
  1047. DH_free(dh);
  1048. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1049. goto err;
  1050. }
  1051. if ((dh=DHparams_dup(dhp)) == NULL)
  1052. {
  1053. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1054. goto err;
  1055. }
  1056. s->s3->tmp.dh=dh;
  1057. if ((dhp->pub_key == NULL ||
  1058. dhp->priv_key == NULL ||
  1059. (s->options & SSL_OP_SINGLE_DH_USE)))
  1060. {
  1061. if(!DH_generate_key(dh))
  1062. {
  1063. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
  1064. ERR_R_DH_LIB);
  1065. goto err;
  1066. }
  1067. }
  1068. else
  1069. {
  1070. dh->pub_key=BN_dup(dhp->pub_key);
  1071. dh->priv_key=BN_dup(dhp->priv_key);
  1072. if ((dh->pub_key == NULL) ||
  1073. (dh->priv_key == NULL))
  1074. {
  1075. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
  1076. goto err;
  1077. }
  1078. }
  1079. r[0]=dh->p;
  1080. r[1]=dh->g;
  1081. r[2]=dh->pub_key;
  1082. }
  1083. else
  1084. #endif
  1085. #ifndef OPENSSL_NO_ECDH
  1086. if (type & SSL_kECDHE)
  1087. {
  1088. ecdhp=cert->ecdh_tmp;
  1089. if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
  1090. {
  1091. ecdhp=s->cert->ecdh_tmp_cb(s,
  1092. SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
  1093. SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
  1094. }
  1095. if (ecdhp == NULL)
  1096. {
  1097. al=SSL_AD_HANDSHAKE_FAILURE;
  1098. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1099. goto f_err;
  1100. }
  1101. if (s->s3->tmp.ecdh != NULL)
  1102. {
  1103. EC_KEY_free(s->s3->tmp.ecdh);
  1104. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  1105. goto err;
  1106. }
  1107. /* Duplicate the ECDH structure. */
  1108. if (ecdhp == NULL)
  1109. {
  1110. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1111. goto err;
  1112. }
  1113. if (!EC_KEY_up_ref(ecdhp))
  1114. {
  1115. SSLerr(SSL_F_SSL3_CTRL,ERR_R_ECDH_LIB);
  1116. goto err;
  1117. }
  1118. ecdh = ecdhp;
  1119. s->s3->tmp.ecdh=ecdh;
  1120. if ((ecdh->pub_key == NULL) ||
  1121. (ecdh->priv_key == NULL) ||
  1122. (s->options & SSL_OP_SINGLE_ECDH_USE))
  1123. {
  1124. if(!EC_KEY_generate_key(ecdh))
  1125. {
  1126. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1127. goto err;
  1128. }
  1129. }
  1130. if ((ecdh->group == NULL) ||
  1131. (ecdh->pub_key == NULL) ||
  1132. (ecdh->priv_key == NULL))
  1133. {
  1134. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1135. goto err;
  1136. }
  1137. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1138. (EC_GROUP_get_degree(ecdh->group) > 163))
  1139. {
  1140. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1141. goto err;
  1142. }
  1143. /* XXX: For now, we only support ephemeral ECDH
  1144. * keys over named (not generic) curves. For
  1145. * supported named curves, curve_id is non-zero.
  1146. */
  1147. if ((curve_id =
  1148. nid2curve_id(EC_GROUP_get_nid(ecdh->group)))
  1149. == 0)
  1150. {
  1151. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1152. goto err;
  1153. }
  1154. /* Encode the public key.
  1155. * First check the size of encoding and
  1156. * allocate memory accordingly.
  1157. */
  1158. encodedlen = EC_POINT_point2oct(ecdh->group,
  1159. ecdh->pub_key,
  1160. POINT_CONVERSION_UNCOMPRESSED,
  1161. NULL, 0, NULL);
  1162. encodedPoint = (unsigned char *)
  1163. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  1164. bn_ctx = BN_CTX_new();
  1165. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1166. {
  1167. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
  1168. goto err;
  1169. }
  1170. encodedlen = EC_POINT_point2oct(ecdh->group,
  1171. ecdh->pub_key,
  1172. POINT_CONVERSION_UNCOMPRESSED,
  1173. encodedPoint, encodedlen, bn_ctx);
  1174. if (encodedlen == 0)
  1175. {
  1176. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
  1177. goto err;
  1178. }
  1179. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  1180. /* XXX: For now, we only support named (not
  1181. * generic) curves in ECDH ephemeral key exchanges.
  1182. * In this situation, we need three additional bytes
  1183. * to encode the entire ServerECDHParams
  1184. * structure.
  1185. */
  1186. n = 3 + encodedlen;
  1187. /* We'll generate the serverKeyExchange message
  1188. * explicitly so we can set these to NULLs
  1189. */
  1190. r[0]=NULL;
  1191. r[1]=NULL;
  1192. r[2]=NULL;
  1193. }
  1194. else
  1195. #endif /* !OPENSSL_NO_ECDH */
  1196. {
  1197. al=SSL_AD_HANDSHAKE_FAILURE;
  1198. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1199. goto f_err;
  1200. }
  1201. for (i=0; r[i] != NULL; i++)
  1202. {
  1203. nr[i]=BN_num_bytes(r[i]);
  1204. n+=2+nr[i];
  1205. }
  1206. if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
  1207. {
  1208. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
  1209. == NULL)
  1210. {
  1211. al=SSL_AD_DECODE_ERROR;
  1212. goto f_err;
  1213. }
  1214. kn=EVP_PKEY_size(pkey);
  1215. }
  1216. else
  1217. {
  1218. pkey=NULL;
  1219. kn=0;
  1220. }
  1221. if (!BUF_MEM_grow_clean(buf,n+4+kn))
  1222. {
  1223. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
  1224. goto err;
  1225. }
  1226. d=(unsigned char *)s->init_buf->data;
  1227. p= &(d[4]);
  1228. for (i=0; r[i] != NULL; i++)
  1229. {
  1230. s2n(nr[i],p);
  1231. BN_bn2bin(r[i],p);
  1232. p+=nr[i];
  1233. }
  1234. #ifndef OPENSSL_NO_ECDH
  1235. if (type & SSL_kECDHE)
  1236. {
  1237. /* XXX: For now, we only support named (not generic) curves.
  1238. * In this situation, the serverKeyExchange message has:
  1239. * [1 byte CurveType], [1 byte CurveName]
  1240. * [1 byte length of encoded point], followed by
  1241. * the actual encoded point itself
  1242. */
  1243. *p = NAMED_CURVE_TYPE;
  1244. p += 1;
  1245. *p = curve_id;
  1246. p += 1;
  1247. *p = encodedlen;
  1248. p += 1;
  1249. memcpy((unsigned char*)p,
  1250. (unsigned char *)encodedPoint,
  1251. encodedlen);
  1252. OPENSSL_free(encodedPoint);
  1253. p += encodedlen;
  1254. }
  1255. #endif
  1256. /* not anonymous */
  1257. if (pkey != NULL)
  1258. {
  1259. /* n is the length of the params, they start at &(d[4])
  1260. * and p points to the space at the end. */
  1261. #ifndef OPENSSL_NO_RSA
  1262. if (pkey->type == EVP_PKEY_RSA)
  1263. {
  1264. q=md_buf;
  1265. j=0;
  1266. for (num=2; num > 0; num--)
  1267. {
  1268. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1269. ?s->ctx->md5:s->ctx->sha1, NULL);
  1270. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1271. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1272. EVP_DigestUpdate(&md_ctx,&(d[4]),n);
  1273. EVP_DigestFinal_ex(&md_ctx,q,
  1274. (unsigned int *)&i);
  1275. q+=i;
  1276. j+=i;
  1277. }
  1278. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1279. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1280. {
  1281. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
  1282. goto err;
  1283. }
  1284. s2n(u,p);
  1285. n+=u+2;
  1286. }
  1287. else
  1288. #endif
  1289. #if !defined(OPENSSL_NO_DSA)
  1290. if (pkey->type == EVP_PKEY_DSA)
  1291. {
  1292. /* lets do DSS */
  1293. EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
  1294. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1295. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1296. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1297. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1298. (unsigned int *)&i,pkey))
  1299. {
  1300. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
  1301. goto err;
  1302. }
  1303. s2n(i,p);
  1304. n+=i+2;
  1305. }
  1306. else
  1307. #endif
  1308. #if !defined(OPENSSL_NO_ECDSA)
  1309. if (pkey->type == EVP_PKEY_EC)
  1310. {
  1311. /* let's do ECDSA */
  1312. EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
  1313. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1314. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1315. EVP_SignUpdate(&md_ctx,&(d[4]),n);
  1316. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1317. (unsigned int *)&i,pkey))
  1318. {
  1319. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
  1320. goto err;
  1321. }
  1322. s2n(i,p);
  1323. n+=i+2;
  1324. }
  1325. else
  1326. #endif
  1327. {
  1328. /* Is this error check actually needed? */
  1329. al=SSL_AD_HANDSHAKE_FAILURE;
  1330. SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
  1331. goto f_err;
  1332. }
  1333. }
  1334. *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
  1335. l2n3(n,d);
  1336. /* we should now have things packed up, so lets send
  1337. * it off */
  1338. s->init_num=n+4;
  1339. s->init_off=0;
  1340. }
  1341. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1342. EVP_MD_CTX_cleanup(&md_ctx);
  1343. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1344. f_err:
  1345. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1346. err:
  1347. #ifndef OPENSSL_NO_ECDH
  1348. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1349. BN_CTX_free(bn_ctx);
  1350. #endif
  1351. EVP_MD_CTX_cleanup(&md_ctx);
  1352. return(-1);
  1353. }
  1354. static int ssl3_send_certificate_request(SSL *s)
  1355. {
  1356. unsigned char *p,*d;
  1357. int i,j,nl,off,n;
  1358. STACK_OF(X509_NAME) *sk=NULL;
  1359. X509_NAME *name;
  1360. BUF_MEM *buf;
  1361. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1362. {
  1363. buf=s->init_buf;
  1364. d=p=(unsigned char *)&(buf->data[4]);
  1365. /* get the list of acceptable cert types */
  1366. p++;
  1367. n=ssl3_get_req_cert_type(s,p);
  1368. d[0]=n;
  1369. p+=n;
  1370. n++;
  1371. off=n;
  1372. p+=2;
  1373. n+=2;
  1374. sk=SSL_get_client_CA_list(s);
  1375. nl=0;
  1376. if (sk != NULL)
  1377. {
  1378. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1379. {
  1380. name=sk_X509_NAME_value(sk,i);
  1381. j=i2d_X509_NAME(name,NULL);
  1382. if (!BUF_MEM_grow_clean(buf,4+n+j+2))
  1383. {
  1384. SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
  1385. goto err;
  1386. }
  1387. p=(unsigned char *)&(buf->data[4+n]);
  1388. if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
  1389. {
  1390. s2n(j,p);
  1391. i2d_X509_NAME(name,&p);
  1392. n+=2+j;
  1393. nl+=2+j;
  1394. }
  1395. else
  1396. {
  1397. d=p;
  1398. i2d_X509_NAME(name,&p);
  1399. j-=2; s2n(j,d); j+=2;
  1400. n+=j;
  1401. nl+=j;
  1402. }
  1403. }
  1404. }
  1405. /* else no CA names */
  1406. p=(unsigned char *)&(buf->data[4+off]);
  1407. s2n(nl,p);
  1408. d=(unsigned char *)buf->data;
  1409. *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
  1410. l2n3(n,d);
  1411. /* we should now have things packed up, so lets send
  1412. * it off */
  1413. s->init_num=n+4;
  1414. s->init_off=0;
  1415. #ifdef NETSCAPE_HANG_BUG
  1416. p=(unsigned char *)s->init_buf->data + s->init_num;
  1417. /* do the header */
  1418. *(p++)=SSL3_MT_SERVER_DONE;
  1419. *(p++)=0;
  1420. *(p++)=0;
  1421. *(p++)=0;
  1422. s->init_num += 4;
  1423. #endif
  1424. s->state = SSL3_ST_SW_CERT_REQ_B;
  1425. }
  1426. /* SSL3_ST_SW_CERT_REQ_B */
  1427. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  1428. err:
  1429. return(-1);
  1430. }
  1431. static const int KDF1_SHA1_len = 20;
  1432. static void *KDF1_SHA1(void *in, size_t inlen, void *out, size_t outlen)
  1433. {
  1434. #ifndef OPENSSL_NO_SHA
  1435. if (outlen != SHA_DIGEST_LENGTH)
  1436. return NULL;
  1437. return SHA1(in, inlen, out);
  1438. #else
  1439. return NULL;
  1440. #endif
  1441. }
  1442. static int ssl3_get_client_key_exchange(SSL *s)
  1443. {
  1444. int i,al,ok;
  1445. long n;
  1446. unsigned long l;
  1447. unsigned char *p;
  1448. #ifndef OPENSSL_NO_RSA
  1449. RSA *rsa=NULL;
  1450. EVP_PKEY *pkey=NULL;
  1451. #endif
  1452. #ifndef OPENSSL_NO_DH
  1453. BIGNUM *pub=NULL;
  1454. DH *dh_srvr;
  1455. #endif
  1456. #ifndef OPENSSL_NO_KRB5
  1457. KSSL_ERR kssl_err;
  1458. #endif /* OPENSSL_NO_KRB5 */
  1459. #ifndef OPENSSL_NO_ECDH
  1460. EC_KEY *srvr_ecdh = NULL;
  1461. EVP_PKEY *clnt_pub_pkey = NULL;
  1462. EC_POINT *clnt_ecpoint = NULL;
  1463. BN_CTX *bn_ctx = NULL;
  1464. #endif
  1465. n=ssl3_get_message(s,
  1466. SSL3_ST_SR_KEY_EXCH_A,
  1467. SSL3_ST_SR_KEY_EXCH_B,
  1468. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1469. 2048, /* ??? */
  1470. &ok);
  1471. if (!ok) return((int)n);
  1472. p=(unsigned char *)s->init_msg;
  1473. l=s->s3->tmp.new_cipher->algorithms;
  1474. #ifndef OPENSSL_NO_RSA
  1475. if (l & SSL_kRSA)
  1476. {
  1477. /* FIX THIS UP EAY EAY EAY EAY */
  1478. if (s->s3->tmp.use_rsa_tmp)
  1479. {
  1480. if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
  1481. rsa=s->cert->rsa_tmp;
  1482. /* Don't do a callback because rsa_tmp should
  1483. * be sent already */
  1484. if (rsa == NULL)
  1485. {
  1486. al=SSL_AD_HANDSHAKE_FAILURE;
  1487. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
  1488. goto f_err;
  1489. }
  1490. }
  1491. else
  1492. {
  1493. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1494. if ( (pkey == NULL) ||
  1495. (pkey->type != EVP_PKEY_RSA) ||
  1496. (pkey->pkey.rsa == NULL))
  1497. {
  1498. al=SSL_AD_HANDSHAKE_FAILURE;
  1499. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
  1500. goto f_err;
  1501. }
  1502. rsa=pkey->pkey.rsa;
  1503. }
  1504. /* TLS */
  1505. if (s->version > SSL3_VERSION)
  1506. {
  1507. n2s(p,i);
  1508. if (n != i+2)
  1509. {
  1510. if (!(s->options & SSL_OP_TLS_D5_BUG))
  1511. {
  1512. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1513. goto err;
  1514. }
  1515. else
  1516. p-=2;
  1517. }
  1518. else
  1519. n=i;
  1520. }
  1521. i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
  1522. al = -1;
  1523. if (i != SSL_MAX_MASTER_KEY_LENGTH)
  1524. {
  1525. al=SSL_AD_DECODE_ERROR;
  1526. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
  1527. }
  1528. if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
  1529. {
  1530. /* The premaster secret must contain the same version number as the
  1531. * ClientHello to detect version rollback attacks (strangely, the
  1532. * protocol does not offer such protection for DH ciphersuites).
  1533. * However, buggy clients exist that send the negotiated protocol
  1534. * version instead if the server does not support the requested
  1535. * protocol version.
  1536. * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
  1537. if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
  1538. (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
  1539. {
  1540. al=SSL_AD_DECODE_ERROR;
  1541. /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
  1542. /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
  1543. * (http://eprint.iacr.org/2003/052/) exploits the version
  1544. * number check as a "bad version oracle" -- an alert would
  1545. * reveal that the plaintext corresponding to some ciphertext
  1546. * made up by the adversary is properly formatted except
  1547. * that the version number is wrong. To avoid such attacks,
  1548. * we should treat this just like any other decryption error. */
  1549. }
  1550. }
  1551. if (al != -1)
  1552. {
  1553. /* Some decryption failure -- use random value instead as countermeasure
  1554. * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
  1555. * (see RFC 2246, section 7.4.7.1). */
  1556. ERR_clear_error();
  1557. i = SSL_MAX_MASTER_KEY_LENGTH;
  1558. p[0] = s->client_version >> 8;
  1559. p[1] = s->client_version & 0xff;
  1560. RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
  1561. }
  1562. s->session->master_key_length=
  1563. s->method->ssl3_enc->generate_master_secret(s,
  1564. s->session->master_key,
  1565. p,i);
  1566. OPENSSL_cleanse(p,i);
  1567. }
  1568. else
  1569. #endif
  1570. #ifndef OPENSSL_NO_DH
  1571. if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1572. {
  1573. n2s(p,i);
  1574. if (n != i+2)
  1575. {
  1576. if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
  1577. {
  1578. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1579. goto err;
  1580. }
  1581. else
  1582. {
  1583. p-=2;
  1584. i=(int)n;
  1585. }
  1586. }
  1587. if (n == 0L) /* the parameters are in the cert */
  1588. {
  1589. al=SSL_AD_HANDSHAKE_FAILURE;
  1590. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
  1591. goto f_err;
  1592. }
  1593. else
  1594. {
  1595. if (s->s3->tmp.dh == NULL)
  1596. {
  1597. al=SSL_AD_HANDSHAKE_FAILURE;
  1598. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
  1599. goto f_err;
  1600. }
  1601. else
  1602. dh_srvr=s->s3->tmp.dh;
  1603. }
  1604. pub=BN_bin2bn(p,i,NULL);
  1605. if (pub == NULL)
  1606. {
  1607. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
  1608. goto err;
  1609. }
  1610. i=DH_compute_key(p,pub,dh_srvr);
  1611. if (i <= 0)
  1612. {
  1613. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
  1614. goto err;
  1615. }
  1616. DH_free(s->s3->tmp.dh);
  1617. s->s3->tmp.dh=NULL;
  1618. BN_clear_free(pub);
  1619. pub=NULL;
  1620. s->session->master_key_length=
  1621. s->method->ssl3_enc->generate_master_secret(s,
  1622. s->session->master_key,p,i);
  1623. OPENSSL_cleanse(p,i);
  1624. }
  1625. else
  1626. #endif
  1627. #ifndef OPENSSL_NO_KRB5
  1628. if (l & SSL_kKRB5)
  1629. {
  1630. krb5_error_code krb5rc;
  1631. krb5_data enc_ticket;
  1632. krb5_data authenticator;
  1633. krb5_data enc_pms;
  1634. KSSL_CTX *kssl_ctx = s->kssl_ctx;
  1635. EVP_CIPHER_CTX ciph_ctx;
  1636. EVP_CIPHER *enc = NULL;
  1637. unsigned char iv[EVP_MAX_IV_LENGTH];
  1638. unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
  1639. + EVP_MAX_BLOCK_LENGTH];
  1640. int padl, outl;
  1641. krb5_timestamp authtime = 0;
  1642. krb5_ticket_times ttimes;
  1643. EVP_CIPHER_CTX_init(&ciph_ctx);
  1644. if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
  1645. n2s(p,i);
  1646. enc_ticket.length = i;
  1647. enc_ticket.data = (char *)p;
  1648. p+=enc_ticket.length;
  1649. n2s(p,i);
  1650. authenticator.length = i;
  1651. authenticator.data = (char *)p;
  1652. p+=authenticator.length;
  1653. n2s(p,i);
  1654. enc_pms.length = i;
  1655. enc_pms.data = (char *)p;
  1656. p+=enc_pms.length;
  1657. /* Note that the length is checked again below,
  1658. ** after decryption
  1659. */
  1660. if(enc_pms.length > sizeof pms)
  1661. {
  1662. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1663. SSL_R_DATA_LENGTH_TOO_LONG);
  1664. goto err;
  1665. }
  1666. if (n != enc_ticket.length + authenticator.length +
  1667. enc_pms.length + 6)
  1668. {
  1669. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1670. SSL_R_DATA_LENGTH_TOO_LONG);
  1671. goto err;
  1672. }
  1673. if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
  1674. &kssl_err)) != 0)
  1675. {
  1676. #ifdef KSSL_DEBUG
  1677. printf("kssl_sget_tkt rtn %d [%d]\n",
  1678. krb5rc, kssl_err.reason);
  1679. if (kssl_err.text)
  1680. printf("kssl_err text= %s\n", kssl_err.text);
  1681. #endif /* KSSL_DEBUG */
  1682. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1683. kssl_err.reason);
  1684. goto err;
  1685. }
  1686. /* Note: no authenticator is not considered an error,
  1687. ** but will return authtime == 0.
  1688. */
  1689. if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
  1690. &authtime, &kssl_err)) != 0)
  1691. {
  1692. #ifdef KSSL_DEBUG
  1693. printf("kssl_check_authent rtn %d [%d]\n",
  1694. krb5rc, kssl_err.reason);
  1695. if (kssl_err.text)
  1696. printf("kssl_err text= %s\n", kssl_err.text);
  1697. #endif /* KSSL_DEBUG */
  1698. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
  1699. kssl_err.reason);
  1700. goto err;
  1701. }
  1702. if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
  1703. {
  1704. SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
  1705. goto err;
  1706. }
  1707. #ifdef KSSL_DEBUG
  1708. kssl_ctx_show(kssl_ctx);
  1709. #endif /* KSSL_DEBUG */
  1710. enc = kssl_map_enc(kssl_ctx->enctype);
  1711. if (enc == NULL)
  1712. goto err;
  1713. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1714. if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
  1715. {
  1716. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1717. SSL_R_DECRYPTION_FAILED);
  1718. goto err;
  1719. }
  1720. if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
  1721. (unsigned char *)enc_pms.data, enc_pms.length))
  1722. {
  1723. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1724. SSL_R_DECRYPTION_FAILED);
  1725. goto err;
  1726. }
  1727. if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  1728. {
  1729. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1730. SSL_R_DATA_LENGTH_TOO_LONG);
  1731. goto err;
  1732. }
  1733. if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
  1734. {
  1735. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1736. SSL_R_DECRYPTION_FAILED);
  1737. goto err;
  1738. }
  1739. outl += padl;
  1740. if (outl > SSL_MAX_MASTER_KEY_LENGTH)
  1741. {
  1742. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1743. SSL_R_DATA_LENGTH_TOO_LONG);
  1744. goto err;
  1745. }
  1746. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1747. s->session->master_key_length=
  1748. s->method->ssl3_enc->generate_master_secret(s,
  1749. s->session->master_key, pms, outl);
  1750. if (kssl_ctx->client_princ)
  1751. {
  1752. int len = strlen(kssl_ctx->client_princ);
  1753. if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
  1754. {
  1755. s->session->krb5_client_princ_len = len;
  1756. memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
  1757. }
  1758. }
  1759. /* Was doing kssl_ctx_free() here,
  1760. ** but it caused problems for apache.
  1761. ** kssl_ctx = kssl_ctx_free(kssl_ctx);
  1762. ** if (s->kssl_ctx) s->kssl_ctx = NULL;
  1763. */
  1764. }
  1765. else
  1766. #endif /* OPENSSL_NO_KRB5 */
  1767. #ifndef OPENSSL_NO_ECDH
  1768. if ((l & SSL_kECDH) || (l & SSL_kECDHE))
  1769. {
  1770. int ret = 1;
  1771. int field_size = 0;
  1772. /* initialize structures for server's ECDH key pair */
  1773. if ((srvr_ecdh = EC_KEY_new()) == NULL)
  1774. {
  1775. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1776. ERR_R_MALLOC_FAILURE);
  1777. goto err;
  1778. }
  1779. /* Let's get server private key and group information */
  1780. if (l & SSL_kECDH)
  1781. {
  1782. /* use the certificate */
  1783. srvr_ecdh->group = s->cert->key->privatekey-> \
  1784. pkey.eckey->group;
  1785. srvr_ecdh->priv_key = s->cert->key->privatekey-> \
  1786. pkey.eckey->priv_key;
  1787. }
  1788. else
  1789. {
  1790. /* use the ephermeral values we saved when
  1791. * generating the ServerKeyExchange msg.
  1792. */
  1793. srvr_ecdh->group = s->s3->tmp.ecdh->group;
  1794. srvr_ecdh->priv_key = s->s3->tmp.ecdh->priv_key;
  1795. }
  1796. /* Let's get client's public key */
  1797. if ((clnt_ecpoint = EC_POINT_new(srvr_ecdh->group))
  1798. == NULL)
  1799. {
  1800. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1801. ERR_R_MALLOC_FAILURE);
  1802. goto err;
  1803. }
  1804. if (n == 0L)
  1805. {
  1806. /* Client Publickey was in Client Certificate */
  1807. if (l & SSL_kECDHE)
  1808. {
  1809. al=SSL_AD_HANDSHAKE_FAILURE;
  1810. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
  1811. goto f_err;
  1812. }
  1813. if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
  1814. == NULL) ||
  1815. (clnt_pub_pkey->type != EVP_PKEY_EC))
  1816. {
  1817. /* XXX: For now, we do not support client
  1818. * authentication using ECDH certificates
  1819. * so this branch (n == 0L) of the code is
  1820. * never executed. When that support is
  1821. * added, we ought to ensure the key
  1822. * received in the certificate is
  1823. * authorized for key agreement.
  1824. * ECDH_compute_key implicitly checks that
  1825. * the two ECDH shares are for the same
  1826. * group.
  1827. */
  1828. al=SSL_AD_HANDSHAKE_FAILURE;
  1829. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1830. SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
  1831. goto f_err;
  1832. }
  1833. EC_POINT_copy(clnt_ecpoint,
  1834. clnt_pub_pkey->pkey.eckey->pub_key);
  1835. ret = 2; /* Skip certificate verify processing */
  1836. }
  1837. else
  1838. {
  1839. /* Get client's public key from encoded point
  1840. * in the ClientKeyExchange message.
  1841. */
  1842. if ((bn_ctx = BN_CTX_new()) == NULL)
  1843. {
  1844. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1845. ERR_R_MALLOC_FAILURE);
  1846. goto err;
  1847. }
  1848. /* Get encoded point length */
  1849. i = *p;
  1850. p += 1;
  1851. if (EC_POINT_oct2point(srvr_ecdh->group,
  1852. clnt_ecpoint, p, i, bn_ctx) == 0)
  1853. {
  1854. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1855. ERR_R_EC_LIB);
  1856. goto err;
  1857. }
  1858. /* p is pointing to somewhere in the buffer
  1859. * currently, so set it to the start
  1860. */
  1861. p=(unsigned char *)s->init_buf->data;
  1862. }
  1863. /* Compute the shared pre-master secret */
  1864. field_size = EC_GROUP_get_degree(srvr_ecdh->group);
  1865. if (field_size <= 0)
  1866. {
  1867. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1868. ERR_R_ECDH_LIB);
  1869. goto err;
  1870. }
  1871. /* If field size is not more than 24 octets, then use SHA-1 hash of result;
  1872. * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt;
  1873. * this is new with this version of the Internet Draft).
  1874. */
  1875. if (field_size <= 24 * 8)
  1876. i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1);
  1877. else
  1878. i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL);
  1879. if (i <= 0)
  1880. {
  1881. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1882. ERR_R_ECDH_LIB);
  1883. goto err;
  1884. }
  1885. EVP_PKEY_free(clnt_pub_pkey);
  1886. EC_POINT_free(clnt_ecpoint);
  1887. if (srvr_ecdh != NULL)
  1888. {
  1889. srvr_ecdh->priv_key = NULL;
  1890. srvr_ecdh->group = NULL;
  1891. EC_KEY_free(srvr_ecdh);
  1892. }
  1893. BN_CTX_free(bn_ctx);
  1894. /* Compute the master secret */
  1895. s->session->master_key_length = s->method->ssl3_enc-> \
  1896. generate_master_secret(s, s->session->master_key, p, i);
  1897. OPENSSL_cleanse(p, i);
  1898. return (ret);
  1899. }
  1900. else
  1901. #endif
  1902. {
  1903. al=SSL_AD_HANDSHAKE_FAILURE;
  1904. SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
  1905. SSL_R_UNKNOWN_CIPHER_TYPE);
  1906. goto f_err;
  1907. }
  1908. return(1);
  1909. f_err:
  1910. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1911. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
  1912. err:
  1913. #endif
  1914. #ifndef OPENSSL_NO_ECDH
  1915. EVP_PKEY_free(clnt_pub_pkey);
  1916. EC_POINT_free(clnt_ecpoint);
  1917. if (srvr_ecdh != NULL)
  1918. {
  1919. srvr_ecdh->priv_key = NULL;
  1920. srvr_ecdh->group = NULL;
  1921. EC_KEY_free(srvr_ecdh);
  1922. }
  1923. BN_CTX_free(bn_ctx);
  1924. #endif
  1925. return(-1);
  1926. }
  1927. static int ssl3_get_cert_verify(SSL *s)
  1928. {
  1929. EVP_PKEY *pkey=NULL;
  1930. unsigned char *p;
  1931. int al,ok,ret=0;
  1932. long n;
  1933. int type=0,i,j;
  1934. X509 *peer;
  1935. n=ssl3_get_message(s,
  1936. SSL3_ST_SR_CERT_VRFY_A,
  1937. SSL3_ST_SR_CERT_VRFY_B,
  1938. -1,
  1939. 514, /* 514? */
  1940. &ok);
  1941. if (!ok) return((int)n);
  1942. if (s->session->peer != NULL)
  1943. {
  1944. peer=s->session->peer;
  1945. pkey=X509_get_pubkey(peer);
  1946. type=X509_certificate_type(peer,pkey);
  1947. }
  1948. else
  1949. {
  1950. peer=NULL;
  1951. pkey=NULL;
  1952. }
  1953. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
  1954. {
  1955. s->s3->tmp.reuse_message=1;
  1956. if ((peer != NULL) && (type | EVP_PKT_SIGN))
  1957. {
  1958. al=SSL_AD_UNEXPECTED_MESSAGE;
  1959. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
  1960. goto f_err;
  1961. }
  1962. ret=1;
  1963. goto end;
  1964. }
  1965. if (peer == NULL)
  1966. {
  1967. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
  1968. al=SSL_AD_UNEXPECTED_MESSAGE;
  1969. goto f_err;
  1970. }
  1971. if (!(type & EVP_PKT_SIGN))
  1972. {
  1973. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
  1974. al=SSL_AD_ILLEGAL_PARAMETER;
  1975. goto f_err;
  1976. }
  1977. if (s->s3->change_cipher_spec)
  1978. {
  1979. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
  1980. al=SSL_AD_UNEXPECTED_MESSAGE;
  1981. goto f_err;
  1982. }
  1983. /* we now have a signature that we need to verify */
  1984. p=(unsigned char *)s->init_msg;
  1985. n2s(p,i);
  1986. n-=2;
  1987. if (i > n)
  1988. {
  1989. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
  1990. al=SSL_AD_DECODE_ERROR;
  1991. goto f_err;
  1992. }
  1993. j=EVP_PKEY_size(pkey);
  1994. if ((i > j) || (n > j) || (n <= 0))
  1995. {
  1996. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
  1997. al=SSL_AD_DECODE_ERROR;
  1998. goto f_err;
  1999. }
  2000. #ifndef OPENSSL_NO_RSA
  2001. if (pkey->type == EVP_PKEY_RSA)
  2002. {
  2003. i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
  2004. MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
  2005. pkey->pkey.rsa);
  2006. if (i < 0)
  2007. {
  2008. al=SSL_AD_DECRYPT_ERROR;
  2009. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
  2010. goto f_err;
  2011. }
  2012. if (i == 0)
  2013. {
  2014. al=SSL_AD_DECRYPT_ERROR;
  2015. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
  2016. goto f_err;
  2017. }
  2018. }
  2019. else
  2020. #endif
  2021. #ifndef OPENSSL_NO_DSA
  2022. if (pkey->type == EVP_PKEY_DSA)
  2023. {
  2024. j=DSA_verify(pkey->save_type,
  2025. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2026. SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
  2027. if (j <= 0)
  2028. {
  2029. /* bad signature */
  2030. al=SSL_AD_DECRYPT_ERROR;
  2031. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
  2032. goto f_err;
  2033. }
  2034. }
  2035. else
  2036. #endif
  2037. #ifndef OPENSSL_NO_ECDSA
  2038. if (pkey->type == EVP_PKEY_EC)
  2039. {
  2040. j=ECDSA_verify(pkey->save_type,
  2041. &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
  2042. SHA_DIGEST_LENGTH,p,i,pkey->pkey.eckey);
  2043. if (j <= 0)
  2044. {
  2045. /* bad signature */
  2046. al=SSL_AD_DECRYPT_ERROR;
  2047. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
  2048. SSL_R_BAD_ECDSA_SIGNATURE);
  2049. goto f_err;
  2050. }
  2051. }
  2052. else
  2053. #endif
  2054. {
  2055. SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
  2056. al=SSL_AD_UNSUPPORTED_CERTIFICATE;
  2057. goto f_err;
  2058. }
  2059. ret=1;
  2060. if (0)
  2061. {
  2062. f_err:
  2063. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2064. }
  2065. end:
  2066. EVP_PKEY_free(pkey);
  2067. return(ret);
  2068. }
  2069. static int ssl3_get_client_certificate(SSL *s)
  2070. {
  2071. int i,ok,al,ret= -1;
  2072. X509 *x=NULL;
  2073. unsigned long l,nc,llen,n;
  2074. const unsigned char *p,*q;
  2075. unsigned char *d;
  2076. STACK_OF(X509) *sk=NULL;
  2077. n=ssl3_get_message(s,
  2078. SSL3_ST_SR_CERT_A,
  2079. SSL3_ST_SR_CERT_B,
  2080. -1,
  2081. s->max_cert_list,
  2082. &ok);
  2083. if (!ok) return((int)n);
  2084. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  2085. {
  2086. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  2087. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2088. {
  2089. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2090. al=SSL_AD_HANDSHAKE_FAILURE;
  2091. goto f_err;
  2092. }
  2093. /* If tls asked for a client cert, the client must return a 0 list */
  2094. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  2095. {
  2096. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  2097. al=SSL_AD_UNEXPECTED_MESSAGE;
  2098. goto f_err;
  2099. }
  2100. s->s3->tmp.reuse_message=1;
  2101. return(1);
  2102. }
  2103. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  2104. {
  2105. al=SSL_AD_UNEXPECTED_MESSAGE;
  2106. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
  2107. goto f_err;
  2108. }
  2109. p=d=(unsigned char *)s->init_msg;
  2110. if ((sk=sk_X509_new_null()) == NULL)
  2111. {
  2112. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  2113. goto err;
  2114. }
  2115. n2l3(p,llen);
  2116. if (llen+3 != n)
  2117. {
  2118. al=SSL_AD_DECODE_ERROR;
  2119. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
  2120. goto f_err;
  2121. }
  2122. for (nc=0; nc<llen; )
  2123. {
  2124. n2l3(p,l);
  2125. if ((l+nc+3) > llen)
  2126. {
  2127. al=SSL_AD_DECODE_ERROR;
  2128. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  2129. goto f_err;
  2130. }
  2131. q=p;
  2132. x=d2i_X509(NULL,&p,l);
  2133. if (x == NULL)
  2134. {
  2135. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
  2136. goto err;
  2137. }
  2138. if (p != (q+l))
  2139. {
  2140. al=SSL_AD_DECODE_ERROR;
  2141. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
  2142. goto f_err;
  2143. }
  2144. if (!sk_X509_push(sk,x))
  2145. {
  2146. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
  2147. goto err;
  2148. }
  2149. x=NULL;
  2150. nc+=l+3;
  2151. }
  2152. if (sk_X509_num(sk) <= 0)
  2153. {
  2154. /* TLS does not mind 0 certs returned */
  2155. if (s->version == SSL3_VERSION)
  2156. {
  2157. al=SSL_AD_HANDSHAKE_FAILURE;
  2158. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
  2159. goto f_err;
  2160. }
  2161. /* Fail for TLS only if we required a certificate */
  2162. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2163. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2164. {
  2165. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2166. al=SSL_AD_HANDSHAKE_FAILURE;
  2167. goto f_err;
  2168. }
  2169. }
  2170. else
  2171. {
  2172. i=ssl_verify_cert_chain(s,sk);
  2173. if (!i)
  2174. {
  2175. al=ssl_verify_alarm_type(s->verify_result);
  2176. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
  2177. goto f_err;
  2178. }
  2179. }
  2180. if (s->session->peer != NULL) /* This should not be needed */
  2181. X509_free(s->session->peer);
  2182. s->session->peer=sk_X509_shift(sk);
  2183. s->session->verify_result = s->verify_result;
  2184. /* With the current implementation, sess_cert will always be NULL
  2185. * when we arrive here. */
  2186. if (s->session->sess_cert == NULL)
  2187. {
  2188. s->session->sess_cert = ssl_sess_cert_new();
  2189. if (s->session->sess_cert == NULL)
  2190. {
  2191. SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
  2192. goto err;
  2193. }
  2194. }
  2195. if (s->session->sess_cert->cert_chain != NULL)
  2196. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  2197. s->session->sess_cert->cert_chain=sk;
  2198. /* Inconsistency alert: cert_chain does *not* include the
  2199. * peer's own certificate, while we do include it in s3_clnt.c */
  2200. sk=NULL;
  2201. ret=1;
  2202. if (0)
  2203. {
  2204. f_err:
  2205. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2206. }
  2207. err:
  2208. if (x != NULL) X509_free(x);
  2209. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  2210. return(ret);
  2211. }
  2212. int ssl3_send_server_certificate(SSL *s)
  2213. {
  2214. unsigned long l;
  2215. X509 *x;
  2216. if (s->state == SSL3_ST_SW_CERT_A)
  2217. {
  2218. x=ssl_get_server_send_cert(s);
  2219. if (x == NULL &&
  2220. /* VRS: allow null cert if auth == KRB5 */
  2221. (s->s3->tmp.new_cipher->algorithms
  2222. & (SSL_MKEY_MASK|SSL_AUTH_MASK))
  2223. != (SSL_aKRB5|SSL_kKRB5))
  2224. {
  2225. SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
  2226. return(0);
  2227. }
  2228. l=ssl3_output_cert_chain(s,x);
  2229. s->state=SSL3_ST_SW_CERT_B;
  2230. s->init_num=(int)l;
  2231. s->init_off=0;
  2232. }
  2233. /* SSL3_ST_SW_CERT_B */
  2234. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2235. }
  2236. #ifndef OPENSSL_NO_ECDH
  2237. /* This is the complement of curve_id2nid in s3_clnt.c. */
  2238. static int nid2curve_id(int nid)
  2239. {
  2240. /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
  2241. * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
  2242. switch (nid) {
  2243. case NID_sect163k1: /* sect163k1 (1) */
  2244. return 1;
  2245. case NID_sect163r1: /* sect163r1 (2) */
  2246. return 2;
  2247. case NID_sect163r2: /* sect163r2 (3) */
  2248. return 3;
  2249. case NID_sect193r1: /* sect193r1 (4) */
  2250. return 4;
  2251. case NID_sect193r2: /* sect193r2 (5) */
  2252. return 5;
  2253. case NID_sect233k1: /* sect233k1 (6) */
  2254. return 6;
  2255. case NID_sect233r1: /* sect233r1 (7) */
  2256. return 7;
  2257. case NID_sect239k1: /* sect239k1 (8) */
  2258. return 8;
  2259. case NID_sect283k1: /* sect283k1 (9) */
  2260. return 9;
  2261. case NID_sect283r1: /* sect283r1 (10) */
  2262. return 10;
  2263. case NID_sect409k1: /* sect409k1 (11) */
  2264. return 11;
  2265. case NID_sect409r1: /* sect409r1 (12) */
  2266. return 12;
  2267. case NID_sect571k1: /* sect571k1 (13) */
  2268. return 13;
  2269. case NID_sect571r1: /* sect571r1 (14) */
  2270. return 14;
  2271. case NID_secp160k1: /* secp160k1 (15) */
  2272. return 15;
  2273. case NID_secp160r1: /* secp160r1 (16) */
  2274. return 16;
  2275. case NID_secp160r2: /* secp160r2 (17) */
  2276. return 17;
  2277. case NID_secp192k1: /* secp192k1 (18) */
  2278. return 18;
  2279. case NID_X9_62_prime192v1: /* secp192r1 (19) */
  2280. return 19;
  2281. case NID_secp224k1: /* secp224k1 (20) */
  2282. return 20;
  2283. case NID_secp224r1: /* secp224r1 (21) */
  2284. return 21;
  2285. case NID_secp256k1: /* secp256k1 (22) */
  2286. return 22;
  2287. case NID_X9_62_prime256v1: /* secp256r1 (23) */
  2288. return 23;
  2289. case NID_secp384r1: /* secp384r1 (24) */
  2290. return 24;
  2291. case NID_secp521r1: /* secp521r1 (25) */
  2292. return 25;
  2293. default:
  2294. return 0;
  2295. }
  2296. }
  2297. #endif