picoquic/tls_api.c
static int picoquic_server_encrypt_retry_token(picoquic_quic_t * quic, const struct sockaddr * addr_peer,
    int is_new_token,
    uint8_t * token, size_t * token_length, size_t token_max, const uint8_t * text, size_t text_length)
{
    int ret = 0;
    uint64_t sequence;
    uint8_t* auth_data;
    size_t auth_data_length;

    if (text_length + 1u + 16u > token_max) {
        ret = -1;
        *token_length = 0;
    }
    else {

        if (addr_peer->sa_family == AF_INET) {
            auth_data = (uint8_t*)&((struct sockaddr_in*)addr_peer)->sin_addr;
            auth_data_length = 4;
        }
        else {
            auth_data = (uint8_t*)&((struct sockaddr_in6*)addr_peer)->sin6_addr;
            auth_data_length = 16;
        }
        picoquic_crypto_random(quic, token, 8);
        if (is_new_token) {
            token[0] |= 0x80;
        }
        else {
            token[0] &= 0x7F;
        }
        sequence = PICOPARSE_64(token);

        *token_length = (size_t)8u + picoquic_aead_encrypt_generic(token + 8, text, text_length,
            sequence, auth_data, auth_data_length, quic->aead_encrypt_ticket_ctx);
    }

    return ret;
}
/* 
 * Create or verify a token. Tokens are tied to an IP address and a time of
 * issue, and come in two variations:
 * - specific tokens are tied to an Original DCID.
 * - generic tokens work with a zero length DCID.
 * The structure of the token is:
 * - time valid until: uint64_t
 * - ODCID length, one byte
 * - ODCID, length bytes
 * This is encrypted using the same AEAD contexts as the encryption of session tickets.
 * The encrypted structure is:
 * - 64 bit random sequence number.
 * - Encrypted value of the token.
 * - AEAD checksum.
 * The most significant bit of the random number is set to 1 (0x80) for a "new token",
 * and to zero for a "retry token".
 * When invoking AEAD, the sequence number is used to update the IV, and the IP address
 * is passed as "authenticated" data. The 64 bit random number alleviates the concern of
 * reusing the same AEAD key twice. The authenticated data ensures that if the token is
 * used from a different address, the decryption will fail.
 */
Create or verify a token. Tokens are tied to an IP address and a time of
issue, and come in two variations:
- specific tokens are tied to an Original DCID.
- generic tokens work with a zero length DCID.
The structure of the token is:
- time valid until: uint64_t
- ODCID length, one byte
- ODCID, length bytes
This is encrypted using the same AEAD contexts as the encryption of session tickets.
The encrypted structure is:
- 64 bit random sequence number.
- Encrypted value of the token.
- AEAD checksum.
The most significant bit of the random number is set to 1 (0x80) for a "new token",
and to zero for a "retry token".
When invoking AEAD, the sequence number is used to update the IV, and the IP address
is passed as "authenticated" data. The 64 bit random number alleviates the concern of
reusing the same AEAD key twice. The authenticated data ensures that if the token is
used from a different address, the decryption will fail.
Create or verify a token . Tokens are tied to an IP address and a time of issue and come in two variations : - specific tokens are tied to an Original DCID . - generic tokens work with a zero length DCID . The structure of the token is : - time valid until : uint64_t - ODCID length one byte - ODCID length bytes This is encrypted using the same AEAD contexts as the encryption of session tickets . The encrypted structure is : - 64 bit random sequence number . - Encrypted value of the token . - AEAD checksum . The most significant bit of the random number is set to 1 ( 0x80 ) for a " new token " and to zero for a " retry token " . When invoking AEAD the sequence number is used to update the IV and the IP address is passed as " authenticated " data . The 64 bit random number alleviates the concern of reusing the same AEAD key twice . The authenticated data ensures that if the token is used from a different address the decryption will fail .
static int picoquic_server_encrypt_retry_token(picoquic_quic_t * quic, const struct sockaddr * addr_peer,
    int is_new_token,
    uint8_t * token, size_t * token_length, size_t token_max, const uint8_t * text, size_t text_length)
{
    int ret = 0;
    uint64_t sequence;
    uint8_t* auth_data;
    size_t auth_data_length;
    if (text_length + 1u + 16u > token_max) {
        ret = -1;
        *token_length = 0;
    }
    else {
        if (addr_peer->sa_family == AF_INET) {
            auth_data = (uint8_t*)&((struct sockaddr_in*)addr_peer)->sin_addr;
            auth_data_length = 4;
        }
        else {
            auth_data = (uint8_t*)&((struct sockaddr_in6*)addr_peer)->sin6_addr;
            auth_data_length = 16;
        }
        picoquic_crypto_random(quic, token, 8);
        if (is_new_token) {
            token[0] |= 0x80;
        }
        else {
            token[0] &= 0x7F;
        }
        sequence = PICOPARSE_64(token);
        *token_length = (size_t)8u + picoquic_aead_encrypt_generic(token + 8, text, text_length,
            sequence, auth_data, auth_data_length, quic->aead_encrypt_ticket_ctx);
    }
    return ret;
}
static int picoquic_server_encrypt_retry_token ( picoquic_quic_t * quic , const struct sockaddr * addr_peer , int is_new_token , uint8_t * token , size_t * token_length , size_t token_max , const uint8_t * text , size_t text_length ) { int ret = 0 ; uint64_t sequence ; uint8_t * auth_data ; size_t auth_data_length ; if ( text_length + 1u + 16u > token_max ) { ret = -1 ; * token_length = 0 ; } else { if ( addr_peer -> sa_family == AF_INET ) { auth_data = ( uint8_t * ) & ( ( struct sockaddr_in * ) addr_peer ) -> sin_addr ; auth_data_length = 4 ; } else { auth_data = ( uint8_t * ) & ( ( struct sockaddr_in6 * ) addr_peer ) -> sin6_addr ; auth_data_length = 16 ; } picoquic_crypto_random ( quic , token , 8 ) ; if ( is_new_token ) { token [ 0 ] |= 0x80 ; } else { token [ 0 ] &= 0x7F ; } sequence = PICOPARSE_64 ( token ) ; * token_length = ( size_t ) 8u + picoquic_aead_encrypt_generic ( token + 8 , text , text_length , sequence , auth_data , auth_data_length , quic -> aead_encrypt_ticket_ctx ) ; } return ret ; }
Create or verify a token.
Create or verify a token .