1 """Abstract CarType.
2
3 __author__ = "http://www.gemalto.com"
4
5 Copyright 2001-2012 gemalto
6 Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
7
8 This file is part of pyscard.
9
10 pyscard is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 pyscard is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with pyscard; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 """
24 from __future__ import print_function
25 from smartcard.Exceptions import InvalidATRMaskLengthException
26 from smartcard.System import readers
27 from smartcard.util import toHexString
28
29
31 """Abstract base class for CardTypes.
32
33 Known sub-classes: L{smartcard.CardType.AnyCardType}
34 L{smartcard.CardType.ATRCardType}."""
35
37 """CardType constructor."""
38 pass
39
40 - def matches(self, atr, reader=None):
41 """Returns true if atr and card connected match the CardType.
42
43 @param atr: the atr to chek for matching
44 @param reader: the reader (optional); default is None
45
46 The reader can be use in some sub-classes to do advanced
47 matching that require connecting to the card."""
48 pass
49
50
52 """The AnyCardType matches any card."""
53
54 - def matches(self, atr, reader=None):
55 """Always returns true, i.e. AnyCardType matches any card.
56
57 @param atr: the atr to chek for matching
58 @param reader: the reader (optional); default is None"""
59 return True
60
61
63 """The ATRCardType defines a card from an ATR and a mask."""
64
66 """ATRCardType constructor.
67 @param atr: the ATR of the CardType
68 @param mask: an optional mask to be applied to the ATR for
69 L{CardType} matching default is None
70 """
71 self.atr = list(atr)
72 self.mask = mask
73 if None == mask:
74 self.maskedatr = self.atr
75 else:
76 if len(self.atr) != len(self.mask):
77 raise InvalidATRMaskLengthException(toHexString(mask))
78 self.maskedatr = list(map(lambda x, y: x & y, self.atr, self.mask))
79
80 - def matches(self, atr, reader=None):
81 """Returns true if the atr matches the masked CardType atr.
82
83 @param atr: the atr to chek for matching
84 @param reader: the reader (optional); default is None
85
86 When atr is compared to the CardType ATR, matches returns true if
87 and only if CardType.atr & CardType.mask = atr & CardType.mask,
88 where & is the bitwise logical AND."""
89
90 if len(atr) != len(self.atr):
91 return not True
92
93 if None != self.mask:
94 maskedatr = list(map(lambda x, y: x & y, list(atr), self.mask))
95 else:
96 maskedatr = atr
97 return self.maskedatr == maskedatr
98
99
100 if __name__ == '__main__':
101 """Small sample illustrating the use of CardType.py."""
102 r = readers()
103 print(r)
104 connection = r[0].createConnection()
105 connection.connect()
106 atrct = ATRCardType([0x3B, 0x16, 0x94, 0x20, 0x02, 0x01, 0x00, 0x00, 0x0D])
107 print(atrct.matches(connection.getATR()))
108