1*0Sstevel@tonic-gate /* ssl/s3_both.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate /* ==================================================================== 59*0Sstevel@tonic-gate * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 62*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 63*0Sstevel@tonic-gate * are met: 64*0Sstevel@tonic-gate * 65*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 66*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 67*0Sstevel@tonic-gate * 68*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 69*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 70*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 71*0Sstevel@tonic-gate * distribution. 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 74*0Sstevel@tonic-gate * software must display the following acknowledgment: 75*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 76*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77*0Sstevel@tonic-gate * 78*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79*0Sstevel@tonic-gate * endorse or promote products derived from this software without 80*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 81*0Sstevel@tonic-gate * openssl-core@openssl.org. 82*0Sstevel@tonic-gate * 83*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 84*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 85*0Sstevel@tonic-gate * permission of the OpenSSL Project. 86*0Sstevel@tonic-gate * 87*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 88*0Sstevel@tonic-gate * acknowledgment: 89*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 90*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91*0Sstevel@tonic-gate * 92*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 104*0Sstevel@tonic-gate * ==================================================================== 105*0Sstevel@tonic-gate * 106*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 107*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 108*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 109*0Sstevel@tonic-gate * 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate #include <limits.h> 113*0Sstevel@tonic-gate #include <string.h> 114*0Sstevel@tonic-gate #include <stdio.h> 115*0Sstevel@tonic-gate #include "ssl_locl.h" 116*0Sstevel@tonic-gate #include <openssl/buffer.h> 117*0Sstevel@tonic-gate #include <openssl/rand.h> 118*0Sstevel@tonic-gate #include <openssl/objects.h> 119*0Sstevel@tonic-gate #include <openssl/evp.h> 120*0Sstevel@tonic-gate #include <openssl/x509.h> 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */ 123*0Sstevel@tonic-gate int ssl3_do_write(SSL *s, int type) 124*0Sstevel@tonic-gate { 125*0Sstevel@tonic-gate int ret; 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate ret=ssl3_write_bytes(s,type,&s->init_buf->data[s->init_off], 128*0Sstevel@tonic-gate s->init_num); 129*0Sstevel@tonic-gate if (ret < 0) return(-1); 130*0Sstevel@tonic-gate if (type == SSL3_RT_HANDSHAKE) 131*0Sstevel@tonic-gate /* should not be done for 'Hello Request's, but in that case 132*0Sstevel@tonic-gate * we'll ignore the result anyway */ 133*0Sstevel@tonic-gate ssl3_finish_mac(s,(unsigned char *)&s->init_buf->data[s->init_off],ret); 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if (ret == s->init_num) 136*0Sstevel@tonic-gate { 137*0Sstevel@tonic-gate if (s->msg_callback) 138*0Sstevel@tonic-gate s->msg_callback(1, s->version, type, s->init_buf->data, (size_t)(s->init_off + s->init_num), s, s->msg_callback_arg); 139*0Sstevel@tonic-gate return(1); 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate s->init_off+=ret; 142*0Sstevel@tonic-gate s->init_num-=ret; 143*0Sstevel@tonic-gate return(0); 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate int ssl3_send_finished(SSL *s, int a, int b, const char *sender, int slen) 147*0Sstevel@tonic-gate { 148*0Sstevel@tonic-gate unsigned char *p,*d; 149*0Sstevel@tonic-gate int i; 150*0Sstevel@tonic-gate unsigned long l; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate if (s->state == a) 153*0Sstevel@tonic-gate { 154*0Sstevel@tonic-gate d=(unsigned char *)s->init_buf->data; 155*0Sstevel@tonic-gate p= &(d[4]); 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate i=s->method->ssl3_enc->final_finish_mac(s, 158*0Sstevel@tonic-gate &(s->s3->finish_dgst1), 159*0Sstevel@tonic-gate &(s->s3->finish_dgst2), 160*0Sstevel@tonic-gate sender,slen,s->s3->tmp.finish_md); 161*0Sstevel@tonic-gate s->s3->tmp.finish_md_len = i; 162*0Sstevel@tonic-gate memcpy(p, s->s3->tmp.finish_md, i); 163*0Sstevel@tonic-gate p+=i; 164*0Sstevel@tonic-gate l=i; 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN16 167*0Sstevel@tonic-gate /* MSVC 1.5 does not clear the top bytes of the word unless 168*0Sstevel@tonic-gate * I do this. 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate l&=0xffff; 171*0Sstevel@tonic-gate #endif 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate *(d++)=SSL3_MT_FINISHED; 174*0Sstevel@tonic-gate l2n3(l,d); 175*0Sstevel@tonic-gate s->init_num=(int)l+4; 176*0Sstevel@tonic-gate s->init_off=0; 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate s->state=b; 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* SSL3_ST_SEND_xxxxxx_HELLO_B */ 182*0Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate int ssl3_get_finished(SSL *s, int a, int b) 186*0Sstevel@tonic-gate { 187*0Sstevel@tonic-gate int al,i,ok; 188*0Sstevel@tonic-gate long n; 189*0Sstevel@tonic-gate unsigned char *p; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate /* the mac has already been generated when we received the 192*0Sstevel@tonic-gate * change cipher spec message and is in s->s3->tmp.peer_finish_md 193*0Sstevel@tonic-gate */ 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate n=ssl3_get_message(s, 196*0Sstevel@tonic-gate a, 197*0Sstevel@tonic-gate b, 198*0Sstevel@tonic-gate SSL3_MT_FINISHED, 199*0Sstevel@tonic-gate 64, /* should actually be 36+4 :-) */ 200*0Sstevel@tonic-gate &ok); 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate if (!ok) return((int)n); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* If this occurs, we have missed a message */ 205*0Sstevel@tonic-gate if (!s->s3->change_cipher_spec) 206*0Sstevel@tonic-gate { 207*0Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 208*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_GOT_A_FIN_BEFORE_A_CCS); 209*0Sstevel@tonic-gate goto f_err; 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate s->s3->change_cipher_spec=0; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate p = (unsigned char *)s->init_msg; 214*0Sstevel@tonic-gate i = s->s3->tmp.peer_finish_md_len; 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate if (i != n) 217*0Sstevel@tonic-gate { 218*0Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 219*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_BAD_DIGEST_LENGTH); 220*0Sstevel@tonic-gate goto f_err; 221*0Sstevel@tonic-gate } 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate if (memcmp(p, s->s3->tmp.peer_finish_md, i) != 0) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate al=SSL_AD_DECRYPT_ERROR; 226*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_FINISHED,SSL_R_DIGEST_CHECK_FAILED); 227*0Sstevel@tonic-gate goto f_err; 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate return(1); 231*0Sstevel@tonic-gate f_err: 232*0Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 233*0Sstevel@tonic-gate return(0); 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate /* for these 2 messages, we need to 237*0Sstevel@tonic-gate * ssl->enc_read_ctx re-init 238*0Sstevel@tonic-gate * ssl->s3->read_sequence zero 239*0Sstevel@tonic-gate * ssl->s3->read_mac_secret re-init 240*0Sstevel@tonic-gate * ssl->session->read_sym_enc assign 241*0Sstevel@tonic-gate * ssl->session->read_compression assign 242*0Sstevel@tonic-gate * ssl->session->read_hash assign 243*0Sstevel@tonic-gate */ 244*0Sstevel@tonic-gate int ssl3_send_change_cipher_spec(SSL *s, int a, int b) 245*0Sstevel@tonic-gate { 246*0Sstevel@tonic-gate unsigned char *p; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate if (s->state == a) 249*0Sstevel@tonic-gate { 250*0Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data; 251*0Sstevel@tonic-gate *p=SSL3_MT_CCS; 252*0Sstevel@tonic-gate s->init_num=1; 253*0Sstevel@tonic-gate s->init_off=0; 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate s->state=b; 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate /* SSL3_ST_CW_CHANGE_B */ 259*0Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_CHANGE_CIPHER_SPEC)); 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate unsigned long ssl3_output_cert_chain(SSL *s, X509 *x) 263*0Sstevel@tonic-gate { 264*0Sstevel@tonic-gate unsigned char *p; 265*0Sstevel@tonic-gate int n,i; 266*0Sstevel@tonic-gate unsigned long l=7; 267*0Sstevel@tonic-gate BUF_MEM *buf; 268*0Sstevel@tonic-gate X509_STORE_CTX xs_ctx; 269*0Sstevel@tonic-gate X509_OBJECT obj; 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate int no_chain; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || s->ctx->extra_certs) 274*0Sstevel@tonic-gate no_chain = 1; 275*0Sstevel@tonic-gate else 276*0Sstevel@tonic-gate no_chain = 0; 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate /* TLSv1 sends a chain with nothing in it, instead of an alert */ 279*0Sstevel@tonic-gate buf=s->init_buf; 280*0Sstevel@tonic-gate if (!BUF_MEM_grow_clean(buf,10)) 281*0Sstevel@tonic-gate { 282*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB); 283*0Sstevel@tonic-gate return(0); 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate if (x != NULL) 286*0Sstevel@tonic-gate { 287*0Sstevel@tonic-gate if(!no_chain && !X509_STORE_CTX_init(&xs_ctx,s->ctx->cert_store,NULL,NULL)) 288*0Sstevel@tonic-gate { 289*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_X509_LIB); 290*0Sstevel@tonic-gate return(0); 291*0Sstevel@tonic-gate } 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate for (;;) 294*0Sstevel@tonic-gate { 295*0Sstevel@tonic-gate n=i2d_X509(x,NULL); 296*0Sstevel@tonic-gate if (!BUF_MEM_grow_clean(buf,(int)(n+l+3))) 297*0Sstevel@tonic-gate { 298*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB); 299*0Sstevel@tonic-gate return(0); 300*0Sstevel@tonic-gate } 301*0Sstevel@tonic-gate p=(unsigned char *)&(buf->data[l]); 302*0Sstevel@tonic-gate l2n3(n,p); 303*0Sstevel@tonic-gate i2d_X509(x,&p); 304*0Sstevel@tonic-gate l+=n+3; 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate if (no_chain) 307*0Sstevel@tonic-gate break; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate if (X509_NAME_cmp(X509_get_subject_name(x), 310*0Sstevel@tonic-gate X509_get_issuer_name(x)) == 0) break; 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate i=X509_STORE_get_by_subject(&xs_ctx,X509_LU_X509, 313*0Sstevel@tonic-gate X509_get_issuer_name(x),&obj); 314*0Sstevel@tonic-gate if (i <= 0) break; 315*0Sstevel@tonic-gate x=obj.data.x509; 316*0Sstevel@tonic-gate /* Count is one too high since the X509_STORE_get uped the 317*0Sstevel@tonic-gate * ref count */ 318*0Sstevel@tonic-gate X509_free(x); 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate if (!no_chain) 321*0Sstevel@tonic-gate X509_STORE_CTX_cleanup(&xs_ctx); 322*0Sstevel@tonic-gate } 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate /* Thawte special :-) */ 325*0Sstevel@tonic-gate if (s->ctx->extra_certs != NULL) 326*0Sstevel@tonic-gate for (i=0; i<sk_X509_num(s->ctx->extra_certs); i++) 327*0Sstevel@tonic-gate { 328*0Sstevel@tonic-gate x=sk_X509_value(s->ctx->extra_certs,i); 329*0Sstevel@tonic-gate n=i2d_X509(x,NULL); 330*0Sstevel@tonic-gate if (!BUF_MEM_grow_clean(buf,(int)(n+l+3))) 331*0Sstevel@tonic-gate { 332*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN,ERR_R_BUF_LIB); 333*0Sstevel@tonic-gate return(0); 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate p=(unsigned char *)&(buf->data[l]); 336*0Sstevel@tonic-gate l2n3(n,p); 337*0Sstevel@tonic-gate i2d_X509(x,&p); 338*0Sstevel@tonic-gate l+=n+3; 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate l-=7; 342*0Sstevel@tonic-gate p=(unsigned char *)&(buf->data[4]); 343*0Sstevel@tonic-gate l2n3(l,p); 344*0Sstevel@tonic-gate l+=3; 345*0Sstevel@tonic-gate p=(unsigned char *)&(buf->data[0]); 346*0Sstevel@tonic-gate *(p++)=SSL3_MT_CERTIFICATE; 347*0Sstevel@tonic-gate l2n3(l,p); 348*0Sstevel@tonic-gate l+=4; 349*0Sstevel@tonic-gate return(l); 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate /* Obtain handshake message of message type 'mt' (any if mt == -1), 353*0Sstevel@tonic-gate * maximum acceptable body length 'max'. 354*0Sstevel@tonic-gate * The first four bytes (msg_type and length) are read in state 'st1', 355*0Sstevel@tonic-gate * the body is read in state 'stn'. 356*0Sstevel@tonic-gate */ 357*0Sstevel@tonic-gate long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok) 358*0Sstevel@tonic-gate { 359*0Sstevel@tonic-gate unsigned char *p; 360*0Sstevel@tonic-gate unsigned long l; 361*0Sstevel@tonic-gate long n; 362*0Sstevel@tonic-gate int i,al; 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate if (s->s3->tmp.reuse_message) 365*0Sstevel@tonic-gate { 366*0Sstevel@tonic-gate s->s3->tmp.reuse_message=0; 367*0Sstevel@tonic-gate if ((mt >= 0) && (s->s3->tmp.message_type != mt)) 368*0Sstevel@tonic-gate { 369*0Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 370*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE); 371*0Sstevel@tonic-gate goto f_err; 372*0Sstevel@tonic-gate } 373*0Sstevel@tonic-gate *ok=1; 374*0Sstevel@tonic-gate s->init_msg = s->init_buf->data + 4; 375*0Sstevel@tonic-gate s->init_num = (int)s->s3->tmp.message_size; 376*0Sstevel@tonic-gate return s->init_num; 377*0Sstevel@tonic-gate } 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data; 380*0Sstevel@tonic-gate 381*0Sstevel@tonic-gate if (s->state == st1) /* s->init_num < 4 */ 382*0Sstevel@tonic-gate { 383*0Sstevel@tonic-gate int skip_message; 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate do 386*0Sstevel@tonic-gate { 387*0Sstevel@tonic-gate while (s->init_num < 4) 388*0Sstevel@tonic-gate { 389*0Sstevel@tonic-gate i=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num], 390*0Sstevel@tonic-gate 4 - s->init_num, 0); 391*0Sstevel@tonic-gate if (i <= 0) 392*0Sstevel@tonic-gate { 393*0Sstevel@tonic-gate s->rwstate=SSL_READING; 394*0Sstevel@tonic-gate *ok = 0; 395*0Sstevel@tonic-gate return i; 396*0Sstevel@tonic-gate } 397*0Sstevel@tonic-gate s->init_num+=i; 398*0Sstevel@tonic-gate } 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate skip_message = 0; 401*0Sstevel@tonic-gate if (!s->server) 402*0Sstevel@tonic-gate if (p[0] == SSL3_MT_HELLO_REQUEST) 403*0Sstevel@tonic-gate /* The server may always send 'Hello Request' messages -- 404*0Sstevel@tonic-gate * we are doing a handshake anyway now, so ignore them 405*0Sstevel@tonic-gate * if their format is correct. Does not count for 406*0Sstevel@tonic-gate * 'Finished' MAC. */ 407*0Sstevel@tonic-gate if (p[1] == 0 && p[2] == 0 &&p[3] == 0) 408*0Sstevel@tonic-gate { 409*0Sstevel@tonic-gate s->init_num = 0; 410*0Sstevel@tonic-gate skip_message = 1; 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate if (s->msg_callback) 413*0Sstevel@tonic-gate s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, 4, s, s->msg_callback_arg); 414*0Sstevel@tonic-gate } 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate while (skip_message); 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate /* s->init_num == 4 */ 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate if ((mt >= 0) && (*p != mt)) 421*0Sstevel@tonic-gate { 422*0Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 423*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE); 424*0Sstevel@tonic-gate goto f_err; 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate if ((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) && 427*0Sstevel@tonic-gate (st1 == SSL3_ST_SR_CERT_A) && 428*0Sstevel@tonic-gate (stn == SSL3_ST_SR_CERT_B)) 429*0Sstevel@tonic-gate { 430*0Sstevel@tonic-gate /* At this point we have got an MS SGC second client 431*0Sstevel@tonic-gate * hello (maybe we should always allow the client to 432*0Sstevel@tonic-gate * start a new handshake?). We need to restart the mac. 433*0Sstevel@tonic-gate * Don't increment {num,total}_renegotiations because 434*0Sstevel@tonic-gate * we have not completed the handshake. */ 435*0Sstevel@tonic-gate ssl3_init_finished_mac(s); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate s->s3->tmp.message_type= *(p++); 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gate n2l3(p,l); 441*0Sstevel@tonic-gate if (l > (unsigned long)max) 442*0Sstevel@tonic-gate { 443*0Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER; 444*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE); 445*0Sstevel@tonic-gate goto f_err; 446*0Sstevel@tonic-gate } 447*0Sstevel@tonic-gate if (l > (INT_MAX-4)) /* BUF_MEM_grow takes an 'int' parameter */ 448*0Sstevel@tonic-gate { 449*0Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER; 450*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE); 451*0Sstevel@tonic-gate goto f_err; 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate if (l && !BUF_MEM_grow_clean(s->init_buf,(int)l+4)) 454*0Sstevel@tonic-gate { 455*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB); 456*0Sstevel@tonic-gate goto err; 457*0Sstevel@tonic-gate } 458*0Sstevel@tonic-gate s->s3->tmp.message_size=l; 459*0Sstevel@tonic-gate s->state=stn; 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate s->init_msg = s->init_buf->data + 4; 462*0Sstevel@tonic-gate s->init_num = 0; 463*0Sstevel@tonic-gate } 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate /* next state (stn) */ 466*0Sstevel@tonic-gate p = s->init_msg; 467*0Sstevel@tonic-gate n = s->s3->tmp.message_size - s->init_num; 468*0Sstevel@tonic-gate while (n > 0) 469*0Sstevel@tonic-gate { 470*0Sstevel@tonic-gate i=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n,0); 471*0Sstevel@tonic-gate if (i <= 0) 472*0Sstevel@tonic-gate { 473*0Sstevel@tonic-gate s->rwstate=SSL_READING; 474*0Sstevel@tonic-gate *ok = 0; 475*0Sstevel@tonic-gate return i; 476*0Sstevel@tonic-gate } 477*0Sstevel@tonic-gate s->init_num += i; 478*0Sstevel@tonic-gate n -= i; 479*0Sstevel@tonic-gate } 480*0Sstevel@tonic-gate ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num + 4); 481*0Sstevel@tonic-gate if (s->msg_callback) 482*0Sstevel@tonic-gate s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data, (size_t)s->init_num + 4, s, s->msg_callback_arg); 483*0Sstevel@tonic-gate *ok=1; 484*0Sstevel@tonic-gate return s->init_num; 485*0Sstevel@tonic-gate f_err: 486*0Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 487*0Sstevel@tonic-gate err: 488*0Sstevel@tonic-gate *ok=0; 489*0Sstevel@tonic-gate return(-1); 490*0Sstevel@tonic-gate } 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate int ssl_cert_type(X509 *x, EVP_PKEY *pkey) 493*0Sstevel@tonic-gate { 494*0Sstevel@tonic-gate EVP_PKEY *pk; 495*0Sstevel@tonic-gate int ret= -1,i,j; 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate if (pkey == NULL) 498*0Sstevel@tonic-gate pk=X509_get_pubkey(x); 499*0Sstevel@tonic-gate else 500*0Sstevel@tonic-gate pk=pkey; 501*0Sstevel@tonic-gate if (pk == NULL) goto err; 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate i=pk->type; 504*0Sstevel@tonic-gate if (i == EVP_PKEY_RSA) 505*0Sstevel@tonic-gate { 506*0Sstevel@tonic-gate ret=SSL_PKEY_RSA_ENC; 507*0Sstevel@tonic-gate if (x != NULL) 508*0Sstevel@tonic-gate { 509*0Sstevel@tonic-gate j=X509_get_ext_count(x); 510*0Sstevel@tonic-gate /* check to see if this is a signing only certificate */ 511*0Sstevel@tonic-gate /* EAY EAY EAY EAY */ 512*0Sstevel@tonic-gate } 513*0Sstevel@tonic-gate } 514*0Sstevel@tonic-gate else if (i == EVP_PKEY_DSA) 515*0Sstevel@tonic-gate { 516*0Sstevel@tonic-gate ret=SSL_PKEY_DSA_SIGN; 517*0Sstevel@tonic-gate } 518*0Sstevel@tonic-gate else if (i == EVP_PKEY_DH) 519*0Sstevel@tonic-gate { 520*0Sstevel@tonic-gate /* if we just have a key, we needs to be guess */ 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate if (x == NULL) 523*0Sstevel@tonic-gate ret=SSL_PKEY_DH_DSA; 524*0Sstevel@tonic-gate else 525*0Sstevel@tonic-gate { 526*0Sstevel@tonic-gate j=X509_get_signature_type(x); 527*0Sstevel@tonic-gate if (j == EVP_PKEY_RSA) 528*0Sstevel@tonic-gate ret=SSL_PKEY_DH_RSA; 529*0Sstevel@tonic-gate else if (j== EVP_PKEY_DSA) 530*0Sstevel@tonic-gate ret=SSL_PKEY_DH_DSA; 531*0Sstevel@tonic-gate else ret= -1; 532*0Sstevel@tonic-gate } 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate else 535*0Sstevel@tonic-gate ret= -1; 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate err: 538*0Sstevel@tonic-gate if(!pkey) EVP_PKEY_free(pk); 539*0Sstevel@tonic-gate return(ret); 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate int ssl_verify_alarm_type(long type) 543*0Sstevel@tonic-gate { 544*0Sstevel@tonic-gate int al; 545*0Sstevel@tonic-gate 546*0Sstevel@tonic-gate switch(type) 547*0Sstevel@tonic-gate { 548*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: 549*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_GET_CRL: 550*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER: 551*0Sstevel@tonic-gate al=SSL_AD_UNKNOWN_CA; 552*0Sstevel@tonic-gate break; 553*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: 554*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: 555*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: 556*0Sstevel@tonic-gate case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: 557*0Sstevel@tonic-gate case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: 558*0Sstevel@tonic-gate case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: 559*0Sstevel@tonic-gate case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: 560*0Sstevel@tonic-gate case X509_V_ERR_CERT_NOT_YET_VALID: 561*0Sstevel@tonic-gate case X509_V_ERR_CRL_NOT_YET_VALID: 562*0Sstevel@tonic-gate case X509_V_ERR_CERT_UNTRUSTED: 563*0Sstevel@tonic-gate case X509_V_ERR_CERT_REJECTED: 564*0Sstevel@tonic-gate al=SSL_AD_BAD_CERTIFICATE; 565*0Sstevel@tonic-gate break; 566*0Sstevel@tonic-gate case X509_V_ERR_CERT_SIGNATURE_FAILURE: 567*0Sstevel@tonic-gate case X509_V_ERR_CRL_SIGNATURE_FAILURE: 568*0Sstevel@tonic-gate al=SSL_AD_DECRYPT_ERROR; 569*0Sstevel@tonic-gate break; 570*0Sstevel@tonic-gate case X509_V_ERR_CERT_HAS_EXPIRED: 571*0Sstevel@tonic-gate case X509_V_ERR_CRL_HAS_EXPIRED: 572*0Sstevel@tonic-gate al=SSL_AD_CERTIFICATE_EXPIRED; 573*0Sstevel@tonic-gate break; 574*0Sstevel@tonic-gate case X509_V_ERR_CERT_REVOKED: 575*0Sstevel@tonic-gate al=SSL_AD_CERTIFICATE_REVOKED; 576*0Sstevel@tonic-gate break; 577*0Sstevel@tonic-gate case X509_V_ERR_OUT_OF_MEM: 578*0Sstevel@tonic-gate al=SSL_AD_INTERNAL_ERROR; 579*0Sstevel@tonic-gate break; 580*0Sstevel@tonic-gate case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: 581*0Sstevel@tonic-gate case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: 582*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: 583*0Sstevel@tonic-gate case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: 584*0Sstevel@tonic-gate case X509_V_ERR_CERT_CHAIN_TOO_LONG: 585*0Sstevel@tonic-gate case X509_V_ERR_PATH_LENGTH_EXCEEDED: 586*0Sstevel@tonic-gate case X509_V_ERR_INVALID_CA: 587*0Sstevel@tonic-gate al=SSL_AD_UNKNOWN_CA; 588*0Sstevel@tonic-gate break; 589*0Sstevel@tonic-gate case X509_V_ERR_APPLICATION_VERIFICATION: 590*0Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 591*0Sstevel@tonic-gate break; 592*0Sstevel@tonic-gate case X509_V_ERR_INVALID_PURPOSE: 593*0Sstevel@tonic-gate al=SSL_AD_UNSUPPORTED_CERTIFICATE; 594*0Sstevel@tonic-gate break; 595*0Sstevel@tonic-gate default: 596*0Sstevel@tonic-gate al=SSL_AD_CERTIFICATE_UNKNOWN; 597*0Sstevel@tonic-gate break; 598*0Sstevel@tonic-gate } 599*0Sstevel@tonic-gate return(al); 600*0Sstevel@tonic-gate } 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate int ssl3_setup_buffers(SSL *s) 603*0Sstevel@tonic-gate { 604*0Sstevel@tonic-gate unsigned char *p; 605*0Sstevel@tonic-gate unsigned int extra; 606*0Sstevel@tonic-gate size_t len; 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gate if (s->s3->rbuf.buf == NULL) 609*0Sstevel@tonic-gate { 610*0Sstevel@tonic-gate if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) 611*0Sstevel@tonic-gate extra=SSL3_RT_MAX_EXTRA; 612*0Sstevel@tonic-gate else 613*0Sstevel@tonic-gate extra=0; 614*0Sstevel@tonic-gate len = SSL3_RT_MAX_PACKET_SIZE + extra; 615*0Sstevel@tonic-gate if ((p=OPENSSL_malloc(len)) == NULL) 616*0Sstevel@tonic-gate goto err; 617*0Sstevel@tonic-gate s->s3->rbuf.buf = p; 618*0Sstevel@tonic-gate s->s3->rbuf.len = len; 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gate if (s->s3->wbuf.buf == NULL) 622*0Sstevel@tonic-gate { 623*0Sstevel@tonic-gate len = SSL3_RT_MAX_PACKET_SIZE; 624*0Sstevel@tonic-gate len += SSL3_RT_HEADER_LENGTH + 256; /* extra space for empty fragment */ 625*0Sstevel@tonic-gate if ((p=OPENSSL_malloc(len)) == NULL) 626*0Sstevel@tonic-gate goto err; 627*0Sstevel@tonic-gate s->s3->wbuf.buf = p; 628*0Sstevel@tonic-gate s->s3->wbuf.len = len; 629*0Sstevel@tonic-gate } 630*0Sstevel@tonic-gate s->packet= &(s->s3->rbuf.buf[0]); 631*0Sstevel@tonic-gate return(1); 632*0Sstevel@tonic-gate err: 633*0Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SETUP_BUFFERS,ERR_R_MALLOC_FAILURE); 634*0Sstevel@tonic-gate return(0); 635*0Sstevel@tonic-gate } 636