Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue parsing documents containing marker in signature #10

Open
lstoll opened this issue Sep 6, 2016 · 2 comments
Open

Issue parsing documents containing marker in signature #10

lstoll opened this issue Sep 6, 2016 · 2 comments

Comments

@lstoll
Copy link

lstoll commented Sep 6, 2016

I noticed parsing/validation was failing for a small number of AWS Identity Documents. I dug in a bit, and it looks like there's an issue in the parser. It seems like if the signature (or other data later in the doc) has the marker, it will skip ahead and not read the document correctly.

Unfortunately I can't share the raw data, but here's some debug info I captured.

Here's a good document, from asn1parse:

    0:d=0  hl=2 l=inf  cons: SEQUENCE          
    2:d=1  hl=2 l=   9 prim: OBJECT            :pkcs7-signedData
   13:d=1  hl=2 l=inf  cons: cont [ 0 ]        
   15:d=2  hl=2 l=inf  cons: SEQUENCE          
   17:d=3  hl=2 l=   1 prim: INTEGER           :01
   20:d=3  hl=2 l=  11 cons: SET               
   22:d=4  hl=2 l=   9 cons: SEQUENCE          
   24:d=5  hl=2 l=   5 prim: OBJECT            :sha1
   31:d=5  hl=2 l=   0 prim: NULL              
   33:d=3  hl=2 l=inf  cons: SEQUENCE          
   35:d=4  hl=2 l=   9 prim: OBJECT            :pkcs7-data
   46:d=4  hl=2 l=inf  cons: cont [ 0 ]        
   48:d=5  hl=2 l=inf  cons: OCTET STRING      
   50:d=6  hl=4 l= 432 prim: OCTET STRING      :{
<snip>
}
  486:d=6  hl=2 l=   0 prim: EOC               
  488:d=5  hl=2 l=   0 prim: EOC               
  490:d=4  hl=2 l=   0 prim: EOC               
  492:d=3  hl=4 l= 280 cons: SET               
  496:d=4  hl=4 l= 276 cons: SEQUENCE          
  500:d=5  hl=2 l=   1 prim: INTEGER           :01
  503:d=5  hl=2 l= 105 cons: SEQUENCE          
  505:d=6  hl=2 l=  92 cons: SEQUENCE          
  507:d=7  hl=2 l=  11 cons: SET               
  509:d=8  hl=2 l=   9 cons: SEQUENCE          
  511:d=9  hl=2 l=   3 prim: OBJECT            :countryName
  516:d=9  hl=2 l=   2 prim: PRINTABLESTRING   :US
  520:d=7  hl=2 l=  25 cons: SET               
  522:d=8  hl=2 l=  23 cons: SEQUENCE          
  524:d=9  hl=2 l=   3 prim: OBJECT            :stateOrProvinceName
  529:d=9  hl=2 l=  16 prim: PRINTABLESTRING   :Washington State
  547:d=7  hl=2 l=  16 cons: SET               
  549:d=8  hl=2 l=  14 cons: SEQUENCE          
  551:d=9  hl=2 l=   3 prim: OBJECT            :localityName
  556:d=9  hl=2 l=   7 prim: PRINTABLESTRING   :Seattle
  565:d=7  hl=2 l=  32 cons: SET               
  567:d=8  hl=2 l=  30 cons: SEQUENCE          
  569:d=9  hl=2 l=   3 prim: OBJECT            :organizationName
  574:d=9  hl=2 l=  23 prim: PRINTABLESTRING   :Amazon Web Services LLC
  599:d=6  hl=2 l=   9 prim: INTEGER           :96BA48D9E55E1A67
  610:d=5  hl=2 l=   9 cons: SEQUENCE          
  612:d=6  hl=2 l=   5 prim: OBJECT            :sha1
  619:d=6  hl=2 l=   0 prim: NULL              
  621:d=5  hl=2 l=  93 cons: cont [ 0 ]        
  623:d=6  hl=2 l=  24 cons: SEQUENCE          
  625:d=7  hl=2 l=   9 prim: OBJECT            :contentType
  636:d=7  hl=2 l=  11 cons: SET               
  638:d=8  hl=2 l=   9 prim: OBJECT            :pkcs7-data
  649:d=6  hl=2 l=  28 cons: SEQUENCE          
  651:d=7  hl=2 l=   9 prim: OBJECT            :signingTime
  662:d=7  hl=2 l=  15 cons: SET               
  664:d=8  hl=2 l=  13 prim: UTCTIME           :160609022532Z
  679:d=6  hl=2 l=  35 cons: SEQUENCE          
  681:d=7  hl=2 l=   9 prim: OBJECT            :messageDigest
  692:d=7  hl=2 l=  22 cons: SET               
  694:d=8  hl=2 l=  20 prim: OCTET STRING      [HEX DUMP]:6108B512F7688CAAF55246D66FC7B73D17D379F1
  716:d=5  hl=2 l=   9 cons: SEQUENCE          
  718:d=6  hl=2 l=   7 prim: OBJECT            :dsaWithSHA1
  727:d=5  hl=2 l=  47 prim: OCTET STRING      [HEX DUMP]:302D0214652FCCEC81B461A56B8D25AD10FCE3BDDC0BDE3B021500B090CAF10CDF226BA859D2297CAB5AB5C60A5F69
  776:d=3  hl=2 l=   0 prim: EOC               
  778:d=2  hl=2 l=   0 prim: EOC               
  780:d=1  hl=2 l=   0 prim: EOC               

