RSA_verify()导致分段错误

时间:2017-07-09 10:02:26

标签: c openssl segmentation-fault

下面的Teh函数在到达RSA_verify()部分时会导致分段错误。我是一个初学者,所以我很难找到问题的原因。也许有人可以指出我做错了什么。提前谢谢,这将非常有用。

以下是代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/dsa.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>

#define BUF 20000
#define DSS 20

int matchCipherToSign(unsigned char *signPath, char *cipherPath) {
unsigned char *bufCipher = malloc(BUF);
unsigned char *sha = malloc(SHA_DIGEST_LENGTH);
unsigned char *bufSign = malloc(BUF);
unsigned int retSign;
int ret=0;
int retCipher;
FILE *key;
RSA *rsa = RSA_new();
EVP_MD_CTX ctx;

key = fopen("key.bin", "rb");
if (key == NULL){
    printf("Couldn't open file key.bin.\n");
    exit(EXIT_FAILURE);
}

retSign = readFile(signPath, bufSign);
if (retSign == 0){
    printf("Couldn't read file %s.\n", signPath);
    exit(EXIT_FAILURE);
}

retCipher = readFile(cipherPath, bufCipher);
if (retCipher == 0){
    printf("Couldn't open file %s.\n", cipherPath);
    exit(EXIT_FAILURE);
}

rsa = PEM_read_RSA_PUBKEY(key, &rsa, NULL, NULL);
fclose(key);

if (1!=EVP_DigestInit(&ctx, EVP_sha1())) {
    printf("EVP_DigestInit Error.\n");
    exit(EXIT_FAILURE);
}
if (1!=EVP_DigestUpdate(&ctx, bufCipher, retCipher)) {
    printf("VP_DigestUpdate Error.\n");
    exit(EXIT_FAILURE);
}
if (1!=EVP_DigestFinal(&ctx, sha, NULL)) {
    printf("EVP_DigestFinal Error.\n");
    exit(EXIT_FAILURE);
}

ret = RSA_verify(NID_sha1, sha, SHA_DIGEST_LENGTH, bufSign, sizeof(bufSign), rsa);

RSA_free(rsa);
free(bufCipher);
free(bufSign);
free(sha);

return ret;
}

感谢您的帮助!

0 个答案:

没有答案