# Path: pki/helper.py
# def subject_for_object(obj):
#     """Return a subject string.
#     
#     A OpenSSL compatible subject string is returned.
#     """
#     
#     subj = '/CN=%s/C=%s/ST=%s/localityName=%s/O=%s' % ( obj.common_name,
#                                                         obj.country,
#                                                         obj.state,
#                                                         obj.locality,
#                                                         obj.organization,
#                                                       )
#     
#     if obj.OU:
#         subj += '/organizationalUnitName=%s' % obj.OU
#     
#     if obj.email:
#         subj += '/emailAddress=%s' % obj.email
#     
#     return subj
# 
# Path: pki/settings.py
# PKI_OPENSSL_BIN = getattr(settings, 'PKI_OPENSSL_BIN', '/usr/bin/openssl')
# 
# PKI_OPENSSL_CONF = getattr(settings, 'PKI_OPENSSL_CONF', os.path.join(PKI_DIR, 'openssl.conf'))
# 
# PKI_DIR = getattr(settings, 'PKI_DIR', os.path.join(PKI_APP_DIR, 'PKI'))
# 
# PKI_OPENSSL_TEMPLATE = getattr(settings, 'PKI_OPENSSL_TEMPLATE', 'pki/openssl.conf.in')
# 
# PKI_SELF_SIGNED_SERIAL = getattr(settings, 'PKI_SELF_SIGNED_SERIAL', 0x0)
# 
# PKI_CA_NAME_BLACKLIST = ('_SELF_SIGNED_CERTIFICATES',)


import os
import re
import string
import random
import pki.models
from subprocess import Popen, PIPE, STDOUT
from shutil import rmtree
from logging import getLogger
from django.template.loader import render_to_string
from pki.helper import subject_for_object
from pki.settings import PKI_OPENSSL_BIN, PKI_OPENSSL_CONF, PKI_DIR, PKI_OPENSSL_TEMPLATE, \
                         PKI_SELF_SIGNED_SERIAL, PKI_CA_NAME_BLACKLIST
    from hashlib import md5 as md5_constructor
    from md5 import new as md5_constructor
             'private': 0700,
             'crl'    : 0755,
           }
    
    try:
        # create base PKI directory if necessary
        if not os.path.exists(PKI_DIR):
            logger.info('Creating base PKI directory %s' % PKI_DIR)
            os.mkdir(PKI_DIR, 0700)
        
        # list of old CA directories for possible purging
        purge_dirs = set([os.path.join(PKI_DIR, d) for d in os.listdir(PKI_DIR)
                          if os.path.isdir(os.path.join(PKI_DIR, d))])
        
        # loop over CAs and create necessary filesystem objects
        for ca in ca_list:
            ca_dir = os.path.join(PKI_DIR, ca.name)
            
            # create CA directory if necessary
            if not ca_dir in purge_dirs:
                logger.info("Creating base directory for new CA %s" % ca.name)
                os.mkdir(ca_dir)
                
                # create nested directories for key storage with proper permissions
                for d, m in dirs.items():
                    os.mkdir(os.path.join(ca_dir, d), m)
                
                initial_serial = 0x01
                
                try:

# Path: pki/helper.py
# def subject_for_object(obj):
#     """Return a subject string.
#     
#     A OpenSSL compatible subject string is returned.
#     """
#     
#     subj = '/CN=%s/C=%s/ST=%s/localityName=%s/O=%s' % ( obj.common_name,
#                                                         obj.country,
#                                                         obj.state,
#                                                         obj.locality,
#                                                         obj.organization,
#                                                       )
#     
#     if obj.OU:
#         subj += '/organizationalUnitName=%s' % obj.OU
#     
#     if obj.email:
#         subj += '/emailAddress=%s' % obj.email
#     
#     return subj
# 
# Path: pki/settings.py
# PKI_OPENSSL_BIN = getattr(settings, 'PKI_OPENSSL_BIN', '/usr/bin/openssl')
# 
# PKI_OPENSSL_CONF = getattr(settings, 'PKI_OPENSSL_CONF', os.path.join(PKI_DIR, 'openssl.conf'))
# 
# PKI_DIR = getattr(settings, 'PKI_DIR', os.path.join(PKI_APP_DIR, 'PKI'))
# 
# PKI_OPENSSL_TEMPLATE = getattr(settings, 'PKI_OPENSSL_TEMPLATE', 'pki/openssl.conf.in')
# 
# PKI_SELF_SIGNED_SERIAL = getattr(settings, 'PKI_SELF_SIGNED_SERIAL', 0x0)
# 
# PKI_CA_NAME_BLACKLIST = ('_SELF_SIGNED_CERTIFICATES',)


# Path: pki/openssl.py
import os
import re
import string
import random
import pki.models
from subprocess import Popen, PIPE, STDOUT
from shutil import rmtree
from logging import getLogger
from django.template.loader import render_to_string
from pki.helper import subject_for_object
from pki.settings import PKI_OPENSSL_BIN, PKI_OPENSSL_CONF, PKI_DIR, PKI_OPENSSL_TEMPLATE, \
                         PKI_SELF_SIGNED_SERIAL, PKI_CA_NAME_BLACKLIST
    from hashlib import md5 as md5_constructor
    from md5 import new as md5_constructor

             'private': 0700,
             'crl'    : 0755,
           }
    
    try:
        # create base PKI directory if necessary
        if not os.path.exists(PKI_DIR):
            logger.info('Creating base PKI directory %s' % PKI_DIR)
            os.mkdir(PKI_DIR, 0700)
        
        # list of old CA directories for possible purging
        purge_dirs = set([os.path.join(PKI_DIR, d) for d in os.listdir(PKI_DIR)
                          if os.path.isdir(os.path.join(PKI_DIR, d))])
        
        # loop over CAs and create necessary filesystem objects
        for ca in ca_list:
            ca_dir = os.path.join(PKI_DIR, ca.name)
            
            # create CA directory if necessary
            if not ca_dir in purge_dirs:
                logger.info("Creating base directory for new CA %s" % ca.name)
                os.mkdir(ca_dir)
                
                # create nested directories for key storage with proper permissions
                for d, m in dirs.items():
                    os.mkdir(os.path.join(ca_dir, d), m)
                
                initial_serial = 0x01
                
                try:

                    if not ca.parent and int(PKI_SELF_SIGNED_SERIAL) > 0: