Skip to content

Core\Crypto\PublicKey::read

Reads a public key that somebody else sent you, and gives you the key as an object.

A public key arrives as octets: the plain key material (Raw), a SubjectPublicKeyInfo document (Spki), or a small JSON document (Jwk). These are the three encodings a browser's WebCrypto writes, so you name the one the octets are in. You also name the kind of key, because the octets do not always say it. An RSA key looks the same whether it is meant for RsaPkcs1 or for RsaPss.

The key is checked here, once. A point that is not on the curve, an RSA key that is too small or too large, and a document that does not parse are all refused at the read. Every later member is then handed a key that has been checked.

Good to know: the key you get back checks signatures with Core\Crypto::verify and agrees a shared secret with Core\Crypto::agree.

Parameters

  • $encodedbytesThe bytes of the key, in $format.
  • $kindCore\Crypto\KeyKindThe kind of the key. You must give it, because the bytes do not always say it. An RSA SubjectPublicKeyInfo says rsaEncryption for both RS256 and PS256 keys. Only your program knows which one the key is for.
  • $formatCore\Crypto\PublicKey::FormatThe WebCrypto export format that $encoded is in.

Return value

Returns Core\Crypto\PublicKey. The key, ready to verify a signature or to agree. Its kind() method returns the case it was read as.

Errors

  • LogicErrorThe kind has no such format, for example Raw with an RSA kind. It is also thrown when the JWK has d, because that is a private key given as a public key.
  • RuntimeError$encoded is not a public key of that kind in that format. For example, the point is not on the curve, or a coordinate has the wrong size. Or the modulus is outside the allowed range, the DER does not parse, or the JWK names another key type. All these cases have the same message. The key came from the sender, and a reason would tell the sender what was wrong.

Examples

Check a signature with a key somebody sent

<?nvs
// Shows how to check a signature with a public key somebody sent you. The key
// is an Ed25519 key, sent as the 32 octets that `Raw` stands for.
bytes $sent = Core\Encoding::fromHex('fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac');
// `read` takes the octets, the kind of key they hold, and the encoding they
// arrived in. Octets that are not a key of that kind are refused here.
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
$sent,
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Raw,
);
bytes $message = 'order 1042 shipped' as bytes;
bytes $signature = Core\Encoding::fromHex('4f616bc7d091dd65ef7dd0ca37d36665e6196302848f7f5e3e59c62ed068c5c687a46dbf89b4f82cca4781cb9053f0aa8e3bbfcfd6a807bf18eeda2bf9707504');
// `verify` returns nothing. It throws an error when the signature does not
// belong to this message and this key.
Core\Crypto::verify($message, $signature, $key);
echo 'the signature belongs to this key', "\n";
Output
the signature belongs to this key

A key a browser sent as a jwk

<?nvs
// Shows how to read a key that a browser sent as a JWK. It is then written out
// again in the shorter `Spki` form, which is the one to store.
string $sentByTheBrowser = "{\"key_ops\":[\"verify\"],\"ext\":true,\"alg\":\"Ed25519\",\"crv\":\"Ed25519\",\"x\":\"_M6QCKM1ZSSFYSlafOHMywQLYSm7qP8kdElTu8ASYaw\",\"kty\":\"OKP\"}";
// A JWK is a small JSON document, and `read` takes it as UTF-8 octets. Its
// members are checked against the kind you name.
Core\Crypto\PublicKey $key = Core\Crypto\PublicKey::read(
$sentByTheBrowser as bytes,
Core\Crypto\KeyKind::Ed25519,
Core\Crypto\PublicKey::Format::Jwk,
);
// One key has one spelling in each encoding. The same key sent as `Raw` or as
// `Spki` writes the same line here. This prints the key as hex text.
echo Core\Encoding::toHex($key->write(Core\Crypto\PublicKey::Format::Spki)), "\n";
Output
302a300506032b6570032100fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac

A shared secret with the client that just connected

<?nvs
// Shows a server that sets up an encrypted channel with a client. The client
// sends its public key, the server reads it, and both sides reach the same
// shared secret without sending it.
// The server's own key pair, read from its configuration.
Core\Crypto\KeyPair $server = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex('302e020100300506032b656e042204200172af48574da59d306df1b27cc27457304669fb73e0630b9f84eac038695c2c'),
Core\Crypto\KeyKind::X25519,
);
// What the client sent. A public key off the network is octets until you read
// it, and `read` is where it is checked.
Core\Crypto\PublicKey $client = Core\Crypto\PublicKey::read(
Core\Encoding::fromHex('9c7be3a9c961ce008340674cd3d468fc5be27c98b65a35262ca2d05573d0585e'),
Core\Crypto\KeyKind::X25519,
Core\Crypto\PublicKey::Format::Raw,
);
// `agree` gives the shared secret, and `expandKey` turns it into a key for this
// one session. The text is part of the key, so two sessions get two keys.
secret bytes $key = Core\Crypto::expandKey(
Core\Crypto::agree($server, $client),
'' as bytes,
'chat session',
);
bytes $sealed = Core\Crypto::seal(
'your order is on its way' as bytes,
$key,
Core\Crypto::Cipher::XChaCha20Poly1305,
);
// The client has the server's public key and its own pair, so it reaches the
// same secret and opens the message. This prints `your order is on its way`.
Core\Crypto\KeyPair $clientPair = Core\Crypto\KeyPair::read(
Core\Encoding::fromHex('302e020100300506032b656e042204201135b8655ccc4ea0b976d3a303d6b64b90e9f0b643f65e52288c25bdcebe4fa4'),
Core\Crypto\KeyKind::X25519,
);
secret bytes $clientKey = Core\Crypto::expandKey(
Core\Crypto::agree($clientPair, $server->publicKey()),
'' as bytes,
'chat session',
);
echo Core\Crypto::open($sealed, $clientKey, Core\Crypto::Cipher::XChaCha20Poly1305) as string, "\n";
Output
your order is on its way