* underlying buffer gets freed
*/
__owur static inline int PACKET_peek_bytes(const PACKET *pkt, unsigned char **data,
- size_t len)
+ size_t len)
{
if (PACKET_remaining(pkt) < len)
return 0;
return 1;
}
+/*
+ * Copy packet data to |dest|, and set |len| to the number of copied bytes.
+ * If the packet has more than |dest_len| bytes, nothing is copied.
+ * Returns 1 if the packet data fits in |dest_len| bytes, 0 otherwise.
+ * Does not forward PACKET position (because it is typically the last thing
+ * done with a given PACKET).
+ */
+__owur static inline int PACKET_copy_all(const PACKET *pkt, unsigned char *dest,
+ size_t dest_len, size_t *len) {
+ if (PACKET_remaining(pkt) > dest_len) {
+ *len = 0;
+ return 0;
+ }
+ *len = pkt->remaining;
+ memcpy(dest, pkt->curr, pkt->remaining);
+ return 1;
+}
+
/*
* Copy |pkt| bytes to a newly allocated buffer and store a pointer to the
* result in |*data|, and the length in |len|.
/* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
unsigned char cipher[SSLV2_CIPHER_LEN];
- /*
- * Can this ever happen?
- * This method used to check for s->s3, but did so inconsistently.
- */
- if (s->s3 == NULL) {
- *al = SSL_AD_INTERNAL_ERROR;
- return NULL;
- }
-
s->s3->send_connection_binding = 0;
n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
!(s->session_ctx->session_cache_mode &
SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
SSL_SESSION data;
+ size_t local_len;
data.ssl_version = s->version;
- data.session_id_length = len;
- if (len == 0)
- return 0;
- memcpy(data.session_id, PACKET_data(session_id), len);
+ if (!PACKET_copy_all(session_id, data.session_id,
+ sizeof(data.session_id),
+ &local_len)) {
+ goto err;
+ }
+ data.session_id_length = local_len;
CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
if (ret != NULL) {
return 1;
}
+static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
+{
+ unsigned char dup[BUF_LEN];
+ PACKET pkt;
+ size_t len;
+
+ if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
+ || !PACKET_copy_all(&pkt, dup, BUF_LEN, &len)
+ || len != BUF_LEN
+ || memcmp(buf, dup, BUF_LEN) != 0
+ || PACKET_remaining(&pkt) != BUF_LEN
+ || PACKET_copy_all(&pkt, dup, BUF_LEN - 1, &len)) {
+ fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
+ return 0;
+ }
+
+ return 1;
+}
+
static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
{
unsigned char *data = NULL;
unsigned char buf[BUF_LEN];
PACKET pkt;
- /* Also tests PACKET_get_len() */
+ /* Also tests PACKET_remaining() */
if ( !PACKET_buf_init(&pkt, buf, 4)
|| PACKET_remaining(&pkt) != 4
|| !PACKET_buf_init(&pkt, buf, BUF_LEN)
PACKET pkt;
PACKET_null_init(&pkt);
- /* Also tests PACKET_get_len() */
if ( PACKET_remaining(&pkt) != 0
|| PACKET_forward(&pkt, 1)) {
fprintf(stderr, "test_PACKET_null_init() failed\n");
|| !test_PACKET_get_sub_packet(buf)
|| !test_PACKET_get_bytes(buf)
|| !test_PACKET_copy_bytes(buf)
+ || !test_PACKET_copy_all(buf)
|| !test_PACKET_memdup(buf)
|| !test_PACKET_strndup()
|| !test_PACKET_forward(buf)