Skip to content

Commit

Permalink
Implement a human readable state function for the record layer
Browse files Browse the repository at this point in the history
This allows querying of the record layer to get a human readable state
string out. This resolves two outstanding TODO comments and enables us
to remove the rstate variable from s->rlayer.

Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #18132)
  • Loading branch information
mattcaswell committed Aug 18, 2022
1 parent d4ee345 commit d0b17ea
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 28 deletions.
3 changes: 2 additions & 1 deletion ssl/record/methods/dtls_meth.c
Expand Up @@ -728,5 +728,6 @@ const OSSL_RECORD_METHOD ossl_dtls_record_method = {
NULL,
tls_set_first_handshake,
tls_set_max_pipelines,
dtls_set_in_init
dtls_set_in_init,
tls_get_state
};
3 changes: 2 additions & 1 deletion ssl/record/methods/ktls_meth.c
Expand Up @@ -542,5 +542,6 @@ const OSSL_RECORD_METHOD ossl_ktls_record_method = {
tls_set_plain_alerts,
tls_set_first_handshake,
tls_set_max_pipelines,
NULL
NULL,
tls_get_state
};
2 changes: 2 additions & 0 deletions ssl/record/methods/recmethod_local.h
Expand Up @@ -284,4 +284,6 @@ int tls_set_protocol_version(OSSL_RECORD_LAYER *rl, int version);
void tls_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow);
void tls_set_first_handshake(OSSL_RECORD_LAYER *rl, int first);
void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
const char **longstr);
int rlayer_setup_read_buffer(OSSL_RECORD_LAYER *rl);
26 changes: 25 additions & 1 deletion ssl/record/methods/tls_common.c
Expand Up @@ -1341,6 +1341,29 @@ void tls_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
rl->read_ahead = 1;
}

void tls_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
const char **longstr)
{
const char *shrt, *lng;
switch (rl->rstate) {
case SSL_ST_READ_HEADER:
shrt = "RH";
lng = "read header";
break;
case SSL_ST_READ_BODY:
shrt = "RB";
lng = "read body";
break;
default:
shrt = lng = "unknown";
break;
}
if (shortstr != NULL)
*shortstr = shrt;
if (longstr != NULL)
*longstr = lng;
}

const OSSL_RECORD_METHOD ossl_tls_record_method = {
tls_new_record_layer,
tls_free,
Expand All @@ -1361,5 +1384,6 @@ const OSSL_RECORD_METHOD ossl_tls_record_method = {
tls_set_plain_alerts,
tls_set_first_handshake,
tls_set_max_pipelines,
NULL
NULL,
tls_get_state
};
35 changes: 12 additions & 23 deletions ssl/record/rec_layer_s3.c
Expand Up @@ -34,8 +34,6 @@ void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s)

void RECORD_LAYER_clear(RECORD_LAYER *rl)
{
rl->rstate = SSL_ST_READ_HEADER;

rl->wnum = 0;
memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
rl->handshake_fragment_len = 0;
Expand Down Expand Up @@ -141,43 +139,34 @@ void SSL_set_default_read_buffer_len(SSL *s, size_t len)
const char *SSL_rstate_string_long(const SSL *s)
{
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
const char *lng;

if (sc == NULL)
return NULL;

/* TODO(RECLAYER): Fix me */
switch (sc->rlayer.rstate) {
case SSL_ST_READ_HEADER:
return "read header";
case SSL_ST_READ_BODY:
return "read body";
case SSL_ST_READ_DONE:
return "read done";
default:
if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL)
return "unknown";
}

sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, NULL, &lng);

return lng;
}

const char *SSL_rstate_string(const SSL *s)
{
const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
const char *shrt;

if (sc == NULL)
return NULL;

/* TODO(RECLAYER): Fix me */
switch (sc->rlayer.rstate) {
case SSL_ST_READ_HEADER:
return "RH";
case SSL_ST_READ_BODY:
return "RB";
case SSL_ST_READ_DONE:
return "RD";
default:
if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL)
return "unknown";
}
}

sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, &shrt, NULL);

return shrt;
}

/*
* Call this to write data in records of type 'type' It will return <= 0 if
Expand Down
2 changes: 0 additions & 2 deletions ssl/record/record.h
Expand Up @@ -163,8 +163,6 @@ typedef struct record_layer_st {
* non-blocking reads)
*/
int read_ahead;
/* where we are when reading */
int rstate;
/* How many pipelines can be used to write data */
size_t numwpipes;
/* write IO goes into here */
Expand Down
6 changes: 6 additions & 0 deletions ssl/record/recordmethod.h
Expand Up @@ -314,6 +314,12 @@ struct ossl_record_method_st {
* not. Default at creation of the record layer is "yes".
*/
void (*set_in_init)(OSSL_RECORD_LAYER *rl, int in_init);

/*
* Get a short or long human readable description of the record layer state
*/
void (*get_state)(OSSL_RECORD_LAYER *rl, const char **shortstr,
const char **longstr);
};


Expand Down

0 comments on commit d0b17ea

Please sign in to comment.