Introduction
CAsyncSslSocketLayer is a layer class for CAsyncSocketEx which allows you to establish SSL secured connections to servers.
For information about CAsyncSocketEx and the layer system in general, please read my CAsyncSocketEx article.
How to use
Using this class is really simple. In the easiest case, just add an instance of CAsyncSslSocketLayer to your socket and call InitClientSsl after creation of the socket.
This class only has three new public functions:
InitClientSsl();This functions establishes an SSL connection to the server. You can call it at any time once the socket has been created. Most likely you want to call this function right after calling
Createfor the socket. But sometimes, you'll need to call this function later. One example is for an FTP connection with explicit SSL: In this case you would have to callInitClientSslafter receiving the reply to anAUTH SSLcommand.UsingSSL();Returns true if you've previously called
InitClientSsl()SetNotifyReply(int nID, int nCode, int result);You can call this function only after receiving a layer specific callback with the
SSL_VERIFY_CERTID. See below for details.
This layer sends some layer specific notifications to your socket instance, you can handle them in OnLayerCallback of your socket class. Valid notification IDs are:
SSL_INFO0There are two possible values for
param2:SSL_INFO_ESTABLISHED0 - You'll get this notification if the SSL negotiation was successful.SSL_INFO_SHUTDOWNCOMPLETE1 - You'll get this notification if the SSL connection has been shut down successfully. See below for details.
SSL_FAILURE1This notification is sent if the SSL connection could not be established or if an existing connection failed. Valid values for
param2are:SSL_FAILURE_UNKNOWN0 - Details may have been sent with aSSL_VERBOSE_WARNINGorSSL_VERBOSE_INFOnotification.SSL_FAILURE_ESTABLISH1 - Problem during SSL negotiationSSL_FAILURE_LOADDLLS2SSL_FAILURE_INITSSL4SSL_FAILURE_VERIFYCERT8 - The remote SSL certificate was invalid
SSL_VERBOSE_WARNING3SSL_VERBOSE_INFO4These two notifications contain some additional information. The value given by
param2is a pointer to a null-terminated character string (char *) with some useful information.SSL_VERIFY_CERT2This notification is sent each time a remote certificate has to be verified.
param2is a pointer to at_SslCertDatastructure which contains some information about the remote certificate. Return 1 if you trust the certificate and 0 if you don't trust it. If you're unsure so that the user has to choose to trust the certificate, return 2. In this case, you have to callSetNotifyReplylater to resume the SSL connection.nIDhas to be thepriv_dataelement of thet_SslCertDatastructure andnCodehas to beSSL_VERIFY_CERT. SetnActionto 1 if you trust the certificate and 0 if you don't trust it.
Be careful with closing the connection after sending data, not all data may have been sent already. Before closing the connection, you should call Shutdown() and wait for the SSL_INFO_SHUTDOWNCOMPLETE notification. This assures that all encrypted data really has been sent.
CAsyncSslSocketLayer是CAsyncSocketEx的一个扩展层,用于建立与服务器之间的SSL安全连接。通过简单的实例化并调用InitClientSsl方法即可实现SSL连接。该类提供了一些通知回调来处理SSL连接过程中的各种状态。
5989

被折叠的 条评论
为什么被折叠?



