Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 1 | ## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | # |
| 3 | # Copyright (c) 2011-2013, Regents of the University of California |
| 4 | # Alexander Afanasyev |
| 5 | # |
| 6 | # GNU 3.0 license, See the LICENSE file for more information |
| 7 | # |
| 8 | # Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | # |
| 10 | |
| 11 | # |
| 12 | # Based on PyCCN code, copyrighted and licensed as follows |
| 13 | # |
| 14 | # Copyright (c) 2011-2013, Regents of the University of California |
| 15 | # BSD license, See the COPYING file for more information |
| 16 | # Written by: Derek Kulinski <takeda@takeda.tk> |
| 17 | # Jeff Burke <jburke@ucla.edu> |
| 18 | # |
| 19 | |
| 20 | # Fronts ccn_pkey. |
| 21 | from . import _ndn |
| 22 | from . import Name |
| 23 | |
| 24 | class Key(object): |
| 25 | def __init__(self): |
| 26 | self.type = None |
| 27 | self.publicKeyID = None # SHA256 hash |
| 28 | # ndn |
| 29 | self.ccn_data_dirty = False |
| 30 | self.ccn_data_public = None # backing pkey |
| 31 | self.ccn_data_private = None # backing pkey |
| 32 | |
| 33 | def __get_ccn(self): |
| 34 | pass |
| 35 | |
| 36 | def generateRSA(self, numbits): |
| 37 | _ndn.generate_RSA_key(self, numbits) |
| 38 | |
| 39 | def privateToDER(self): |
| 40 | if not self.ccn_data_private: |
| 41 | raise _ndn.CCNKeyError("Key is not private") |
| 42 | return _ndn.DER_write_key(self.ccn_data_private) |
| 43 | |
| 44 | def publicToDER(self): |
| 45 | return _ndn.DER_write_key(self.ccn_data_public) |
| 46 | |
| 47 | def privateToPEM(self, filename = None, password = None): |
| 48 | if not self.ccn_data_private: |
| 49 | raise _ndn.CCNKeyError("Key is not private") |
| 50 | |
| 51 | if filename: |
| 52 | f = open(filename, 'w') |
| 53 | _ndn.PEM_write_key(self.ccn_data_private, file=f, password = password) |
| 54 | f.close() |
| 55 | else: |
| 56 | return _ndn.PEM_write_key(self.ccn_data_private, password = password) |
| 57 | |
| 58 | def publicToPEM(self, filename = None): |
| 59 | if filename: |
| 60 | f = open(filename, 'w') |
| 61 | _ndn.PEM_write_key(self.ccn_data_public, file=f) |
| 62 | f.close() |
| 63 | else: |
| 64 | return _ndn.PEM_write_key(self.ccn_data_public) |
| 65 | |
| 66 | def fromDER(self, private = None, public = None): |
| 67 | if private: |
| 68 | (self.ccn_data_private, self.ccn_data_public, self.publicKeyID) = \ |
| 69 | _ndn.DER_read_key(private=private) |
| 70 | return |
| 71 | if public: |
| 72 | (self.ccn_data_private, self.ccn_data_public, self.publicKeyID) = \ |
| 73 | _ndn.DER_read_key(public=public) |
| 74 | return |
| 75 | |
| 76 | def fromPEM(self, filename = None, private = None, public = None, password = None): |
| 77 | if filename: |
| 78 | f = open(filename, 'r') |
| 79 | (self.ccn_data_private, self.ccn_data_public, self.publicKeyID) = \ |
| 80 | _ndn.PEM_read_key(file=f, password = password) |
| 81 | f.close() |
| 82 | elif private: |
| 83 | (self.ccn_data_private, self.ccn_data_public, self.publicKeyID) = \ |
| 84 | _ndn.PEM_read_key(private=private, password = password) |
| 85 | elif public: |
| 86 | (self.ccn_data_private, self.ccn_data_public, self.publicKeyID) = \ |
| 87 | _ndn.PEM_read_key(public=public) |
| 88 | |
| 89 | @staticmethod |
| 90 | def createFromDER (private = None, public = None): |
| 91 | key = Key () |
| 92 | key.fromDER (private, public) |
| 93 | return key |
| 94 | |
| 95 | @staticmethod |
| 96 | def createFromPEM (filename = None, private = None, public = None, password = None): |
| 97 | key = Key () |
| 98 | key.fromPEM (filename, private, public, password) |
| 99 | return key |
| 100 | |
| 101 | @staticmethod |
| 102 | def getDefaultKey(): |
| 103 | return _ndn.get_default_key() |
| 104 | |
| 105 | # plus library helper functions to generate and serialize keys? |
| 106 | |
| 107 | class KeyLocator(object): |
| 108 | def __init__(self, arg=None): |
| 109 | #whichever one is not none will be used |
| 110 | #if multiple set, checking order is: keyName, key, certificate |
| 111 | self.key = arg if type(arg) is Key else None |
| 112 | self.keyName = arg if type(arg) is Name.Name else None |
| 113 | self.certificate = None |
| 114 | |
| 115 | # ndn |
| 116 | self.ccn_data_dirty = True |
| 117 | self.ccn_data = None # backing charbuf |
| 118 | |
| 119 | def __setattr__(self, name, value): |
| 120 | if name != "ccn_data" and name != "ccn_data_dirty": |
| 121 | self.ccn_data_dirty = True |
| 122 | object.__setattr__(self, name, value) |
| 123 | |
| 124 | def __getattribute__(self, name): |
| 125 | if name=="ccn_data": |
| 126 | if object.__getattribute__(self, 'ccn_data_dirty'): |
| 127 | if object.__getattribute__(self, 'keyName'): |
| 128 | self.ccn_data = _ndn.KeyLocator_to_ccn( |
| 129 | name=self.keyName.ccn_data) |
| 130 | elif object.__getattribute__(self, 'key'): |
| 131 | self.ccn_data = _ndn.KeyLocator_to_ccn( |
| 132 | key=self.key.ccn_data_public) |
| 133 | elif object.__getattribute__(self, 'certificate'): |
| 134 | #same but with cert= arg |
| 135 | raise NotImplementedError("certificate support is not implemented") |
| 136 | else: |
| 137 | raise TypeError("No name, key nor certificate defined") |
| 138 | |
| 139 | self.ccn_data_dirty = False |
| 140 | return object.__getattribute__(self, name) |
| 141 | |
| 142 | @staticmethod |
| 143 | def getDefaultKeyLocator(): |
| 144 | return KeyLocator (_ndn.get_default_key_name ()) |