Coding with Stunnel... SSL23_GET_CLIENT_HELLO error

Everything else that doesn't fall into one of the other PB categories.
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Coding with Stunnel... SSL23_GET_CLIENT_HELLO error

Post by Hydrate »

Ive recently been coding with STunnel as suggested in another topic for an smtp mail client I am creating. My issue is that it does not seem to work, at all. I know that the code works fine if I connect without STunnel running, which means it must be some sort of error with either STunnel, the SSL connection or my code when it is running. My full code is below:

Code: Select all

; SMTP Email Send

Global ResponseCode.s, cr.s, ConnID.l, SMTPProgress.l;
Global NewList SMTPAttatchments.s();

Global cr.s=Chr(13)+Chr(10) 


Declare.s GetMIMEType(Extension.s);


Declare SMTPConnect(server.s,port.l);
Declare SMTPDisconnect(SMTPID.l);

Declare.s SMTPDataSend(msg.s);
Declare.s SMTPWaitData();

Declare SMTPTalkHelo();
Declare SMTPTalkMailFrom(mailfrom.s);
Declare SMTPTalkMailTo(mailto.s);
Declare SMTPTalkData(mailto.s,mailfrom.s,subject.s,msgbody.s);
Declare SMTPTalkQuit();

Declare SMTPTalkEhlo();

Declare SMTPTalkAttatchments(sBoundry.s);
Declare SMTPAddAttatchment(sLocation.s);
Declare SMTPClearAttatchments();

Declare.l SMTPSendMail(mailserver.s,mailto.s,mailfrom.s,subject.s,msgbody.s,mailpass.s,tls.l);

