home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1693 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  97 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import hmac
  5. import struct
  6. import dns.exception as dns
  7. import dns.rdataclass as dns
  8. import dns.name as dns
  9.  
  10. class BadTime(dns.exception.DNSException):
  11.     pass
  12.  
  13.  
  14. class BadSignature(dns.exception.DNSException):
  15.     pass
  16.  
  17. _alg_name = dns.name.from_text('HMAC-MD5.SIG-ALG.REG.INT.').to_digestable()
  18.  
  19. def hmac_md5(wire, keyname, secret, time, fudge, original_id, error, other_data, request_mac, ctx = None, multi = False, first = True):
  20.     if first:
  21.         ctx = hmac.new(secret)
  22.         ml = len(request_mac)
  23.         if ml > 0:
  24.             ctx.update(struct.pack('!H', ml))
  25.             ctx.update(request_mac)
  26.         
  27.     
  28.     id = struct.pack('!H', original_id)
  29.     ctx.update(id)
  30.     ctx.update(wire[2:])
  31.     if first:
  32.         ctx.update(keyname.to_digestable())
  33.         ctx.update(struct.pack('!H', dns.rdataclass.ANY))
  34.         ctx.update(struct.pack('!I', 0))
  35.     
  36.     long_time = time + 0x0L
  37.     upper_time = long_time >> 32 & 0xFFFFL
  38.     lower_time = long_time & 0xFFFFFFFFL
  39.     time_mac = struct.pack('!HIH', upper_time, lower_time, fudge)
  40.     pre_mac = _alg_name + time_mac
  41.     ol = len(other_data)
  42.     if ol > 65535:
  43.         raise ValueError, 'TSIG Other Data is > 65535 bytes'
  44.     ol > 65535
  45.     post_mac = struct.pack('!HH', error, ol) + other_data
  46.     if first:
  47.         ctx.update(pre_mac)
  48.         ctx.update(post_mac)
  49.     else:
  50.         ctx.update(time_mac)
  51.     mac = ctx.digest()
  52.     mpack = struct.pack('!H', len(mac))
  53.     tsig_rdata = pre_mac + mpack + mac + id + post_mac
  54.     if multi:
  55.         ctx = hmac.new(secret)
  56.         ml = len(mac)
  57.         ctx.update(struct.pack('!H', ml))
  58.         ctx.update(mac)
  59.     else:
  60.         ctx = None
  61.     return (tsig_rdata, mac, ctx)
  62.  
  63.  
  64. def validate(wire, keyname, secret, now, request_mac, tsig_start, tsig_rdata, tsig_rdlen, ctx = None, multi = False, first = True):
  65.     (adcount,) = struct.unpack('!H', wire[10:12])
  66.     if adcount == 0:
  67.         raise dns.exception.FormError
  68.     adcount == 0
  69.     adcount -= 1
  70.     new_wire = wire[0:10] + struct.pack('!H', adcount) + wire[12:tsig_start]
  71.     current = tsig_rdata
  72.     (aname, used) = dns.name.from_wire(wire, current)
  73.     current = current + used
  74.     (upper_time, lower_time, fudge, mac_size) = struct.unpack('!HIHH', wire[current:current + 10])
  75.     time = (upper_time + 0x0L << 32) + lower_time + 0x0L
  76.     current += 10
  77.     mac = wire[current:current + mac_size]
  78.     current += mac_size
  79.     (original_id, error, other_size) = struct.unpack('!HHH', wire[current:current + 6])
  80.     current += 6
  81.     other_data = wire[current:current + other_size]
  82.     current += other_size
  83.     if current != tsig_rdata + tsig_rdlen:
  84.         raise dns.exception.FormError
  85.     current != tsig_rdata + tsig_rdlen
  86.     time_low = time - fudge
  87.     time_high = time + fudge
  88.     if now < time_low or now > time_high:
  89.         raise BadTime
  90.     now > time_high
  91.     (junk, our_mac, ctx) = hmac_md5(new_wire, keyname, secret, time, fudge, original_id, error, other_data, request_mac, ctx, multi, first)
  92.     if our_mac != mac:
  93.         raise BadSignature
  94.     our_mac != mac
  95.     return ctx
  96.  
  97.