Skip to content

Commit 3028b2a

Browse files
authored
Merge pull request #6 from nickaknudson/bugfix/signature
Fix signature record to encode lengths as 16-bits (closes #5). Many thanks to @nickaknudson.
2 parents 91c1412 + dba0297 commit 3028b2a

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/ndef/signature.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,22 @@ def _encode_payload(self):
302302
SIGURI = self.signature_uri.encode('utf-8')
303303
else:
304304
SIGURI = self.signature
305-
SIGNATURE = self._encode_struct('BBB+', SUP | SST, SHT, SIGURI)
305+
SIGNATURE = self._encode_struct('BBH+', SUP | SST, SHT, SIGURI)
306306

307307
# Certificate Field
308308
CUP = 0b10000000 if self.certificate_uri else 0
309309
CCF = (self._certificate_format << 4) & 0b01110000
310310
CNC = len(self.certificate_store) & 0b00001111
311311
CST = b''
312312
for certificate in self._certificate_store:
313-
CST += self._encode_struct('B+', certificate)
313+
CST += self._encode_struct('H+', certificate)
314314
CERTURI = self._certificate_uri.encode('utf-8')
315315
if len(CST):
316316
CERTIFICATE = self._encode_struct(
317-
'B'+str(len(CST))+'sB+', CUP | CCF | CNC, CST, CERTURI)
317+
'B'+str(len(CST))+'sH+', CUP | CCF | CNC, CST, CERTURI)
318318
else:
319319
CERTIFICATE = self._encode_struct(
320-
'BB+', CUP | CCF | CNC, CERTURI)
320+
'BH+', CUP | CCF | CNC, CERTURI)
321321

322322
return VERSION + SIGNATURE + CERTIFICATE
323323

@@ -331,7 +331,7 @@ def _decode_payload(cls, octets, errors):
331331
# decoding steps failed.
332332

333333
(VERSION, SUP_SST, SHT, SIGURI,
334-
CUP_CCF_CNC, CST_CERTURI) = cls._decode_struct('BBBB+B*', octets)
334+
CUP_CCF_CNC, CST_CERTURI) = cls._decode_struct('BBBH+B*', octets)
335335

336336
# Version Field
337337
if not VERSION == cls._version and errors == 'strict':
@@ -364,10 +364,10 @@ def _decode_payload(cls, octets, errors):
364364
certificate_number_of_certificates = CUP_CCF_CNC & 0b00001111
365365
certificate_store = []
366366
for certificate_number in range(certificate_number_of_certificates):
367-
certificate, CST_CERTURI = cls._decode_struct('B+*', CST_CERTURI)
367+
certificate, CST_CERTURI = cls._decode_struct('H+*', CST_CERTURI)
368368
certificate_store.append(certificate)
369369
if certificate_uri_present:
370-
CERTURI = cls._decode_struct('B+', CST_CERTURI)
370+
CERTURI = cls._decode_struct('H+', CST_CERTURI)
371371
try:
372372
certificate_uri = CERTURI.decode('utf-8')
373373
except UnicodeDecodeError:

tests/test_signature.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,35 @@ class TestSignatureRecord(_test_record_base._TestRecordBase):
5555
" '1' does not have a known Certificate Format mapping"),
5656
]
5757
test_decode_valid_data = [
58-
('200002000000',
58+
('2000020000000000',
5959
(None, 'SHA-256', b'', '', 'X.509', [], '')),
60-
(('200b02473045022100a410c28fd9437fd24f6656f121e62bcc5f65e36257f5faadf'
61-
'68e3e83d40d481a0220335b1dff8d6fe722fcf7018be9684d2de5670b256fdfc02a'
62-
'a25bdae16f624b800000'),
60+
(('200b0200473045022100a410c28fd9437fd24f6656f121e62bcc5f65e36257f5faa'
61+
'df68e3e83d40d481a0220335b1dff8d6fe722fcf7018be9684d2de5670b256fdfc0'
62+
'2aa25bdae16f624b80000000'),
6363
('ECDSA-P256', 'SHA-256',
6464
(b'0E\x02!\x00\xa4\x10\xc2\x8f\xd9C\x7f\xd2OfV\xf1!\xe6+\xcc_e\xe3bW'
6565
b'\xf5\xfa\xad\xf6\x8e>\x83\xd4\rH\x1a\x02 3[\x1d\xff\x8do\xe7"\xfc'
6666
b'\xf7\x01\x8b\xe9hM-\xe5g\x0b%o\xdf\xc0*\xa2[\xda\xe1obK\x80'),
6767
'', 'X.509', [], '')),
68-
(('2080020d7369676e61747572655f757269800f63657274696669636174655f75726'
69-
'9'),
68+
(('208002000d7369676e61747572655f75726980000f63657274696669636174655f7'
69+
'57269'),
7070
(None, 'SHA-256', b'', 'signature_uri', 'X.509', [],
7171
'certificate_uri')),
72-
('20000200020131013200',
72+
('2000020000020001310001320000',
7373
(None, None, None, None, None, [b'1', b'2'], None)),
74-
('200b02001000',
74+
('200b020000100000',
7575
('ECDSA-P256', 'SHA-256', None, None, 'M2M', None, None)),
7676
]
7777
test_decode_error_data = [
78-
('100002000000', "decoding of version 16 is not supported"),
79-
('20800201800000', "Signature URI field is not valid UTF-8 data"),
80-
('20800201000000', ("Signature URI field contains "
81-
"invalid characters")),
82-
('20000200800180', "Certificate URI field is not valid UTF-8 data"),
83-
('20000200800100', ("Certificate URI field contains "
84-
"invalid characters")),
78+
('10000200000000', "decoding of version 16 is not supported"),
79+
('2080020001800000', ("Signature URI field is "
80+
"not valid UTF-8 data")),
81+
('2080020001000000', ("Signature URI field contains "
82+
"invalid characters")),
83+
('200002000080000180', ("Certificate URI field is "
84+
"not valid UTF-8 data")),
85+
('200002000080000100', ("Certificate URI field contains "
86+
"invalid characters")),
8587
]
8688
test_decode_relax = None
8789
test_encode_error = None

0 commit comments

Comments
 (0)