0

こんにちは、C++ で SMTP クライアントを作成しようとしています。「ehlo ...」と STARTTLS コマンドの後、接続は問題ないようです SSL_write() を試すと、次のエラーが発生します。

32096:error:140790E5:SSL routines:SSL23_write::ssl handshake failure

SSL_write の前に SSL_do_handshake() を試してみましたが、うまくいきました。

SSL部分のコードは次のとおりです。

typedef struct {
    int socket;
    SSL *sslHandle;
    SSL_CTX *sslContext;
} SSLConnection;

SSLConnection* wrapSslSocket(SOCKET hSocket)
{
    SSLConnection *c;
    c = (SSLConnection *)malloc (sizeof (SSLConnection));
    c->sslHandle = NULL;
    c->sslContext = NULL;
    c->socket = hSocket;
    if (c->socket)
    {
        // Register the error strings for libcrypto & libssl
        SSL_load_error_strings ();
        // Register the available ciphers and digests
        SSL_library_init ();

        c->sslContext = SSL_CTX_new (SSLv23_client_method ()); //I tried SSLv23, SSLv3, SSLv2, TLSv1.2 TLSv1.1, TLSv1.0
        if (c->sslContext == NULL)
          ERR_print_errors_fp (stderr);

        // Create an SSL struct for the connection
        c->sslHandle = SSL_new (c->sslContext);
        if (c->sslHandle == NULL)
          ERR_print_errors_fp (stderr);

        // Connect the SSL struct to our connection
        if (!SSL_set_fd (c->sslHandle, c->socket))
          ERR_print_errors_fp (stderr);

        if (!SSL_set_mode(c->sslHandle, SSL_MODE_AUTO_RETRY))
            ERR_print_errors_fp (stderr);

        // Initiate SSL handshake
        if (SSL_connect (c->sslHandle) != 1)
          ERR_print_errors_fp (stderr);
    }
    return c;
}



// Read all available text from the connection
int sslRead (SSLConnection *c)
{
  const int readSize = 1024;
  char buffer[1024];
  int cb;
  int cbBuffer = 0;

if (c)
{
    while (1)
    {       
        cb = SSL_read( c->sslHandle, buffer + cbBuffer, sizeof(buffer) - 1 - cbBuffer);
        if( cb <= 0 )
        {
            ERR_print_errors_fp (stderr);
            return -1;
        }
        cbBuffer += cb;
        if( memcmp( buffer + cbBuffer - 2, "\r\n", 2 ) == 0 )
        {
            buffer[cbBuffer] = '\0';
            break;
        }
    }
}
    printf("ssl send : %s \n",buffer);
    char status[3];
    memcpy(status,buffer, 3*sizeof(char));
    status[3] = '\0';
    return atoi(status);
}

// Write text to the connection
int sslWrite (SSLConnection *c, char *text)
{
  if (c)
  {
      int v = SSL_do_handshake(c->sslHandle);
      ERR_print_errors_fp (stderr);
      return SSL_write (c->sslHandle, text, strlen (text));
  }
}

smtp.gmail.com ポート 587 でテストしています

ありがとう

4

1 に答える 1