hmacmd5.py :  » Network » Python-SNMP » pysnmp-4.1.13a » pysnmp » proto » secmod » rfc3414 » auth » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Python SNMP 
Python SNMP » pysnmp 4.1.13a » pysnmp » proto » secmod » rfc3414 » auth » hmacmd5.py
try:
    from hashlib import md5
except ImportError:
    import md5
    md5 = md5.new                    
import string
from pysnmp.proto.secmod.rfc3414.auth import base
from pysnmp.proto import error

_twelveZeros = '\x00'*12
_fortyEightZeros = '\x00'*48

# rfc3414: 6.2.4

class HmacMd5(base.AbstractAuthenticationService):
    serviceID = (1, 3, 6, 1, 6, 3, 10, 1, 1, 2)  # usmHMACMD5AuthProtocol
    __ipad = [0x36]*64
    __opad = [0x5C]*64

    # 6.3.1
    def authenticateOutgoingMsg(self, authKey, wholeMsg):
        # Here we expect calling secmod to indicate where the digest
        # should be in the substrate. Also, it pre-sets digest placeholder
        # so we hash wholeMsg out of the box.
        # Yes, that's ugly but that's rfc...
        l = string.find(wholeMsg, _twelveZeros)
        if l == -1:
            raise error.ProtocolError('Cant locate digest placeholder')
        wholeHead = wholeMsg[:l]
        wholeTail = wholeMsg[l+12:]

        # 6.3.1.1 

        # 6.3.1.2a
        extendedAuthKey = map(ord, str(authKey) + _fortyEightZeros)

        # 6.3.1.2b --> noop

        # 6.3.1.2c
        k1 = string.join(
            map(lambda x,y: chr(x^y), extendedAuthKey, self.__ipad), ''
            )

        # 6.3.1.2d --> noop

        # 6.3.1.2e
        k2 = string.join(
            map(lambda x,y: chr(x^y), extendedAuthKey, self.__opad), ''
            )
        
        # 6.3.1.3
        d1 = md5(k1+wholeMsg).digest()
        
        # 6.3.1.4
        d2 = md5(k2+d1).digest()
        mac = d2[:12]

        # 6.3.1.5 & 6
        return '%s%s%s' % (wholeHead, mac, wholeTail)

    # 6.3.2
    def authenticateIncomingMsg(self, authKey, authParameters, wholeMsg):
        # 6.3.2.1 & 2
        if len(authParameters) != 12:
            raise error.StatusInformation(
                errorIndication='authenticationError'
                )

        # 6.3.2.3
        l = string.find(wholeMsg, str(authParameters))
        if l == -1:
            raise error.ProtocolError('Cant locate digest in wholeMsg')
        wholeHead = wholeMsg[:l]
        wholeTail = wholeMsg[l+12:]
        authenticatedWholeMsg = '%s%s%s' % (
            wholeHead, _twelveZeros, wholeTail
            )

        # 6.3.2.4a
        extendedAuthKey = map(ord, str(authKey) + _fortyEightZeros)

        # 6.3.2.4b --> noop
        
        # 6.3.2.4c
        k1 = string.join(
            map(lambda x,y: chr(x^y), extendedAuthKey, self.__ipad), ''
            )

        # 6.3.2.4d --> noop

        # 6.3.2.4e
        k2 = string.join(
            map(lambda x,y: chr(x^y), extendedAuthKey, self.__opad), ''
            )

        # 6.3.2.5a
        d1 = md5(k1+authenticatedWholeMsg).digest()

        # 6.3.2.5b
        d2 = md5(k2+d1).digest()
        
        # 6.3.2.5c
        mac = d2[:12]
         
        # 6.3.2.6
        if mac != authParameters:
            raise error.StatusInformation(
                errorIndication='authenticationFailure'
                )

        return authenticatedWholeMsg
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.