Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2010
    Messaggi
    34

    [C++]Segmentation fault criptazione cfb

    Ciao a tutti ho un problema molto strano.
    Ho una classe XXX che lancia un thread che effettua una connessione via socket e riceve dei pacchetti criptati in aes-cfb a 128 bit.
    Ho testato la classe da sola lanciando il thread e tutto fila liscio.
    Ora ho una classe server che si mette in ascolto e quando riceve una connessione lancia il thread della classe XXX.
    Ora mi aspetterei che il comportamento sia lo stesso ma invece ottengo un errore di segmentation fault su questa funzione
    codice:
    int XXX::decriptaPacchetto(unsigned char * pacchetto, int len)
    {
        AES_KEY ekey;
    
        AES_set_encrypt_key(privKey, 128, &ekey);
    
        AES_cfb128_encrypt(pacchetto,pacchetto, len, &ekey,ivtemprx, &numdec, AES_DECRYPT);
        //cout<<"Il numero di numdec: "<<numdec<<endl;
    
        return 0;
    }
    o almeno questo è quello che sembra visto che se commento questa funzione il programma va.
    Se non sono stato chiaro cercherò di spiegarmi meglio
    Grazie

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Prova a vedere se è lo stesso problema.
    http://stackoverflow.com/questions/3...ion-on-openssl
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2010
    Messaggi
    34
    Ciao non ho ben capito quale fosse il problema descritto nel link che mi hai girato (sembra che non sapesse come far funzionare la libreria se non ho capito male).
    Io invece sono riuscito a far funzionare tutto, infatti se testo la classe XXX questa funziona a meraviglia ti posto un po di codice
    codice:
    int XXX::criptaPacchetto(unsigned char * pacchetto, int len)
    {
        AES_KEY ekey;
    
        AES_set_encrypt_key(privKey, 128, &ekey);
    
        AES_cfb128_encrypt(pacchetto,pacchetto, len, &ekey,ivtemptx, &numenc, AES_ENCRYPT);
    
        //cout<<"Il numero di numenc: "<<numenc<<endl;
    
        return 0;
    }
    
    int XXX::decriptaPacchetto(unsigned char * pacchetto, int len)
    {
        AES_KEY ekey;
    
        AES_set_encrypt_key(privKey, 128, &ekey);
    
        AES_cfb128_encrypt(pacchetto,pacchetto, len, &ekey,ivtemprx, &numdec, AES_DECRYPT);
        //cout<<"Il numero di numdec: "<<numdec<<endl;
    
        return 0;
    }
    
    void XXX::Run()
    {
        int i,j;
        unsigned char pacchetto[512];
        unsigned char buffer[512];
        char temp;
        int n_recv;
        list<Comandi>::iterator execomando;
    
        Socket * clientSock = new Socket();
    
        if(!clientSock->create())
        {
            throw SocketException ("Could not create client socket.");
            return;
        }
        if (!clientSock->connect(ip,port))
        {
            throw SocketException ("Impossibile connettersi");
            return;
        }
    
        clientSock->set_non_blocking(false);
    
        cout<<"Chiave privata: \n\t";
        PRINTPAC(privKey,16);
        memset(pacchetto,0x00,512);
        memset(pacchetto,0x00,16);
        setPublicVector();
        memcpy(ivtemprx,publicVector,16);
        memcpy(ivtemptx,publicVector,16);
    
        cout<<"Chiave pubblica: \n\t";
        PRINTPAC(publicVector,16);
        strcpy((char *)pacchetto,(char *)publicVector);
        cout<<"TX=>";
        PRINTPAC(pacchetto,16);
        if(!clientSock->send(pacchetto,16))
        {
            cout<<"Errore nell'invio\n";
        }
    
        while (!thread_stop)
        {
            if(!comandi.empty())
            {
                execomando = comandi.begin();
                comandi.pop_front();
                memset(pacchetto,0x00,512);
                switch(execomando->getComando())
                {
                    case ARM_PARTITION:
                    {
                        InsertProgram(pacchetto,execomando->getElemento(),2);
                        break;
                    }
                    case ARM_PARTIAL_PARTITION:
                    {
                        InsertProgram(pacchetto,execomando->getElemento(),5);
                        break;
                    }
                    case DISARM_PARTITION:
                    {
                        InsertProgram(pacchetto,execomando->getElemento(),0);
                        break;
                    }
                    case INCLUDE_ZONE:
                    {
                        IncludeZona(pacchetto,execomando->getElemento(),0);
                        break;
                    }
                    case EXCLUDE_ZONE:
                    {
                        IncludeZona(pacchetto,execomando->getElemento(),1);
                        break;
                    }
                    case BLOCK_PROGRAM:
                    {
                        BloccoProgrammatore(pacchetto,execomando->getElemento(),1);
                        break;
                    }case UNBLOCK_PROGRAM:
                    {
                        BloccoProgrammatore(pacchetto,execomando->getElemento(),0);
                        break;
                    }
                }
    
                if(!clientSock->send(pacchetto,11))
                    {
                        cout<<"Errore nell'invio\n";
                    }
    
                memset(buffer,'\0',512);
                    n_recv = clientSock->recv(buffer);
    
                    decriptaPacchetto(buffer,n_recv);
    #if (DEBUGCENT > 4)
                    cout<<"RX<=";
                    PRINTPAC(buffer,n_recv);
    #endif
    
                    ///Individuazione del carattere di start nel caso il buffer fosse sporco.
                    j=0;
                    temp = buffer[j];
                    while(temp != 0x02)
                    {
                        j++;
                        temp=buffer[j];
                    }
    
                    if(buffer[j+4] != 0x06)
                    {
                        comandi.push_back(*execomando);
                    }
    
            }
            for (i = 1; i<513; i++)
            {
    
                if (zone[i] > -1)
                {
                    memset(pacchetto,0x00,512);
                    RequestZonesState(pacchetto, i, i);
    
                    if(!clientSock->send(pacchetto,12))
                    {
                        cout<<"Errore nell'invio\n";
                    }
    
    
    
                    memset(buffer,'\0',512);
                    n_recv = clientSock->recv(buffer);
    #if (DEBUGCENT > 4)
                    cout<<"RX<=";
                    PRINTPAC(buffer,n_recv);
    #endif
                    decriptaPacchetto(buffer,n_recv);
    #if (DEBUGCENT > 4)
                    cout<<"RX<=";
                    PRINTPAC(buffer,n_recv);
    #endif
    
    ....
                }
            }
    
            ///Controllo dello stato delle partizioni.
            for (i = 1; i<33; i++)
            {
                if (partizioni[i] > -1)
                {
    
                    memset(pacchetto,'\0',512);
                    RequestProgramState(pacchetto, i, i);
                    clientSock->send(pacchetto,12);
    
                    memset(buffer,'\0',512);
                    n_recv = clientSock->recv(buffer);
                    decriptaPacchetto(buffer, n_recv);
    #if (DEBUGCENT > 4)
                    cout<<"RX<=";
                    PRINTPAC(buffer,n_recv);
    #endif
    .....
    
            }
            ///Richiesta stato centrale
            {
                memset(pacchetto,'\0',512);
                RequestCentraleState(pacchetto,1,1);
                clientSock->send(pacchetto,12);
    
                memset(buffer,'\0',512);
                n_recv = clientSock->recv(buffer);
    
                decriptaPacchetto(buffer, n_recv);
    #if (DEBUGCENT > 4)
                cout<<"RX<=";
                PRINTPAC(buffer,n_recv);
    #endif
    .......
                }
    
            }
    
        }
    }
    Poi c'è un metodo Start in una classe astratta che viene ereditata da XXX che lancia Run come thread
    codice:
    void Centrale::Start()
    {
        t1 = std::move(std::thread(&Centrale::Run,this));
        thread_stop = false;
    }
    se io nel mio main faccio
    codice:
    Centrale * test = new XXX();
    test->Start();
    ......
    test->WaitForCompletation();
    tutto funziona.
    Se io le faccio all'interno di un altro thread mi compare l'errore.

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2010
    Messaggi
    34
    Ok sono pirla avevo dimenticato di inizializzare ivtemprx e numdec
    la cosa che non capisco è perchè quando tesvo solo la classe non ottenevo quell'errore

  5. #5
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Piccolo consiglio: quando devi usare puntatori e non devi copiarteli il giro, usa std::unique_ptr.
    codice:
    // NO!
    //Socket * clientSock = new Socket();
    std::unique_ptr<Socket> clientSock(new Socket());
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.