# Path: cc/json.py
# class Struct(dbdict):
#     """Base class. All structure components must inherit or
#     instantiate Struct. Magic happens in metaclass.
#     """
#     __metaclass__ = _MetaStruct
# 
#     @classmethod
#     def from_json(cls, jsonstr):
#         """creates object from json string"""
#         return cls(loads(jsonstr))
# 
#     def dump_json(self):
#         """dumps object to json string"""
#         return dumps(self)
# 
#     def getas(self, name, cast = None, default = None):
#         """get value by name with optional casting and default"""
#         value = self.get(name, default)
#         if cast:
#             value = cast(value)
#         return value
# 
# Path: cc/message.py
# class CCMessage(object):
#     """CC multipart message.
# 
#     Format is similar to usual REQ+REP sockets:
# 
#     - id_hopX
#     - id_hop2
#     - id_hop1
#     - id_req (optional)
#     - empty part
#     - cc dest
#     - cc payload (json)
#     - cc signature
#     - cc blob data
#     """
#     __slots__ = ('zmsg', 'rpos', 'parsed', 'signature')
# 
#     def __init__(self, zmsg):
#         assert isinstance(zmsg, list)
#         self.zmsg = zmsg
#         self.rpos = zmsg.index('')
#         self.parsed = None
#         self.signature = None
#         assert_msg_req (self.get_dest())
# 
#     def get_route(self):
#         """Route parts"""
#         return self.zmsg[ : self.rpos]
# 
#     def get_non_route(self):
#         """Payload parts"""
#         return self.zmsg[ self.rpos + 1 : ]
# 
#     def get_dest(self):
#         """Return destination part"""
#         return self.zmsg[self.rpos + 1]
# 
#     def get_part1(self):
#         """Return body (json) as string"""
#         return self.zmsg[self.rpos + 2]
# 
#     def get_part2(self):
#         """Return signature"""
#         if self.rpos + 3 >= len(self.zmsg):
#             return ''
#         return self.zmsg[self.rpos + 3]
# 
#     def get_part3(self):
#         """Return blob"""
#         if self.rpos + 4 >= len(self.zmsg):
#             return None
#         return self.zmsg[self.rpos + 4]
# 
#     def get_size(self):
#         return zmsg_size (self.zmsg)
# 
#     def __repr__(self):
#         x = repr(self.zmsg)
#         if len(x) > 300:
#             x = x[:300] + '...'
#         return 'CCMessage(%s)' % x
# 
#     def __str__(self):
#         x = repr(self.get_non_route())
#         if len(x) > 300:
#             x = x[:300] + '...'
#         return 'CCMessage(%s)' % x
# 
#     def set_route (self, route):
#         """Fill local route with another route."""
#         self.zmsg[:self.rpos] = route
#         self.rpos = len(route)
# 
#     def take_route(self, cmsg):
#         """Fill local route with route from another message."""
#         r = cmsg.get_route()
#         self.set_route(r)
# 
#     def get_payload(self, xtx):
#         if self.parsed:
#             return self.parsed
#         msg, sgn = xtx.parse_cmsg(self)
#         if not msg:
#             return None
#         self.parsed = msg
#         self.signature = sgn
#         return msg
# 
#     def get_signature(self, xtx):
#         self.get_payload(xtx)
#         return self.signature
# 
#     def send_to (self, sock):
#         sock.send_multipart (self.zmsg)


import os.path
import time
import skytools
    import time
from hashlib import sha1
from M2Crypto import SMIME, BIO, X509
from cc.json import Struct
from cc.message import CCMessage
from M2Crypto import m2, Err
        pairs = (('cms-verify-ca', 'ca_name'),
                 ('cms-decrypt', 'decrypt_name'),
                 ('cms-sign', 'sign_name'),
                 ('cms-encrypt', 'encrypt_name'),
                 ('cms-keystore', 'ks_dir'))
        for n1, n2 in pairs:
            v = getattr(self, n2)
            if v and n1 not in cf_dict:
                cf_dict[n1] = v

    def create_cmsg(self, msg, blob=None):
        if blob is not None and self.sign_name:
            msg.blob_hash = "SHA-1:" + sha1(blob).hexdigest()
        js = msg.dump_json()
        part1 = js
        part2 = ''
        if self.encrypt_name and self.sign_name:
            self.log.trace("encrypt: %s", msg['req'])
            part1 = 'ENC1'
            part2 = self.cms.sign_and_encrypt(js, self.sign_name, self.encrypt_name)
        elif self.encrypt_name:
            raise Exception('encrypt_name without sign_name ?')
        elif self.sign_name:
            self.log.trace("sign: %s", msg['req'])
            part2 = self.cms.sign(js, self.sign_name)
        else:
            self.log.trace("no crypto: %s", msg['req'])
        zmsg = ['', msg.req.encode('utf8'), part1, part2]
        if blob is not None:
            zmsg.append(blob)

