Skip to content

Commit 46ed66a

Browse files
authored
Merge pull request #64 from asaero/fix-truncated-strings
Changed _fix_string_length calls to instead use string slices.
2 parents a47667d + f56c12b commit 46ed66a

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

pkcs11/_pkcs11.pyx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,13 @@ class Slot(types.Slot):
192192

193193
assertRV(_funclist.C_GetTokenInfo(self.slot_id, &info))
194194

195-
_fix_string_length(info.label, sizeof(info.label))
196-
_fix_string_length(info.serialNumber, sizeof(info.serialNumber))
197-
_fix_string_length(info.manufacturerID, sizeof(info.manufacturerID))
198-
_fix_string_length(info.model, sizeof(info.model))
195+
label = info.label[:sizeof(info.label)]
196+
serialNumber = info.serialNumber[:sizeof(info.serialNumber)]
197+
model = info.model[:sizeof(info.model)]
198+
manufacturerID = info.manufacturerID[:sizeof(info.manufacturerID)]
199199

200-
return Token(self, **info)
200+
return Token(self, label, serialNumber, model, manufacturerID,
201+
info.hardwareVersion, info.firmwareVersion, info.flags)
201202

202203
def get_mechanisms(self):
203204
cdef CK_ULONG count
@@ -1247,13 +1248,11 @@ cdef class lib:
12471248
cdef CK_INFO info
12481249
assertRV(_funclist.C_GetInfo(&info))
12491250

1250-
_fix_string_length(info.manufacturerID,
1251-
sizeof(info.manufacturerID))
1252-
_fix_string_length(info.libraryDescription,
1253-
sizeof(info.libraryDescription))
1251+
manufacturerID = info.manufacturerID[:sizeof(info.manufacturerID)]
1252+
libraryDescription = info.libraryDescription[:sizeof(info.libraryDescription)]
12541253

1255-
self.manufacturer_id = _CK_UTF8CHAR_to_str(info.manufacturerID)
1256-
self.library_description = _CK_UTF8CHAR_to_str(info.libraryDescription)
1254+
self.manufacturer_id = _CK_UTF8CHAR_to_str(manufacturerID)
1255+
self.library_description = _CK_UTF8CHAR_to_str(libraryDescription)
12571256
self.cryptoki_version = _CK_VERSION_to_tuple(info.cryptokiVersion)
12581257
self.library_version = _CK_VERSION_to_tuple(info.libraryVersion)
12591258

@@ -1291,12 +1290,13 @@ cdef class lib:
12911290
for slotID in slotIDs:
12921291
assertRV(_funclist.C_GetSlotInfo(slotID, &info))
12931292

1294-
_fix_string_length(info.slotDescription,
1295-
sizeof(info.slotDescription))
1296-
_fix_string_length(info.manufacturerID,
1297-
sizeof(info.manufacturerID))
1293+
slotDescription = info.slotDescription[:sizeof(info.slotDescription)]
1294+
manufacturerID = info.manufacturerID[:sizeof(info.manufacturerID)]
12981295

1299-
slots.append(Slot(self, slotID, **info))
1296+
slots.append(
1297+
Slot(self, slotID, slotDescription, manufacturerID,
1298+
info.hardwareVersion, info.firmwareVersion, info.flags)
1299+
)
13001300

13011301
return slots
13021302

pkcs11/_utils.pyx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,3 @@ cdef _unpack_attributes(key, value):
3636
except KeyError:
3737
raise NotImplementedError("Can't unpack this %s. "
3838
"Expand ATTRIBUTE_TYPES!" % key)
39-
40-
41-
cdef _fix_string_length(CK_UTF8CHAR *string, size_t size):
42-
"""Insert NUL-bytes into strings so Python knows their length."""
43-
string[size - 1] = b'\0'

tests/test_aes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def test_sign_stream(self):
107107

108108
@requires(Mechanism.AES_KEY_WRAP)
109109
@FIXME.opencryptoki # can't set key attributes
110-
@FIXME.travis # Travis has an old OpenSSL
111110
def test_wrap(self):
112111
key = self.session.generate_key(pkcs11.KeyType.AES, 128, template={
113112
pkcs11.Attribute.EXTRACTABLE: True,

0 commit comments

Comments
 (0)