Exemple #1
0
 def __call__(self, C=None, Q=None, P=None, S=None, T=None):
     datainput = self.ocrasuite + '\0'
     if self.C:
         if C is None or not isinstance(C, int) or C < 0 or C > 2**64:
             raise ValueError, ('Invalid counter value', C)
         datainput += hotp.int2beint64(C)
     if self.Q:
         if Q is None or not isinstance(Q, str) or len(Q) > self.Q[1]:
             raise ValueError, 'challenge'
         if self.Q[0] == 'N' and not Q.isdigit():
             raise ValueError, 'challenge'
         if self.Q[0] == 'A' and not Q.isalnum():
             raise ValueError, 'challenge'
         if self.Q[0] == 'H':
             try:
                 int(Q, 16)
             except ValueError:
                 raise ValueError, 'challenge'
         datainput += Q
         datainput += '0' * (128-len(Q))
     if self.P:
         if P is None:
             raise ValueError, 'Pin/Password'
         datainput += self.P(P).digest()
     if self.S:
         if S is None or len(S) != self.S:
             raise ValueError, 'session'
         datainput += S
     if self.T:
         if not isinstance(T, int):
             raise ValueError, 'timestamp'
         datainput += hotp.int2beint64(int(T / self.T))
     return datainput
Exemple #2
0
 def __call__(self, C=None, Q=None, P=None, P_digest=None, S=None, T=None,
         T_precomputed=None, Qsc=None):
     datainput = ''
     if self.C:
         try:
             C = int(C)
             if C < 0 or C > 2**64:
                 raise Exception()
         except:
             raise ValueError, ('Invalid counter value', C)
         datainput += hotp.int2beint64(int(C))
     if self.Q:
         max_length = self.Q[1]
         if Qsc is not None:
             # Mutual Challenge-Response
             Q = Qsc
             max_length *= 2
         if Q is None or not isinstance(Q, str) or len(Q) > max_length:
             raise ValueError, 'challenge'
         if self.Q[0] == 'N' and not Q.isdigit():
             raise ValueError, 'challenge'
         if self.Q[0] == 'A' and not Q.isalnum():
             raise ValueError, 'challenge'
         if self.Q[0] == 'H':
             try:
                 int(Q, 16)
             except ValueError:
                 raise ValueError, 'challenge'
         if self.Q[0] == 'N':
             Q = hex(int(Q))[2:]
             Q += '0' * (len(Q) % 2)
             Q = Q.decode('hex')
         if self.Q[0] == 'A':
             pass
         if self.Q[0] == 'H':
             Q = Q.decode('hex')
         datainput += Q
         datainput += '\0' * (128-len(Q))
     if self.P:
         if P_digest:
             if len(P) == self.P.digest_size:
                 datainput += P_digest
             elif len(P) == 2*self.P.digest_size:
                 datainput += P_digest.decode('hex')
             else:
                 raise ValueError, ('Pin/Password digest invalid', P_digest)
         elif P is None:
             raise ValueError, 'Pin/Password missing'
         else:
             datainput += self.P(P).digest()
     if self.S:
         if S is None or len(S) != self.S:
             raise ValueError, 'session'
         datainput += S
     if self.T:
         if is_int(T_precomputed):
             datainput += hotp.int2beint64(int(T_precomputed))
         elif is_int(T):
             datainput += hotp.int2beint64(int(T / self.T))
         else:
             raise ValueError, 'timestamp'
     return datainput