Core\Http\Identity::read
Reads your client certificate and its private key, and checks that they belong together.
Some servers, often banks and payment services, ask the client to prove who it is when the connection starts. For this you get two files: a certificate chain in PEM format, and a private key. Read the key with Core\Crypto\KeyPair::read first, then pass both here. The result is a Core\Http\Identity. You give it to a request as the identity option, and it is sent only when the server asks for it.
The first certificate in the chain must be yours, and the key must match it. The certificates after it are only passed on to the server. If the key belongs to a different certificate, or the text is not a certificate at all, this throws a LogicError. So you find a wrong file when the program starts, and not later when a request fails.
Good to know: an X25519 key cannot sign anything, so it is not allowed here. Use a P256, Ed25519 or RSA key.
Parameters
$chainPembytesThe chain as PEM. The leaf certificate comes first, then the intermediate certificates the server needs to check it. Other HTTP clients use the same order.$keyCore\Crypto\KeyPairThe private key of the leaf certificate. The TLS handshake is signed with this key. A key that can only do key agreement, such asX25519, throws an error.
Return value
Returns Core\Http\Identity. The identity. Pass it as a request's identity option. It is sent only when the server asks for it. Two different identities never share a pooled connection.
Errors
LogicError$chainPemis not a PEM certificate chain, or it contains no certificate. Or$keycannot sign a TLS handshake. Or the leaf certificate has a different public key from$key. A client identity is part of the program's own deployment, so this error always means a mistake in the deployed files.
Examples
A certificate and its key
<?nvs// Shows how to read a client certificate and its private key into one identity.
// The certificate chain, as the PEM text you were given with it.string $chain = <<<'PEM'-----BEGIN CERTIFICATE-----MIIBTDCB/6ADAgECAhQ17j2Xe6rvGOZqgU0W011sTuW+IzAFBgMrZXAwGzEZMBcGA1UEAwwQZWQubm92aXMuZXhhbXBsZTAgFw0yNjA5MTIyMzQzMjJaGA8yMTI2MDgxOTIzNDMyMlowGzEZMBcGA1UEAwwQZWQubm92aXMuZXhhbXBsZTAqMAUGAytlcAMhACT/4hW7zbJtISV79UQYEtXJzOQ18C0qB16Tags7Vpcao1MwUTAdBgNVHQ4EFgQU5hV1V6iqg9AL4FmXJoVbzAHpg2wwHwYDVR0jBBgwFoAU5hV1V6iqg9AL4FmXJoVbzAHpg2wwDwYDVR0TAQH/BAUwAwEB/zAFBgMrZXADQQBSkCaW0oa7UWSQSqLBhxgDlRHq1kQ2BhwRhBHgl6n/0GK47grLHlIkdnLjiBygbjeYlE4JdgRVr1rWrpcZZLQF-----END CERTIFICATE-----PEM;
// The private key that goes with the certificate. It is an Ed25519 key.secret bytes $keyFile = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIM9sUcYAGspJOAqm3qMBkB//vV3Hnpj7+pgIXp43jW+t\n-----END PRIVATE KEY-----\n" as bytes;
// `Core\Crypto\KeyPair::read` reads the key. `Core\Http\Identity::read` then checks// that the key matches the first certificate in the chain.Core\Crypto\KeyPair $key = Core\Crypto\KeyPair::read($keyFile, Core\Crypto\KeyKind::Ed25519);Core\Http\Identity $identity = Core\Http\Identity::read($chain as bytes, $key);
// The key matches, so no error is thrown and this line prints.echo 'the certificate and the key belong together', "\n";the certificate and the key belong togetherFiles that do not match
<?nvs// Shows the errors you get when the certificate and the key do not belong together.
string $chain = <<<'PEM'-----BEGIN CERTIFICATE-----MIIBTDCB/6ADAgECAhQ17j2Xe6rvGOZqgU0W011sTuW+IzAFBgMrZXAwGzEZMBcGA1UEAwwQZWQubm92aXMuZXhhbXBsZTAgFw0yNjA5MTIyMzQzMjJaGA8yMTI2MDgxOTIzNDMyMlowGzEZMBcGA1UEAwwQZWQubm92aXMuZXhhbXBsZTAqMAUGAytlcAMhACT/4hW7zbJtISV79UQYEtXJzOQ18C0qB16Tags7Vpcao1MwUTAdBgNVHQ4EFgQU5hV1V6iqg9AL4FmXJoVbzAHpg2wwHwYDVR0jBBgwFoAU5hV1V6iqg9AL4FmXJoVbzAHpg2wwDwYDVR0TAQH/BAUwAwEB/zAFBgMrZXADQQBSkCaW0oa7UWSQSqLBhxgDlRHq1kQ2BhwRhBHgl6n/0GK47grLHlIkdnLjiBygbjeYlE4JdgRVr1rWrpcZZLQF-----END CERTIFICATE-----PEM;
// A new Ed25519 key is a valid key, but the certificate was made for a different key.// So `read` throws a `LogicError`.Core\Crypto\KeyPair $otherKey = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);try { Core\Http\Identity::read($chain as bytes, $otherKey);} catch (LogicError $wrongKey) { echo $wrongKey->message, "\n";}
// An X25519 key cannot sign, so it cannot prove who the client is.// `read` throws a `LogicError` for this key too.Core\Crypto\KeyPair $agreeOnly = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::X25519);try { Core\Http\Identity::read($chain as bytes, $agreeOnly);} catch (LogicError $cannotSign) { echo 'an X25519 key is not allowed', "\n";}
// Text without a certificate in it is not a chain. This is the error you see// when the wrong file was used.try { Core\Http\Identity::read('hello' as bytes, $otherKey);} catch (LogicError $notAChain) { echo $notAChain->message, "\n";}Core\Http\Identity::read(): the chain's leaf certificate carries a different public key than this key pair.an X25519 key is not allowedCore\Http\Identity::read(): the chain holds no certificate.Checking every partner at start up
<?nvs// Shows a service that checks the client certificate for each partner when it// starts, so a wrong file is reported before the first request.
string $bankChain = <<<'PEM'-----BEGIN CERTIFICATE-----MIIBTDCB/6ADAgECAhQ17j2Xe6rvGOZqgU0W011sTuW+IzAFBgMrZXAwGzEZMBcGA1UEAwwQZWQubm92aXMuZXhhbXBsZTAgFw0yNjA5MTIyMzQzMjJaGA8yMTI2MDgxOTIzNDMyMlowGzEZMBcGA1UEAwwQZWQubm92aXMuZXhhbXBsZTAqMAUGAytlcAMhACT/4hW7zbJtISV79UQYEtXJzOQ18C0qB16Tags7Vpcao1MwUTAdBgNVHQ4EFgQU5hV1V6iqg9AL4FmXJoVbzAHpg2wwHwYDVR0jBBgwFoAU5hV1V6iqg9AL4FmXJoVbzAHpg2wwDwYDVR0TAQH/BAUwAwEB/zAFBgMrZXADQQBSkCaW0oa7UWSQSqLBhxgDlRHq1kQ2BhwRhBHgl6n/0GK47grLHlIkdnLjiBygbjeYlE4JdgRVr1rWrpcZZLQF-----END CERTIFICATE-----PEM;string $bankKey = "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIM9sUcYAGspJOAqm3qMBkB//vV3Hnpj7+pgIXp43jW+t\n-----END PRIVATE KEY-----\n";
string $insurerChain = <<<'PEM'-----BEGIN CERTIFICATE-----MIIBlTCCATugAwIBAgIUOutG9T2sNn5Gk1F/4t/VZaL4xIcwCgYIKoZIzj0EAwIwHzEdMBsGA1UEAwwUY2xpZW50Lm5vdmlzLmV4YW1wbGUwIBcNMjYwOTEyMjMzOTM1WhgPMjEyNjA4MTkyMzM5MzVaMB8xHTAbBgNVBAMMFGNsaWVudC5ub3Zpcy5leGFtcGxlMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEGZ6FIKLkAP9KaHYqOSfnWvD/zFMph12te1fB2axeNOcWs8WecxZwltj1G2dwhJcf6vt8c5CGfghFEfOFENRms6NTMFEwHQYDVR0OBBYEFGIhmVGjbjICXl8yK/ztiD1oh2tzMB8GA1UdIwQYMBaAFGIhmVGjbjICXl8yK/ztiD1oh2tzMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIgR3Jf92T6sR7qKE/BQwSx40DqTSxekum8n3xTfErf9i4CIQChybRuk12tFXPwqQ0biQfTzl+cQ+Cc/2cw72NUT8a7Jg==-----END CERTIFICATE-----PEM;
// The certificate for each partner, and the key file installed beside it.array<string> $chains = ['bank' => $bankChain, 'insurer' => $insurerChain];// A mistake in the setup: the insurer's certificate was installed with the bank's key.array<string> $keyFiles = ['bank' => $bankKey, 'insurer' => $bankKey];
array<Core\Http\Identity> $identities = [];uint $problems = 0;foreach ($chains as string $partner => string $chain) { try { secret bytes $keyFile = $keyFiles[$partner] as bytes; Core\Crypto\KeyPair $key = Core\Crypto\KeyPair::read($keyFile, Core\Crypto\KeyKind::Ed25519); // Each identity is kept. Later requests to this partner pass it as the `identity` option. $identities[$partner] = Core\Http\Identity::read($chain as bytes, $key); echo $partner, ': ready', "\n"; } catch (LogicError $error) { echo $partner, ': not ready: ', $error->message, "\n"; $problems = $problems + 1; }}
// The service starts only when every partner is ready. Here one is not, so this prints// `1 partner(s) not ready, the service does not start`.if ($problems > 0) { echo $problems, ' partner(s) not ready, the service does not start', "\n";}bank: readyinsurer: not ready: Core\Http\Identity::read(): the chain's leaf certificate carries a different public key than this key pair.1 partner(s) not ready, the service does not start