[Nym3-commit] r181 - trunk/nym3
jr at conuropsis.org
jr at conuropsis.org
Sun Apr 10 13:22:41 CEST 2005
Author: jr
Date: 2005-04-10 13:22:40 +0200 (Sun, 10 Apr 2005)
New Revision: 181
Modified:
trunk/nym3/Message.py
Log:
-change the way keys are handled in Newpk
Modified: trunk/nym3/Message.py
===================================================================
--- trunk/nym3/Message.py 2005-04-09 16:56:31 UTC (rev 180)
+++ trunk/nym3/Message.py 2005-04-10 11:22:40 UTC (rev 181)
@@ -31,7 +31,6 @@
import nym3.Common as Common
import mixminion.Crypto as _cr
-
# Every command type has a numerical code as described in nym-spec.
CToSCODE = { 'Create': 0, 'Create2': 1, 'Surb': 2, 'Newpk': 3,
'Relay': 4, 'Get': 5, 'Summarize': 6, 'Delete': 7,
@@ -436,11 +435,11 @@
s = self.surbs
assert len(s) < pow(256, 3)
return chr(self.ct()) + intToStrBE(len(s), 3) + s
-
+
class Newpk(CommandCToS):
"""Newpk command
- self.idKey : identity RSA Key, ASN.1 encoded (str)
- self.encKey : encryption RSA Key, ASN.1 encoded (str)"""
+ self.ekid : ASN.1 encoding of the identity key
+ self.ekenc : ASN.1 encoding of the encryption key"""
def __init__(self):
"""Build a Newpk empty object"""
@@ -450,36 +449,46 @@
"""return a int for the code of the command"""
return CToSCODE['Newpk']
- def fromData(self, idKey, encKey):
- """Fill a Newpk Object from 2 Key objects"""
- # TODO : add keylength compliance checks.
- self.idKey = _cr.pk_encode_public_key(idKey)
- self.encKey = _cr.pk_encode_public_key(encKey)
+ def fromData(self, sid, senc):
+ """Fill a Newpk Object from 2 keys"""
+ if sid == None or senc == None:
+ raise BadArgument("Newpk.fromData : sid and senc mustn't be None")
+ id_l = nbBits(_cr.pk_get_modulus(sid))
+ id_l2 = nbBits(_cr.pk_get_modulus(senc))
+ if( ((id_l != 1024) and (id_l != 2048)) or ((id_l2 != 1024) and (id_l2 != 2048))):
+ raise BadArgument("Newpk.fromData : sid or senc as not a valid size")
+ self.ekid = _cr.pk_encode_public_key(sid)
+ self.ekenc = _cr.pk_encode_public_key(senc)
+ #command body is small enough
+ def check_key(self, ekey):
+ key = _cr.pk_decode_public_key(ekey)
+ mod = _cr.pk_get_modulus(key)
+ nb = nbBits(mod)
+ return (nb == 1024 or nb == 2048) and _cr.pk_same_public_key(key, _cr.pk_from_modulus(mod))
+
def fromStrReader(self, sr, cs):
- """Fill a Newpk Object from a StrReader
- raise ParseError if it is malformed"""
- # TODO : b0rken.
- try:
- id_l = strToIntBE(sr.next(2))
- if((id_l != 128) and (id_l != 256)):
- raise ParseError("Bad Formed Command : Newpk")
- id_l2 = cs-2 - id_l
- if((id_l2 != 128) and (id_l2 != 256)):
- raise ParseError("Bad Formed Command : Newpk")
+ """Fill a Newpk Object from a StrReader
+ raise ParseError if it is malformed"""
+ try:
+ id_l = strToIntBE(sr.next(2))
+ id_l2 = cs-2 - id_l
+
+ self.ekid = sr.next(id_l)
+ self.ekenc = sr.next(id_l2)
+ if not (self.check_key(self.ekid) and self.check_key(self.ekenc)):
+ raise BadArgument("Illegal keys : Newpk")
+
+ except IndexError:
+ raise ParseError("Bad Formed Command : Newpk")
- self.kid = strToIntBE(sr.next(id_l))
- self.kenc = strToIntBE(sr.next(id_l2))
- self.idf = id_l / 128
- self.encf = id_l2 / 128
- except IndexError:
- raise ParseError("Bad Formed Command : Newpk")
+ def __str__(self):
+ """Give the string of the Command that would be sent in a control message"""
+ s=intToStrBE(len(self.ekid), 2) + self.ekid + self.ekenc
+ if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ raise BadArgument("Newpk.__str__ : command body too long")
+ return chr(self.ct()) + intToStrBE(len(s),3) + s
- def __str__(self):
- """Give the string of the Command that would be sent
- in a control message"""
- data = intToStrBE(len(self.idKey), 2) + self.idKey + self.encKey
- return chr(self.ct()) + intToStrBE(len(data), 3) + data
class Relay(CommandCToS):
"""Relay command
@@ -988,7 +997,7 @@
"""Turn a list of Command into a sendable message"""
s = ""
for c in comList:
- s = s + str(c)
+ s = s + str(c)
return s
if (__name__ == '__main__'):
@@ -1026,7 +1035,9 @@
assert s1 == s2
print "test Newpk"
C = Newpk()
- C.fromData(166043335247949820064815071273366034639989291753710681801054469261751398297628983820646212658964492977698778059504084070552574143058521756622243987832034798616497725850572149696353832190479836330444809381945631021624844072571174471185526330192208148191737102653794442385041774144222636560699168166510090606307L,166043335247949820064815071273366034639989291753710681801054469261751398297628983820646212658964492977698778059504084070552574143058521756622243987832034798616497725850572149696353832190479836330444809381945631021624844072571174471185526330192208148191737102653794442385041774144222636560699168166510090606307L)
+ k1 = _cr.pk_generate()
+ k2 = _cr.pk_generate()
+ C.fromData(k1, k2)
s1 = str(C)
S = StrReader(s1)
D = S.readCommandCToS()
@@ -1126,3 +1137,4 @@
D = S.readCommandSToC()
s2 = str(D)
assert s1 == s2
+
More information about the Nym3-commit
mailing list