spp_prxy.c 49.1 KB
Newer Older
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
                s->init_num=next_st->init_num=0;
                
                // Receive and forward the certificates for proxies between us and the server
                if ((proxy = spp_get_next_proxy(s, this_proxy, 0)) != NULL) {
                    // Flush our cert in both directions
                    // Need to receive and forward certificates from proxies between this proxy and the client
                    s->state=SPP_ST_SW_AHEAD_FLUSH;
                    s->s3->tmp.next_state=SPP_ST_PR_AHEAD_A;
                } else {
                    // No proxies between us and client, wait for client key exchange
                    s->state=SPP_ST_SW_AHEAD_FLUSH;
                    s->s3->tmp.next_state=SSL3_ST_SR_KEY_EXCH_A;
                }
                break;

            case SPP_ST_PR_BEHIND_A:
            case SPP_ST_PR_BEHIND_B:
                                #ifdef DEBUG
                //printf("Receiving message from behind proxy %d\n", proxy->proxy_id);
				log_time("Waiting for messages from proxies behind\n", &currTime, &prevTime, &originTime); 
				#endif
                while (proxy != NULL) {
                    next_st->state=SPP_ST_PR_BEHIND_A;
                    ret=get_proxy_msg(next_st, SPP_ST_PR_BEHIND_A, SPP_ST_PR_BEHIND_B, -1, 1);
                                #ifdef DEBUG
				log_time("Proxy message received\n", &currTime, &prevTime, &originTime); 
				#endif
                    if (ret <= 0) goto end;
                    s->init_num=next_st->init_num=0;
                    if (next_st->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
                        if ((proxy = spp_get_next_proxy(s, proxy, 0)) == NULL || proxy->proxy_id == s->proxy_id) {
                            s->state=SPP_ST_SW_AHEAD_FLUSH;
                            s->s3->tmp.next_state=SSL3_ST_SW_CERT_A;
                            break;
                        }
                        //printf("Receiving message from behind proxy %d\n", proxy->proxy_id);
                    }
                }
                
                break;
                
            case SPP_ST_PR_AHEAD_A:
            case SPP_ST_PR_AHEAD_B:
                                #ifdef DEBUG
//printf("Receiving message from ahead proxy %d\n", proxy->proxy_id);
				log_time("Waiting for messages from proxies ahead\n", &currTime, &prevTime, &originTime); 
				#endif
                while (proxy != NULL) {
                    s->state=SPP_ST_PR_AHEAD_A;
                    ret=get_proxy_msg(s, SPP_ST_PR_AHEAD_A, SPP_ST_PR_AHEAD_B, -1, 1);
                                #ifdef DEBUG
				log_time("Proxy message received\n", &currTime, &prevTime, &originTime); 
				#endif
                    if (ret <= 0) goto end;
                    s->init_num=next_st->init_num=0;
                    if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
                        if ((proxy = spp_get_next_proxy(s, proxy, 0)) == NULL) {
                            //s->state=SPP_ST_CW_BEHIND_FLUSH;
                            //s->s3->tmp.next_state=SSL3_ST_SR_KEY_EXCH_A;
                            s->state=SSL3_ST_SR_KEY_EXCH_A;
                            break;
                        } 
                        //printf("Receiving message from ahead proxy %d\n", proxy->proxy_id);
                    }
                }
                
                break;
		
            case SSL3_ST_SW_FLUSH:
                /* This code originally checked to see if
                 * any data was pending using BIO_CTRL_INFO
                 * and then flushed. This caused problems
                 * as documented in PR#1939. The proposed
                 * fix doesn't completely resolve this issue
                 * as buggy implementations of BIO_CTRL_PENDING
                 * still exist. So instead we just flush
                 * unconditionally.
                 */

                s->rwstate=SSL_WRITING;
                if (BIO_flush(s->wbio) <= 0) {
                    ret= -1;
                    goto end;
                }
                s->rwstate=SSL_NOTHING;
                next_st->rwstate=SSL_WRITING;
                if (BIO_flush(next_st->wbio) <= 0) {
                    ret= -1;
                    goto end;
                }
                next_st->rwstate=SSL_NOTHING;

                s->state=s->s3->tmp.next_state;
                break;
                
            case SPP_ST_SW_AHEAD_FLUSH:
                /* Flush between the proxy and the client
                 */

                s->rwstate=SSL_WRITING;
                if (BIO_flush(s->wbio) <= 0) {
                    ret= -1;
                    goto end;
                }
                s->rwstate=SSL_NOTHING;

                s->state=s->s3->tmp.next_state;
                break;
                
            case SPP_ST_CW_BEHIND_FLUSH:
                /* Flush between the proxy and the server
                 */

                next_st->rwstate=SSL_WRITING;
                if (BIO_flush(next_st->wbio) <= 0) {
                    ret= -1;
                    goto end;
                }
                next_st->rwstate=SSL_NOTHING;

                s->state=s->s3->tmp.next_state;
                break;
                
            case SSL3_ST_SR_KEY_EXCH_A:
            case SSL3_ST_SR_KEY_EXCH_B:
                                #ifdef DEBUG
				log_time("Waiting for client key exchange\n", &currTime, &prevTime, &originTime); 
				#endif
                ret=get_proxy_msg(s, SSL3_ST_SR_KEY_EXCH_A, SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE,1);
                                #ifdef DEBUG
				log_time("Received client key exchange\n", &currTime, &prevTime, &originTime); 
				#endif
                if (ret <= 0) goto end;
                
                s->state=SPP_ST_CR_PRXY_MAT_A;
                s->init_num=next_st->init_num=0;
                break;
                
            case SPP_ST_CR_PRXY_MAT_A:
            case SPP_ST_CR_PRXY_MAT_B:
                                #ifdef DEBUG
				log_time("Receiving client key material\n", &currTime, &prevTime, &originTime); 
				#endif
                // Receive proxy key material for each proxy and the server
                // Pass all messages on
                for (i = 0; i <= s->proxies_len; i++) {
                    s->state = next_st->state = SPP_ST_CR_PRXY_MAT_A;
                    ret=get_proxy_msg(s, SPP_ST_CR_PRXY_MAT_A, SPP_ST_CR_PRXY_MAT_B, SPP_MT_PROXY_KEY_MATERIAL,1);                    
                    if (ret <= 0) goto end;
                    ret=get_proxy_material(s, 0); // From client
                    if (ret <= 0) goto end;
                    s->init_num=next_st->init_num=0;
                }
                
