Hi,
thank you Steve my verification function works now. But I want to check the RSA key before I use it. If I run "if(RSA_check_key(r) != 1) return -5;" I get a "Segmentation fault ". It works without this line.


#include <string>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/bn.h>
#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/objects.h>
#include <openssl/evp.h>
#include <iostream>
#include "../base64/base64.h"
#include "verify.h"


using namespace std;


int verify::verifyHash(const string & hash, const string & sign,
                        string & cert ){
    if(hash.length() != 20) return -1;
    if(sign.length() != 128) return -2;
    char sha1[21];
    char signature[129];
    RSA *r;
    X509 *x509Cert;
    hash.copy(sha1, 20);
    sign.copy(signature, 128);
    char* buffer = new(char[cert.length() + 1]);
    cert.copy(buffer,cert.length());
    unsigned char *cp = (unsigned char *) buffer;
    x509Cert = d2i_X509(0, &cp , cert.length());
    if (x509Cert == NULL) return -3;
    EVP_PKEY* evpKey = X509_get_pubkey(x509Cert);
    if (evpKey == 0) return -4;
    r = EVP_PKEY_get1_RSA(evpKey);
    if (r == 0) return -4;
    BN_CTX *c;
    int ret;

    if(RSA_check_key(r) != 1) return -5;

    if(!(c = BN_CTX_new())) return -6;
    if(!RSA_blinding_on(r, c)){
        BN_CTX_free(c);
        return -7;
    };
    ret = RSA_verify(NID_sha1, (unsigned char *) sha1, 20,
                            (unsigned char *) signature, 128, r);
    RSA_blinding_off(r);
    BN_CTX_free(c);
    RSA_free(r);
    if (ret < 0) return -8;
    if(ret == 1) return 1;
    return 0;
};


Thanks


Thomas

Reply via email to