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
Importante
Questa libreria è ospitata nel repository Amazon-FreeRTOS che è obsoleto. Ti consigliamo di iniziare da qui quando crei un nuovo progetto. Se disponi già di un progetto FreeRTOS esistente basato sull'ormai obsoleto repository Amazon-FreerTOS, consulta il. Guida alla migrazione del repository Github di Amazon-FreeRTOS
Panoramica
Puoi utilizzare la libreria FreerTOS Secure 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.
Nota
Attualmente, solo il client APIs, più un'implementazione IP leggera (LWiPBind
lato server, sono supportati per FreerTOS Secure Sockets.
Dipendenze e requisiti
La libreria FreerTOS Secure Sockets dipende da uno stack TCP/IP e da un'implementazione TLS. Le porte per FreerTOS soddisfano queste dipendenze in tre modi:
-
Un'implementazione personalizzata sia di TCP/IP sia di TLS
-
Un'implementazione personalizzata di TCP/IP e il livello TLS FreerTOS con mbedTLS
Il diagramma delle dipendenze seguente mostra l'implementazione di riferimento inclusa nella libreria FreerTOS Secure Sockets. L'implementazione di riferimento supporta TLS e TCP/IP su Ethernet e Wi-Fi con FreeRTOS+TCP e mbedTLS come dipendenze. Per ulteriori informazioni sul layer TLS FreerTOS, vedere. Transport Layer Security

Funzionalità
Le funzionalità della libreria FreerTOS Secure Sockets includono:
-
Un'interfaccia standard basata sui socket di Berkeley
-
Thread-safe per l'invio e la ricezione di dati APIs
-
Easy-to-enable TLS
Risoluzione dei problemi
Codici di errore
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.
Nota
Se l'API FreerTOS Secure Sockets restituisce un codice di errore,, libreria CoreMQTT che dipende dalla libreria FreerTOS Secure Sockets, restituisce il codice di errore. AWS_IOT_MQTT_SEND_ERROR
Supporto per gli sviluppatori
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
Solo i socket TCP sono supportati dalla libreria FreerTOS Secure Sockets. I socket UDP non sono supportati.
APIs I server non sono supportati dalla libreria FreerTOS Secure Sockets, ad eccezione di un'implementazione IP leggera (LWiPBind
I client APIs sono supportati.
Inizializzazione
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 FreeRTOS_IPInit
Riferimento API
Per un riferimento completo alle API, consulta Secure Sockets API Reference.
Esempio di utilizzo
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.
Portabilità
FreerTOS Secure Sockets dipende da uno stack TCP/IP 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 TCP/IP FreeRTOS+TCP
Per ulteriori informazioni sul porting, consulta Porting the Secure Sockets Library nella FreerTOS Porting Guide.