#include <openssl/ssl.h>
#include <openssl/bytestring.h>
#include <openssl/err.h>
#include <openssl/mem.h>
#include "internal.h"
BSSL_NAMESPACE_BEGIN
bool ssl_transfer_supported(const SSL *in) {
if (in == NULL) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_UNSUPPORTED);
return false;
}
if (!SSL_is_server(in) || SSL_is_dtls(in) || in->quic_method != nullptr || !in->s3 || !in->s3->established_session ||
SSL_in_init(in) || in->version != TLS1_2_VERSION || in->s3->unreported_bytes_written > 0 || in->s3->pending_write.size() > 0 ||
in->s3->read_shutdown != ssl_shutdown_none || in->s3->write_shutdown != ssl_shutdown_none) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_UNSUPPORTED);
return false;
}
return true;
}
BSSL_NAMESPACE_END
using namespace bssl;
static bool SSL3_STATE_parse_octet_string(CBS *cbs, Array<uint8_t> *out,
unsigned tag) {
CBS value;
if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return false;
}
return out->CopyFrom(value);
}
static int parse_optional_string(CBS *cbs, UniquePtr<char> *out, unsigned tag, int reason) {
CBS value;
int present;
if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
OPENSSL_PUT_ERROR(SSL, reason);
return 0;
}
if (present) {
if (CBS_contains_zero_byte(&value)) {
OPENSSL_PUT_ERROR(SSL, reason);
return 0;
}
char *raw = nullptr;
if (!CBS_strdup(&value, &raw)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
out->reset(raw);
} else {
out->reset();
}
return 1;
}
static bool SSL3_STATE_get_optional_octet_string(CBS *cbs, void *dst,
unsigned tag,
size_t target_len) {
int present;
CBS value;
if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return false;
}
if (!present) {
return true;
}
size_t data_len = CBS_len(&value);
if (target_len > 0 && data_len != target_len) {
return false;
}
OPENSSL_memcpy(dst, CBS_data(&value), data_len);
return true;
}
static const unsigned kS3Version = 1;
static const unsigned kS3EstablishedSessionTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
static const unsigned kS3SessionReusedTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
static const unsigned kS3HostNameTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
static const unsigned kS3ALPNSelectedTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
static const unsigned kS3NextProtoNegotiatedTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
static const unsigned kS3ChannelIdValidTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
static const unsigned kS3ChannelIdTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
static const unsigned kS3SendConnectionBindingTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 7;
static const unsigned kS3PendingAppDataTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
static const unsigned kS3ReadBufferTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
static const unsigned kS3NotResumableTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
static int SSL3_STATE_to_bytes(SSL3_STATE *in, CBB *cbb) {
if (in == NULL || cbb == NULL) {
return 0;
}
CBB s3, child, child2;
if (!CBB_add_asn1(cbb, &s3, CBS_ASN1_SEQUENCE) ||
!CBB_add_asn1_uint64(&s3, kS3Version) ||
!CBB_add_asn1_octet_string(&s3, in->read_sequence, TLS_SEQ_NUM_SIZE) ||
!CBB_add_asn1_octet_string(&s3, in->write_sequence, TLS_SEQ_NUM_SIZE) ||
!CBB_add_asn1_octet_string(&s3, in->server_random, SSL3_RANDOM_SIZE) ||
!CBB_add_asn1_octet_string(&s3, in->client_random, SSL3_RANDOM_SIZE) ||
!CBB_add_asn1_octet_string(&s3, in->send_alert, SSL3_SEND_ALERT_SIZE) ||
!CBB_add_asn1_int64(&s3, in->rwstate) ||
!CBB_add_asn1_int64(&s3, in->early_data_reason) ||
!CBB_add_asn1_octet_string(&s3, in->previous_client_finished, PREV_FINISHED_MAX_SIZE) ||
!CBB_add_asn1_uint64(&s3, in->previous_client_finished_len) ||
!CBB_add_asn1_octet_string(&s3, in->previous_server_finished, PREV_FINISHED_MAX_SIZE) ||
!CBB_add_asn1_uint64(&s3, in->previous_server_finished_len) ||
!CBB_add_asn1_uint64(&s3, in->empty_record_count) ||
!CBB_add_asn1_uint64(&s3, in->warning_alert_count) ||
!CBB_add_asn1_uint64(&s3, in->total_renegotiations)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!CBB_add_asn1(&s3, &child, kS3EstablishedSessionTag) ||
!ssl_session_serialize(in->established_session.get(), &child)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
if (in->session_reused) {
if (!CBB_add_asn1(&s3, &child, kS3SessionReusedTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (in->hostname) {
if (!CBB_add_asn1(&s3, &child, kS3HostNameTag) ||
!CBB_add_asn1_octet_string(&child,
(const uint8_t *)(in->hostname.get()),
strlen(in->hostname.get()))) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (!in->alpn_selected.empty()) {
if (!CBB_add_asn1(&s3, &child, kS3ALPNSelectedTag) ||
!CBB_add_asn1_octet_string(&child, in->alpn_selected.data(),
in->alpn_selected.size())) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (!in->next_proto_negotiated.empty()) {
if (!CBB_add_asn1(&s3, &child, kS3NextProtoNegotiatedTag) ||
!CBB_add_asn1_octet_string(&child, in->next_proto_negotiated.data(),
in->next_proto_negotiated.size())) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (in->channel_id_valid) {
if (!CBB_add_asn1(&s3, &child, kS3ChannelIdValidTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!CBB_add_asn1(&s3, &child, kS3ChannelIdTag) ||
!CBB_add_asn1_octet_string(&child, in->channel_id, SSL3_CHANNEL_ID_SIZE)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (in->send_connection_binding) {
if (!CBB_add_asn1(&s3, &child, kS3SendConnectionBindingTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (!in->pending_app_data.empty()) {
if (!in->read_buffer.buf_ptr() || in->read_buffer.buf_ptr() > in->pending_app_data.data()) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return 0;
}
uint64_t offset = in->pending_app_data.data() - in->read_buffer.buf_ptr();
if (!CBB_add_asn1(&s3, &child, kS3PendingAppDataTag) ||
!CBB_add_asn1(&child, &child2, CBS_ASN1_SEQUENCE) ||
!CBB_add_asn1_uint64(&child2, offset) ||
!CBB_add_asn1_uint64(&child2, in->pending_app_data.size())) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (!in->pending_app_data.empty() ||
!in->read_buffer.empty()) {
if (!CBB_add_asn1(&s3, &child, kS3ReadBufferTag) ||
!in->read_buffer.DoSerialization(&child)) {
return 0;
}
}
if (in->established_session.get()->not_resumable) {
if (!CBB_add_asn1(&s3, &child, kS3NotResumableTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
return CBB_flush(cbb);
}
static int SSL3_STATE_parse_session(CBS *cbs, UniquePtr<SSL_SESSION> *out, const SSL_CTX *ctx) {
CBS value;
int present;
if (!CBS_get_optional_asn1(cbs, &value, &present, kS3EstablishedSessionTag)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return 0;
}
if (present) {
UniquePtr<SSL_SESSION> ptr =
SSL_SESSION_parse(&value, ctx->x509_method, ctx->pool);
if (!ptr) {
return 0;
}
out->reset(ptr.release());
return 1;
} else {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
out->reset();
return 0;
}
}
static int SSL3_STATE_from_bytes(SSL *ssl, CBS *cbs, const SSL_CTX *ctx) {
SSL3_STATE *out = ssl->s3;
CBS s3, read_seq, write_seq, server_random, client_random, send_alert, pending_app_data, read_buffer;
CBS previous_client_finished, previous_server_finished;
int session_reused, channel_id_valid, send_connection_binding, not_resumable;
uint64_t version, early_data_reason, previous_client_finished_len, previous_server_finished_len;
uint64_t empty_record_count, warning_alert_count, total_renegotiations;
int64_t rwstate;
int pending_app_data_present, read_buffer_present;
if (!CBS_get_asn1(cbs, &s3, CBS_ASN1_SEQUENCE) ||
!CBS_get_asn1_uint64(&s3, &version) ||
version != kS3Version ||
!CBS_get_asn1(&s3, &read_seq, CBS_ASN1_OCTETSTRING) ||
CBS_len(&read_seq) != TLS_SEQ_NUM_SIZE ||
!CBS_get_asn1(&s3, &write_seq, CBS_ASN1_OCTETSTRING) ||
CBS_len(&write_seq) != TLS_SEQ_NUM_SIZE ||
!CBS_get_asn1(&s3, &server_random, CBS_ASN1_OCTETSTRING) ||
CBS_len(&server_random) != SSL3_RANDOM_SIZE ||
!CBS_get_asn1(&s3, &client_random, CBS_ASN1_OCTETSTRING) ||
CBS_len(&client_random) != SSL3_RANDOM_SIZE ||
!CBS_get_asn1(&s3, &send_alert, CBS_ASN1_OCTETSTRING) ||
CBS_len(&send_alert) != SSL3_SEND_ALERT_SIZE ||
!CBS_get_asn1_int64(&s3, &rwstate) ||
!CBS_get_asn1_uint64(&s3, &early_data_reason) ||
early_data_reason > ssl_early_data_reason_max_value ||
!CBS_get_asn1(&s3, &previous_client_finished, CBS_ASN1_OCTETSTRING) ||
CBS_len(&previous_client_finished) != PREV_FINISHED_MAX_SIZE ||
!CBS_get_asn1_uint64(&s3, &previous_client_finished_len) ||
previous_client_finished_len > PREV_FINISHED_MAX_SIZE ||
!CBS_get_asn1(&s3, &previous_server_finished, CBS_ASN1_OCTETSTRING) ||
CBS_len(&previous_server_finished) != PREV_FINISHED_MAX_SIZE ||
!CBS_get_asn1_uint64(&s3, &previous_server_finished_len) ||
previous_server_finished_len > PREV_FINISHED_MAX_SIZE ||
!CBS_get_asn1_uint64(&s3, &empty_record_count) ||
!CBS_get_asn1_uint64(&s3, &warning_alert_count) ||
!CBS_get_asn1_uint64(&s3, &total_renegotiations) ||
!SSL3_STATE_parse_session(&s3, &(out->established_session), ctx) ||
!CBS_get_optional_asn1_bool(&s3, &session_reused, kS3SessionReusedTag, 0 ) ||
!parse_optional_string(&s3, &(out->hostname), kS3HostNameTag, SSL_R_SERIALIZATION_INVALID_SSL3_STATE) ||
!SSL3_STATE_parse_octet_string(&s3, &(out->alpn_selected), kS3ALPNSelectedTag) ||
!SSL3_STATE_parse_octet_string(&s3, &(out->next_proto_negotiated), kS3NextProtoNegotiatedTag) ||
!CBS_get_optional_asn1_bool(&s3, &channel_id_valid, kS3ChannelIdValidTag, 0 ) ||
!SSL3_STATE_get_optional_octet_string(&s3, out->channel_id, kS3ChannelIdTag, SSL3_CHANNEL_ID_SIZE) ||
!CBS_get_optional_asn1_bool(&s3, &send_connection_binding, kS3SendConnectionBindingTag, 0 ) ||
!CBS_get_optional_asn1(&s3, &pending_app_data, &pending_app_data_present, kS3PendingAppDataTag) ||
!CBS_get_optional_asn1(&s3, &read_buffer, &read_buffer_present, kS3ReadBufferTag) ||
!CBS_get_optional_asn1_bool(&s3, ¬_resumable, kS3NotResumableTag, 0 ) ||
CBS_len(&s3) != 0) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return 0;
}
if (read_buffer_present && !out->read_buffer.DoDeserialization(&read_buffer)) {
return 0;
}
if (pending_app_data_present) {
if (!read_buffer_present) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return 0;
}
CBS app_seq;
uint64_t pending_app_data_offset, pending_app_data_size;
if (!CBS_get_asn1(&pending_app_data, &app_seq, CBS_ASN1_SEQUENCE) ||
!CBS_get_asn1_uint64(&app_seq, &pending_app_data_offset) ||
!CBS_get_asn1_uint64(&app_seq, &pending_app_data_size)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return 0;
}
if (pending_app_data_size > out->read_buffer.buf_size()) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE);
return 0;
}
out->pending_app_data = MakeSpan(out->read_buffer.buf_ptr() + pending_app_data_offset, pending_app_data_size);
}
out->hs.reset();
out->have_version = true;
OPENSSL_memcpy(out->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
OPENSSL_memcpy(out->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
SSL_SESSION *session = out->established_session.get();
Array<uint8_t> key_block1, key_block2;
if (!tls1_configure_aead(ssl, evp_aead_seal, &key_block1, session, {})) {
return 0;
}
if (!tls1_configure_aead(ssl, evp_aead_open, &key_block2, session, {})) {
return 0;
}
OPENSSL_memcpy(out->read_sequence, CBS_data(&read_seq), TLS_SEQ_NUM_SIZE);
OPENSSL_memcpy(out->write_sequence, CBS_data(&write_seq), TLS_SEQ_NUM_SIZE);
OPENSSL_memcpy(out->send_alert, CBS_data(&send_alert), SSL3_SEND_ALERT_SIZE);
OPENSSL_memcpy(out->previous_client_finished, CBS_data(&previous_client_finished), PREV_FINISHED_MAX_SIZE);
OPENSSL_memcpy(out->previous_server_finished, CBS_data(&previous_server_finished), PREV_FINISHED_MAX_SIZE);
out->early_data_reason = static_cast<ssl_early_data_reason_t>(early_data_reason);
out->rwstate = rwstate;
out->session_reused = !!session_reused;
if (out->session_reused) {
ssl->session = UpRef(out->established_session);
}
out->channel_id_valid = !!channel_id_valid;
out->previous_client_finished_len = previous_client_finished_len;
out->previous_server_finished_len = previous_server_finished_len;
out->v2_hello_done = true;
out->initial_handshake_complete = true;
out->empty_record_count = empty_record_count;
out->warning_alert_count = warning_alert_count;
out->total_renegotiations = total_renegotiations;
out->send_connection_binding = !!send_connection_binding;
out->established_session.get()->not_resumable = !!not_resumable;
return 1;
}
static const unsigned kSSLConfigVersion = 1;
static const unsigned kSSLConfigOcspStaplingEnabledTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
static const unsigned kSSLConfigJdk11WorkaroundTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
static int SSL_CONFIG_to_bytes(SSL_CONFIG *in, CBB *cbb) {
if (in == NULL || cbb == NULL) {
return 0;
}
CBB config, child;
if (!CBB_add_asn1(cbb, &config, CBS_ASN1_SEQUENCE) ||
!CBB_add_asn1_uint64(&config, kSSLConfigVersion) ||
!CBB_add_asn1_uint64(&config, in->conf_max_version) ||
!CBB_add_asn1_uint64(&config, in->conf_min_version)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
if (in->ocsp_stapling_enabled) {
if (!CBB_add_asn1(&config, &child, kSSLConfigOcspStaplingEnabledTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (in->jdk11_workaround) {
if (!CBB_add_asn1(&config, &child, kSSLConfigJdk11WorkaroundTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
return CBB_flush(cbb);
}
static int SSL_CONFIG_from_bytes(SSL_CONFIG *out, CBS *cbs) {
CBS config;
int ocsp_stapling_enabled, jdk11_workaround;
uint64_t version, conf_max_version, conf_min_version;
if (!CBS_get_asn1(cbs, &config, CBS_ASN1_SEQUENCE) ||
!CBS_get_asn1_uint64(&config, &version) ||
version != kSSLConfigVersion ||
!CBS_get_asn1_uint64(&config, &conf_max_version) ||
!CBS_get_asn1_uint64(&config, &conf_min_version) ||
!CBS_get_optional_asn1_bool(&config, &ocsp_stapling_enabled, kSSLConfigOcspStaplingEnabledTag, 0 ) ||
!CBS_get_optional_asn1_bool(&config, &jdk11_workaround, kSSLConfigJdk11WorkaroundTag, 0 ) ||
CBS_len(&config) != 0) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_CONFIG);
return 0;
}
out->conf_max_version = conf_max_version;
out->conf_min_version = conf_min_version;
out->ocsp_stapling_enabled = !!ocsp_stapling_enabled;
out->jdk11_workaround = !!jdk11_workaround;
out->handoff = false;
out->shed_handshake_config = false;
return 1;
}
#define SSL_SERIAL_VERSION 1
static const unsigned kSSLQuietShutdownTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
static const unsigned kSSLConfigTag =
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
static int SSL_to_bytes_full(const SSL *in, CBB *cbb) {
CBB ssl, child;
if (!CBB_add_asn1(cbb, &ssl, CBS_ASN1_SEQUENCE) ||
!CBB_add_asn1_uint64(&ssl, SSL_SERIAL_VERSION) ||
!CBB_add_asn1_uint64(&ssl, in->version) ||
!CBB_add_asn1_uint64(&ssl, in->max_send_fragment) ||
!SSL3_STATE_to_bytes(in->s3, &ssl) ||
!CBB_add_asn1_uint64(&ssl, in->mode) ||
!CBB_add_asn1_uint64(&ssl, in->options)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
if (in->quiet_shutdown) {
if (!CBB_add_asn1(&ssl, &child, kSSLQuietShutdownTag) ||
!CBB_add_asn1_bool(&child, true)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
if (in->config) {
if (!CBB_add_asn1(&ssl, &child, kSSLConfigTag) ||
!SSL_CONFIG_to_bytes(in->config.get(), &child)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
return 0;
}
}
return CBB_flush(cbb);
}
static int SSL_parse(SSL *ssl, CBS *cbs, SSL_CTX *ctx) {
CBS ssl_cbs, ssl_config;
uint64_t ssl_serial_ver, version, max_send_fragment, mode, options;
int quiet_shutdown;
int ssl_config_present = 0;
if (!CBS_get_asn1(cbs, &ssl_cbs, CBS_ASN1_SEQUENCE) ||
CBS_len(cbs) != 0 ||
!CBS_get_asn1_uint64(&ssl_cbs, &ssl_serial_ver)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return 0;
}
assert(ssl_serial_ver <= SSL_SERIAL_VERSION);
if (!CBS_get_asn1_uint64(&ssl_cbs, &version) ||
!CBS_get_asn1_uint64(&ssl_cbs, &max_send_fragment)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return 0;
}
ssl->version = version;
ssl->max_send_fragment = max_send_fragment;
SSL_set_accept_state(ssl);
if (!SSL3_STATE_from_bytes(ssl, &ssl_cbs, ssl->ctx.get())) {
return 0;
}
if (!CBS_get_asn1_uint64(&ssl_cbs, &mode) ||
!CBS_get_asn1_uint64(&ssl_cbs, &options)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return 0;
}
ssl->mode = mode;
ssl->options = options;
if (!CBS_get_optional_asn1_bool(&ssl_cbs, &quiet_shutdown, kSSLQuietShutdownTag, 0 )) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return 0;
}
ssl->quiet_shutdown = !!quiet_shutdown;
if (!CBS_get_optional_asn1(&ssl_cbs, &ssl_config, &ssl_config_present, kSSLConfigTag)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return 0;
}
if (ssl_config_present) {
if (!SSL_CONFIG_from_bytes(ssl->config.get(), &ssl_config)) {
return 0;
}
} else {
ssl->config.reset();
}
if (CBS_len(&ssl_cbs) != 0) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return 0;
}
return 1;
}
SSL *SSL_from_bytes(const uint8_t *in, size_t in_len, SSL_CTX *ctx) {
if (!in || !in_len || !ctx) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_UNSUPPORTED);
return NULL;
}
SSL *ssl = SSL_new(ctx);
if (ssl == NULL) {
return NULL;
}
UniquePtr<SSL> ret(ssl);
CBS cbs, seq;
CBS_init(&cbs, in, in_len);
if (!CBS_get_asn1(&cbs, &seq, CBS_ASN1_SEQUENCE) ||
(CBS_len(&cbs) != 0)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL);
return NULL;
}
if (!SSL_parse(ret.get(), &seq, ctx)) {
return NULL;
}
return ret.release();
}
int SSL_to_bytes(const SSL *in, uint8_t **out_data, size_t *out_len) {
if (!ssl_transfer_supported(in)) {
return 0;
}
ScopedCBB cbb;
CBB seq;
if (!CBB_init(cbb.get(), 1024) ||
!CBB_add_asn1(cbb.get(), &seq, CBS_ASN1_SEQUENCE) ||
!SSL_to_bytes_full(in, &seq)) {
return 0;
}
return CBB_finish(cbb.get(), out_data, out_len);
}