#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
                s->state=SSL3_ST_SR_FINISHED_A;
#else
                if (s->s3->next_proto_neg_seen)
                    s->state=SSL3_ST_SR_NEXT_PROTO_A;
                else
                    s->state=SSL3_ST_SR_FINISHED_A;
#endif
                //s->s3->tmp.next_state=SPP_ST_SR_PRXY_MAT_A;
                //s->state=SSL3_ST_SW_FLUSH;
                break;
                
            case SPP_ST_SR_PRXY_MAT_A:
            case SPP_ST_SR_PRXY_MAT_B:
                                #ifdef DEBUG
				log_time("Receiving server key material\n", &currTime, &prevTime, &originTime); 
				#endif
                // Receive proxy key material for each proxy and the server
                // Pass all messages on
                for (i = 0; i <= s->proxies_len; i++) {
                    //printf("Waiting for proxy material message\n");
                    s->state = next_st->state = SPP_ST_SR_PRXY_MAT_A;
                    ret=get_proxy_msg(next_st, SPP_ST_SR_PRXY_MAT_A, SPP_ST_SR_PRXY_MAT_B, SPP_MT_PROXY_KEY_MATERIAL, 1);
                    if (ret <= 0) goto end;
                    //printf("Got material message\n");
                    ret=get_proxy_material(next_st, 1); // From server
                    if (ret <= 0) goto end;
                    s->init_num=next_st->init_num=0;
                }
                
#ifndef OPENSSL_NO_TLSEXT
                if (s->tlsext_ticket_expected)
                    s->state=SSL3_ST_CR_FINISHED_A;
                else
#endif
                    s->state=SSL3_ST_CR_FINISHED_A;
                break;

#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
            case SSL3_ST_SR_NEXT_PROTO_A:
            case SSL3_ST_SR_NEXT_PROTO_B:
                                #ifdef DEBUG
				log_time("Receiving next proto\n", &currTime, &prevTime, &originTime); 
				#endif
                ret=get_proxy_msg(s, SSL3_ST_SR_NEXT_PROTO_A, SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO,1);
                if (ret <= 0) goto end;
                s->init_num=next_st->init_num=0;
                s->state=SSL3_ST_SR_FINISHED_A;
                break;
