Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

CSR parsing ends in 'PHP Fatal error' => ParserException #57

Closed
dol opened this issue Sep 29, 2015 · 7 comments
Closed

CSR parsing ends in 'PHP Fatal error' => ParserException #57

dol opened this issue Sep 29, 2015 · 7 comments
Assignees
Labels

Comments

@dol
Copy link

dol commented Sep 29, 2015

I try to parse a CSR from binary. This following example ends with the following fatal error:

PHP Fatal error:  Uncaught exception 'FG\ASN1\Exception\ParserException' with message 'ASN.1 Parser Exception at offset 544: Context-Specific explicitly tagged object [0] starting at offset 544 is longer than allowed in the outer tag' in /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/ExplicitlyTaggedObject.php:97
Stack trace:
#0 /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/Object.php(201): FG\ASN1\ExplicitlyTaggedObject::fromBinary('0??X0??????0W1?...', 555)
#1 /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/Construct.php(149): FG\ASN1\Object::fromBinary('0??X0??????0W1?...', 555)
#2 /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/Object.php(222): FG\ASN1\Construct::fromBinary('0??X0??????0W1?...', 555)
#3 /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/Construct.php(149): FG\ASN1\Object::fromBinary('0??X0??????0W1?...', 555)
#4 /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/Object.php(222): FG\ASN1\Construct::fromBinary('0??X0??????0W1?...', 555)
#5 /tmp/foo/test.php(50): FG\ASN1\Object::fromBinary('0??X0??????0W1?...')
#6 {main}
  thrown in /tmp/foo/vendor/fgrosse/phpasn1/lib/ASN1/ExplicitlyTaggedObject.php on line 97
<?php

require_once __DIR__ . '/vendor/autoload.php';

use FG\ASN1\Object;

// Test vector CSR from https://github.com/pyca/cryptography/blob/b5bb49d115e9f325d90bb96aebc15e6a1ef30f0d/vectors/cryptography_vectors/x509/requests/dsa_sha1.pem
$origCsr = <<<'EOF'
# Use this command to check the if the ASN1 parsing of openssl
(cat <<CATEOF | openssl asn1parse -inform PEM
-----BEGIN CERTIFICATE REQUEST-----
MIICWDCCAhgCAQAwVzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5LmlvMQ0wCwYDVQQK
DARQeUNBMQswCQYDVQQGEwJVUzEOMAwGA1UECAwFVGV4YXMxDzANBgNVBAcMBkF1
c3RpbjCCAbYwggErBgcqhkjOOAQBMIIBHgKBgQCNf628CeKEqvppFUzqJBdwBJCe
UZ+LNdaFzeW07NyVg+dNNwoPiK2pjwJvJ3Yvs9XaeDb5ht/Ns1ieW5Jb6hFN78A+
+B2uMMJLvG3z1YjpNCe7pkID1KWxaHsrXjtkPUxhSXb4n5WjjT5MiQZfupdRTCLF
Ctu/KJFjp0tUhZs1twIVAINd5WvQfPf4LiAy/niUmu0ReqLvAoGAH3F7Wgd4L8Lk
5o4xH+qRpU7dNrhqxjTRTwWmipfq6dLvMfse895Cw9EA35ymT1vcKux7/ftHTPgx
/qBYU7XgWfLSSYCgrEY/HoGK81I+PLeaOdRfqScxiXdShCRpz4VAsBSRAk6q+85g
GOih9GWMND9Lp8CyHlN2oh9L64SRlh4DgYQAAoGABxPwdkH2Npu1qVRSdKLUwBmY
Nn+zcbueE0NjY2cu1o+CF0wt4FyOg5vG3laN1QuijY2dhxlCOq7FVX3xDXc6si1t
Zcu4eASml7yP2WW5Uvn36FDt8TyKzbXXU7bRDlngtXMuPIK6+hQDQrxKO7oWvQaB
yKai27t+/mziuEY7FwugADAJBgcqhkjOOAQDAy8AMCwCFGHVjcAo0BEIGKfYF9dC
NXJ8Ss/fAhQJe1LhmOzpXeFyc/CpJN8jzp2BiA==
-----END CERTIFICATE REQUEST-----
CATEOF
)

EOF;

echo $origCsr;