This parses fine

--> (compute length) marker found at offset: 780
--> length        : 778
--> length        : 9
--> (compute length) marker found at offset: 778
--> length        : 763
--> (compute length) marker found at offset: 776
--> length        : 759
--> length        : 1
--> length        : 11
--> length        : 9
--> length        : 5
--> length        : 0
--> (compute length) marker found at offset: 490
--> length        : 455
--> length        : 9
--> (compute length) marker found at offset: 488
--> length        : 440
--> (compute length) marker found at offset: 486
--> length        : 436
--> (compute length) indicator byte: 82
--> (compute length) length bytes: 01 B0
--> length        : 432
--> (compute length) indicator byte: 82
--> (compute length) length bytes: 01 18
--> length        : 280
--> (compute length) indicator byte: 82
--> (compute length) length bytes: 01 14
--> length        : 276
--> length        : 1
--> length        : 105
--> length        : 92
--> length        : 11
--> length        : 9
--> length        : 3
--> length        : 2
--> length        : 25
--> length        : 23
--> length        : 3
--> length        : 16
--> length        : 16
--> length        : 14
--> length        : 3
--> length        : 7
--> length        : 32
--> length        : 30
--> length        : 3
--> length        : 23
--> length        : 9
--> length        : 9
--> length        : 5
--> length        : 0
--> length        : 93
--> length        : 24
--> length        : 9
--> length        : 11
--> length        : 9
--> length        : 28
--> length        : 9
--> length        : 15
--> length        : 13
--> length        : 35
--> length        : 9
--> length        : 22
--> length        : 20
--> length        : 9
--> length        : 7
--> length        : 47

