

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Libreria Secure Sockets
<a name="secure-sockets"></a>

**Importante**  <a name="deprecation-message-library"></a>
Questa libreria è ospitata nel Amazon-FreeRTOS repository che è obsoleto. Ti consigliamo di [iniziare da qui](freertos-getting-started-modular.md) quando crei un nuovo progetto. Se hai già un progetto FreerTOS esistente basato sul repository ora obsoleto, Amazon-FreeRTOS consulta il. [Amazon-FreeRTOS Guida alla migrazione del repository Github](github-repo-migration.md)

## Panoramica di
<a name="freertos-secure-sockets-overview"></a>

Puoi utilizzare la libreria [FreerTOS Secure](https://docs.aws.amazon.com/freertos/latest/lib-ref/html2/secure_sockets/index.html) Sockets per creare applicazioni integrate che comunicano in modo sicuro. La libreria è progettata per agevolare l'onboarding di sviluppatori di software con background di programmazione di rete diversi.

La libreria FreerTOS Secure Sockets si basa sull'interfaccia Berkeley Sockets, con un'opzione di comunicazione sicura aggiuntiva tramite protocollo TLS. [Per informazioni sulle differenze tra la libreria FreerTOS Secure Sockets e l'interfaccia Berkeley sockets, `SOCKETS_SetSockOpt` vedere nel Secure Sockets API Reference.](https://docs.aws.amazon.com/freertos/latest/lib-ref/html2/secure_sockets/index.html)

**Nota**  
Attualmente, solo le API client, più un'implementazione [IP leggera (LWiP](https://savannah.nongnu.org/projects/lwip/)) dell'API `Bind` lato server, sono supportate per FreerTOS Secure Sockets.

## Dipendenze e requisiti
<a name="freertos-secure-sockets-dependencies"></a>

La libreria FreerTOS Secure Sockets dipende da TCP/IP uno stack e da un'implementazione TLS. Le porte per FreerTOS soddisfano queste dipendenze in tre modi:
+ Un'implementazione personalizzata di entrambi e TLS TCP/IP 
+ [Un'implementazione personalizzata e il livello TCP/IP TLS FreerTOS con mbedTLS](https://en.wikipedia.org/wiki/Mbed_TLS)
+ [FreerTOS\+TCP](https://freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/index.html) [e il livello FreerTOS TLS con mbedTLS](https://en.wikipedia.org/wiki/Mbed_TLS)

Il diagramma delle dipendenze seguente mostra l'implementazione di riferimento inclusa nella libreria FreerTOS Secure Sockets. Questa implementazione di riferimento supporta TLS e TCP/IP over Ethernet e Wi-Fi con FreeRTOS\+TCP e mbedTLS come dipendenze. Per ulteriori informazioni sul layer TLS FreerTOS, vedere. [Transport Layer Security](security-tls.md)

![Architettura Secure Sockets Library con componenti FreeRTOS+TCP, TLS Layer e TLS.](http://docs.aws.amazon.com/it_it/freertos/latest/userguide/images/sockets-dependencies.png)


## Funzionalità
<a name="freertos-secure-sockets-features"></a>

Le funzionalità della libreria FreerTOS Secure Sockets includono:
+ Un'interfaccia standard di Berkeley Sockets-based 
+ Thread-safe API per l'invio e la ricezione di dati
+ Easy-to-enable TLS

## Risoluzione dei problemi
<a name="freertos-secure-sockets-troubleshooting"></a>

### Codici di errore
<a name="w2aac31b9c13c45c15b5"></a>

I codici di errore restituiti dalla libreria FreerTOS Secure Sockets sono valori negativi. Per ulteriori informazioni su ciascun codice di errore, consulta Codici di errore Secure Sockets nella [Documentazione di riferimento sull'API Secure Sockets](https://docs.aws.amazon.com/freertos/latest/lib-ref/html2/secure_sockets/index.html).

**Nota**  
Se l'API FreerTOS Secure Sockets restituisce un codice di errore,, [libreria CoreMQTT](coremqtt.md) che dipende dalla libreria FreerTOS Secure Sockets, restituisce il codice di errore. `AWS_IOT_MQTT_SEND_ERROR`

## Supporto per gli sviluppatori
<a name="freertos-secure-sockets-support"></a>

La libreria FreerTOS Secure Sockets include due macro di supporto per la gestione degli indirizzi IP:

**`SOCKETS_inet_addr_quick`**  
Questa macro converte un indirizzo IP espresso con quattro ottetti numerici separati in un indirizzo IP espresso con un numero a 32 bit nell'ordine dei byte di rete.

**`SOCKETS_inet_ntoa`**  
Questa macro converte un indirizzo IP espresso con un numero a 32 bit nell'ordine dei byte di rete in una stringa con notazione decimale puntata.

## Limitazioni d'uso
<a name="freertos-secure-sockets-restrictions"></a>

Solo i socket TCP sono supportati dalla libreria FreerTOS Secure Sockets. I socket UDP non sono supportati.

Le API del server non sono supportate dalla libreria FreerTOS Secure Sockets, ad eccezione di un'implementazione [IP leggera (LWiP](https://savannah.nongnu.org/projects/lwip/)) dell'API lato server. `Bind` Le API client sono supportate.

## Inizializzazione
<a name="freertos-secure-sockets-initialization"></a>

Per utilizzare la libreria FreerTOS Secure Sockets, è necessario inizializzare la libreria e le sue dipendenze. Per inizializzare la libreria Secure Sockets, utilizza il codice seguente nell'applicazione:

```
BaseType_t xResult = pdPASS;
xResult = SOCKETS_Init();
```

Le librerie dipendenti devono essere inizializzate separatamente. Ad esempio, se FreeRTOS\+TCP è una dipendenza, nella tua applicazione devi invocare anche [https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/API/FreeRTOS_IPInit.html](https://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/API/FreeRTOS_IPInit.html).

## Guida di riferimento alle API
<a name="freertos-secure-sockets-api"></a>

[Per un riferimento completo alle API, consulta Secure Sockets API Reference.](https://docs.aws.amazon.com/freertos/latest/lib-ref/html2/secure_sockets/index.html)

## Esempio di utilizzo
<a name="freertos-secure-sockets-example"></a>

Il codice seguente collega un client a un server.

```
#include "aws_secure_sockets.h"

#define configSERVER_ADDR0                     127
#define configSERVER_ADDR1                     0
#define configSERVER_ADDR2                     0
#define configSERVER_ADDR3                     1
#define configCLIENT_PORT                      443

/* Rx and Tx timeouts are used to ensure the sockets do not wait too long for
 * missing data. */
static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 2000 );
static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 2000 );

/* PEM-encoded server certificate */
/* The certificate used below is one of the Amazon Root CAs.\
Change this to the certificate of your choice. */
static const char cTlsECHO_SERVER_CERTIFICATE_PEM[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIBtjCCAVugAwIBAgITBmyf1XSXNmY/Owua2eiedgPySjAKBggqhkjOPQQDAjA5\n"
"MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6b24g\n"
"Um9vdCBDQSAzMB4XDTE1MDUyNjAwMDAwMFoXDTQwMDUyNjAwMDAwMFowOTELMAkG\n"
"A1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJvb3Qg\n"
"Q0EgMzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABCmXp8ZBf8ANm+gBG1bG8lKl\n"
"ui2yEujSLtf6ycXYqm0fc4E7O5hrOXwzpcVOho6AF2hiRVd9RFgdszflZwjrZt6j\n"
"QjBAMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgGGMB0GA1UdDgQWBBSr\n"
"ttvXBp43rDCGB5Fwx5zEGbF4wDAKBggqhkjOPQQDAgNJADBGAiEA4IWSoxe3jfkr\n"
"BqWTrBqYaGFy+uGh0PsceGCmQ5nFuMQCIQCcAu/xlJyzlvnrxir4tiz+OpAUFteM\n"
"YyRIHN8wfdVoOw==\n"
"-----END CERTIFICATE-----\n";

static const uint32_t ulTlsECHO_SERVER_CERTIFICATE_LENGTH = sizeof( cTlsECHO_SERVER_CERTIFICATE_PEM );

void vConnectToServerWithSecureSocket( void )
{
    Socket_t xSocket;
    SocketsSockaddr_t xEchoServerAddress;
    BaseType_t xTransmitted, lStringLength;

    xEchoServerAddress.usPort = SOCKETS_htons( configCLIENT_PORT );
    xEchoServerAddress.ulAddress = SOCKETS_inet_addr_quick( configSERVER_ADDR0,
                                                            configSERVER_ADDR1,
                                                            configSERVER_ADDR2,
                                                            configSERVER_ADDR3 );
                                                            
    /* Create a TCP socket. */
    xSocket = SOCKETS_Socket( SOCKETS_AF_INET, SOCKETS_SOCK_STREAM, SOCKETS_IPPROTO_TCP );
    configASSERT( xSocket != SOCKETS_INVALID_SOCKET );
    
    /* Set a timeout so a missing reply does not cause the task to block indefinitely. */
    SOCKETS_SetSockOpt( xSocket, 0, SOCKETS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
    SOCKETS_SetSockOpt( xSocket, 0, SOCKETS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );

    /* Set the socket to use TLS. */
    SOCKETS_SetSockOpt( xSocket, 0, SOCKETS_SO_REQUIRE_TLS, NULL, ( size_t ) 0 );
    SOCKETS_SetSockOpt( xSocket, 0, SOCKETS_SO_TRUSTED_SERVER_CERTIFICATE, cTlsECHO_SERVER_CERTIFICATE_PEM, ulTlsECHO_SERVER_CERTIFICATE_LENGTH );

    if( SOCKETS_Connect( xSocket, &xEchoServerAddress, sizeof( xEchoServerAddress ) ) == 0 )
    {
        /* Send the string to the socket. */
        xTransmitted = SOCKETS_Send( xSocket,                         /* The socket receiving. */
                                     ( void * )"some message",        /* The data being sent. */
                                     12,                              /* The length of the data being sent. */
                                     0 );                             /* No flags. */

        if( xTransmitted < 0 )
        {
            /* Error while sending data */
            return;
        }

        SOCKETS_Shutdown( xSocket, SOCKETS_SHUT_RDWR );
    }
    else
    {
        //failed to connect to server
    }

    SOCKETS_Close( xSocket );
}
```

Per un esempio completo, consulta [Demo Client Echo Secure Sockets](secure-sockets-demo.md).

## Portabilità
<a name="freertos-secure-sockets-porting"></a>

FreerTOS Secure Sockets dipende da TCP/IP uno stack e da un'implementazione TLS. A seconda del tuo stack, per effettuare il porting della libreria Secure Sockets, potresti dover trasferire alcuni dei seguenti elementi:
+ Lo stack [FreeRTOS\+TCP](https://freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/index.html) TCP/IP 
+ La [Libreria CorePKCS11](security-pkcs.md)
+ La [Transport Layer Security](security-tls.md)

Per ulteriori informazioni sul porting, consulta [Porting the Secure Sockets Library](https://docs.aws.amazon.com/freertos/latest/portingguide/afr-porting-ss.html) nella FreerTOS Porting Guide.