$csr = <<<'EOF'
MIICWDCCAhgCAQAwVzEYMBYGA1UEAwwPY3J5cHRvZ3JhcGh5LmlvMQ0wCwYDVQQK
DARQeUNBMQswCQYDVQQGEwJVUzEOMAwGA1UECAwFVGV4YXMxDzANBgNVBAcMBkF1
c3RpbjCCAbYwggErBgcqhkjOOAQBMIIBHgKBgQCNf628CeKEqvppFUzqJBdwBJCe
UZ+LNdaFzeW07NyVg+dNNwoPiK2pjwJvJ3Yvs9XaeDb5ht/Ns1ieW5Jb6hFN78A+
+B2uMMJLvG3z1YjpNCe7pkID1KWxaHsrXjtkPUxhSXb4n5WjjT5MiQZfupdRTCLF
Ctu/KJFjp0tUhZs1twIVAINd5WvQfPf4LiAy/niUmu0ReqLvAoGAH3F7Wgd4L8Lk
5o4xH+qRpU7dNrhqxjTRTwWmipfq6dLvMfse895Cw9EA35ymT1vcKux7/ftHTPgx
/qBYU7XgWfLSSYCgrEY/HoGK81I+PLeaOdRfqScxiXdShCRpz4VAsBSRAk6q+85g
GOih9GWMND9Lp8CyHlN2oh9L64SRlh4DgYQAAoGABxPwdkH2Npu1qVRSdKLUwBmY
Nn+zcbueE0NjY2cu1o+CF0wt4FyOg5vG3laN1QuijY2dhxlCOq7FVX3xDXc6si1t
Zcu4eASml7yP2WW5Uvn36FDt8TyKzbXXU7bRDlngtXMuPIK6+hQDQrxKO7oWvQaB
yKai27t+/mziuEY7FwugADAJBgcqhkjOOAQDAy8AMCwCFGHVjcAo0BEIGKfYF9dC
NXJ8Ss/fAhQJe1LhmOzpXeFyc/CpJN8jzp2BiA==
EOF;

// $csr = str_replace("\n", '', $csr);
$asnObject = Object::fromBinary(base64_decode($csr));

Can you figure out what I'm doing wrong or do you spot the problem. Currently used version is 1.3.2 due to a local limitation of using PHP 5.6 requirement.

@fgrosse
Copy link
Owner

fgrosse commented Sep 29, 2015

I just had a look at your issue and found a bug that prevents any tagged objects to be empty.
This will be fixed in 1.4.2 and for you on 1.3.3

fgrosse added a commit that referenced this issue Sep 29, 2015
Apparently tagged objects can be entirely empty (have no content)

Fixes #57
@fgrosse
Copy link
Owner

fgrosse commented Sep 29, 2015

If you reallly need to stick to an older PHP version you can use the dev-compat/php5-5 branch alias in your composer.json. For everybody else, this fix is avilable in PHPASN version >= 1.4.2.

Thanks for reporting the issue Dominic :)

@fgrosse fgrosse added the bug label Sep 29, 2015
@fgrosse fgrosse self-assigned this Sep 29, 2015
@dol
Copy link
Author

dol commented Sep 29, 2015

Thank you for the fix.

@dol
Copy link
Author

dol commented Sep 30, 2015

I'd like to reopen this issue because I found some multi attribute CSR's that have the same parsing issue.

E.g:

<?php
// https://github.com/phpseclib/phpseclib/blob/master/tests/Unit/File/X509/CSRTest.php
$csrWithMultipleAttributes = <<<'EOF'
MIIFGDCCAwACAQAwOjEWMBQGCgmSJomT8ixkARkWBnNlY3VyZTEgMB4GA1UEAxMX
LlNlY3VyZSBFbnRlcnByaXNlIENBIDEwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw
ggIKAoICAQCzgEpL+Za7a3y7YpURDrxlGIBlks25fD0tHaZIYkBTaXA5h+9MWoXn
FA7AlIUt8pbBvXdJbOCmGaeQmBfBH0Qy9vTbx/DR2IOwzqy2ZHuurI5bPL12ceE2
Mxa9xgY/i7U6MAUtoA3amEd7cKj2fz9EWZruRladOX0DXv9KexSan+45QjCWH+u2
Cxem2zH9ZDNPGBuAF9YsAvkdHdAoX8aSm05ZAjUiO2e/+L57whh7zZiDY3WIhin7
N/2JNTKVO6lx50S8a34XUKBt3SKgSR941hcLrBYUNftUYsTPo40bzKKcWqemiH+w
jQiDrln4V2b5EbVeoGWe4UDPXCVmC6UPklG7iYfF0eeK4ujV8uc9PtV2LvGLOFdm
AYE3+FAba5byQATw/DY8EJKQ7ptPigJhVe47NNeJlsKwk1haJ9k8ZazjS+vT45B5
pqe0yBFAEon8TFnOLnAOblmKO12i0zqMUNAAlmr1c8jNjLr+dhruS+QropZmzZ24
mAnFG+Y0qpfhMzAxTGQyVjyGwDfRK/ARmtrGpmROjj5+6VuMmZ6Ljf3xN09epmtH
gJe+lYNBlpfUYg16tm+OusnziYnXL6nIo2ChOY/7GNJJif9fjvvaPDCC98K64av5
5rpIx7N/XH4hwHeQQkEQangExE+8UMy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ClNvbWUtU3RhdGUx
ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDENMAsGA1UEAwwEdGVz
dDEcMBoGCSqGSIb3DQEJARYNdGVzdEB0ZXN0LmNvbTCCASIwDQYJKoZIhvcNAQEB
BQADggEPADCCAQoCggEBAL7t6CR90kOZ0m3qJx9koHBFdXnunEq9ELSUvsZ7oFiw
6zJsyi18S9znVRLEgsW4v2rTsLiJmKnMF2Kyd9AQ2X2zKUK49CTMqG1p/4lF0oLa
nQgNmjYC4VL61wTte6CUAJX7wZA8LL00QfCdgILKodP12E3c7Yp3yw5TAOBdA3Av
KnpsUiNLmQN3HSn9dwk9rCHulrP8LL97l//H2KSCvOvSfk1XeN7ADSyWMC6ZPBD/
7WVnQo7GgMAA0Qhz+nIIFA5AyuFEbRiff+L5EEl+Bg6R9tgx+QUt9JXxF260dkAb
iKBmPcJldl2/GEmz6RAXhMwJnN35tbdzw0LVLzsypr0CAwEAAaAsMBMGCSqGSIb3
DQEJAjEGDAR0ZXN0MBUGCSqGSIb3DQEJBzEIDAYxMjM0NTYwDQYJKoZIhvcNAQEF
BQADggEBACqZ5+3GP7QbTJAqnUloMRBJGYZl0/EyW6dAMoq7E+RiR2OlJGXgP+ga
DPv7Wa4jZiYmf2CZCWKP+WvTvcg4AQq5h7hhAN+u+s8kk0lIjYjnkKRAedGYKDUO
fPD9WowISru3RB1xyxlvgeZS6WoA6TD8REVa1UyFoNzUewvsrNVkKSHh1xk/+ePx
2Ovvrcg9pAWY4K8FvMRdFQKnEud9CAoMxqz3kszhxDW6rcr2mgFPSrKi5WNj+Scg
Tqod8xbB753JWjEbG6Hui9LIMihTX3ZJ0c2GB0buhEgz49y83X/byFHSGGSQpzxX
qXDFVov9UZ+sGy8CJ5ahII79yrfKpxY=
EOF;

