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 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 20 | import ns.core |
| 21 | import ns.ndnSIM |
| 22 | from Name import Name |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | fce5bbd | 2013-08-07 18:50:00 -0700 | [diff] [blame] | 24 | class Key (object): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 25 | def __init__ (self): |
| 26 | # self.publicKeyID = None # SHA256 hash |
| 27 | self.fakeKey = None |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 28 | |
| 29 | def generateRSA(self, numbits): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 30 | randVar = ns.core.UniformVariable () |
| 31 | self.fakeKey = randVar.GetInteger (0, 2147483647) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 32 | |
| 33 | def privateToDER(self): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 34 | return self.fakeKey |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 35 | |
| 36 | def publicToDER(self): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 37 | return self.privateToDER () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 38 | |
| 39 | def privateToPEM(self, filename = None, password = None): |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 40 | if filename: |
| 41 | f = open(filename, 'w') |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 42 | f.write (self.fakeKey) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 43 | f.close() |
| 44 | else: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 45 | return self.fakeKey |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 46 | |
| 47 | def publicToPEM(self, filename = None): |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 48 | return privateToPEM (filename) |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 49 | |
| 50 | def fromDER(self, private = None, public = None): |
| 51 | if private: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 52 | self.fakeKey = private |
| 53 | elif public: |
| 54 | self.fakeKey = public |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 55 | |
| 56 | def fromPEM(self, filename = None, private = None, public = None, password = None): |
| 57 | if filename: |
| 58 | f = open(filename, 'r') |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 59 | self.fakeKey = f.read () |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 60 | f.close() |
| 61 | elif private: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 62 | self.fakeKey = private |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 63 | elif public: |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 64 | self.fakeKey = public |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 65 | |
| 66 | @staticmethod |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 67 | def createFromDER (private = None, public = None): |
| 68 | key = Key () |
| 69 | key.fromDER (private, public) |
| 70 | return key |
Alexander Afanasyev | 5ba9036 | 2013-07-15 19:58:38 -0700 | [diff] [blame] | 71 | |
Alexander Afanasyev | 39f5316 | 2013-07-18 16:24:31 -0700 | [diff] [blame] | 72 | @staticmethod |
| 73 | def createFromPEM (filename = None, private = None, public = None, password = None): |
| 74 | key = Key () |
| 75 | key.fromPEM (filename, private, public, password) |
| 76 | return key |
| 77 | |
| 78 | @staticmethod |
| 79 | def getDefaultKey (): |
| 80 | context = ns.core.Simulator.GetContext () |
| 81 | key = Key () |
| 82 | key.fakeKey = context |
| 83 | return key |