# Path: cc/json.py
# class Struct(dbdict):
#     """Base class. All structure components must inherit or
#     instantiate Struct. Magic happens in metaclass.
#     """
#     __metaclass__ = _MetaStruct
# 
#     @classmethod
#     def from_json(cls, jsonstr):
#         """creates object from json string"""
#         return cls(loads(jsonstr))
# 
#     def dump_json(self):
#         """dumps object to json string"""
#         return dumps(self)
# 
#     def getas(self, name, cast = None, default = None):
#         """get value by name with optional casting and default"""
#         value = self.get(name, default)
#         if cast:
#             value = cast(value)
#         return value
# 
# Path: cc/message.py
# class CCMessage(object):
#     """CC multipart message.
# 
#     Format is similar to usual REQ+REP sockets:
# 
#     - id_hopX
#     - id_hop2
#     - id_hop1
#     - id_req (optional)
#     - empty part
#     - cc dest
#     - cc payload (json)
#     - cc signature
#     - cc blob data
#     """
#     __slots__ = ('zmsg', 'rpos', 'parsed', 'signature')
# 
#     def __init__(self, zmsg):
#         assert isinstance(zmsg, list)
#         self.zmsg = zmsg
#         self.rpos = zmsg.index('')
#         self.parsed = None
#         self.signature = None
#         assert_msg_req (self.get_dest())
# 
#     def get_route(self):
#         """Route parts"""
#         return self.zmsg[ : self.rpos]
# 
#     def get_non_route(self):
#         """Payload parts"""
#         return self.zmsg[ self.rpos + 1 : ]
# 
#     def get_dest(self):
#         """Return destination part"""
#         return self.zmsg[self.rpos + 1]
# 
#     def get_part1(self):
#         """Return body (json) as string"""
#         return self.zmsg[self.rpos + 2]
# 
#     def get_part2(self):
#         """Return signature"""
#         if self.rpos + 3 >= len(self.zmsg):
#             return ''
#         return self.zmsg[self.rpos + 3]
# 
#     def get_part3(self):
#         """Return blob"""
#         if self.rpos + 4 >= len(self.zmsg):
#             return None
#         return self.zmsg[self.rpos + 4]
# 
#     def get_size(self):
#         return zmsg_size (self.zmsg)
# 
#     def __repr__(self):
#         x = repr(self.zmsg)
#         if len(x) > 300:
#             x = x[:300] + '...'
#         return 'CCMessage(%s)' % x
# 
#     def __str__(self):
#         x = repr(self.get_non_route())
#         if len(x) > 300:
#             x = x[:300] + '...'
#         return 'CCMessage(%s)' % x
# 
#     def set_route (self, route):
#         """Fill local route with another route."""
#         self.zmsg[:self.rpos] = route
#         self.rpos = len(route)
# 
#     def take_route(self, cmsg):
#         """Fill local route with route from another message."""
#         r = cmsg.get_route()
#         self.set_route(r)
# 
#     def get_payload(self, xtx):
#         if self.parsed:
#             return self.parsed
#         msg, sgn = xtx.parse_cmsg(self)
#         if not msg:
#             return None
#         self.parsed = msg
#         self.signature = sgn
#         return msg
# 
#     def get_signature(self, xtx):
#         self.get_payload(xtx)
#         return self.signature
# 
#     def send_to (self, sock):
#         sock.send_multipart (self.zmsg)


# Path: cc/crypto.py
import os.path
import time
import skytools
    import time
from hashlib import sha1
from M2Crypto import SMIME, BIO, X509
from cc.json import Struct
from cc.message import CCMessage
from M2Crypto import m2, Err

        pairs = (('cms-verify-ca', 'ca_name'),
                 ('cms-decrypt', 'decrypt_name'),
                 ('cms-sign', 'sign_name'),
                 ('cms-encrypt', 'encrypt_name'),
                 ('cms-keystore', 'ks_dir'))
        for n1, n2 in pairs:
            v = getattr(self, n2)
            if v and n1 not in cf_dict:
                cf_dict[n1] = v

    def create_cmsg(self, msg, blob=None):
        if blob is not None and self.sign_name:
            msg.blob_hash = "SHA-1:" + sha1(blob).hexdigest()
        js = msg.dump_json()
        part1 = js
        part2 = ''
        if self.encrypt_name and self.sign_name:
            self.log.trace("encrypt: %s", msg['req'])
            part1 = 'ENC1'
            part2 = self.cms.sign_and_encrypt(js, self.sign_name, self.encrypt_name)
        elif self.encrypt_name:
            raise Exception('encrypt_name without sign_name ?')
        elif self.sign_name:
            self.log.trace("sign: %s", msg['req'])
            part2 = self.cms.sign(js, self.sign_name)
        else:
            self.log.trace("no crypto: %s", msg['req'])
        zmsg = ['', msg.req.encode('utf8'), part1, part2]
        if blob is not None:
            zmsg.append(blob)

        return CCMessage(zmsg)