spp_pkt.c 47.4 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
        
        if (mac_size < 0)
            goto err;
    }

    /* 'create_empty_fragment' is true only when this function calls itself */
    /*if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
        /* countermeasure against known-IV weakness in CBC ciphersuites
         * (see http://www.openssl.org/~bodo/tls-cbc.txt) */

        /*if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
            /* recursive function call with 'create_empty_fragment' set;
             * this prepares and buffers the data for an empty fragment
             * (these 'prefix_len' bytes are sent out later
             * together with the actual payload) */
            /*prefix_len = do_spp_write(s, type, buf, 0, 1);
            if (prefix_len <= 0)
                    goto err;

            if (prefix_len > (SPP_RT_HEADER_LENGTH + 
                SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
                    /* insufficient space */
                    /*SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
                    goto err;
            }
        }

        s->s3->empty_fragment_done = 1;
    }*/

    if (create_empty_fragment) {
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
        /* extra fragment would be couple of cipher blocks,
         * which would be multiple of SSL3_ALIGN_PAYLOAD, so
         * if we want to align the real payload, then we can
         * just pretent we simply have two headers. */
        align = (long)wb->buf + 2*SPP_RT_HEADER_LENGTH;
        align = (-align)&(SSL3_ALIGN_PAYLOAD-1);
#endif
        p = wb->buf + align;
        wb->offset  = align;
    } else if (prefix_len) {
        p = wb->buf + wb->offset + prefix_len;
    } else {
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
        align = (long)wb->buf + SPP_RT_HEADER_LENGTH;
        align = (-align)&(SSL3_ALIGN_PAYLOAD-1);
#endif
        p = wb->buf + align;
        wb->offset  = align;
    }

    /* write the header */

    *(p++)=type&0xff;
    wr->type=type;

    *(p++)=(s->version>>8);
    *(p++)=s->version&0xff;            

    /* field where we are to write out packet length */
    plen=p; 
    p+=2;

    /* Write the slice ID as the 4th byte of the header. */
    wr->slice_id = slice == NULL ? 0 : slice->slice_id;
    *(p++)=wr->slice_id;
    
    /* Stats */
    if (type == SSL3_RT_APPLICATION_DATA)
        s->write_stats.app_bytes += len;
    else if (type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC)
        s->write_stats.handshake_bytes += len;
    else if (type == SSL3_RT_ALERT)
        s->write_stats.alert_bytes += len;
    
    if (type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC)
        s->write_stats.handshake_bytes += SPP_RT_HEADER_LENGTH;
    else
        s->write_stats.header_bytes += SPP_RT_HEADER_LENGTH;
#ifdef DEBUG
    fprintf(stderr, "Writing record header: ");
    spp_print_buffer(wb->buf + wb->offset, SPP_RT_HEADER_LENGTH);
#endif
    
    /* Explicit IV length, block ciphers and TLS version 1.1 or later */
    if (s->enc_write_ctx && s->version >= TLS1_1_VERSION) {
        int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
        if (mode == EVP_CIPH_CBC_MODE) {
            eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
            if (eivlen <= 1)
                eivlen = 0;
        }
        /* Need explicit part of IV for GCM mode */
        else if (mode == EVP_CIPH_GCM_MODE)
            eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
        else
            eivlen = 0;
    } else {
        eivlen = 0;
    }

    /* lets setup the record stuff. */
    wr->data=p + eivlen;
    wr->length=(int)len;
    wr->input=(unsigned char *)buf;

    /* we now 'read' from wr->input, wr->length bytes into
     * wr->data */
    
