Thread: segmentation fault issue

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    27

    segmentation fault issue

    Im writing a program that is supposed to sign and verify a file using DSA and is supposed to include pem objects that set the parameters and keys.Im testing it involving a sentence first,but when I try to run it I get a segmentation fault. Any help would be appreciated.

    Code:
    #include <stdio .h>
    
    # include <string .h>
    
    # include <openssl /evp.h>
    
    # include <openssl /dsa.h>
    
    int main (int argc , char ** argv )
    
    {
    
    EVP_MD_CTX ctx;
    
    DSA *dsa ;
    
    char str [] = " This is a message that will be signed ";
    
    unsigned char *sig;
    
    int len , siglen ;
    
    unsigned char md[ EVP_MAX_MD_SIZE ];
    
    FILE *fp;
    
    fp = fopen (" dsa-param.pem", "w+");
    
    FILE *fp2;
    
    fp2 = fopen (" dsa-priv.pem", "w+");
    
    FILE *fp3;
    
    fp3 = fopen (" dsa-pub.pem", "w+");
    
     
    
     
    
     
    
    /* Compute the SHA1 digest of str */
    
    EVP_DigestInit (& ctx , EVP_sha1 ());
    
    EVP_DigestUpdate (& ctx , str , strlen (str ));
    
    EVP_DigestFinal (& ctx , md , &len );
    
     
    
    /* Generate DSA parameters */
    
    DSA * PEM_read_DSAparams ( FILE *fp , DSA ** dsa ,
    
    pem_password_cb * callback , void * cb_arg );
    
     
    
    /* Generate DSA keys */
    
    DSA * PEM_read_DSA_PUBKEY ( FILE *fp3 , DSA ** dsa ,
    
    pem_password_cb * callback , void * cb_arg );
    
    DSA * PEM_read_DSAPrivateKey ( FILE *fp2 , DSA ** rsa ,
    
    pem_password_cb * callback , void * cb_arg );
    
     
    
    /* Allocate buffer space for signature */
    
    sig = ( unsigned char *) malloc ( DSA_size (dsa ));
    
     
    
    /* Sign message digest */
    
    DSA_sign (fp3, md , len , sig , &siglen , dsa );
    
     
    
    /* Verify signature */
    
    if ( DSA_verify (fp2 , md , len , sig , siglen , dsa) == 1)
    
    printf (" Signature is verified !\n");
    
    else
    
    printf (" Signature verification failed ...\ n");
    
    /* Destroy the DSA object */
    
    DSA_free (dsa );
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by kakashi316 View Post
    Code:
    #include <stdio .h>
    /* Generate DSA parameters */
    
    DSA * PEM_read_DSAparams ( FILE *fp , DSA ** dsa ,
    
    pem_password_cb * callback , void * cb_arg );
    
     
    
    /* Generate DSA keys */
    
    DSA * PEM_read_DSA_PUBKEY ( FILE *fp3 , DSA ** dsa ,
    
    pem_password_cb * callback , void * cb_arg );
    
    DSA * PEM_read_DSAPrivateKey ( FILE *fp2 , DSA ** rsa ,
    
    pem_password_cb * callback , void * cb_arg );
    None of those lines actually do anything. They're just function prototypes in the middle of your main function. You should just pass the name of a variable, with no type information, similar to how you do with DSA_Verify on line 87.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    What does the stack trace look like? Which line is the segmentation fault on? Have you run this through a debugger?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm guessing the reason why you didn't catch what anduril said about prototypes is because of your indentation. Your current style with a space and a newline between everything makes the error a lot harder to notice on first glance, not to mention the fact that it's annoying to read.

    I'm also guessing that your wild spamming of the spacebar could be responsible for more problems as well, such as the path in fopen(), and the files in the #includes. Not sure though, I haven't tried to compile it.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Seek medical attention.

    Whitespace-itis can be deadly if left untreated.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I was too hasty in my first post and missed a few things. You need to check the return value of fopen and malloc. If either one fails (returns NULL), you need to print an error and exit. Look into perror() or errno/strerror() for printing meaningful messages.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    yes make use of the feof (fp) and the other to check whether the problem arised from opeining the file stream...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Nyah Check View Post
    yes make use of the feof (fp) and the other to check whether the problem arised from opeining the file stream...
    NO! Nyah Check, your desire to help is admirable, but nearly all of the advice give is just plain wrong. Not just a little bit wrong, completely wrong in a way that will cause serious problems such as undefined behavior, seg faults and more, and you will only confuse those that are trying to learn C. You fail to study/research the topics you try to help others with before replying to their thread, so you don't learn and you give incorrect advice. A simple 5 line test program, or a quick read of the C standard, and you would know that feof is wrong in this case. You are not yet proficient enough in C to help others, so please, just stop giving advice. You also have a habit of replying to threads that already have a complete answer to the question, so even in the rare case that you do give correct advice, you add absolutely no value to the thread. This sounds harsh, but I do encourage you to continue studying and improving your C skills and, to keep posting back here when you have trouble. Eventually, you will reach a point where you can contribute your knowledge of C to this community in a meaningful way. But now is not that time.

    feof is a function that determines if you read to the end of a file. If the file was not opened, and the file pointer is null, you will likely have a seg fault or undefined behavior. The same goes for the ferror and most (or maybe all) of the file functions. Those functions only work on files that have been opened successfully.

    To check if a file was not opened successfully, simply check the return value of fopen to see if it is NULL. That is the correct way, and that is the way I already told kakashi316 to do it.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok thanks for highlighting that....better my skills.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault issue
    By audifanatic518 in forum C++ Programming
    Replies: 9
    Last Post: 04-28-2012, 09:02 PM
  2. Segmentation fault / using std::map
    By Alexandre in forum C++ Programming
    Replies: 8
    Last Post: 08-26-2011, 07:20 AM
  3. Fscanf segmentation fault issue (I think)
    By zouru in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 03:00 PM
  4. Segmentation fault issue
    By Stenland in forum C Programming
    Replies: 12
    Last Post: 09-19-2009, 04:56 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM