phpseclib $X509->setDomain on CSR?

你。 提交于 2020-02-06 04:38:49

问题


Is it possible to set x509 v3 extended attribute for subjectAltName on a CSR?

I am able to successfully generate a CSR, and then pass that to a CA to sign.

The CA is able to call functions like X509->setDomain("bob.com","*.bob.com","asdf.org"); and they appear in the final cert without issue (note: i am doing the sign, reload, set extensions, resign workaround for phpseclib).

The CSR process calling the same functions $X509->setExtension("id-ce-subjectAltName",array("names","here") ) or $X509->setDomain("domain1","domain2"); does not appear to set some X509 extended attributes in the CSR itself (openssl can), I have only been able to add SAN by the CA after re-import before the second signing.

Thanks!

Edit: I created an example CSR (sign, reload, x509v3 extensions, resign workaround) and it seems possible to set SOME extended attributes (CA: false, key usage, etc.) but NOT the subjectAltName info:

-----BEGIN CERTIFICATE REQUEST-----
MIIC1TCCAb8CAQAwVDELMAkGA1UEBgwCU1MxEDAOBgNVBAgMB09ic2N1cmUxDzAN
BgNVBAcMBlNlY3VyZTEQMA4GA1UECgwHYm9iY29ycDEQMA4GA1UEAwwHYm9iLmNv
bTCCASAwCwYJKoZIhvcNAQEBA4IBDwAwggEKAoIBAQCunNrjxEOILsESZ1osUkT3
zSeAHlzNiCBQnc/Xf+oW7Ir7wKfbHkV10cM583mw3Zy8rlCT/lUq0H+f3Uoc5/FA
dsYWhatlJRlTjv+yjSsxyB9i/hZ/KliP3Ix2O+Pq1wZIWfvk40hmCHHSB6YDtqt0
vXqUTIhH3SfyLtK9nd/6WG8bgIq9jgL6xvF3h44ynEwkuOnHt4a9WrflGX4KKcwo
OM98M/TAntDgSXBEYoLxenIbl3ypa7gtghVHHls3QOSay5QM87K3PJ3kSWVlZ3tN
tZGfbFdLiS+3MH8G3ujSX8XYeBnUj5jXi4SzpzQ9o5pyArL8DD8kC/Q9P/el1aZb
AgMBAAGgQDA+BgkqhkiG9w0BCQ4xMTAvMAwGA1UdEwEB/wQCMAAwFgYDVR0lAQH/
BAwwCgYIKwYBBQUHAwEwBwYDVR0RBAAwCwYJKoZIhvcNAQELA4IBAQAUIhgLf6LH
WwhncU+yrNy6yHPHjFbipehUaS/Wa6FBbANpLyEdslqNwFD2FyXofSQVCB0L6VnH
NNXHcfZqWOT8+Xd0yNmfr/w+vg+s1yY4S0VAG6mxGsqwdIJqgXm2jaw0rMpzRs/k
wfOpJ+gyRTrQz9VJrn4xC4Uv6zTGNj56qZehDievW74SGISbGzj7AGmCxpp5/qZD
LN4Ls3wZ7I4TfuYZDh7qWuFwEAEEv40JPF2qO3VitvyAYrKg3bnUlFzQrOsfs4Ot
xzihLwkFEobih1bOEhzTnSv+lMckfw0DQ5Eb8mtFTC+/KOqEMMU5Hq1fm3B9Bkgs
FmqQZk4QlerY
-----END CERTIFICATE REQUEST-----

回答1:


I figured it out, the SignCSR function in phpseclib doesnt respsect the setDomain function, or any content in the $this->domains array however it IS possible to manually encode and force set the subject alt names yourself:

$OPTIONS["altnames"] = array(
                                "bob.com",
                                "10.123.123.123",
                                "*.bob.com",
                                "asdf.fdsa",
                            );

        $ALTNAMES = $this->altnames($OPTIONS["altnames"]);
        if ( count($ALTNAMES) )
        { 
            $CSR->setExtension("id-ce-subjectAltName"   , $ALTNAMES );
        }

protected function altnames($ALTNAMES)
{ 
    //Sort names and IPs into two different arrays
    $DOMAINS = array(); $IPS = array();
    foreach ($ALTNAMES as $ALTNAME)
    { 
        if ( filter_var($ALTNAME, FILTER_VALIDATE_IP) )
        {
            array_push($IPS,$ALTNAME);
        }else{
            array_push($DOMAINS,$ALTNAME);
        }
    }
    // Create our altname array for the subjectAltName parser
    $RETURN = array();
    foreach ($DOMAINS as $DOMAIN)   { array_push($RETURN, array("dNSName"   => $DOMAIN  ) ); }
    foreach ($IPS as $IP)           { array_push($RETURN, array("iPAddress" => $IP      ) ); }
    return $RETURN;
}

Now, the one caveat I found (that is a feature issue for phpseclib to consider) is the lack of IPv6 support. If you provide a valid IPv6 address in the altnames array (2620:153::1234) it will translate to IP Address: 0.0.0.0 because of the use of IP2LONG and LONG2IP embedded in the X509 code.

While not a showstopper for my use case, it is a minor annoyance and I have experimented with using a custom IP2BIN and BIN2IP function replacing the encode and decode logic for an IP in the library and not met with much success.

Example CSR generated with my code above:

-----BEGIN CERTIFICATE REQUEST-----
MIIC/DCCAeYCAQAwVDELMAkGA1UEBgwCU1MxEDAOBgNVBAgMB09ic2N1cmUxDzAN
BgNVBAcMBlNlY3VyZTEQMA4GA1UECgwHYm9iY29ycDEQMA4GA1UEAwwHYm9iLmNv
bTCCASAwCwYJKoZIhvcNAQEBA4IBDwAwggEKAoIBAQDOGLdjEr2IyW27e08hmRN+
Bcu4uOWAWEvxvY3+5pHdBQQEXhww5YQuEvpmgbKtav0j7aqFPDYNSXTv+aQNe9fq
cP3nZmKAWU+qbQMjWxwV9mEJOlWI214v7C8lLbMvBlny141J7KTvv1TGGLCBH/V2
EnQSdJzGDwXmJ2k0iChlQ7zl1TlonamYX9gefzp3N/DDp6kNhuPSX9zRorYIp5CC
WEIRmDdegwxHACrNu0K4xwuPjTRJf0oUkRsfBuDxqvBalQ0bzd/23fiQ51MEVla3
fUWL/+b2SKOlvgfu0XbfZ+Qx21DeyRQpqIWnv0gR4AM8qltxUgRjZloUfK5IQ8rp
AgMBAAGgZzBlBgkqhkiG9w0BCQ4xWDBWMAwGA1UdEwEB/wQCMAAwFgYDVR0lAQH/
BAwwCgYIKwYBBQUHAwEwLgYDVR0RBCcwJYIHYm9iLmNvbYIJKi5ib2IuY29tgglh
c2RmLmZkc2GHBAp7e3swCwYJKoZIhvcNAQELA4IBAQAf1Vlt09ZBhVpxlmi+n93u
Gm8fNrd5afeIzWj0h4dFGJSOg5T8SkfL5txk2C9tQEdayQWB1kllx5rIqXAPe6gz
kbcjJn8l2IB3khIoKYmylmtV8Yo0Fl/Xba1oLCAsixbK+UxiSLgXqMryz9DBy40s
5oYXpy5JOnqL7BRC7b+Lk/chw7CcncPZI4rei6HM8WATymTySdrPoQegvBj0VIar
qHBZrMV9lsjTREJ9hvA/FycA/PNlP9y8N+eTF9SBrnTi8ix+v+Iirc43xeD2EVLG
6uqXecClji6OEOKcdDsH0D0HD1PMFmKB0FWvq71dt7eVIHkTPwTLFG2XAjfn6Fb8
-----END CERTIFICATE REQUEST-----

Decodes to:

Certificate Request:
Data:
    Version: 0 (0x0)
    Subject: C=SS, ST=Obscure, L=Secure, O=bobcorp, CN=bob.com
    Subject Public Key Info:
        Public Key Algorithm: rsaEncryption
            Public-Key: (2048 bit)
            Modulus:
                00:ce:18:b7:63:12:bd:88:c9:6d:bb:7b:4f:21:99:
                13:7e:05:cb:b8:b8:e5:80:58:4b:f1:bd:8d:fe:e6:
                91:dd:05:04:04:5e:1c:30:e5:84:2e:12:fa:66:81:
                b2:ad:6a:fd:23:ed:aa:85:3c:36:0d:49:74:ef:f9:
                a4:0d:7b:d7:ea:70:fd:e7:66:62:80:59:4f:aa:6d:
                03:23:5b:1c:15:f6:61:09:3a:55:88:db:5e:2f:ec:
                2f:25:2d:b3:2f:06:59:f2:d7:8d:49:ec:a4:ef:bf:
                54:c6:18:b0:81:1f:f5:76:12:74:12:74:9c:c6:0f:
                05:e6:27:69:34:88:28:65:43:bc:e5:d5:39:68:9d:
                a9:98:5f:d8:1e:7f:3a:77:37:f0:c3:a7:a9:0d:86:
                e3:d2:5f:dc:d1:a2:b6:08:a7:90:82:58:42:11:98:
                37:5e:83:0c:47:00:2a:cd:bb:42:b8:c7:0b:8f:8d:
                34:49:7f:4a:14:91:1b:1f:06:e0:f1:aa:f0:5a:95:
                0d:1b:cd:df:f6:dd:f8:90:e7:53:04:56:56:b7:7d:
                45:8b:ff:e6:f6:48:a3:a5:be:07:ee:d1:76:df:67:
                e4:31:db:50:de:c9:14:29:a8:85:a7:bf:48:11:e0:
                03:3c:aa:5b:71:52:04:63:66:5a:14:7c:ae:48:43:
                ca:e9
            Exponent: 65537 (0x10001)
    Attributes:
    Requested Extensions:
        X509v3 Basic Constraints: critical
            CA:FALSE
        X509v3 Extended Key Usage: critical
            TLS Web Server Authentication
        X509v3 Subject Alternative Name: 
            DNS:bob.com, DNS:*.bob.com, DNS:asdf.fdsa, IP Address:10.123.123.123
Signature Algorithm: sha256WithRSAEncryption
     1f:d5:59:6d:d3:d6:41:85:5a:71:96:68:be:9f:dd:ee:1a:6f:
     1f:36:b7:79:69:f7:88:cd:68:f4:87:87:45:18:94:8e:83:94:
     fc:4a:47:cb:e6:dc:64:d8:2f:6d:40:47:5a:c9:05:81:d6:49:
     65:c7:9a:c8:a9:70:0f:7b:a8:33:91:b7:23:26:7f:25:d8:80:
     77:92:12:28:29:89:b2:96:6b:55:f1:8a:34:16:5f:d7:6d:ad:
     68:2c:20:2c:8b:16:ca:f9:4c:62:48:b8:17:a8:ca:f2:cf:d0:
     c1:cb:8d:2c:e6:86:17:a7:2e:49:3a:7a:8b:ec:14:42:ed:bf:
     8b:93:f7:21:c3:b0:9c:9d:c3:d9:23:8a:de:8b:a1:cc:f1:60:
     13:ca:64:f2:49:da:cf:a1:07:a0:bc:18:f4:54:86:ab:a8:70:
     59:ac:c5:7d:96:c8:d3:44:42:7d:86:f0:3f:17:27:00:fc:f3:
     65:3f:dc:bc:37:e7:93:17:d4:81:ae:74:e2:f2:2c:7e:bf:e2:
     22:ad:ce:37:c5:e0:f6:11:52:c6:ea:ea:97:79:c0:a5:8e:2e:
     8e:10:e2:9c:74:3b:07:d0:3d:07:0f:53:cc:16:62:81:d0:55:
     af:ab:bd:5d:b7:b7:95:20:79:13:3f:04:cb:14:6d:97:02:37:
     e7:e8:56:fc



回答2:


OK, I think i found a "feature" in phpseclib's X509 signing function:

function sign($issuer, $subject, $signatureAlgorithm = 'sha1WithRSAEncryption')

If you sign the CSR, the code in the sign function claims to copy the X509 v3 extended attributes, however by the time it executes this line:

$csrexts = $subject->getAttribute('pkcs-9-at-extensionRequest', 0);

The result is always blank (an empty array), the attributes in $THIS (new) cert were overwritten somewhere in the previous ~30 lines of code.

I moved the csrexts line to the top of the Sign function, and attributes are now being copied correctly into generated certificates with X509 extended attributes in the CSR (that had to be manually calculated and set by my previous answer)

I will use sourceforge to try and post a bug request to save others the struggle hopefully.




回答3:


OK, so I've got this to work using an example from the PHPSecLib documentation and playing around with it for a good few hours. Perhaps this is something added post February, but in any case, here is how to generate a CSR with an Subject Alternative Name:

    $privKey = new Crypt_RSA();
    extract($privKey->createKey());
    $privKey->loadKey($privatekey);

    $x509 = new File_X509();
    $x509->setPrivateKey($privKey);
    $x509->setDNProp('id-at-organizationName', 'Company');
    $x509->setDNProp('id-at-organizationalUnitName', 'CompanyDepartment');
    $x509->setDNProp('id-at-commonName', 'myurl.com');
    $x509->setDNProp('id-at-localityName', $this->application);

    $x509->loadCSR($x509->saveCSR($x509->signCSR()));

    // Set extension request.
    $x509->setExtension("id-ce-subjectAltName", [
        ['dNSName' => 'myalternativename.com', 'iPAddress' => 127.0.0.1]
    ]);

    echo $x509->saveCSR($x509->signCSR());

That should print out the CSR with myalternativename.com as the SAN.



来源:https://stackoverflow.com/questions/28594577/phpseclib-x509-setdomain-on-csr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!