# Path: pki/helper.py
# def get_pki_icon_html(img, title="", css="centered", id=""):
#         """Return HTML for given image.
#         
#         Can add optional alt and title parameters.
#         """
#         
#         if css:
#             css_class = 'class=%s' % css
#         else:
#             css_class = ''
#         
#         img_path = os.path.join(PKI_BASE_URL, MEDIA_URL, 'pki/img', img)
#         return '<img id="%s" %s src="%s" alt="%s" title="%s"/>' % (id, css_class, img_path, title, title)
# 
# Path: pki/openssl.py
# def refresh_pki_metadata(ca_list):
#     def __init__(self, instance):
#     def exec_openssl(self, command, env_vars=None):
#     def generate_key(self):
#     def generate_self_signed_cert(self):
#     def generate_csr(self):
#     def generate_der_encoded(self):
#     def remove_der_encoded(self):
#     def generate_pkcs12_encoded(self):
#     def remove_pkcs12_encoded(self):
#     def remove_complete_certificate(self):
#     def sign_csr(self):
#     def revoke_certificate(self, ppf):
#     def generate_crl(self, ca=None, pf=None):
#     def update_ca_chain_file(self):
#     def get_serial_from_cert(self):
#     def get_hash_from_cert(self):
#     def get_revoke_status_from_cert(self):
#     def dump_certificate(self):
#     def rollback(self):
# class Openssl():
# 
# Path: pki/settings.py
# MEDIA_URL = getattr(settings, 'MEDIA_URL')
# 
# PKI_DEFAULT_COUNTRY = getattr(settings, 'PKI_DEFAULT_COUNTRY', 'DE')
# 
# PKI_ENABLE_GRAPHVIZ = getattr(settings, 'PKI_ENABLE_GRAPHVIZ', False)
# 
# PKI_ENABLE_EMAIL = getattr(settings, 'PKI_ENABLE_EMAIL', False)
# 
# PKI_PASSPHRASE_MIN_LENGTH = getattr(settings, 'PKI_PASSPHRASE_MIN_LENGTH', 8)
# 
# PKI_DEFAULT_KEY_LENGTH = getattr(settings, 'PKI_DEFAULT_KEY_LENGTH', 1024)


import os
import re
import datetime
from logging import getLogger
from django.db import models
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.core.validators import MinLengthValidator, MinValueValidator, RegexValidator, URLValidator
from django.core.exceptions import ValidationError
from django.contrib.admin.filterspecs import FilterSpec, RelatedFilterSpec
from pki.helper import get_pki_icon_html
from pki.openssl import Openssl, md5_constructor, refresh_pki_metadata
from pki.settings import MEDIA_URL, PKI_DEFAULT_COUNTRY, PKI_ENABLE_GRAPHVIZ, \
                         PKI_ENABLE_EMAIL, PKI_PASSPHRASE_MIN_LENGTH, PKI_DEFAULT_KEY_LENGTH
                    from pki.views import chain_recursion as r_chain_recursion
        return '<div id="clock_container"><img src="%spki/img/clock-frame.png" style="margin-right:5px"/><span id="clock"></span></div>' % MEDIA_URL
    
    CA_Clock.allow_tags = True
    CA_Clock.short_description = "CA clock"
    
    def Update_Changelog(self, obj, user, action, changes):
        """Update changelog for given object"""
        
        PkiChangelog(model_id=ContentType.objects.get_for_model(obj).pk, object_id=obj.pk, action=action, user=user, changes="; ".join(changes)).save()
    
    def Delete_Changelog(self, obj):
        """Delete changelogs for a given object"""
        
        PkiChangelog.objects.filter(model_id=ContentType.objects.get_for_model(obj).pk, object_id=obj.pk).delete()
    
##------------------------------------------------------------------##
## Certificate authority class
##------------------------------------------------------------------##

class CertificateAuthority(CertificateBase):
    """Certificate Authority model"""
    
    ##---------------------------------##
    ## Model definition
    ##---------------------------------##
    
    common_name       = models.CharField(max_length=64, unique=True)
    name              = models.CharField(max_length=64, unique=True, validators=[RegexValidator("[a-zA-Z0-9-_\.]+", message='Name may only contain characters in range a-Z0-9_-.')], \
                                         help_text="Only change the suggestion if you really know what you're doing")
    parent            = models.ForeignKey('self', blank=True, null=True)