Online ASN1 parser view:
csrWithMultipleAttributes
[csrPerl](// https://lapo.it/asn1js/#308202E3308201CB0201003072310B30090603550406130241553113301106035504080C0A536F6D652D53746174653121301F060355040A0C18496E7465726E6574205769646769747320507479204C7464310D300B06035504030C0474657374311C301A06092A864886F70D010901160D7465737440746573742E636F6D30820122300D06092A864886F70D01010105000382010F003082010A0282010100BEEDE8247DD24399D26DEA271F64A070457579EE9C4ABD10B494BEC67BA058B0EB326CCA2D7C4BDCE75512C482C5B8BF6AD3B0B88998A9CC1762B277D010D97DB32942B8F424CCA86D69FF8945D282DA9D080D9A3602E152FAD704ED7BA0940095FBC1903C2CBD3441F09D8082CAA1D3F5D84DDCED8A77CB0E5300E05D03702F2A7A6C52234B9903771D29FD77093DAC21EE96B3FC2CBF7B97FFC7D8A482BCEBD27E4D5778DEC00D2C96302E993C10FFED6567428EC680C000D10873FA7208140E40CAE1446D189F7FE2F910497E060E91F6D831F9052DF495F1176EB476401B88A0663DC265765DBF1849B3E9101784CC099CDDF9B5B773C342D52F3B32A6BD0203010001A02C301306092A864886F70D01090231060C0474657374301506092A864886F70D01090731080C06313233343536300D06092A864886F70D010105050003820101002A99E7EDC63FB41B4C902A9D4968311049198665D3F1325BA740328ABB13E4624763A52465E03FE81A0CFBFB59AE236626267F609909628FF96BD3BDC838010AB987B86100DFAEFACF249349488D88E790A44079D19828350E7CF0FD5A8C084ABBB7441D71CB196F81E652E96A00E930FC44455AD54C85A0DCD47B0BECACD5642921E1D7193FF9E3F1D8EBEFADC83DA40598E0AF05BCC45D1502A712E77D080A0CC6ACF792CCE1C435BAADCAF69A014F4AB2A2E56363F927204EAA1DF316C1EF9DC95A311B1BA1EE8BD2C83228535F7649D1CD860746EE844833E3DCBCDD7FDBC851D2186490A73C57A970C5568BFD519FAC1B2F022796A1208EFDCAB7CAA716)

@fgrosse fgrosse reopened this Sep 30, 2015
@fgrosse
Copy link
Owner

fgrosse commented Sep 30, 2015

The issue with the two given inputs is that the tag seems to be applied to multiple objects instead of exactly one. I didnt know this was possible and have to meditate over the solution a bit :)

@fgrosse
Copy link
Owner

fgrosse commented Sep 30, 2015

The second fix is available in version 1.5.0. I decided for the minor version increase since having multiple objects under one tag felt more like a feature to me. Again I ported the change to the dev-compat/php5-5 branch alias as well.

Additionally you should have a look at examples/Issue57.php which contains all three of your inputs.

Best regards
Friedrich

@dol
Copy link
Author

dol commented Oct 1, 2015

Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants