blob: b5c065b71d08f874d10d744eef342cb7b8bbc83c [file] [log] [blame]
Alexander Afanasyev5ba90362013-07-15 19:58:38 -07001## -*- 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 Afanasyev39f53162013-07-18 16:24:31 -070020import ns.core
21import ns.ndnSIM
22from Name import Name
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070023
24class Key(object):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070025 def __init__ (self):
26 # self.publicKeyID = None # SHA256 hash
27 self.fakeKey = None
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070028
29 def generateRSA(self, numbits):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070030 randVar = ns.core.UniformVariable ()
31 self.fakeKey = randVar.GetInteger (0, 2147483647)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070032
33 def privateToDER(self):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070034 return self.fakeKey
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070035
36 def publicToDER(self):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070037 return self.privateToDER ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070038
39 def privateToPEM(self, filename = None, password = None):
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070040 if filename:
41 f = open(filename, 'w')
Alexander Afanasyev39f53162013-07-18 16:24:31 -070042 f.write (self.fakeKey)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070043 f.close()
44 else:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070045 return self.fakeKey
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070046
47 def publicToPEM(self, filename = None):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070048 return privateToPEM (filename)
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070049
50 def fromDER(self, private = None, public = None):
51 if private:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070052 self.fakeKey = private
53 elif public:
54 self.fakeKey = public
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070055
56 def fromPEM(self, filename = None, private = None, public = None, password = None):
57 if filename:
58 f = open(filename, 'r')
Alexander Afanasyev39f53162013-07-18 16:24:31 -070059 self.fakeKey = f.read ()
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070060 f.close()
61 elif private:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070062 self.fakeKey = private
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070063 elif public:
Alexander Afanasyev39f53162013-07-18 16:24:31 -070064 self.fakeKey = public
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070065
66 @staticmethod
Alexander Afanasyev39f53162013-07-18 16:24:31 -070067 def createFromDER (private = None, public = None):
68 key = Key ()
69 key.fromDER (private, public)
70 return key
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070071
Alexander Afanasyev39f53162013-07-18 16:24:31 -070072 @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
84
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070085
86class KeyLocator(object):
Alexander Afanasyev39f53162013-07-18 16:24:31 -070087 def __init__(self, keyName = None):
88 self.keyName = keyName
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070089
Alexander Afanasyev39f53162013-07-18 16:24:31 -070090 @staticmethod
91 def getDefaultKeyLocator():
92 context = ns.core.Simulator.GetContext ()
93 keyLocator = ns.ndnSIM.ndn.Name ()
94 keyLocator.\
95 append ("default-key").\
96 append (str (context))
Alexander Afanasyev5ba90362013-07-15 19:58:38 -070097
Alexander Afanasyev39f53162013-07-18 16:24:31 -070098 return Name (keyLocator)