#endif

            case SSL3_ST_SR_FINISHED_A:
            case SSL3_ST_SR_FINISHED_B:
                                #ifdef DEBUG
				log_time("Waiting for finished from client\n", &currTime, &prevTime, &originTime); 
				#endif
                s->s3->flags |= SSL3_FLAGS_CCS_OK;
                ret=spp_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
                if (ret <= 0) goto end;
                s->init_num=next_st->init_num=0;
                
                s->state=SPP_ST_CW_BEHIND_FLUSH;
                if (s->hit)
                    s->s3->tmp.next_state=SSL_ST_OK;
                else
                    s->s3->tmp.next_state=SPP_ST_SR_PRXY_MAT_A;

                
                /* Cipher state should actually be changed before the finished message.
                 * Since proxies do not actually validate the Finished message, however,
                 * it may be done after. */
                /*if (!s->method->ssl3_enc->change_cipher_state(s, SSL3_CHANGE_CIPHER_SERVER_READ)) {
                    ret= -1;
                    goto end;
                }*/ /* Done when receiving the change cipher spec message */
                next_st->session->cipher=s->s3->tmp.new_cipher;
                if (!next_st->method->ssl3_enc->setup_key_block(next_st))
                        { ret= -1; goto end; }
                if (!next_st->method->ssl3_enc->change_cipher_state(next_st, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
                    ret= -1;
                    goto end;
                }
                break;
                
            case SSL3_ST_SW_SESSION_TICKET_A:
            case SSL3_ST_SW_SESSION_TICKET_B:
                                #ifdef DEBUG
				log_time("Waiting for session ticket\n", &currTime, &prevTime, &originTime); 
				#endif
                ret=get_proxy_msg(next_st, SSL3_ST_SW_SESSION_TICKET_A, SSL3_ST_SW_SESSION_TICKET_B, SSL3_MT_NEWSESSION_TICKET,1);
                if (ret <= 0) goto end;
                s->state=SSL3_ST_CR_FINISHED_A;
                s->init_num=next_st->init_num=0;
                break;
                
            case SSL3_ST_CR_FINISHED_A:
            case SSL3_ST_CR_FINISHED_B:
                                #ifdef DEBUG
				log_time("Waiting for finished from server\n", &currTime, &prevTime, &originTime); 
				#endif
                next_st->s3->flags |= SSL3_FLAGS_CCS_OK;
                ret=spp_get_finished(next_st, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
                if (ret <= 0) goto end;
                s->init_num=next_st->init_num=0;
                
                s->s3->tmp.next_state=SSL_ST_OK;
                s->state=SPP_ST_SW_AHEAD_FLUSH;
                
                s->session->cipher=s->s3->tmp.new_cipher;
                if (!s->method->ssl3_enc->setup_key_block(s))
                        { ret= -1; goto end; }
                if (!s->method->ssl3_enc->change_cipher_state(s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
                    ret= -1;
                    goto end;
                }
                /*if (!next_st->method->ssl3_enc->change_cipher_state(next_st, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
                    ret= -1;
                    goto end;
                }*/ /* Done when receiving the change cipher spec message */
                
                /* Setup the slices now that we have the necessary state. */
                ret = spp_init_slices_st(s, SSL3_CHANGE_CIPHER_SERVER_WRITE);
                if (ret <= 0) goto end;
                ret = spp_init_slices_st(s, SSL3_CHANGE_CIPHER_SERVER_READ);
                if (ret <= 0) goto end;
                ret = spp_init_slices_st(next_st, SSL3_CHANGE_CIPHER_CLIENT_WRITE);
                if (ret <= 0) goto end;
                ret = spp_init_slices_st(next_st, SSL3_CHANGE_CIPHER_CLIENT_READ);
                if (ret <= 0) goto end;
                break;

            case SSL_ST_OK:
                                #ifdef DEBUG
				log_time("Handshake OK\n", &currTime, &prevTime, &originTime); 
				#endif
                next_st->state=SSL_ST_OK;
                /* clean a few things up */
                ssl3_cleanup_key_block(s);
                ssl3_cleanup_key_block(next_st);

                BUF_MEM_free(s->init_buf);
                s->init_buf=NULL;
                BUF_MEM_free(next_st->init_buf);
                next_st->init_buf=NULL;

                /* remove buffering on output */
                ssl_free_wbio_buffer(s);
                if (!(next_st->s3->flags & SSL3_FLAGS_POP_BUFFER))
                ssl_free_wbio_buffer(next_st);

                s->init_num=0;

                /* skipped if we just sent a HelloRequest */
                if (s->renegotiate == 2) {
                    s->renegotiate=0;
                    s->new_session=0;

                    ssl_update_cache(s,SSL_SESS_CACHE_SERVER);

                    s->ctx->stats.sess_accept_good++;
                    /* s->server=1; */
                    s->handshake_func=spp_proxy_accept;

                    if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
                }
                
                next_st->init_num=0;
                next_st->renegotiate=0;
                next_st->new_session=0;

                next_st->handshake_func=spp_proxy_connect;

                ret = 1;
                goto end;
                /* break; */

            default:
                SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
                ret= -1;
                goto end;
                /* break; */
        }
		
        if (!s->s3->tmp.reuse_message && !skip) {
            if (s->debug) {
                if ((ret=BIO_flush(s->wbio)) <= 0)
                    goto end;
            }

            if ((cb != NULL) && (s->state != state)) {
                new_state=s->state;
                s->state=state;
                cb(s,SSL_CB_ACCEPT_LOOP,1);
                s->state=new_state;
            }
        }
        skip=0;
    }
end:
                    #ifdef DEBUG
                    log_time("Handshake end\n", &currTime, &prevTime, &originTime); 
                    #endif
    /* BIO_flush(s->wbio); */

    s->s3->change_cipher_spec=0;
    s->in_handshake--;
    if (next_st != NULL) {
        next_st->in_handshake--;
        next_st->s3->change_cipher_spec=0;
    }
    if (cb != NULL)
            cb(s,SSL_CB_ACCEPT_EXIT,ret);
    return(ret);
}