简体   繁体   中英

Cannot read X509 object into a temporary file using openssl for windows C programming

int _tmain(int argc, _TCHAR* argv[])
{
    X509 *x;
    EVP_PKEY *pkey;
    PKCS12 *p12;
    STACK_OF(X509) *ca=NULL;
    FILE *fp;
    int code;



    CRYPTO_malloc_init();
    SSL_library_init();
    SSL_load_error_strings();
    ERR_load_BIO_strings();
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();


    x = X509_new();


    code = fopen_s(&fp,PRIVATE_CERTIFICATE, "rb"); 
    //fp = fopen(PRIVATE_CERTIFICATE, "rb");

    p12 = d2i_PKCS12_fp(fp, NULL);

    fclose(fp);

    if (!PKCS12_parse(p12, KEYPASS, &pkey, &x, &ca)) {
        printf(" Error while parsing\n");
    }
    PKCS12_free(p12);

    code = fopen_s(&fp,TEMP_STORE_CERTIFICATE, "w"); 



    PEM_write_X509(fp, x);
    fclose(fp);



    //RSA Private Certificate
    fp = fopen(TEMP_STORE_KEY_CERTIFICATE,"w");
    PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
    fclose(fp);


    return 0;
}

When the execution gets up to PEM_write_X509, it gets stuck and does not proceed. I guess, it enters into the infinite loop, and the execution doesnt goes beyond that. A console simply opens up which doesnt goes away. Someone please help

File pointed by fp is opened into "rb" mode where PEM_write_fp will try to write on a file which is already opened into read mode. Close the file and open it into write mode before writing.

Moreover, it does not seems good to overwrite the content of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM