PEM_read_bio_PrivateKey返回NULL

时间:2018-09-30 00:31:23

标签: openssl private-key libcrypto

我有一个包含私钥的JSON文件(已从Google Developer Console for Service Accounts下载)。对于构建JWT,当我尝试使用EVP_PKEY将私钥作为PEM_read_bio_PrivateKey()结构加载时,它将返回NULL(错误)。

代码

const char *private_key = 
   json_object_get_string(json_object_object_get(token_file_obj, 
                                                   "private_key"));
printf("Private key --> [%s]\n", private_key);

/* Not sure whether this is needed, just added all three in case needed X-D */
OpenSSL_add_all_algorithms();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();

BIO *bio = BIO_new_mem_buf(private_key, (int) sizeof(private_key));
if (bio == NULL) {
    printf("Bio is null **** \n");
    return FAILED_OPERATION;
}

EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, 0, NULL);
if (pkey == NULL) {
    printf("Failed operation error code %d\n", errno);
    return FAILED_OPERATION;
}

输出

注意:我已更改了从Output复制的私钥。

Private key --> [-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFBASCBKYwggSiAgEAAoIBAQC9UzuLuU7z0XTS
aixN0uQSGxxw+Ngbm2CyRr01Fm3Zoi4Df1yoVJKxRLscJgzv4hkVzGXsniUIVuqY
peeZ9VAPdqmx1Oz3w/xPUcy6clINTRgg+/xKUQetuxplN4EY9YuQYp8PVqHO3AiZ
WE+VufX0saITyikMCGvIcNdfEky/Kf7CtniEKw1g0eHzbHdxk06wzYmvHxodVwC0
mf8lQ62uFNvw3t0TVaENm0B64NSL+7uCHPZICxJNW352eKgLxisOgV7v6xIYlFz/
O5xoThkRoychsc338/3Pscqd6HVeQkSG/T4lQTl2TFVwJHEF422d8d9ybXFQ7Xe/
qPbhd2A1AgMBAAECggEACtHDHSLHp4jgiXbOVds3s+4vA5iQ5xD712xR+0LvH6kT
qJ51TlhY8r74B22IPzw3TUbbS0ilWqLm8LbIc8SwYNXZ8EL4ENe3J0xILB6E4GQ3
vQjRe+jCDNRct4ySuNutcGCm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4IGN9rebkzd
UwOpaDRE32RuQyFfP/tMqA==
-----END PRIVATE KEY-----
]
Failed operation error code 0

我尝试过的事情

  1. 用换行符替换JSON文件中私钥中的\n,并将其与使用printf()打印的私钥进行比较。两者具有相同的校验和。
  2. 从私钥中删除了-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----并尝试-无效。

0 个答案:

没有答案