Skip to content

Commit

Permalink
Merge pull request #1 from hammer/master
Browse files Browse the repository at this point in the history
Update create_csr to return CSR, upgrade to Python 3, publish to PyPI
  • Loading branch information
jandd committed Apr 22, 2014
2 parents 115a017 + 13b4c6f commit 779a62e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
include version.txt
include LICENSE


21 changes: 13 additions & 8 deletions pkiutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-

from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
Expand All @@ -7,7 +7,9 @@
from pyasn1.codec.der import encoder, decoder
from pyasn1.type import univ
import base64
import binascii
import logging
import collections

log = logging.getLogger(__name__)

Expand All @@ -16,7 +18,7 @@

def _der_to_pem(derbytes, typestr):
pem = "-----BEGIN {0}-----\n".format(typestr)
pem += base64.encodestring(derbytes)
pem += base64.encodestring(derbytes).decode()
pem += "-----END {0}-----".format(typestr)
return pem

Expand Down Expand Up @@ -48,7 +50,7 @@ def create_rsa_key(bits=2048,
raise Exception(
"passphrase is only supported for PEM encoded private keys")
rsakey = RSA.generate(bits)
if passphrase and callable(passphrase):
if passphrase and isinstance(passphrase, collections.Callable):
passphrase = passphrase()
output = rsakey.exportKey(format=format, passphrase=passphrase)
if keyfile:
Expand Down Expand Up @@ -124,7 +126,7 @@ def _build_dn(dnspec):
dndict[key] = value
dnparts = rfc2314.RDNSequence()
count = 0
for key, value in dndict.iteritems():
for key, value in dndict.items():
rdn = rfc2314.RelativeDistinguishedName()
rdn.setComponentByPosition(0, _build_dn_component(key, value))
dnparts.setComponentByPosition(count, rdn)
Expand All @@ -145,7 +147,8 @@ def _build_subject_publickey_info(key):
def _build_signature(key, certreqinfo):
hashvalue = SHA.new(encoder.encode(certreqinfo))
signer = PKCS1_v1_5.new(key)
signaturevalue = "'{0}'H".format(signer.sign(hashvalue).encode('hex'))
signaturevalue = "'{0}'H".format(binascii.hexlify(signer.sign(hashvalue)).decode())
logging.debug("signaturevalue: %s" % signaturevalue)

return rfc2314.Signature(signaturevalue)

Expand All @@ -156,7 +159,7 @@ def _ip_str_to_octets(ipstr):
af = AF_INET6
else:
af = AF_INET
return inet_pton(af, ipstr).encode('hex')
return binascii.hexlify(inet_pton(af, ipstr)).decode()


def _build_general_name(generalname):
Expand All @@ -179,7 +182,7 @@ def _build_general_name(generalname):


def _build_subject_alt_name(value):
if isinstance(value, basestring):
if isinstance(value, str):
value = (value,)
retval = rfc2314.SubjectAltName()
count = 0
Expand Down Expand Up @@ -268,7 +271,7 @@ def _build_attributes(attributes, attrtype):
return attrtype
attr = attrtype.clone()
count = 0
for key, value in attributes.items():
for key, value in list(attributes.items()):
attritem = _build_attribute(key, value)
if attritem:
attr.setComponentByPosition(count, attritem)
Expand Down Expand Up @@ -326,3 +329,5 @@ def create_csr(key, dn, csrfilename=None, attributes=None):
with open(csrfilename, 'w') as csrfile:
csrfile.write(output)
log.info("generated certification request:\n\n%s", output)
return output

3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
description='a set of public key infrastructure utilities',
long_description=DESCRIPTION,
install_requires=['pycrypto', 'pyasn1', 'pyasn1_modules'],
setup_requires=['vcversioner'],
vcversioner={},
version=0.1,
author=__author__,
author_email='jan@dittberner.info',
url='https://github.com/jandd/python-pkiutils',
Expand Down

0 comments on commit 779a62e

Please sign in to comment.