I have a .p8 file download from Apple's iOS developer portal for PushNotifications.
I am trying to load the P8 file with the following code in Python:
from ctypes import *
OpenSSL = cdll.LoadLibrary("/opt/local/lib/libssl.1.0.0.dylib")
def loadPrivateKey(path):
bio = OpenSSL.BIO_new_file(path.encode("utf-8"), "rb".encode("utf-8"))
#pKey = OpenSSL.PEM_read_bio_PrivateKey(bio, None, None, None)
OpenSSL.BIO_free(bio)
def main():
loadPrivateKey("/users/Brandon/Desktop/APNsAuthKey.p8")
main()
However, it seg faults on the line: OpenSSL.BIO_free(bio). I have checked if bio has a value other than 0 (it does).
If I do the same thing in C, it works:
struct EVP_PKEY* loadPrivateKey(const char* path)
{
struct BIO* bio = BIO_new_file(path, "rb");
struct EVP_PKEY* pKey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
BIO_free(bio);
return pKey;
}
int main()
{
struct EVP_PKEY* pKey = loadPrivateKey("/users/Brandon/Desktop/APNsAuthKey.p8");
EVP_PKEY_free(pKey);
}
I have verified in C that the code works and I have used it to sign data. I have been unable to do the same in Python3 because freeing the BIO segfaults with code 11.
I have tried pyOpenssl, and it also segfaults when I try to read the key with loadprivatekey(FILETYPE_PEM, key) where key is the contents of the P8 file.
Any ideas why it would segfault?
pyOpenSSLsegfaults even though it is usingffi. I ended up using the solution I posted since none of the third party libraries work atm and I can't find any other solution.