1
votes

I indent to verify 'google inapp billing signature' in server side implemented c or c++

I found the way through php(Android in-app billing signature verification in php server) and advice it could using EVP_Verify... functions in openssl..but to use :

int EVP_VerifyFinal(EVP_MD_CTX *ctx,unsigned char *sigbuf, unsigned int siglen,EVP_PKEY *pkey);

It needs instance of 'EVP_PKEY* pkey' structure as a parameter...but I don't have it!

I beleive there is the way convert 'Google InApp Billing signature string(base64 encoded SHA1-with-RAS signature with PKCS#1 padding)' to EVP_PKEY..but I don't know how to it.

How CAN I PASS it as a parameter for EVP_VerifyFinal function??

int main()
{
    OPENSSL_config(NULL);
    OpenSSL_add_all_digests();
    ERR_load_crypto_strings();

    EVP_MD_CTX* ctx = EVP_MD_CTX_create();
    const EVP_MD* md = EVP_get_digestbyname("SHA1");
    if(0 == EVP_VerifyInit_ex(ctx, md, NULL))
    {
        std::cerr << "init error" << std::endl;
        return 0;
    }

    const char* data = "{\"orderId\":\"12999763169054705758.1310724241212373\",\"packageName\":\"com.company.game.google\",\"productId\":\"game_product_001\",\"purchaseTime\":1358227642000,\"purchaseState\":0,\"developerPayload\":\"12322144\",\"purchaseToken\":\"}";
    if(0 == EVP_VerifyUpdate(ctx, (void*)data, strlen(data)))
    {
        std::cerr << "update error" << std::endl;
        return 0;
    }

    const char* key_context = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh0UDlUA1aYPvfRFq6qUjDVG/fi5EetC6LsjdT/WNmHUAy0muvuOTFfQEzBoELciDfh23VXgwVnb/XsfuvQrCgtnQbuMKsj+sDhofLjeq8TznEMlQcJ//0LsGSM8rRVHw72BYA2mSVKi04k1GIicB9J25c2f+eIwF7lEWJlWqVDlNqOS7GIIjnq3HPhqki3ZRSA9c";

    EVP_PKEY* pub_key = EVP_PKEY_new();
    if(NULL == pub_key)
    {
        std::cerr << "evp_pkey new error" << std::endl;
        return 0;
    }

    // I have to call 'EVP_VerifyFinish' here!! but I don't know how to!!
    ERR_remove_state(0);
    ERR_free_strings();

    return 0;
}

please somebody help me!!

2

2 Answers

1
votes

Code not tested but it should be something like this:

BIO *mem= BIO_new(BIO_s_mem());
BIO_puts(mem, "your data");
RSA *cipher;
PEM_read_bio_RSAPublicKey(mem, &cipher, NULL, NULL);
EVP_PKEY *pkey  = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pkey, cipher);
0
votes

thanks for answering.. but i did it like below.. (It works find..I've tested!!)

ps. Base64Decode funciton in the the code..you may find many sources on the internet..

int Verify_GoogleInappBilling_Signature(const char* data, const char* signature, const char* pub_key_id)
{
    std::shared_ptr<EVP_MD_CTX> mdctx = std::shared_ptr<EVP_MD_CTX>(EVP_MD_CTX_create(), EVP_MD_CTX_destroy);
    const EVP_MD* md = EVP_get_digestbyname("SHA1");
    if(NULL == md)
    {
        return -1;
    }
    if(0 == EVP_VerifyInit_ex(mdctx.get(), md, NULL))
    {
        return -1;
    }

    if(0 == EVP_VerifyUpdate(mdctx.get(), (void*)data, strlen(data)))
    {
        return -1;
    }
//!!!!!!!!!!!!!!!!!!!
    std::shared_ptr<BIO> b64 = std::shared_ptr<BIO>(BIO_new(BIO_f_base64()), BIO_free);
    BIO_set_flags(b64.get(),BIO_FLAGS_BASE64_NO_NL);

    std::shared_ptr<BIO> bPubKey = std::shared_ptr<BIO>(BIO_new(BIO_s_mem()), BIO_free);
    BIO_puts(bPubKey.get(),pub_key_id);
    BIO_push(b64.get(), bPubKey.get());

    std::shared_ptr<EVP_PKEY> pubkey = std::shared_ptr<EVP_PKEY>(d2i_PUBKEY_bio(b64.get(), NULL), EVP_PKEY_free);
//!!!!!!!!!!!!!!!!!!!
    std::string decoded_signature = Base64Decode(std::string(signature));
    return EVP_VerifyFinal(mdctx.get(), (unsigned char*)decoded_signature.c_str(), decoded_signature.length(), pubkey.get());
}