# Path: pki/helper.py
# def get_pki_icon_html(img, title="", css="centered", id=""):
#         """Return HTML for given image.
#         
#         Can add optional alt and title parameters.
#         """
#         
#         if css:
#             css_class = 'class=%s' % css
#         else:
#             css_class = ''
#         
#         img_path = os.path.join(PKI_BASE_URL, MEDIA_URL, 'pki/img', img)
#         return '<img id="%s" %s src="%s" alt="%s" title="%s"/>' % (id, css_class, img_path, title, title)
# 
# Path: pki/openssl.py
# def refresh_pki_metadata(ca_list):
#     def __init__(self, instance):
#     def exec_openssl(self, command, env_vars=None):
#     def generate_key(self):
#     def generate_self_signed_cert(self):
#     def generate_csr(self):
#     def generate_der_encoded(self):
#     def remove_der_encoded(self):
#     def generate_pkcs12_encoded(self):
#     def remove_pkcs12_encoded(self):
#     def remove_complete_certificate(self):
#     def sign_csr(self):
#     def revoke_certificate(self, ppf):
#     def generate_crl(self, ca=None, pf=None):
#     def update_ca_chain_file(self):
#     def get_serial_from_cert(self):
#     def get_hash_from_cert(self):
#     def get_revoke_status_from_cert(self):
#     def dump_certificate(self):
#     def rollback(self):
# class Openssl():
# 
# Path: pki/settings.py
# MEDIA_URL = getattr(settings, 'MEDIA_URL')
# 
# PKI_DEFAULT_COUNTRY = getattr(settings, 'PKI_DEFAULT_COUNTRY', 'DE')
# 
# PKI_ENABLE_GRAPHVIZ = getattr(settings, 'PKI_ENABLE_GRAPHVIZ', False)
# 
# PKI_ENABLE_EMAIL = getattr(settings, 'PKI_ENABLE_EMAIL', False)
# 
# PKI_PASSPHRASE_MIN_LENGTH = getattr(settings, 'PKI_PASSPHRASE_MIN_LENGTH', 8)
# 
# PKI_DEFAULT_KEY_LENGTH = getattr(settings, 'PKI_DEFAULT_KEY_LENGTH', 1024)


# Path: pki/models.py
import os
import re
import datetime
from logging import getLogger
from django.db import models
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.core.validators import MinLengthValidator, MinValueValidator, RegexValidator, URLValidator
from django.core.exceptions import ValidationError
from django.contrib.admin.filterspecs import FilterSpec, RelatedFilterSpec
from pki.helper import get_pki_icon_html
from pki.openssl import Openssl, md5_constructor, refresh_pki_metadata
from pki.settings import MEDIA_URL, PKI_DEFAULT_COUNTRY, PKI_ENABLE_GRAPHVIZ, \
                         PKI_ENABLE_EMAIL, PKI_PASSPHRASE_MIN_LENGTH, PKI_DEFAULT_KEY_LENGTH
                    from pki.views import chain_recursion as r_chain_recursion

        return '<div id="clock_container"><img src="%spki/img/clock-frame.png" style="margin-right:5px"/><span id="clock"></span></div>' % MEDIA_URL
    
    CA_Clock.allow_tags = True
    CA_Clock.short_description = "CA clock"
    
    def Update_Changelog(self, obj, user, action, changes):
        """Update changelog for given object"""
        
        PkiChangelog(model_id=ContentType.objects.get_for_model(obj).pk, object_id=obj.pk, action=action, user=user, changes="; ".join(changes)).save()
    
    def Delete_Changelog(self, obj):
        """Delete changelogs for a given object"""
        
        PkiChangelog.objects.filter(model_id=ContentType.objects.get_for_model(obj).pk, object_id=obj.pk).delete()
    
##------------------------------------------------------------------##
## Certificate authority class
##------------------------------------------------------------------##

class CertificateAuthority(CertificateBase):
    """Certificate Authority model"""
    
    ##---------------------------------##
    ## Model definition
    ##---------------------------------##
    
    common_name       = models.CharField(max_length=64, unique=True)
    name              = models.CharField(max_length=64, unique=True, validators=[RegexValidator("[a-zA-Z0-9-_\.]+", message='Name may only contain characters in range a-Z0-9_-.')], \
                                         help_text="Only change the suggestion if you really know what you're doing")
    parent            = models.ForeignKey('self', blank=True, null=True)

    passphrase        = models.CharField(max_length=255, blank=True, validators=[MinLengthValidator(PKI_PASSPHRASE_MIN_LENGTH)],  help_text="At least 8 characters. Remeber this passphrase - <font color='red'> \