;Find the MIME type for a given file extension 
Procedure.s GetMIMEType(Extension.s);
    Extension = "." + Extension;
    hKey.l = 0;
    KeyValue.s = Space(255);
    datasize.l = 255;
    If RegOpenKeyEx_(#HKEY_CLASSES_ROOT, Extension, 0, #KEY_READ, @hKey);
        KeyValue = "application/octet-stream";
    Else;
        If RegQueryValueEx_(hKey, "Content Type", 0, 0, @KeyValue, @datasize);
            KeyValue = "application/octet-stream";
        Else;
            KeyValue = Left(KeyValue, datasize-1);
        EndIf;
        RegCloseKey_(hKey);
    EndIf;
    ProcedureReturn KeyValue;
EndProcedure 



; SMTP Procedures
Procedure SMTPConnect(server.s,port.l);
  ProcedureReturn OpenNetworkConnection(server.s,port.l) 
EndProcedure;
Procedure SMTPDisconnect(SMTPID.l);
  CloseNetworkConnection(SMTPID);
EndProcedure;

; Low level
Procedure.s SMTPDataSend(msg.s);
  SendNetworkData(ConnID,@msg,Len(msg));
  Debug "Client: "+msg 
 ProcedureReturn "send: "+msg; 
EndProcedure;
Procedure.s SMTPWaitData(); 
  ResponseCode="";
  For tmp=1 To 4999;
    ResponseCode+" ";
  Next;
  ReceiveNetworkData(ConnID,@ResponseCode,4999);
  Debug "Server: "+ ResponseCode
  ResponseCode=Left(ResponseCode,3);
  ProcedureReturn ResponseCode;
EndProcedure;

; Communication (Normal)
Procedure SMTPTalkHelo();
  SMTPDataSend("HELO Postbo"+cr);
  SMTPWaitData();
EndProcedure;
Procedure SMTPTalkMailFrom(mailfrom.s);
  Delay(100);
  SMTPDataSend("MAIL FROM: <"+mailfrom+">"+cr) ;
  SMTPWaitData();
EndProcedure;
Procedure SMTPTalkMailTo(mailto.s);
  SMTPDataSend("RCPT TO: <"+mailto+">"+cr);
  SMTPWaitData();
EndProcedure;
Procedure SMTPTalkData(mailto.s,mailfrom.s,subject.s,msgbody.s);
  SMTPDataSend("DATA"+cr);
  SMTPWaitData();
  If ResponseCode="354";
    Delay(100);
    SMTPDataSend("Date: " + FormatDate("%dd/%mm/%yyyy @ %hh:%ii:%ss",Date())+cr);
    SMTPDataSend("From: <"+mailfrom+">"+cr);
    SMTPDataSend("Reply-To: <"+mailfrom+">"+cr);
    SMTPDataSend("To: <"+mailto+">"+cr);
    SMTPDataSend("Subject: "+subject+cr);
    SMTPDataSend("X-Mailer: PostBox"+cr);
    
    sBoundry.s = "Postbox_"+ FormatDate("%dd%mm%yyyy%hh%ii%ss", Date()) 
    SMTPDataSend("Content-Type: multipart/mixed; boundary=" + Chr(34) + sBoundry + Chr(34) + cr) 
    SMTPDataSend(cr);
    Delay(100);
    SMTPProgress=40;
    SMTPDataSend("--" + sBoundry + cr); Boundry 
    SMTPDataSend("Content-Type: text/plain; charset=us-ascii" + cr);
    SMTPDataSend("Content-Transfer-Encoding: 7bit" + cr);
    SMTPDataSend(cr);
    Delay(100);
    SMTPDataSend(msgbody);
    Delay(100);
    SMTPProgress=50;
    
    SMTPTalkAttatchments(sBoundry.s);
    SMTPProgress=80;
    Delay(100);
  ;  SMTPDataSend("--"+cr+"--"+cr+cr) 
    
    Delay(100);
    SMTPDataSend(""+cr);
    SMTPDataSend("."+cr);
    SMTPWaitData();
  EndIf;
  ProcedureReturn 1;
EndProcedure;
Procedure SMTPTalkQuit();
  Delay(100);
  SMTPDataSend("QUIT"+cr);
  SMTPWaitData();
  ProcedureReturn 1;
EndProcedure;

; Communication (TLS)
Procedure SMTPTalkEhlo();
  SMTPDataSend("EHLO Postbox"+cr);
  SMTPWaitData();
  SMTPDataSend("STARTTLS"+cr);
  SMTPWaitData();
  Delay(100);
  Select ResponseCode;
    Case "220";
    SMTPDataSend("TLS_RSA_WITH_RC4_128_SHA"+cr);
    SMTPWaitData();
    Case "454"; TLS Not Supported
    ProcedureReturn 0;
    
    Case "501"; Syntax Error
    ProcedureReturn 0;
    
  EndSelect;
EndProcedure;
 

; Attatchments
Procedure SMTPTalkAttatchments(sBoundry.s);
  FirstElement(SMTPAttatchments());
  If SMTPAttatchments() = "yes";
    While NextElement(SMTPAttatchments());
      ; Headers
      SMTPDataSend(cr);
      SMTPDataSend("--" + sBoundry + cr); Boundry 
      SMTPDataSend("Content-Type: " + GetMIMEType(GetExtensionPart(SMTPAttatchments())) + "; name=" + Chr(34) + GetFilePart(SMTPAttatchments()) + Chr(34) + cr); 
      SMTPDataSend("Content-Transfer-Encoding: base64" + cr);
      SMTPDataSend("Content-Disposition: Attachment; filename=" + Chr(34) + GetFilePart(SMTPAttatchments()) + Chr(34) + cr);
      SMTPDataSend(cr);
      Delay(100);
      ; Files
      If ReadFile(0, SMTPAttatchments());
        InputBufferLength.l = Lof(0);
        OutputBufferLength.l = InputBufferLength * 1.4;
        *memin=AllocateMemory(InputBufferLength);
        *memout=AllocateMemory(OutputBufferLength);
        ReadData(0, *memin,InputBufferLength);
        Base64Encoder(*memin,60,*memout,OutputBufferLength);
        SMTPDataSend(PeekS(*memout,60));
        Base64Encoder(*memin,InputBufferLength,*memout,OutputBufferLength);
        For i=1 To OutputBufferLength/60;
          temp.s=Trim(PeekS(*memout+i*60,60));
          If Len(temp)>0;
            SMTPDataSend(temp+cr);
          EndIf;
        Next;
        SMTPDataSend(cr);
        CloseFile(0);
      Else;
        Debug "Error,file does not exist: "+SMTPAttatchments()
      EndIf;
    Wend;
    SMTPDataSend("--" + sBoundry + cr); Boundry 
  EndIf;
EndProcedure;
Procedure SMTPInitAttatchments();
  AddElement(SMTPAttatchments());
  SMTPAttatchments() = "none";
EndProcedure;
Procedure SMTPAddAttatchment(sLocation.s);
  FirstElement(SMTPAttatchments());
  If SMTPAttatchments() = "none";
    SMTPAttatchments() = "yes";
    NextElement(SMTPAttatchments());
  EndIf;

  AddElement(SMTPAttatchments());
  SMTPAttatchments() = sLocation.s;
EndProcedure;
Procedure SMTPClearAttatchments();
  ClearList(SMTPAttatchments());
  AddElement(SMTPAttatchments());
  SMTPAttatchments() = "none";
EndProcedure;

Procedure.l SMTPSendMail(mailserver.s,mailto.s,mailfrom.s,subject.s,msgbody.s,mailpass.s,lTLS.l);
    SMTPProgress = 0;
    ;StunnelExe$ = GetPathPart(ProgramFilename())+"stunnel\stunnel.exe";
    StunnelExe$ = "C:\Users\Elliott\Desktop\Postbox\"+"stunnel\stunnel.exe";

    
    ;RunProgram(StunnelExe$,"-install","");
    ;Delay(500)
    ;RunProgram(StunnelExe$,"-start","");
    Delay(100);
    ConnID = SMTPConnect(mailserver,1099);
    If ConnID;
      ;SMTPWaitData() 
      error=0 
      ;SMTPTalkEhlo();
      ;SMTPTalkHelo();
      ;SMTPDataSend("HELO"+cr);
      SMTPDataSend("HELO"+cr);
      SMTPWaitData();
;       If ResponseCode="220";
;         If lTLS = 1;
;           If SMTPTalkHelo()=0;
;             ProcedureReturn 0;
;           EndIf;
;         Else;
;           If SMTPTalkHelo()=0;
;             ProcedureReturn 0;
;           EndIf;
;         EndIf;
;         SMTPProgress=5;
;       EndIf;
;       
      If ResponseCode="250";
        SMTPTalkMailFrom(mailfrom.s);
        SMTPProgress=20;
      EndIf;
      
      If ResponseCode="250";
        SMTPTalkMailTo(mailto.s);
        SMTPProgress=30;
      EndIf;
        
      If ResponseCode="250";
        If SMTPTalkData(mailto.s,mailfrom.s,subject.s,msgbody.s) =0;
          ProcedureReturn 0;
        EndIf;
        SMTPProgress=90;
      EndIf;
      
      If ResponseCode="250";
        SMTPTalkQuit();
        SMTPDisconnect(ConnID);
        SMTPProgress=100;
      EndIf;
      ;RunProgram(StunnelExe$," -stop","");
      ;RunProgram(StunnelExe$," -uninstall","");
      ProcedureReturn 1;
    Else;
      ;RunProgram(StunnelExe$," -stop","");
      ;RunProgram(StunnelExe$," -uninstall","");
      ProcedureReturn 0;
    EndIf;
    

    
    
EndProcedure;




;mailserver.s="smtp.virgin.net";
mailserver.s="127.0.0.1";
mailto.s="sfbvbxvb@gmail.com";
mailfrom.s="cbcvbcvb@ukonline.co.uk";
mailpass.s="";
subject.s="Test Email";
lTLS.l = 1;


If InitNetwork();
  ;SMTPInitAttatchments();
  ;SMTPAddAttatchment(OpenFileRequester("Add Attachment...","C:/","",0));

  If SMTPSendMail(mailserver,mailto,mailfrom,subject,"This is a test message!"+cr+"What do you think?",mailpass,lTLS.l);
      MessageRequester("Done","Mail Sent Successfully!",0);
    Else;
      MessageRequester("Error","Error Sending Mail.",#MB_ICONERROR);
  EndIf;
  
  ;SMTPClearAttatchments();
EndIf;
End 1; 
Also, here is my STunnel configuration file:

Code: Select all

; Sample stunnel configuration file by Michal Trojnara 2002-2006
cert = stunnel.pem

; Some performance tunings
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1

; Service-level configuration

[SMTP]
accept  = 127.0.0.1:1099
connect = smtp.gmail.com:465

[pop3s]
accept  = 995
connect = 110

[imaps]
accept  = 993
connect = 143

[ssmtp]
accept  = 127.0.0.1:1099
connect = smtp.gmail.com:465

;[https]
;accept  = 443
;connect = 80
;TIMEOUTclose = 0

; vim:ft=dosini
When I run it, the following is output in STunnel:

2009.10.29 00:59:49 LOG5[2688:2188]: SMTP accepted connection from 127.0.0.1:1990
2009.10.29 00:59:49 LOG3[2688:2188]: SSL_accept: 140760FC: error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
2009.10.29 00:59:49 LOG5[2688:2188]: Connection reset: 0 bytes sent to SSL, 0 bytes sent to socket

Any help or advice on this would be much appreciated.
.::Image::.