and here's a copy of a doc that was failing:

    0:d=0  hl=2 l=inf  cons: SEQUENCE          
    2:d=1  hl=2 l=   9 prim: OBJECT            :pkcs7-signedData
   13:d=1  hl=2 l=inf  cons: cont [ 0 ]        
   15:d=2  hl=2 l=inf  cons: SEQUENCE          
   17:d=3  hl=2 l=   1 prim: INTEGER           :01
   20:d=3  hl=2 l=  11 cons: SET               
   22:d=4  hl=2 l=   9 cons: SEQUENCE          
   24:d=5  hl=2 l=   5 prim: OBJECT            :sha1
   31:d=5  hl=2 l=   0 prim: NULL              
   33:d=3  hl=2 l=inf  cons: SEQUENCE          
   35:d=4  hl=2 l=   9 prim: OBJECT            :pkcs7-data
   46:d=4  hl=2 l=inf  cons: cont [ 0 ]        
   48:d=5  hl=2 l=inf  cons: OCTET STRING      
   50:d=6  hl=4 l= 419 prim: OCTET STRING      :{
<snip>
}
  473:d=6  hl=2 l=   0 prim: EOC               
  475:d=5  hl=2 l=   0 prim: EOC               
  477:d=4  hl=2 l=   0 prim: EOC               
  479:d=3  hl=4 l= 279 cons: SET               
  483:d=4  hl=4 l= 275 cons: SEQUENCE          
  487:d=5  hl=2 l=   1 prim: INTEGER           :01
  490:d=5  hl=2 l= 105 cons: SEQUENCE          
  492:d=6  hl=2 l=  92 cons: SEQUENCE          
  494:d=7  hl=2 l=  11 cons: SET               
  496:d=8  hl=2 l=   9 cons: SEQUENCE          
  498:d=9  hl=2 l=   3 prim: OBJECT            :countryName
  503:d=9  hl=2 l=   2 prim: PRINTABLESTRING   :US
  507:d=7  hl=2 l=  25 cons: SET               
  509:d=8  hl=2 l=  23 cons: SEQUENCE          
  511:d=9  hl=2 l=   3 prim: OBJECT            :stateOrProvinceName
  516:d=9  hl=2 l=  16 prim: PRINTABLESTRING   :Washington State
  534:d=7  hl=2 l=  16 cons: SET               
  536:d=8  hl=2 l=  14 cons: SEQUENCE          
  538:d=9  hl=2 l=   3 prim: OBJECT            :localityName
  543:d=9  hl=2 l=   7 prim: PRINTABLESTRING   :Seattle
  552:d=7  hl=2 l=  32 cons: SET               
  554:d=8  hl=2 l=  30 cons: SEQUENCE          
  556:d=9  hl=2 l=   3 prim: OBJECT            :organizationName
  561:d=9  hl=2 l=  23 prim: PRINTABLESTRING   :Amazon Web Services LLC
  586:d=6  hl=2 l=   9 prim: INTEGER           :96BA48D9E55E1A67
  597:d=5  hl=2 l=   9 cons: SEQUENCE          
  599:d=6  hl=2 l=   5 prim: OBJECT            :sha1
  606:d=6  hl=2 l=   0 prim: NULL              
  608:d=5  hl=2 l=  93 cons: cont [ 0 ]        
  610:d=6  hl=2 l=  24 cons: SEQUENCE          
  612:d=7  hl=2 l=   9 prim: OBJECT            :contentType
  623:d=7  hl=2 l=  11 cons: SET               
  625:d=8  hl=2 l=   9 prim: OBJECT            :pkcs7-data
  636:d=6  hl=2 l=  28 cons: SEQUENCE          
  638:d=7  hl=2 l=   9 prim: OBJECT            :signingTime
  649:d=7  hl=2 l=  15 cons: SET               
  651:d=8  hl=2 l=  13 prim: UTCTIME           :160817213415Z
  666:d=6  hl=2 l=  35 cons: SEQUENCE          
  668:d=7  hl=2 l=   9 prim: OBJECT            :messageDigest
  679:d=7  hl=2 l=  22 cons: SET               
  681:d=8  hl=2 l=  20 prim: OCTET STRING      [HEX DUMP]:E89C4873A60EECD94FCE9054D51C479912051810
  703:d=5  hl=2 l=   9 cons: SEQUENCE          
  705:d=6  hl=2 l=   7 prim: OBJECT            :dsaWithSHA1
  714:d=5  hl=2 l=  46 prim: OCTET STRING      [HEX DUMP]:302C02140488BD1B4F10EC11CF4A4628D7C7C10000B4C1AF02145498779E1F609A00CC1F817445218B778C802025
  762:d=3  hl=2 l=   0 prim: EOC               
  764:d=2  hl=2 l=   0 prim: EOC               
  766:d=1  hl=2 l=   0 prim: EOC               

and the debug output:

--> (compute length) marker found at offset: 766
--> length        : 764
--> length        : 9
--> (compute length) marker found at offset: 764
--> length        : 749
--> (compute length) marker found at offset: 762
--> length        : 745
--> length        : 1
--> length        : 11
--> length        : 9
--> length        : 5
--> length        : 0
--> (compute length) marker found at offset: 735
--> length        : 700
--> length        : 9
--> (compute length) marker found at offset: 477
--> length        : 429
--> (compute length) marker found at offset: 475
--> length        : 425
--> (compute length) indicator byte: 82
--> (compute length) length bytes: 01 A3
--> length        : 419
--> length        : 0
--> (compute length) indicator byte: 82
--> (compute length) length bytes: 01 17
--> length        : 279
p7bad Failed at pkcs7.Parse(sigDecode.Bytes): ber2der: BER tag length is more than available data

You can see in this latter one it's skipping the marker in the middle of the document, in favor of the one at 735. This is bang in the middle of the signature, where you can find 0000 - so I think this is triggering as a marker here and breaking the parsing.

@fullsailor
Copy link
Owner

I'll check it out. Some of the ber2der routines were pretty naive. It might be scanning byte sequences it should be ignoring.

@lstoll
Copy link
Author

lstoll commented Sep 6, 2016

Awesome, thanks. We ended up shifting off to a different method that doesn't need PKCS7, but I figured I'd report it anyway. Sorry I couldn't share the docs, but I've kept them handy so can test anything out. If I can get something to share I'll report back too.

chrisccoulson pushed a commit to chrisccoulson/pkcs7 that referenced this issue Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants