t1_enc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /* ssl/t1_enc.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-2007 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 2005 Nokia. All rights reserved.
  113. *
  114. * The portions of the attached software ("Contribution") is developed by
  115. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  116. * license.
  117. *
  118. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  119. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  120. * support (see RFC 4279) to OpenSSL.
  121. *
  122. * No patent licenses or other rights except those expressly stated in
  123. * the OpenSSL open source license shall be deemed granted or received
  124. * expressly, by implication, estoppel, or otherwise.
  125. *
  126. * No assurances are provided by Nokia that the Contribution does not
  127. * infringe the patent or other intellectual property rights of any third
  128. * party or that the license provides you with all the necessary rights
  129. * to make use of the Contribution.
  130. *
  131. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  132. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  133. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  134. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  135. * OTHERWISE.
  136. */
  137. #include <stdio.h>
  138. #include "ssl_locl.h"
  139. #ifndef OPENSSL_NO_COMP
  140. #include <openssl/comp.h>
  141. #endif
  142. #include <openssl/evp.h>
  143. #include <openssl/hmac.h>
  144. #include <openssl/md5.h>
  145. #include <openssl/rand.h>
  146. #ifdef KSSL_DEBUG
  147. #include <openssl/des.h>
  148. #endif
  149. /* seed1 through seed5 are virtually concatenated */
  150. static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
  151. int sec_len,
  152. const void *seed1, int seed1_len,
  153. const void *seed2, int seed2_len,
  154. const void *seed3, int seed3_len,
  155. const void *seed4, int seed4_len,
  156. const void *seed5, int seed5_len,
  157. unsigned char *out, int olen)
  158. {
  159. int chunk,n;
  160. unsigned int j;
  161. HMAC_CTX ctx;
  162. HMAC_CTX ctx_tmp;
  163. unsigned char A1[EVP_MAX_MD_SIZE];
  164. unsigned int A1_len;
  165. chunk=EVP_MD_size(md);
  166. OPENSSL_assert(chunk >= 0);
  167. HMAC_CTX_init(&ctx);
  168. HMAC_CTX_init(&ctx_tmp);
  169. HMAC_Init_ex(&ctx,sec,sec_len,md, NULL);
  170. HMAC_Init_ex(&ctx_tmp,sec,sec_len,md, NULL);
  171. if (seed1 != NULL) HMAC_Update(&ctx,seed1,seed1_len);
  172. if (seed2 != NULL) HMAC_Update(&ctx,seed2,seed2_len);
  173. if (seed3 != NULL) HMAC_Update(&ctx,seed3,seed3_len);
  174. if (seed4 != NULL) HMAC_Update(&ctx,seed4,seed4_len);
  175. if (seed5 != NULL) HMAC_Update(&ctx,seed5,seed5_len);
  176. HMAC_Final(&ctx,A1,&A1_len);
  177. n=0;
  178. for (;;)
  179. {
  180. HMAC_Init_ex(&ctx,NULL,0,NULL,NULL); /* re-init */
  181. HMAC_Init_ex(&ctx_tmp,NULL,0,NULL,NULL); /* re-init */
  182. HMAC_Update(&ctx,A1,A1_len);
  183. HMAC_Update(&ctx_tmp,A1,A1_len);
  184. if (seed1 != NULL) HMAC_Update(&ctx,seed1,seed1_len);
  185. if (seed2 != NULL) HMAC_Update(&ctx,seed2,seed2_len);
  186. if (seed3 != NULL) HMAC_Update(&ctx,seed3,seed3_len);
  187. if (seed4 != NULL) HMAC_Update(&ctx,seed4,seed4_len);
  188. if (seed5 != NULL) HMAC_Update(&ctx,seed5,seed5_len);
  189. if (olen > chunk)
  190. {
  191. HMAC_Final(&ctx,out,&j);
  192. out+=j;
  193. olen-=j;
  194. HMAC_Final(&ctx_tmp,A1,&A1_len); /* calc the next A1 value */
  195. }
  196. else /* last one */
  197. {
  198. HMAC_Final(&ctx,A1,&A1_len);
  199. memcpy(out,A1,olen);
  200. break;
  201. }
  202. }
  203. HMAC_CTX_cleanup(&ctx);
  204. HMAC_CTX_cleanup(&ctx_tmp);
  205. OPENSSL_cleanse(A1,sizeof(A1));
  206. }
  207. /* seed1 through seed5 are virtually concatenated */
  208. static void tls1_PRF(long digest_mask,
  209. const void *seed1, int seed1_len,
  210. const void *seed2, int seed2_len,
  211. const void *seed3, int seed3_len,
  212. const void *seed4, int seed4_len,
  213. const void *seed5, int seed5_len,
  214. const unsigned char *sec, int slen,
  215. unsigned char *out1,
  216. unsigned char *out2, int olen)
  217. {
  218. int len,i,idx,count;
  219. const unsigned char *S1;
  220. long m;
  221. const EVP_MD *md;
  222. /* Count number of digests and partition sec evenly */
  223. count=0;
  224. for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) {
  225. if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) count++;
  226. }
  227. len=slen/count;
  228. S1=sec;
  229. memset(out1,0,olen);
  230. for (idx=0;ssl_get_handshake_digest(idx,&m,&md);idx++) {
  231. if ((m<<TLS1_PRF_DGST_SHIFT) & digest_mask) {
  232. if (!md) {
  233. SSLerr(SSL_F_TLS1_PRF,
  234. SSL_R_UNSUPPORTED_DIGEST_TYPE);
  235. return;
  236. }
  237. tls1_P_hash(md ,S1,len+(slen&1),
  238. seed1,seed1_len,seed2,seed2_len,seed3,seed3_len,seed4,seed4_len,seed5,seed5_len,
  239. out2,olen);
  240. S1+=len;
  241. for (i=0; i<olen; i++)
  242. {
  243. out1[i]^=out2[i];
  244. }
  245. }
  246. }
  247. }
  248. static void tls1_generate_key_block(SSL *s, unsigned char *km,
  249. unsigned char *tmp, int num)
  250. {
  251. tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
  252. TLS_MD_KEY_EXPANSION_CONST,TLS_MD_KEY_EXPANSION_CONST_SIZE,
  253. s->s3->server_random,SSL3_RANDOM_SIZE,
  254. s->s3->client_random,SSL3_RANDOM_SIZE,
  255. NULL,0,NULL,0,
  256. s->session->master_key,s->session->master_key_length,
  257. km,tmp,num);
  258. #ifdef KSSL_DEBUG
  259. printf("tls1_generate_key_block() ==> %d byte master_key =\n\t",
  260. s->session->master_key_length);
  261. {
  262. int i;
  263. for (i=0; i < s->session->master_key_length; i++)
  264. {
  265. printf("%02X", s->session->master_key[i]);
  266. }
  267. printf("\n"); }
  268. #endif /* KSSL_DEBUG */
  269. }
  270. int tls1_change_cipher_state(SSL *s, int which)
  271. {
  272. static const unsigned char empty[]="";
  273. unsigned char *p,*key_block,*mac_secret;
  274. unsigned char *exp_label;
  275. unsigned char tmp1[EVP_MAX_KEY_LENGTH];
  276. unsigned char tmp2[EVP_MAX_KEY_LENGTH];
  277. unsigned char iv1[EVP_MAX_IV_LENGTH*2];
  278. unsigned char iv2[EVP_MAX_IV_LENGTH*2];
  279. unsigned char *ms,*key,*iv,*er1,*er2;
  280. int client_write;
  281. EVP_CIPHER_CTX *dd;
  282. const EVP_CIPHER *c;
  283. #ifndef OPENSSL_NO_COMP
  284. const SSL_COMP *comp;
  285. #endif
  286. const EVP_MD *m;
  287. int mac_type;
  288. int *mac_secret_size;
  289. EVP_MD_CTX *mac_ctx;
  290. EVP_PKEY *mac_key;
  291. int is_export,n,i,j,k,exp_label_len,cl;
  292. int reuse_dd = 0;
  293. is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
  294. c=s->s3->tmp.new_sym_enc;
  295. m=s->s3->tmp.new_hash;
  296. mac_type = s->s3->tmp.new_mac_pkey_type;
  297. #ifndef OPENSSL_NO_COMP
  298. comp=s->s3->tmp.new_compression;
  299. #endif
  300. key_block=s->s3->tmp.key_block;
  301. #ifdef KSSL_DEBUG
  302. printf("tls1_change_cipher_state(which= %d) w/\n", which);
  303. printf("\talg= %ld/%ld, comp= %p\n",
  304. s->s3->tmp.new_cipher->algorithm_mkey,
  305. s->s3->tmp.new_cipher->algorithm_auth,
  306. comp);
  307. printf("\tevp_cipher == %p ==? &d_cbc_ede_cipher3\n", c);
  308. printf("\tevp_cipher: nid, blksz= %d, %d, keylen=%d, ivlen=%d\n",
  309. c->nid,c->block_size,c->key_len,c->iv_len);
  310. printf("\tkey_block: len= %d, data= ", s->s3->tmp.key_block_length);
  311. {
  312. int i;
  313. for (i=0; i<s->s3->tmp.key_block_length; i++)
  314. printf("%02x", key_block[i]); printf("\n");
  315. }
  316. #endif /* KSSL_DEBUG */
  317. if (which & SSL3_CC_READ)
  318. {
  319. if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  320. s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
  321. else
  322. s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
  323. if (s->enc_read_ctx != NULL)
  324. reuse_dd = 1;
  325. else if ((s->enc_read_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
  326. goto err;
  327. else
  328. /* make sure it's intialized in case we exit later with an error */
  329. EVP_CIPHER_CTX_init(s->enc_read_ctx);
  330. dd= s->enc_read_ctx;
  331. mac_ctx=ssl_replace_hash(&s->read_hash,NULL);
  332. #ifndef OPENSSL_NO_COMP
  333. if (s->expand != NULL)
  334. {
  335. COMP_CTX_free(s->expand);
  336. s->expand=NULL;
  337. }
  338. if (comp != NULL)
  339. {
  340. s->expand=COMP_CTX_new(comp->method);
  341. if (s->expand == NULL)
  342. {
  343. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
  344. goto err2;
  345. }
  346. if (s->s3->rrec.comp == NULL)
  347. s->s3->rrec.comp=(unsigned char *)
  348. OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
  349. if (s->s3->rrec.comp == NULL)
  350. goto err;
  351. }
  352. #endif
  353. /* this is done by dtls1_reset_seq_numbers for DTLS1_VERSION */
  354. if (s->version != DTLS1_VERSION)
  355. memset(&(s->s3->read_sequence[0]),0,8);
  356. mac_secret= &(s->s3->read_mac_secret[0]);
  357. mac_secret_size=&(s->s3->read_mac_secret_size);
  358. }
  359. else
  360. {
  361. if (s->s3->tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
  362. s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
  363. else
  364. s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
  365. if (s->enc_write_ctx != NULL)
  366. reuse_dd = 1;
  367. else if ((s->enc_write_ctx=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
  368. goto err;
  369. else
  370. /* make sure it's intialized in case we exit later with an error */
  371. EVP_CIPHER_CTX_init(s->enc_write_ctx);
  372. dd= s->enc_write_ctx;
  373. mac_ctx = ssl_replace_hash(&s->write_hash,NULL);
  374. #ifndef OPENSSL_NO_COMP
  375. if (s->compress != NULL)
  376. {
  377. COMP_CTX_free(s->compress);
  378. s->compress=NULL;
  379. }
  380. if (comp != NULL)
  381. {
  382. s->compress=COMP_CTX_new(comp->method);
  383. if (s->compress == NULL)
  384. {
  385. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
  386. goto err2;
  387. }
  388. }
  389. #endif
  390. /* this is done by dtls1_reset_seq_numbers for DTLS1_VERSION */
  391. if (s->version != DTLS1_VERSION)
  392. memset(&(s->s3->write_sequence[0]),0,8);
  393. mac_secret= &(s->s3->write_mac_secret[0]);
  394. mac_secret_size = &(s->s3->write_mac_secret_size);
  395. }
  396. if (reuse_dd)
  397. EVP_CIPHER_CTX_cleanup(dd);
  398. p=s->s3->tmp.key_block;
  399. i=*mac_secret_size=s->s3->tmp.new_mac_secret_size;
  400. cl=EVP_CIPHER_key_length(c);
  401. j=is_export ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
  402. cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
  403. /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
  404. k=EVP_CIPHER_iv_length(c);
  405. er1= &(s->s3->client_random[0]);
  406. er2= &(s->s3->server_random[0]);
  407. if ( (which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
  408. (which == SSL3_CHANGE_CIPHER_SERVER_READ))
  409. {
  410. ms= &(p[ 0]); n=i+i;
  411. key= &(p[ n]); n+=j+j;
  412. iv= &(p[ n]); n+=k+k;
  413. exp_label=(unsigned char *)TLS_MD_CLIENT_WRITE_KEY_CONST;
  414. exp_label_len=TLS_MD_CLIENT_WRITE_KEY_CONST_SIZE;
  415. client_write=1;
  416. }
  417. else
  418. {
  419. n=i;
  420. ms= &(p[ n]); n+=i+j;
  421. key= &(p[ n]); n+=j+k;
  422. iv= &(p[ n]); n+=k;
  423. exp_label=(unsigned char *)TLS_MD_SERVER_WRITE_KEY_CONST;
  424. exp_label_len=TLS_MD_SERVER_WRITE_KEY_CONST_SIZE;
  425. client_write=0;
  426. }
  427. if (n > s->s3->tmp.key_block_length)
  428. {
  429. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_INTERNAL_ERROR);
  430. goto err2;
  431. }
  432. memcpy(mac_secret,ms,i);
  433. mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,
  434. mac_secret,*mac_secret_size);
  435. EVP_DigestSignInit(mac_ctx,NULL,m,NULL,mac_key);
  436. EVP_PKEY_free(mac_key);
  437. #ifdef TLS_DEBUG
  438. printf("which = %04X\nmac key=",which);
  439. { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
  440. #endif
  441. if (is_export)
  442. {
  443. /* In here I set both the read and write key/iv to the
  444. * same value since only the correct one will be used :-).
  445. */
  446. tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
  447. exp_label,exp_label_len,
  448. s->s3->client_random,SSL3_RANDOM_SIZE,
  449. s->s3->server_random,SSL3_RANDOM_SIZE,
  450. NULL,0,NULL,0,
  451. key,j,tmp1,tmp2,EVP_CIPHER_key_length(c));
  452. key=tmp1;
  453. if (k > 0)
  454. {
  455. tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
  456. TLS_MD_IV_BLOCK_CONST,TLS_MD_IV_BLOCK_CONST_SIZE,
  457. s->s3->client_random,SSL3_RANDOM_SIZE,
  458. s->s3->server_random,SSL3_RANDOM_SIZE,
  459. NULL,0,NULL,0,
  460. empty,0,iv1,iv2,k*2);
  461. if (client_write)
  462. iv=iv1;
  463. else
  464. iv= &(iv1[k]);
  465. }
  466. }
  467. s->session->key_arg_length=0;
  468. #ifdef KSSL_DEBUG
  469. {
  470. int i;
  471. printf("EVP_CipherInit_ex(dd,c,key=,iv=,which)\n");
  472. printf("\tkey= "); for (i=0; i<c->key_len; i++) printf("%02x", key[i]);
  473. printf("\n");
  474. printf("\t iv= "); for (i=0; i<c->iv_len; i++) printf("%02x", iv[i]);
  475. printf("\n");
  476. }
  477. #endif /* KSSL_DEBUG */
  478. EVP_CipherInit_ex(dd,c,NULL,key,iv,(which & SSL3_CC_WRITE));
  479. #ifdef TLS_DEBUG
  480. printf("which = %04X\nkey=",which);
  481. { int z; for (z=0; z<EVP_CIPHER_key_length(c); z++) printf("%02X%c",key[z],((z+1)%16)?' ':'\n'); }
  482. printf("\niv=");
  483. { int z; for (z=0; z<k; z++) printf("%02X%c",iv[z],((z+1)%16)?' ':'\n'); }
  484. printf("\n");
  485. #endif
  486. OPENSSL_cleanse(tmp1,sizeof(tmp1));
  487. OPENSSL_cleanse(tmp2,sizeof(tmp1));
  488. OPENSSL_cleanse(iv1,sizeof(iv1));
  489. OPENSSL_cleanse(iv2,sizeof(iv2));
  490. return(1);
  491. err:
  492. SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,ERR_R_MALLOC_FAILURE);
  493. err2:
  494. return(0);
  495. }
  496. int tls1_setup_key_block(SSL *s)
  497. {
  498. unsigned char *p1,*p2;
  499. const EVP_CIPHER *c;
  500. const EVP_MD *hash;
  501. int num;
  502. SSL_COMP *comp;
  503. int mac_type= NID_undef,mac_secret_size=0;
  504. #ifdef KSSL_DEBUG
  505. printf ("tls1_setup_key_block()\n");
  506. #endif /* KSSL_DEBUG */
  507. if (s->s3->tmp.key_block_length != 0)
  508. return(1);
  509. if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secret_size,&comp))
  510. {
  511. SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
  512. return(0);
  513. }
  514. s->s3->tmp.new_sym_enc=c;
  515. s->s3->tmp.new_hash=hash;
  516. s->s3->tmp.new_mac_pkey_type = mac_type;
  517. s->s3->tmp.new_mac_secret_size = mac_secret_size;
  518. num=EVP_CIPHER_key_length(c)+mac_secret_size+EVP_CIPHER_iv_length(c);
  519. num*=2;
  520. ssl3_cleanup_key_block(s);
  521. if ((p1=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  522. goto err;
  523. if ((p2=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  524. goto err;
  525. s->s3->tmp.key_block_length=num;
  526. s->s3->tmp.key_block=p1;
  527. #ifdef TLS_DEBUG
  528. printf("client random\n");
  529. { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->client_random[z],((z+1)%16)?' ':'\n'); }
  530. printf("server random\n");
  531. { int z; for (z=0; z<SSL3_RANDOM_SIZE; z++) printf("%02X%c",s->s3->server_random[z],((z+1)%16)?' ':'\n'); }
  532. printf("pre-master\n");
  533. { int z; for (z=0; z<s->session->master_key_length; z++) printf("%02X%c",s->session->master_key[z],((z+1)%16)?' ':'\n'); }
  534. #endif
  535. tls1_generate_key_block(s,p1,p2,num);
  536. OPENSSL_cleanse(p2,num);
  537. OPENSSL_free(p2);
  538. #ifdef TLS_DEBUG
  539. printf("\nkey block\n");
  540. { int z; for (z=0; z<num; z++) printf("%02X%c",p1[z],((z+1)%16)?' ':'\n'); }
  541. #endif
  542. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  543. {
  544. /* enable vulnerability countermeasure for CBC ciphers with
  545. * known-IV problem (http://www.openssl.org/~bodo/tls-cbc.txt)
  546. */
  547. s->s3->need_empty_fragments = 1;
  548. if (s->session->cipher != NULL)
  549. {
  550. if (s->session->cipher->algorithm_enc == SSL_eNULL)
  551. s->s3->need_empty_fragments = 0;
  552. #ifndef OPENSSL_NO_RC4
  553. if (s->session->cipher->algorithm_enc == SSL_RC4)
  554. s->s3->need_empty_fragments = 0;
  555. #endif
  556. }
  557. }
  558. return(1);
  559. err:
  560. SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,ERR_R_MALLOC_FAILURE);
  561. return(0);
  562. }
  563. int tls1_enc(SSL *s, int send)
  564. {
  565. SSL3_RECORD *rec;
  566. EVP_CIPHER_CTX *ds;
  567. unsigned long l;
  568. int bs,i,ii,j,k,n=0;
  569. const EVP_CIPHER *enc;
  570. if (send)
  571. {
  572. if (EVP_MD_CTX_md(s->write_hash))
  573. {
  574. n=EVP_MD_CTX_size(s->write_hash);
  575. OPENSSL_assert(n >= 0);
  576. }
  577. ds=s->enc_write_ctx;
  578. rec= &(s->s3->wrec);
  579. if (s->enc_write_ctx == NULL)
  580. enc=NULL;
  581. else
  582. {
  583. int ivlen;
  584. enc=EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
  585. /* For TLSv1.1 and later explicit IV */
  586. if (s->version >= TLS1_1_VERSION)
  587. ivlen = EVP_CIPHER_iv_length(enc);
  588. else
  589. ivlen = 0;
  590. if (ivlen > 1)
  591. {
  592. if ( rec->data != rec->input)
  593. /* we can't write into the input stream:
  594. * Can this ever happen?? (steve)
  595. */
  596. fprintf(stderr,
  597. "%s:%d: rec->data != rec->input\n",
  598. __FILE__, __LINE__);
  599. else if (RAND_bytes(rec->input, ivlen) <= 0)
  600. return -1;
  601. }
  602. }
  603. }
  604. else
  605. {
  606. if (EVP_MD_CTX_md(s->read_hash))
  607. {
  608. n=EVP_MD_CTX_size(s->read_hash);
  609. OPENSSL_assert(n >= 0);
  610. }
  611. ds=s->enc_read_ctx;
  612. rec= &(s->s3->rrec);
  613. if (s->enc_read_ctx == NULL)
  614. enc=NULL;
  615. else
  616. enc=EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
  617. }
  618. #ifdef KSSL_DEBUG
  619. printf("tls1_enc(%d)\n", send);
  620. #endif /* KSSL_DEBUG */
  621. if ((s->session == NULL) || (ds == NULL) ||
  622. (enc == NULL))
  623. {
  624. memmove(rec->data,rec->input,rec->length);
  625. rec->input=rec->data;
  626. }
  627. else
  628. {
  629. l=rec->length;
  630. bs=EVP_CIPHER_block_size(ds->cipher);
  631. if ((bs != 1) && send)
  632. {
  633. i=bs-((int)l%bs);
  634. /* Add weird padding of upto 256 bytes */
  635. /* we need to add 'i' padding bytes of value j */
  636. j=i-1;
  637. if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG)
  638. {
  639. if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
  640. j++;
  641. }
  642. for (k=(int)l; k<(int)(l+i); k++)
  643. rec->input[k]=j;
  644. l+=i;
  645. rec->length+=i;
  646. }
  647. #ifdef KSSL_DEBUG
  648. {
  649. unsigned long ui;
  650. printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
  651. ds,rec->data,rec->input,l);
  652. printf("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n",
  653. ds->buf_len, ds->cipher->key_len,
  654. DES_KEY_SZ, DES_SCHEDULE_SZ,
  655. ds->cipher->iv_len);
  656. printf("\t\tIV: ");
  657. for (i=0; i<ds->cipher->iv_len; i++) printf("%02X", ds->iv[i]);
  658. printf("\n");
  659. printf("\trec->input=");
  660. for (ui=0; ui<l; ui++) printf(" %02x", rec->input[ui]);
  661. printf("\n");
  662. }
  663. #endif /* KSSL_DEBUG */
  664. if (!send)
  665. {
  666. if (l == 0 || l%bs != 0)
  667. {
  668. SSLerr(SSL_F_TLS1_ENC,SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
  669. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECRYPTION_FAILED);
  670. return 0;
  671. }
  672. }
  673. EVP_Cipher(ds,rec->data,rec->input,l);
  674. #ifdef KSSL_DEBUG
  675. {
  676. unsigned long i;
  677. printf("\trec->data=");
  678. for (i=0; i<l; i++)
  679. printf(" %02x", rec->data[i]); printf("\n");
  680. }
  681. #endif /* KSSL_DEBUG */
  682. if ((bs != 1) && !send)
  683. {
  684. ii=i=rec->data[l-1]; /* padding_length */
  685. i++;
  686. /* NB: if compression is in operation the first packet
  687. * may not be of even length so the padding bug check
  688. * cannot be performed. This bug workaround has been
  689. * around since SSLeay so hopefully it is either fixed
  690. * now or no buggy implementation supports compression
  691. * [steve]
  692. */
  693. if ( (s->options&SSL_OP_TLS_BLOCK_PADDING_BUG)
  694. && !s->expand)
  695. {
  696. /* First packet is even in size, so check */
  697. if ((memcmp(s->s3->read_sequence,
  698. "\0\0\0\0\0\0\0\0",8) == 0) && !(ii & 1))
  699. s->s3->flags|=TLS1_FLAGS_TLS_PADDING_BUG;
  700. if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
  701. i--;
  702. }
  703. /* TLS 1.0 does not bound the number of padding bytes by the block size.
  704. * All of them must have value 'padding_length'. */
  705. if (i > (int)rec->length)
  706. {
  707. /* Incorrect padding. SSLerr() and ssl3_alert are done
  708. * by caller: we don't want to reveal whether this is
  709. * a decryption error or a MAC verification failure
  710. * (see http://www.openssl.org/~bodo/tls-cbc.txt) */
  711. return -1;
  712. }
  713. for (j=(int)(l-i); j<(int)l; j++)
  714. {
  715. if (rec->data[j] != ii)
  716. {
  717. /* Incorrect padding */
  718. return -1;
  719. }
  720. }
  721. rec->length -=i;
  722. if (s->version >= TLS1_1_VERSION)
  723. {
  724. rec->data += bs; /* skip the explicit IV */
  725. rec->input += bs;
  726. rec->length -= bs;
  727. }
  728. }
  729. }
  730. return(1);
  731. }
  732. int tls1_cert_verify_mac(SSL *s, int md_nid, unsigned char *out)
  733. {
  734. unsigned int ret;
  735. EVP_MD_CTX ctx, *d=NULL;
  736. int i;
  737. if (s->s3->handshake_buffer)
  738. if (!ssl3_digest_cached_records(s))
  739. return 0;
  740. for (i=0;i<SSL_MAX_DIGEST;i++)
  741. {
  742. if (s->s3->handshake_dgst[i]&&EVP_MD_CTX_type(s->s3->handshake_dgst[i])==md_nid)
  743. {
  744. d=s->s3->handshake_dgst[i];
  745. break;
  746. }
  747. }
  748. if (!d) {
  749. SSLerr(SSL_F_TLS1_CERT_VERIFY_MAC,SSL_R_NO_REQUIRED_DIGEST);
  750. return 0;
  751. }
  752. EVP_MD_CTX_init(&ctx);
  753. EVP_MD_CTX_copy_ex(&ctx,d);
  754. EVP_DigestFinal_ex(&ctx,out,&ret);
  755. EVP_MD_CTX_cleanup(&ctx);
  756. return((int)ret);
  757. }
  758. int tls1_final_finish_mac(SSL *s,
  759. const char *str, int slen, unsigned char *out)
  760. {
  761. unsigned int i;
  762. EVP_MD_CTX ctx;
  763. unsigned char buf[2*EVP_MAX_MD_SIZE];
  764. unsigned char *q,buf2[12];
  765. int idx;
  766. long mask;
  767. int err=0;
  768. const EVP_MD *md;
  769. q=buf;
  770. if (s->s3->handshake_buffer)
  771. if (!ssl3_digest_cached_records(s))
  772. return 0;
  773. EVP_MD_CTX_init(&ctx);
  774. for (idx=0;ssl_get_handshake_digest(idx,&mask,&md);idx++)
  775. {
  776. if (mask & s->s3->tmp.new_cipher->algorithm2)
  777. {
  778. int hashsize = EVP_MD_size(md);
  779. if (hashsize < 0 || hashsize > (int)(sizeof buf - (size_t)(q-buf)))
  780. {
  781. /* internal error: 'buf' is too small for this cipersuite! */
  782. err = 1;
  783. }
  784. else
  785. {
  786. EVP_MD_CTX_copy_ex(&ctx,s->s3->handshake_dgst[idx]);
  787. EVP_DigestFinal_ex(&ctx,q,&i);
  788. if (i != (unsigned int)hashsize) /* can't really happen */
  789. err = 1;
  790. q+=i;
  791. }
  792. }
  793. }
  794. tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
  795. str,slen, buf,(int)(q-buf), NULL,0, NULL,0, NULL,0,
  796. s->session->master_key,s->session->master_key_length,
  797. out,buf2,sizeof buf2);
  798. EVP_MD_CTX_cleanup(&ctx);
  799. if (err)
  800. return 0;
  801. else
  802. return sizeof buf2;
  803. }
  804. int tls1_mac(SSL *ssl, unsigned char *md, int send)
  805. {
  806. SSL3_RECORD *rec;
  807. unsigned char *mac_sec,*seq;
  808. EVP_MD_CTX *hash;
  809. size_t md_size;
  810. int i;
  811. EVP_MD_CTX hmac, *mac_ctx;
  812. unsigned char buf[5];
  813. int stream_mac = (send?(ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM):(ssl->mac_flags&SSL_MAC_FLAG_READ_MAC_STREAM));
  814. int t;
  815. if (send)
  816. {
  817. rec= &(ssl->s3->wrec);
  818. mac_sec= &(ssl->s3->write_mac_secret[0]);
  819. seq= &(ssl->s3->write_sequence[0]);
  820. hash=ssl->write_hash;
  821. }
  822. else
  823. {
  824. rec= &(ssl->s3->rrec);
  825. mac_sec= &(ssl->s3->read_mac_secret[0]);
  826. seq= &(ssl->s3->read_sequence[0]);
  827. hash=ssl->read_hash;
  828. }
  829. t=EVP_MD_CTX_size(hash);
  830. OPENSSL_assert(t >= 0);
  831. md_size=t;
  832. buf[0]=rec->type;
  833. buf[1]=(unsigned char)(ssl->version>>8);
  834. buf[2]=(unsigned char)(ssl->version);
  835. buf[3]=rec->length>>8;
  836. buf[4]=rec->length&0xff;
  837. /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
  838. if (stream_mac)
  839. {
  840. mac_ctx = hash;
  841. }
  842. else
  843. {
  844. EVP_MD_CTX_copy(&hmac,hash);
  845. mac_ctx = &hmac;
  846. }
  847. if (ssl->version == DTLS1_VERSION || ssl->version == DTLS1_BAD_VER)
  848. {
  849. unsigned char dtlsseq[8],*p=dtlsseq;
  850. s2n(send?ssl->d1->w_epoch:ssl->d1->r_epoch, p);
  851. memcpy (p,&seq[2],6);
  852. EVP_DigestSignUpdate(mac_ctx,dtlsseq,8);
  853. }
  854. else
  855. EVP_DigestSignUpdate(mac_ctx,seq,8);
  856. EVP_DigestSignUpdate(mac_ctx,buf,5);
  857. EVP_DigestSignUpdate(mac_ctx,rec->input,rec->length);
  858. t=EVP_DigestSignFinal(mac_ctx,md,&md_size);
  859. OPENSSL_assert(t > 0);
  860. if (!stream_mac) EVP_MD_CTX_cleanup(&hmac);
  861. #ifdef TLS_DEBUG
  862. printf("sec=");
  863. {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",mac_sec[z]); printf("\n"); }
  864. printf("seq=");
  865. {int z; for (z=0; z<8; z++) printf("%02X ",seq[z]); printf("\n"); }
  866. printf("buf=");
  867. {int z; for (z=0; z<5; z++) printf("%02X ",buf[z]); printf("\n"); }
  868. printf("rec=");
  869. {unsigned int z; for (z=0; z<rec->length; z++) printf("%02X ",buf[z]); printf("\n"); }
  870. #endif
  871. if (ssl->version != DTLS1_VERSION && ssl->version != DTLS1_BAD_VER)
  872. {
  873. for (i=7; i>=0; i--)
  874. {
  875. ++seq[i];
  876. if (seq[i] != 0) break;
  877. }
  878. }
  879. #ifdef TLS_DEBUG
  880. {unsigned int z; for (z=0; z<md_size; z++) printf("%02X ",md[z]); printf("\n"); }
  881. #endif
  882. return(md_size);
  883. }
  884. int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
  885. int len)
  886. {
  887. unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
  888. const void *co = NULL, *so = NULL;
  889. int col = 0, sol = 0;
  890. #ifdef KSSL_DEBUG
  891. printf ("tls1_generate_master_secret(%p,%p, %p, %d)\n", s,out, p,len);
  892. #endif /* KSSL_DEBUG */
  893. #ifdef TLSEXT_TYPE_opaque_prf_input
  894. if (s->s3->client_opaque_prf_input != NULL && s->s3->server_opaque_prf_input != NULL &&
  895. s->s3->client_opaque_prf_input_len > 0 &&
  896. s->s3->client_opaque_prf_input_len == s->s3->server_opaque_prf_input_len)
  897. {
  898. co = s->s3->client_opaque_prf_input;
  899. col = s->s3->server_opaque_prf_input_len;
  900. so = s->s3->server_opaque_prf_input;
  901. sol = s->s3->client_opaque_prf_input_len; /* must be same as col (see draft-rescorla-tls-opaque-prf-input-00.txt, section 3.1) */
  902. }
  903. #endif
  904. tls1_PRF(s->s3->tmp.new_cipher->algorithm2,
  905. TLS_MD_MASTER_SECRET_CONST,TLS_MD_MASTER_SECRET_CONST_SIZE,
  906. s->s3->client_random,SSL3_RANDOM_SIZE,
  907. co, col,
  908. s->s3->server_random,SSL3_RANDOM_SIZE,
  909. so, sol,
  910. p,len,
  911. s->session->master_key,buff,sizeof buff);
  912. #ifdef KSSL_DEBUG
  913. printf ("tls1_generate_master_secret() complete\n");
  914. #endif /* KSSL_DEBUG */
  915. return(SSL3_MASTER_SECRET_SIZE);
  916. }
  917. int tls1_alert_code(int code)
  918. {
  919. switch (code)
  920. {
  921. case SSL_AD_CLOSE_NOTIFY: return(SSL3_AD_CLOSE_NOTIFY);
  922. case SSL_AD_UNEXPECTED_MESSAGE: return(SSL3_AD_UNEXPECTED_MESSAGE);
  923. case SSL_AD_BAD_RECORD_MAC: return(SSL3_AD_BAD_RECORD_MAC);
  924. case SSL_AD_DECRYPTION_FAILED: return(TLS1_AD_DECRYPTION_FAILED);
  925. case SSL_AD_RECORD_OVERFLOW: return(TLS1_AD_RECORD_OVERFLOW);
  926. case SSL_AD_DECOMPRESSION_FAILURE:return(SSL3_AD_DECOMPRESSION_FAILURE);
  927. case SSL_AD_HANDSHAKE_FAILURE: return(SSL3_AD_HANDSHAKE_FAILURE);
  928. case SSL_AD_NO_CERTIFICATE: return(-1);
  929. case SSL_AD_BAD_CERTIFICATE: return(SSL3_AD_BAD_CERTIFICATE);
  930. case SSL_AD_UNSUPPORTED_CERTIFICATE:return(SSL3_AD_UNSUPPORTED_CERTIFICATE);
  931. case SSL_AD_CERTIFICATE_REVOKED:return(SSL3_AD_CERTIFICATE_REVOKED);
  932. case SSL_AD_CERTIFICATE_EXPIRED:return(SSL3_AD_CERTIFICATE_EXPIRED);
  933. case SSL_AD_CERTIFICATE_UNKNOWN:return(SSL3_AD_CERTIFICATE_UNKNOWN);
  934. case SSL_AD_ILLEGAL_PARAMETER: return(SSL3_AD_ILLEGAL_PARAMETER);
  935. case SSL_AD_UNKNOWN_CA: return(TLS1_AD_UNKNOWN_CA);
  936. case SSL_AD_ACCESS_DENIED: return(TLS1_AD_ACCESS_DENIED);
  937. case SSL_AD_DECODE_ERROR: return(TLS1_AD_DECODE_ERROR);
  938. case SSL_AD_DECRYPT_ERROR: return(TLS1_AD_DECRYPT_ERROR);
  939. case SSL_AD_EXPORT_RESTRICTION: return(TLS1_AD_EXPORT_RESTRICTION);
  940. case SSL_AD_PROTOCOL_VERSION: return(TLS1_AD_PROTOCOL_VERSION);
  941. case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY);
  942. case SSL_AD_INTERNAL_ERROR: return(TLS1_AD_INTERNAL_ERROR);
  943. case SSL_AD_USER_CANCELLED: return(TLS1_AD_USER_CANCELLED);
  944. case SSL_AD_NO_RENEGOTIATION: return(TLS1_AD_NO_RENEGOTIATION);
  945. case SSL_AD_UNSUPPORTED_EXTENSION: return(TLS1_AD_UNSUPPORTED_EXTENSION);
  946. case SSL_AD_CERTIFICATE_UNOBTAINABLE: return(TLS1_AD_CERTIFICATE_UNOBTAINABLE);
  947. case SSL_AD_UNRECOGNIZED_NAME: return(TLS1_AD_UNRECOGNIZED_NAME);
  948. case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: return(TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
  949. case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: return(TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
  950. case SSL_AD_UNKNOWN_PSK_IDENTITY:return(TLS1_AD_UNKNOWN_PSK_IDENTITY);
  951. #if 0 /* not appropriate for TLS, not used for DTLS */
  952. case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return
  953. (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
  954. #endif
  955. default: return(-1);
  956. }
  957. }