AzureMXChip/MXCHIPAZ3166/core/lib/netxduo/addons/web/nx_tcpserver.c
UINT _nx_tcpserver_tls_setup(NX_TCPSERVER *server_ptr, const NX_SECURE_TLS_CRYPTO *crypto_table,
                             VOID *metadata_buffer, ULONG metadata_size, UCHAR* packet_buffer, UINT packet_buffer_size, NX_SECURE_X509_CERT *identity_certificate,
                             NX_SECURE_X509_CERT *trusted_certificates[], UINT trusted_certs_num, NX_SECURE_X509_CERT *remote_certificates[], UINT remote_certs_num, UCHAR *remote_certificate_buffer, UINT remote_cert_buffer_size)
{
NX_SECURE_TLS_SESSION *tls_session;
ULONG session_metadata_size;
UCHAR *session_metadata;
UCHAR *session_cert_buffer;
UINT session_cert_buffer_size = 0;
UINT session_pkt_buffer_size;
UINT num_sessions;
UINT i;
UINT cert_count;
UINT status;

    /* Get the number of sessions in the TCP server. */
    num_sessions = server_ptr -> nx_tcpserver_sessions_count;

    /* Get our per-session packet buffer size. */
    session_pkt_buffer_size = packet_buffer_size / num_sessions;
    
    /* Get our per-session metadata. */
    session_metadata = metadata_buffer;
    session_metadata_size = metadata_size / num_sessions;

    /* It's OK to have zero remote certs allocated. */
    if(remote_certs_num > 0)
    {
        /* Divide the remote certificate buffer size by the total number of
           remote certificates to get per-certificate size. */
        session_cert_buffer_size = remote_cert_buffer_size / remote_certs_num;
    }
    
    /* Loop through all sessions, initialize each one. */
    for(i = 0; i < num_sessions; ++i)
    {
        /* This server instance is using TLS so mark as such. */
        server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_using_tls = NX_TRUE;
      
        /* Setup per-session data. Get the session from the server structure, then initialize it. */
        tls_session =  &(server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_tls_session);
        status = nx_secure_tls_session_create(tls_session, crypto_table, session_metadata, session_metadata_size);

        if(status != NX_SUCCESS)
        {
            return(status);
        }

        /* Allocate space for packet reassembly. */
        status = nx_secure_tls_session_packet_buffer_set(tls_session, &packet_buffer[i * session_pkt_buffer_size], 
                                                         session_pkt_buffer_size);

        if(status != NX_SUCCESS)
        {
            return(status);
        }
        
        /* Add identity certificate. */
        status = nx_secure_tls_local_certificate_add(tls_session, identity_certificate);

        if(status != NX_SUCCESS)
        {
            return(status);
        }       
        
        /* See if remote certificates are provided. */
        if(remote_certs_num > 0)
        {
            /* We have remote certificates to allocate so enable client certificate authentication. */
            nx_secure_tls_session_client_verify_enable(tls_session);

            /* Add trusted certificates to each TLS session. */
            for(cert_count = 0; cert_count < trusted_certs_num; ++cert_count)
            {
                status = nx_secure_tls_trusted_certificate_add(tls_session, trusted_certificates[cert_count]);

                if(status != NX_SUCCESS)
                {
                    return(status);
                }
            }

            /* Allocate remote certificates (if remote_certs_num > 0). The remote certificate
               buffers are shared between all TLS Sessions. This is possible since they are only
               needed for the certificate verification process and are freed immediately after. */
            session_cert_buffer = remote_certificate_buffer;

            for(cert_count = 0; cert_count < remote_certs_num; ++cert_count)
            {
                /* Allocate a remote certificate from the provided array. */
                status = nx_secure_tls_remote_certificate_allocate(tls_session, remote_certificates[i],
                                                                   session_cert_buffer, session_cert_buffer_size);

                if(status != NX_SUCCESS)
                {
                    return(status);
                }

                /* Now get the next certificate and its buffer. */
                session_cert_buffer += session_cert_buffer_size;
            }
        }

        /* Advance metadata buffer. */
        session_metadata = &session_metadata[session_metadata_size];
    }
    return(NX_SUCCESS);
}
/* Setup TLS state per server session. */
Setup TLS state per server session.
Setup TLS state per server session .
UINT _nx_tcpserver_tls_setup(NX_TCPSERVER *server_ptr, const NX_SECURE_TLS_CRYPTO *crypto_table,
                             VOID *metadata_buffer, ULONG metadata_size, UCHAR* packet_buffer, UINT packet_buffer_size, NX_SECURE_X509_CERT *identity_certificate,
                             NX_SECURE_X509_CERT *trusted_certificates[], UINT trusted_certs_num, NX_SECURE_X509_CERT *remote_certificates[], UINT remote_certs_num, UCHAR *remote_certificate_buffer, UINT remote_cert_buffer_size)
{
NX_SECURE_TLS_SESSION *tls_session;
ULONG session_metadata_size;
UCHAR *session_metadata;
UCHAR *session_cert_buffer;
UINT session_cert_buffer_size = 0;
UINT session_pkt_buffer_size;
UINT num_sessions;
UINT i;
UINT cert_count;
UINT status;
    num_sessions = server_ptr -> nx_tcpserver_sessions_count;
    session_pkt_buffer_size = packet_buffer_size / num_sessions;
    session_metadata = metadata_buffer;
    session_metadata_size = metadata_size / num_sessions;
    if(remote_certs_num > 0)
    {
        session_cert_buffer_size = remote_cert_buffer_size / remote_certs_num;
    }
    for(i = 0; i < num_sessions; ++i)
    {
        server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_using_tls = NX_TRUE;
        tls_session =  &(server_ptr -> nx_tcpserver_sessions[i].nx_tcp_session_tls_session);
        status = nx_secure_tls_session_create(tls_session, crypto_table, session_metadata, session_metadata_size);
        if(status != NX_SUCCESS)
        {
            return(status);
        }
        status = nx_secure_tls_session_packet_buffer_set(tls_session, &packet_buffer[i * session_pkt_buffer_size], 
                                                         session_pkt_buffer_size);
        if(status != NX_SUCCESS)
        {
            return(status);
        }
        status = nx_secure_tls_local_certificate_add(tls_session, identity_certificate);
        if(status != NX_SUCCESS)
        {
            return(status);
        }       
        if(remote_certs_num > 0)
        {
            nx_secure_tls_session_client_verify_enable(tls_session);
            for(cert_count = 0; cert_count < trusted_certs_num; ++cert_count)
            {
                status = nx_secure_tls_trusted_certificate_add(tls_session, trusted_certificates[cert_count]);
                if(status != NX_SUCCESS)
                {
                    return(status);
                }
            }
            session_cert_buffer = remote_certificate_buffer;
            for(cert_count = 0; cert_count < remote_certs_num; ++cert_count)
            {
                status = nx_secure_tls_remote_certificate_allocate(tls_session, remote_certificates[i],
                                                                   session_cert_buffer, session_cert_buffer_size);
                if(status != NX_SUCCESS)
                {
                    return(status);
                }
                session_cert_buffer += session_cert_buffer_size;
            }
        }
        session_metadata = &session_metadata[session_metadata_size];
    }
    return(NX_SUCCESS);
}
UINT _nx_tcpserver_tls_setup ( NX_TCPSERVER * server_ptr , const NX_SECURE_TLS_CRYPTO * crypto_table , VOID * metadata_buffer , ULONG metadata_size , UCHAR * packet_buffer , UINT packet_buffer_size , NX_SECURE_X509_CERT * identity_certificate , NX_SECURE_X509_CERT * trusted_certificates [ ] , UINT trusted_certs_num , NX_SECURE_X509_CERT * remote_certificates [ ] , UINT remote_certs_num , UCHAR * remote_certificate_buffer , UINT remote_cert_buffer_size ) { NX_SECURE_TLS_SESSION * tls_session ; ULONG session_metadata_size ; UCHAR * session_metadata ; UCHAR * session_cert_buffer ; UINT session_cert_buffer_size = 0 ; UINT session_pkt_buffer_size ; UINT num_sessions ; UINT i ; UINT cert_count ; UINT status ; num_sessions = server_ptr -> nx_tcpserver_sessions_count ; session_pkt_buffer_size = packet_buffer_size / num_sessions ; session_metadata = metadata_buffer ; session_metadata_size = metadata_size / num_sessions ; if ( remote_certs_num > 0 ) { session_cert_buffer_size = remote_cert_buffer_size / remote_certs_num ; } for ( i = 0 ; i < num_sessions ; ++ i ) { server_ptr -> nx_tcpserver_sessions [ i ] . nx_tcp_session_using_tls = NX_TRUE ; tls_session = & ( server_ptr -> nx_tcpserver_sessions [ i ] . nx_tcp_session_tls_session ) ; status = nx_secure_tls_session_create ( tls_session , crypto_table , session_metadata , session_metadata_size ) ; if ( status != NX_SUCCESS ) { return ( status ) ; } status = nx_secure_tls_session_packet_buffer_set ( tls_session , & packet_buffer [ i * session_pkt_buffer_size ] , session_pkt_buffer_size ) ; if ( status != NX_SUCCESS ) { return ( status ) ; } status = nx_secure_tls_local_certificate_add ( tls_session , identity_certificate ) ; if ( status != NX_SUCCESS ) { return ( status ) ; } if ( remote_certs_num > 0 ) { nx_secure_tls_session_client_verify_enable ( tls_session ) ; for ( cert_count = 0 ; cert_count < trusted_certs_num ; ++ cert_count ) { status = nx_secure_tls_trusted_certificate_add ( tls_session , trusted_certificates [ cert_count ] ) ; if ( status != NX_SUCCESS ) { return ( status ) ; } } session_cert_buffer = remote_certificate_buffer ; for ( cert_count = 0 ; cert_count < remote_certs_num ; ++ cert_count ) { status = nx_secure_tls_remote_certificate_allocate ( tls_session , remote_certificates [ i ] , session_cert_buffer , session_cert_buffer_size ) ; if ( status != NX_SUCCESS ) { return ( status ) ; } session_cert_buffer += session_cert_buffer_size ; } } session_metadata = & session_metadata [ session_metadata_size ] ; } return ( NX_SUCCESS ) ; }
Setup TLS state per server session.
Setup TLS state per server session .
/* Get the number of sessions in the TCP server. */ /* Get our per-session packet buffer size. */ /* Get our per-session metadata. */ /* It's OK to have zero remote certs allocated. */ /* Divide the remote certificate buffer size by the total number of
           remote certificates to get per-certificate size. */ /* Loop through all sessions, initialize each one. */ /* This server instance is using TLS so mark as such. */ /* Setup per-session data. Get the session from the server structure, then initialize it. */ /* Allocate space for packet reassembly. */ /* Add identity certificate. */ /* See if remote certificates are provided. */ /* We have remote certificates to allocate so enable client certificate authentication. */ /* Add trusted certificates to each TLS session. */ /* Allocate remote certificates (if remote_certs_num > 0). The remote certificate
               buffers are shared between all TLS Sessions. This is possible since they are only
               needed for the certificate verification process and are freed immediately after. */ /* Allocate a remote certificate from the provided array. */ /* Now get the next certificate and its buffer. */ /* Advance metadata buffer. */