question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError: tuple index out of range when encoding X509 Cert

See original GitHub issue

I think this one was not reported yet 😃

I used pyasn1 to extract the X509 certificate part of an PKCS#7 Message, here is an example:

import binascii
from pyasn1.codec.der.decoder import decode
from pyasn1.codec.der.encoder import encode

certhex  = b"3082039506092a864886f70d010702a082038630820382020101310b300906052b0e03021a0500300b06092a864886f70d010701a08202d0308202cc30820289a003020102020466eda548300b06072a8648ce380403050030363110300e06035504061307736961766173683110300e060355040a1307736961766173683110300e06035504031307736961766173683020170d3136303132353037323533305a180f32303534303532353037323533305a30363110300e06035504061307736961766173683110300e060355040a1307736961766173683110300e0603550403130773696176617368308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a0381850002818100e9b46a500aabcbb7b42e93477db9f8ac7c819eadd392f4fa2dd7c10973c399d00d45796052bfb376567a518cfe1432a0f845e4be75aa76462335bf7f84b6c634bb6c884b6ae2e7b6f1cc78c3be6f98b65980832c40d9da3d2b047cba98b24c877b2ace7908b0af56fd62367df355e011a0482c9b8751c373a8d131076cd01421a321301f301d0603551d0e04160414eeac32e5f08781bb6f2f392814951b328868ae4c300b06072a8648ce3804030500033000302d0215009209eb12e966aafae281620e7da9743774d7846302144bf36a9634967ca47639bbc10712571b036e13d531818e30818b020101303e30363110300e06035504061307736961766173683110300e060355040a1307736961766173683110300e0603550403130773696176617368020466eda548300906052b0e03021a0500300b06072a8648ce3804010500042e302c02145de23a86f0c2e45dcad93ef8cab675ec56a6ff4b021404c1a9f7f791631ae00d61fe8ae23f502c33fe84"

pkcs7message = binascii.unhexlify(certhex)

message, _ = decode(pkcs7message)
cert = encode(message[1][3])
print(cert)

With version 0.3.3 this fails due to the following error, it worked fine with the versions before 0.3.1:

Traceback (most recent call last):
  File "bla.py", line 12, in <module>
    cert = encode(message[1][3])
  File "C:\Program Files\Anaconda3\lib\site-packages\pyasn1\codec\der\encoder.py", line 56, in __call__
    return encoder.Encoder.__call__(self, value, defMode, maxChunkSize, ifNotEmpty=ifNotEmpty)
  File "C:\Program Files\Anaconda3\lib\site-packages\pyasn1\codec\cer\encoder.py", line 208, in __call__
    return encoder.Encoder.__call__(self, value, defMode, maxChunkSize, ifNotEmpty)
  File "C:\Program Files\Anaconda3\lib\site-packages\pyasn1\codec\ber\encoder.py", line 483, in __call__
    self, value, defMode, maxChunkSize, ifNotEmpty=ifNotEmpty
  File "C:\Program Files\Anaconda3\lib\site-packages\pyasn1\codec\ber\encoder.py", line 61, in encode
    encodeFun, value, defMode, maxChunkSize, ifNotEmpty=ifNotEmpty
  File "C:\Program Files\Anaconda3\lib\site-packages\pyasn1\codec\cer\encoder.py", line 156, in encodeValue
    substrate = encodeFun(value[idx], defMode, maxChunkSize, namedTypes[idx].isOptional) + substrate
  File "C:\Program Files\Anaconda3\lib\site-packages\pyasn1\type\namedtype.py", line 155, in __getitem__
    return self.__namedTypes[idx]
IndexError: tuple index out of range

Maybe, it has to do how I use it, maybe it is a real bug.

By the way, this is how I use it: https://github.com/androguard/androguard/blob/master/androguard/core/bytecodes/apk.py#L836 I also need to remove the first identifier in the DER Coded sequence, which does not look right, but I did not found another way. (If you know a better way to extract the cert from the message please tell me 😃)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
reoxcommented, Sep 7, 2017

I tested again with version 0.3.4 directly from pypi and it works fine! looks like I had pyasn1 half installed or something. Thanks a lot for fixing!

0reactions
etingofcommented, Sep 7, 2017

BTW, 0.3.4 is out on PyPi

Read more comments on GitHub >

github_iconTop Results From Across the Web

IndexError: tuple index out of range ----- Python - Stack Overflow
It's saying that the index (position) you are accessing doesn't exist. – Ramchandra Apte. Nov 30, 2013 at 3:34. what code ...
Read more >
Python IndexError: tuple index out of range Solution
The IndexError: tuple index out of range error occurs when you try to access an item in a tuple that does not exist....
Read more >
crypto — Generic cryptographic module — pyOpenSSL 22.1.0 ...
Load a certificate (X509) from the string buffer encoded with the type type. Parameters ... IndexError – If the extension index was out...
Read more >
Tuple index out of range - Faceswap Forum
It looks like you have images that are not faces inside your faces folder.... Ultimately all images in the faces folder need to...
Read more >
Class: IndexError (Ruby 3.1.2)
Class : IndexError - Ruby 3.1.2. ... Raised when the given index is invalid. ... nil a.fetch(4) #=> IndexError: index 4 outside of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found