Question

I am having difficulties debugging a django/python application that requires a public key to be used to sign a certificate. 1. I created a key in PEM format from my private key using the following command

openssl rsa -pubout -in ~/.ssh/id_rsa > samplePEMKey.pub

The key is given below

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8K/LfOBi+QrINSseqWwc
JGo4uE49Dc42zF2Jwbmc6iXIln8AWX5M+yn15dYOXzbHGbopH8bpF9CsJ/DPHg87
D1At1XwAy5aR5w7pXE//5p/saW50Sm/eY6Nugs9hgAXUTcMMAEXCIG17dLoUzhwE
YdzuPm350h39bTmI7Fz95koTbsKJUi+6337GJ3cWKxMBkEmWvpGo4Nko01xtEnUZ
GweKexr1roE8jZPpzuNeUy+S5tvH5emyBIBdroBVxBtz1OUIoP0oAOrQ5RtRPAcT
TsSWjMtyF2gO5GxeMErRqnLAtWJs4QlaJacVnMMTAgRjrGkUZdsqngM/fjQXfeZV
SwIDAQAB
-----END PUBLIC KEY-----

In my python app I am using OpenSSL and trying to load the key using

pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, samplePEMKey)

When running the django app I get the following error at the above line.

Request Method:     POST
Request URL:    http://127.0.0.1:8000/snapshotuploader/single/
Django Version:     1.3.1
Exception Type:     Error
Exception Value:    

[('PEM routines', 'PEM_read_bio', 'no start line')]

Here's how I read the public key from the file

    file_path = '/home/XXXX/co/certserver/LicenseServer/home_folder/dldl/samplePEMKey.pub' 
try: 
  fin = open(file_path, 'r') 
except IOError: 
  return HttpResponse('IO Error opening uploaded file.') 
try: 
  samplePEMKey = fin.read() 
  fin.close() 
except IOError: 
  return HttpResponse('IO Error reading uploaded file.') 

Appreciate any help in solving this issue. Thanks in advance.

Was it helpful?

Solution

Took a while to spot it, but you are trying to read a public key with a private key method. If you want to use load_privatekey you should use the original id_rsa as input.

I don't remember how to do the same with OpenSSL.crypto, but at least with the pycrypto toolkit's RSA implementation your sample key imports just fine:

>>> kt = open("samplePEMKey.pub").read()
>>> from Crypto.PublicKey import RSA
>>> key = RSA.import_key(kt)
>>> key
<_RSAobj @0x109916d40 n(2048),e>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top