/*#if DEBUG
    fprintf(stderr, "Decrypted packet:");
    spp_print_buffer(wr->input, wr->len);
#endif*/

    /* first we compress */
    if (s->compress != NULL) {
        if (!ssl3_do_compress(s)) {
            SSLerr(SSL_F_DO_SSL3_WRITE,SSL_R_COMPRESSION_FAILURE);
            goto err;
        }
    } else {
        memcpy(wr->data,wr->input,wr->length);
        wr->input=wr->data;
    }

    /* we should still have the output to wr->data and the input
     * from wr->input.  Length should be wr->length.
     * wr->data still points in the wb->buf */
    if (mac_size != 0 && slice != NULL) {
#ifdef DEBUG
        printf("Generating 3MAC\n");
#endif
        s->write_stats.mac_bytes += mac_size*3;
        /* Must have read access, so write the read MAC. */
        spp_copy_mac_state(s, slice->read_mac, 1);
        if (s->method->ssl3_enc->mac(s,&(p[wr->length + eivlen]),1) < 0)
                goto err;
        
        /* Compute the write hash. */
        if (slice->write_mac != NULL) {
#ifdef DEBUG
            printf("Generating write MAC\n");
#endif
            spp_copy_mac_state(s, slice->write_mac, 1);
            if (s->method->ssl3_enc->mac(s,&(p[wr->length + eivlen + mac_size]),1) < 0)
                goto err;
        } else {
            /* Copy from the previous record. */
            memcpy(&(p[wr->length + eivlen + mac_size]), spp_ctx->write_mac, mac_size);
        }
        /* Must be an end point, write the integrity hash. */
        if (s->def_ctx->read_access) {
#ifdef DEBUG
            printf("Generating integrity MAC\n");
#endif
            spp_copy_mac_state(s, s->def_ctx->read_mac, 1);
            if (s->method->ssl3_enc->mac(s,&(p[wr->length + eivlen + (mac_size*2)]),1) < 0)
                goto err;            
        } else {
            /* Copy from the previous record. */
            memcpy(&(p[wr->length + eivlen + (mac_size*2)]), spp_ctx->integrity_mac, mac_size);
        }        
        wr->length+=(mac_size*3);        
    } else if (mac_size != 0) {
        /* This will only happen when sending the finished message at the end of the handshake. 
         * Instead of using a slice, use the parameters computed via the standard TLS handshake to 
         * both encrypt and generate MAC. */
        s->write_stats.mac_bytes += mac_size;
        if (s->method->ssl3_enc->mac(s,&(p[wr->length + eivlen]),1) < 0)
            goto err;
	wr->length+=mac_size;
    }
    /* If we do not have read access, then the MACs were interpreted as part of the payload. */
    if (spp_ctx != NULL) {
        if (s->proxy) {
            OPENSSL_free(spp_ctx->read_mac);
        }
        OPENSSL_free(spp_ctx);
    }

    wr->input=p;
    wr->data=p;

    if (eivlen) {
/*	if (RAND_pseudo_bytes(p, eivlen) <= 0)
                goto err; */
        wr->length += eivlen;
    }
    
#ifdef DEBUG
    printf("Unencrypted packet: ");
    spp_print_buffer(wr->data, wr->length);
#endif

    /* ssl3_enc can only have an error on read */
    /* This is a call to spp_enc which will encrypt or not 
     * depending upon whether we have the encryption material. */
    s->method->ssl3_enc->enc(s,1);

#ifdef DEBUG
    printf("Encrypted packet: ");
    spp_print_buffer(wr->data, wr->length);
#endif
    
    s->write_stats.bytes += wr->length + SPP_RT_HEADER_LENGTH;
    
    /* record length after mac and block padding */
    s2n(wr->length,plen);

    /* we should now have
     * wr->data pointing to the encrypted data, which is
     * wr->length long */
    wr->type=type; /* not needed but helps for debugging */
    wr->length+=SPP_RT_HEADER_LENGTH;

    if (create_empty_fragment) {
        /* we are in a recursive call;
         * just return the length, don't write out anything here
         */
        return wr->length;
    }

    /* now let's set up wb */
    wb->left = prefix_len + wr->length;

    /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
    s->s3->wpend_tot=len;
    s->s3->wpend_buf=buf;
    s->s3->wpend_type=type;
    s->s3->wpend_ret=len;

done:
    /* we now just need to write the buffer */
    return ssl3_write_pending(s,type,buf,len);
    /* Does not need changing since above function simply writes buffer to 
     * the wire. */
err:
    return -1;
}

/* This function is not actually changed from ssl3_write_bytes, 
 * but we need to change do_write, so we copy this here as well. */
int spp_write_bytes(SSL *s, int type, const void *buf_, int len) {
    const unsigned char *buf=buf_;
    unsigned int n,nw;
    int i,tot;

    s->rwstate=SSL_NOTHING;
    OPENSSL_assert(s->s3->wnum <= INT_MAX);
    tot=s->s3->wnum;
    s->s3->wnum=0;

    if (SSL_in_init(s) && !s->in_handshake) {
	i=s->handshake_func(s);
	if (i < 0) return(i);
	if (i == 0) {
            SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
            return -1;
	}
    }

    /* ensure that if we end up with a smaller value of data to write 
     * out than the the original len from a write which didn't complete 
     * for non-blocking I/O and also somehow ended up avoiding 
     * the check for this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as
     * it must never be possible to end up with (len-tot) as a large
     * number that will then promptly send beyond the end of the users
     * buffer ... so we trap and report the error in a way the user
     * will notice
     */
    if (len < tot) {
        SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_BAD_LENGTH);
        return(-1);
    }    

    n=(len-tot);
    for (;;) {
	if (!s->proxy && n > s->max_send_fragment)
            nw=s->max_send_fragment;
        else
            nw=n;

        i=do_spp_write(s, type, &(buf[tot]), nw, 0);
	if (i <= 0) {
            s->s3->wnum=tot;
            return i;
	}

	if ((i == (int)n) ||
            (type == SSL3_RT_APPLICATION_DATA &&
            (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
		/* next chunk of data should get another prepended empty fragment
		 * in ciphersuites with known-IV weakness: */
		s->s3->empty_fragment_done = 0;
		return tot+i;
	}

	n-=i;
	tot+=i;
    }
}