×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    26

    Problem with CSecureSocket::NewL

    hi everybody,

    I'm testing SSL on Symbian (3rd)... I have a problem with NewL method of CSecureSocket class....When my script calls this method the emulator crashes and dont't return anything...this is a piece of my code:
    Code:
    	CX509Certificate* cert;
    	Rsocket socket;	
    
    	
    	TRequestStatus ssl_status; 
    	CSecureSocket* 	 secSock;		
    	_LIT(KServerTlsProtocol, "TLS1.0");
    	secSock = CSecureSocket::NewL(socket, KServerTlsProtocol);	// here crash
    	secSock->FlushSessionCache();
    	TInt errSsc = secSock->SetServerCert(*cert);
    	// chech if the certificate is set correctly
    	if(errSsc == KErrNone){
    		_LIT(mess91,"SetServerCert failed...\n");
    		console->Printf(mess91);
    	}
    	else {
    		_LIT(mess101,"SetServerCert done...\n");
    		console->Printf(mess101);
    	}
    	
    	secSock->StartClientHandshake(ssl_status);
    	// check if handshake fails or not
    	if(ssl_status!=KErrNone){
    		_LIT(mess51,"Handshake failed...\n");
    		console->Printf(mess51);
    	}
    	else {
    		_LIT(mess61,"Handshake done...\n");
    		console->Printf(mess61);
    	}
    I find some examples that do the same thinghs....Can anyone help me?

    thanks in advance

    Giuseppe

  2. #2
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Problem with CSecureSocket::NewL

    Your socket isn't connected to anything, so I guess your code is panicing with KERN-EXEC 0. You need to establish a connection first, and then open a secure socket with that connection.

    Also, it looks like you're attempting to use CSecureSocket as a server socket. If that's the case, you'll be disappointed. CSecureSocket is client only.
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    26

    Re: Problem with CSecureSocket::NewL

    My socket is a client and is connected to a server....I didn't quote entire code...the entire code is:
    Code:
    	RSocketServ sockServ;
    	User::LeaveIfError(sockServ.Connect());
    
    	// posto --> 1 FRODO
    	//	     --> 0 CASA
    	TInt posto = 1;
    	TInetAddr s_addr; 
    	TInetAddr c_addr;
    	TInt s_porta;
    	TInt c_porta;
    	TUint32 KInetAddrS;
    	TUint32 KInetAddrC;
    	
    	// FRODO
    	if(posto == 1){
    		KInetAddrS = INET_ADDR(10,20,10,22);
    		s_porta = 10000;
    		KInetAddrC = INET_ADDR(10,20,179,155);
    		c_porta = 6680;
    	} 
    	// CASA
    	else{
    		KInetAddrS = INET_ADDR(192,168,1,2);
    		s_porta = 10000;
    		KInetAddrC = INET_ADDR(192,168,1,44);
    		c_porta = 6680;
    	}
    	
    	s_addr.SetAddress(KInetAddrS);
    	s_addr.SetPort(s_porta);
    	c_addr.SetAddress(KInetAddrC);
    	c_addr.SetPort(c_porta);
    	
    	RSocket socket;
    	TInt errno;
    	errno=socket.Open(sockServ,KAfInet,KSockStream, KProtocolInetTcp);
    	if(errno!=KErrNone){
    		_LIT(mess1,"Error opening socket...\n");
    		console->Printf(mess1);
    	}
    	else {
    		_LIT(mess2,"Socket opened...\n");
    		console->Printf(mess2);
    	}
    
    	TInt errBin;
    	errBin=socket.Bind(c_addr);
    	if(errBin!=KErrNone){
    	 _LIT(mess5,"Error binding CLIENT...\n");
    	 console->Printf(mess5);
    	}
    	else {
    	 _LIT(mess6,"Socket binded...\n");
    	 console->Printf(mess6);
    	}
    	
    	TRequestStatus status;
    	socket.Connect(s_addr, status);
    	if(status!=KErrNone){
    		_LIT(mess3,"Error connecting SERVER...\n");
    		console->Printf(mess3);
    	}
    	else {
    	_LIT(mess4,"Socket connected...\n");
    	console->Printf(mess4);
    	}
    	
    	
    	// apro certificato e lo salvo in un buffer
    	TBuf8<500000> certBuf;
    	RFs fsSession;
    	_LIT(KFileName,"C:\\serverCert.cer");  
    	RFileReadStream fileStream;
    	User::LeaveIfError(fsSession.Connect());
    	TInt errOp = fileStream.Open(fsSession,KFileName,EFileStream);	
    	_LIT(retr,"errOp value: %d\n");	
    	console->Printf(retr, errOp);
    	
    	// ottengo la dimensione del file
    	TEntry entry;
    	User::LeaveIfError(fsSession.Entry(KFileName, entry));
    	TInt fileSize = entry.iSize;
    	
    	// leggo lo stream e lo salvo nel buffer
    	fileStream.ReadL(certBuf, fileSize); 
    	_LIT(testRead,"dimensione file: %d\n");
    	console->Printf(testRead, fileSize); 
    	
    	// chiudo la sessione
    	fsSession.Close();
    	
    	CX509Certificate* cert;
    	cert = CX509Certificate::NewL(certBuf);
    
    	TRequestStatus ssl_status; 
    	CSecureSocket* 	 secSock;		// socket protetto con ssl
    	_LIT(KServerTlsProtocol, "TLS1.0");
    	
    
    	
    	secSock = CSecureSocket::NewL(socket, KServerTlsProtocol);
    	secSock->FlushSessionCache();
    	TInt errSsc = secSock->SetServerCert(*cert);
    	if(errSsc == KErrNone){
    		_LIT(mess91,"SetServerCert failed...\n");
    		console->Printf(mess91);
    	}
    	else {
    		_LIT(mess101,"SetServerCert done...\n");
    		console->Printf(mess101);
    	}
    	
    	secSock->StartClientHandshake(ssl_status);
    	
    	if(ssl_status!=KErrNone){
    		_LIT(mess51,"Handshake failed...\n");
    		console->Printf(mess51);
    	}
    	else {
    		_LIT(mess61,"Handshake done...\n");
    		console->Printf(mess61);
    	}
    	
    	secSock->Recv(recBuff, ssl_status);
    
    
    	socket.CancelAll();
    	secSock->CancelAll();
    	//chiudo il secure socket
    	secSock->Close();
    	delete secSock;
    	// chiudo il socket sockServ e socket
    	socket.Close(); 
    	sockServ.Close();    
       
    }

  4. #4
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Problem with CSecureSocket::NewL

    Erm. I don't know where to start really.

    The biggest problem is that you're calling asynchronous functions and charging on without waiting for them to complete. RSocket::Connect, for instance.

    I suggest you start by reading about active objects.

    Then, you're mixing things that a server should do (binding a socket, selecting a certificate) with things a client should do (connecting to a remote host, handshaking.) There is no need to select a port for client sockets, it'll automatically select a high numbered port for you.

    Ditto with certificates if you're using client auth, the server will send you a list of of CAs it supports, and the framework will select an installed client certificate to use for the transaction if it has one.

    Then there is things like "TBuf8<500000> certBuf;", that's a 500 kilobyte buffer. On the stack. The stack is by default 8Kbs.
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    26

    Thumbs up Re: Problem with CSecureSocket::NewL

    hi

    i tried to use active objects, but I still have some problem...
    After connecting to the server with RSocket::Connect, my program crashes....(this happens when I put SetActive() command after RSocket::Connect). The same problem happens when I use CSecureSocket::StartClientHandshake...
    I paste my .h and .cpp files....I only try to setup a SSL connection without sending/receiving file...

    sslEngine.h:
    Code:
    //sslEngine.h
    #ifndef __SSLENGINE_H__
    #define __SSLENGINE_H__
    
    
    //  Include Files
    #include <e32base.h>
    #include <in_sock.h>
    #include <e32def.h>
    #include <es_sock.h>
    #include <e32std.h>
    #include <e32cons.h>
    #include <x509cert.h>
    #include <securesocket.h>
    
    //#include <>
     
    
    //  Function Prototypes
    GLDEF_C TInt E32Main();
    
    class CSslEngine : public CActive {
    
    public:
    
    	//Costruttore
    	static CSslEngine *NewL();
    
    	//Distruttore
    	~CSslEngine();
    
    	// ConnectL
    	void ConnectL(TUint32 anAddress, TInt aPort);
    	
    	// setConsole
    	void setConsole(CConsoleBase* aConsole);
    	
    private:
    
    	//costruttore
    	CSslEngine();
    	
    	//ConstructL
    	void ConstructL();
    	
           // RunL
           void RunL();
        
          // DoCancel
          void DoCancel();
    	
         // makeSecureConL
         void makeSecureConL();
    	
         // CloseCoL()
         void CloseConL();
    	
    	// RunError
    	TInt RunError(TInt aError);
    	
    	
    	
    	
        
    private:
    	// oggetti della classe
    	RSocket  sockEng;
    	RSocketServ sockServEng;
    	TInetAddr servAddr;
    	TRequestStatus iStatus;
    	CConsoleBase* iConsole;
    	CSecureSocket* iTlsSock;
    	
    	
    	// engine state
    	enum eSslState
    		{
    			eNotCon,					// non connesso
    			eConnected,				// connesso al Rsocket
    			eSecConnected,			// socket protetto con ssl
    			eDataTransfered,	// invio/Ricezione dati terminato
    			
    		};
    	
    	// variabile indicante lo stato dell' engine
    	TInt iSslState;
    };
    
    #endif  // __SSLENGINE_H__

    sslEngine.cpp


    Code:
    // sslEngine.cpp
    
    //  Include Files  
    #include "sslEngine.h"
    
    
    // COSTANTI
    _LIT(KErrOpen,"Error opening socket...\n");
    _LIT(KNerrOpen,"Socket opened...\n");
    _LIT(KErrCon,"Error connecting to the server...\n");
    _LIT(KNerrCon,"Socket Connected...\n");
    _LIT(KErrHand,"Handshake Failed...\n");
    _LIT(KNerrHand,"Handshake Done...\n");
    _LIT(KErrRecv,"Error receiving data...\n");
    _LIT(KNerrRecv,"Data Received");
    _LIT(KErrSend,"Error sending data...\n");
    _LIT(KNerrSend,"Data Sent...\n");
    _LIT(KCloseConn,"Connection Closed...\n");
    
    
    // CLASS CSSLENGINE
    
    // costruttore pubblico
    CSslEngine* CSslEngine::NewL()
    	{
    		CSslEngine* me = new(ELeave) CSslEngine;
    		CleanupStack::PushL(me);
    		me->ConstructL();
    		CleanupStack::Pop(); 
    		return me;
    	}
    
    // costruttore privato
    CSslEngine::CSslEngine()
    : CActive(0)
    	{
    		//CActiveScheduler::Add( this );
    	}
    
    // distruttore
    CSslEngine::~CSslEngine()
    	{
    		sockServEng.Close();
    	}
    
    // ConstructL
    void CSslEngine::ConstructL()
    	{
    		User::LeaveIfError(sockServEng.Connect());	
    		CActiveScheduler::Add( this );
    	}
    
    
    // RunL
    void CSslEngine::RunL()
    	{ 
    		switch(iSslState)
    			{
    				case eConnected:
    					makeSecureConL();
    					break;
    				
    				//case eSecConnected:
    					// invio-ricezione				
    					//break;
    			
    				//case eDataTransfered:
    					//closeConL();	
    					//break;
    			}
    	}
    
    //DoCancel
    void CSslEngine::DoCancel()
    	{
    	
    	}
    
    // ConnectL
    void CSslEngine::ConnectL(TUint32 anAddress, TInt aPort)
    	{
    		TInt errorCode;
    		servAddr.SetAddress(anAddress);
    		servAddr.SetPort(aPort);
    		errorCode = sockEng.Open(sockServEng, KAfInet, KSockStream,KProtocolInetTcp);
    		if(errorCode != KErrNone)
    			{
    				iConsole->Write(KErrOpen);
    			}
    		else
    			{
    				iConsole->Write(KNerrOpen);
    			}
    		//SetActive();	 		
    		sockEng.Connect(servAddr, iStatus);
    
    		if(iStatus != KErrNone)
    			{
    				iConsole->Write(KErrCon);	
    				iSslState = eConnected;				
    			}
    		else
    			{
    				iConsole->Write(KNerrCon); 
    			}
    		SetActive(); 
    		
    	}
    
    void CSslEngine::setConsole(CConsoleBase* aConsole)
    	{
    		iConsole = aConsole;		
    	}
    
    void CSslEngine::makeSecureConL()
    	{
    		_LIT(KSerTls, "TLS1.0");
    		iTlsSock = CSecureSocket::NewL(sockEng, KSerTls);
    		iTlsSock->FlushSessionCache();
    		//SetActive();
    		iTlsSock->StartClientHandshake(iStatus);
    		iSslState = eSecConnected;
    		SetActive();
    	
    	}
    
    void CSslEngine::CloseConL() 
    	{
    		iTlsSock->CancelAll();
    		iTlsSock->Close();
    		delete iTlsSock;
    		sockEng.CancelAll();
    		sockEng.Close();		
    	}
    
    
    TInt CSslEngine::RunError(TInt aError)
    	{
    		TInt y = aError;
    		return KErrNone;
    	}
    
    
    
    
    //==============================================================
    //==============================================================
    //==============================================================
    
    
    //  Constants
    _LIT(KTextConsoleTitle, "Console");
    _LIT(KTextFailed, " failed, leave code = %d");
    _LIT(KTextPressAnyKey, " [press any key]\n");
    
    //  Global Variables
    LOCAL_D CConsoleBase* console; 
    
    
    //  Local Functions
    //LOCAL_C void MainL()
    //	{	
    //		console->Write(_L("Hello, world!\n"));
    //	}
    
    
    LOCAL_C void DoStartL()
    	{
    	// Create active scheduler (to run active objects)
    	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
    	CleanupStack::PushL(scheduler);
    	CActiveScheduler::Install(scheduler);
    	//CActiveScheduler::Start();
    	
    	TInt tipo = 1;
    	TUint32 serv;
    	//porta
    	TInt port = 10000; 
    	//indirizzo ip
    	if(tipo == 1){
    		serv = INET_ADDR(10,20,10,22);
    	}
    	else{
    		serv = INET_ADDR(192,168,1,2);
    	}
    	 
    	
    	CSslEngine* pippo;
    	pippo = CSslEngine::NewL();
    	pippo->setConsole(console);
    	pippo->ConnectL(serv, port);			
    	pippo->~CSslEngine();
    
    
    	//CActiveScheduler::Stop();
    	// Delete active scheduler
    	CleanupStack::PopAndDestroy(scheduler);
    	}
    
    
    //  Global Functions
    
    GLDEF_C TInt E32Main()
    	{
    	// Create cleanup stack
    	__UHEAP_MARK;
    	CTrapCleanup* cleanup = CTrapCleanup::New();
    
    	// Create output console
    	TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen)));
    	if (createError)
    		return createError;
    
    	// Run application code inside TRAP harness, wait keypress when terminated
    	TRAPD(mainError, DoStartL());
    	if (mainError)
    		console->Printf(KTextFailed, mainError);
    	console->Printf(KTextPressAnyKey);
    	console->Getch();
    	
    	delete console;
    	delete cleanup;
    	__UHEAP_MARKEND;
    	return KErrNone;
    	}

  6. #6
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Problem with CSecureSocket::NewL

    You still aren't checking the error code returned by RSocket::Connect. Checking what it is immediately after the call isn't helpful, you have to wait until after it has completed, in RunL.

    Also, as I said earlier, get rid of the call to FlushSessionCache. At best it's totally redundant.

    It would also help if you told us what the panic code was, rather than saying "it crashes"
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    26

    Re: Problem with CSecureSocket::NewL

    RSocket::Connect works well. The socket is connected to server, but as soon as it is connected, my script exits not correctly closing emulator window...
    how can i check the return code?

    thanks in advance

    Giuseppe

  8. #8
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Problem with CSecureSocket::NewL

    look in epocwind.out and tell us what the panic code is....
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    26

    Re: Problem with CSecureSocket::NewL

    hi,
    the error is a connection failed error, 10061.
    server spec: openssl version 0.9.71 on mac os X 10..4.9 (kernel Darwin 8.9.1)

    Sorry the error is not 10061, when I tested my code and checked the epocwind.out, the server SSl was down...Now I check the real error
    Last edited by giuseppeottelli; 2007-07-27 at 13:48.

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    26

    Re: Problem with CSecureSocket::NewL

    checking the epocwind.out file, the only error that I've found is:
    [DOSSERVER] RDosServer::Connect()-CreateSession err: -1
    But this method is called again in the file, returning no error.

    Also I checked the messages exchange with wireshark (i tried to send a simple string to the server).
    After been connected to the server, my socket sends the string (the server gives errors because it expects SSL parameters)
    and then the emulator window shuts.

    I upload my epocwind.out file here:

    http://losamigosdejack.altervista.org/temp/epocwind.out

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    26

    Re: Problem with CSecureSocket::NewL

    anybody can help me?

    Is my code correct?

    I did some tests and I found that the problem is the SetActive call after RSocket::Connect. Commenting this method, script doesn't crash and the socket connects to the server....Perhaps my AO implementation is not correct...

    thanks in advance

    Giuseppe

  12. #12
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Problem with CSecureSocket::NewL

    For the third time, TELL US WHAT THE PANIC CODE IS. We aren't psychic. You have to help us to help you. It isn't good enough to say "it crashes".
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    26

    Re: Problem with CSecureSocket::NewL

    E32USER-CBase 42

  14. #14
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Problem with CSecureSocket::NewL

    Right, well that's an easy one, isn't it.

    From the devlib:

    This panic is raised by the SetActive() member function of an active object, a CActive. It is caused by an attempt to flag the active object as active when it is already active, i.e. a request is still outstanding.
    You're calling SetActive while the object is active already.
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  15. #15
    Super Contributor
    Join Date
    Nov 2004
    Location
    Wiltshire, UK
    Posts
    3,644

    Re: Problem with CSecureSocket::NewL

    There are examples of using the secure socket API in the 2nd FP2 Codewarrior SDK in Examples\networking\securesockets
    and also in the 3.1 UIQ SDK
    Examples\SymbianNotCompatible\Networking\SecureSockets

    Email me (remember to make your email address visiible) if you want the example code for this example and don't want to download the entire SDK.

Similar Threads

  1. Problem with eglSwapBuffers and heap corruption
    By greatape in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2007-05-24, 03:35
  2. netcards - Problem with opening adapter
    By kernj in forum Symbian Tools & SDKs
    Replies: 5
    Last Post: 2007-01-10, 08:56
  3. WAP service problem on Apache
    By bigg_o in forum Browsing and Mark-ups
    Replies: 3
    Last Post: 2005-04-17, 16:05
  4. Problem: S60 SDK for CW in PC with .NET
    By anttij in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2005-02-28, 11:36

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×