*/ final public const COSE_ALGORITHM_MAP = [ self::COSE_ALGORITHM_ES256 => OPENSSL_ALGO_SHA256, self::COSE_ALGORITHM_ES256K => OPENSSL_ALGO_SHA256, self::COSE_ALGORITHM_ES384 => OPENSSL_ALGO_SHA384, self::COSE_ALGORITHM_ES512 => OPENSSL_ALGO_SHA512, self::COSE_ALGORITHM_ESP256 => OPENSSL_ALGO_SHA256, self::COSE_ALGORITHM_ESP384 => OPENSSL_ALGO_SHA384, self::COSE_ALGORITHM_ESP512 => OPENSSL_ALGO_SHA512, self::COSE_ALGORITHM_ESB256 => OPENSSL_ALGO_SHA256, self::COSE_ALGORITHM_ESB320 => OPENSSL_ALGO_SHA384, self::COSE_ALGORITHM_ESB384 => OPENSSL_ALGO_SHA384, self::COSE_ALGORITHM_ESB512 => OPENSSL_ALGO_SHA512, self::COSE_ALGORITHM_RS256 => OPENSSL_ALGO_SHA256, self::COSE_ALGORITHM_RS384 => OPENSSL_ALGO_SHA384, self::COSE_ALGORITHM_RS512 => OPENSSL_ALGO_SHA512, self::COSE_ALGORITHM_RS1 => OPENSSL_ALGO_SHA1, ]; /** * The name of the digest each algorithm identifier uses, as hash() spells it. * * The EdDSA identifiers (-8, -19, -53) are absent: they name one-shot signature schemes with no separately * applicable digest. * * @internal this constant bypasses getHashAlgorithmFor() and the acknowledgement it requires for RS1; it will * become private in the next major version * @var array */ final public const COSE_HASH_MAP = [ self::COSE_ALGORITHM_ES256K => 'sha256', self::COSE_ALGORITHM_ES256 => 'sha256', self::COSE_ALGORITHM_ES384 => 'sha384', self::COSE_ALGORITHM_ES512 => 'sha512', self::COSE_ALGORITHM_ESP256 => 'sha256', self::COSE_ALGORITHM_ESP384 => 'sha384', self::COSE_ALGORITHM_ESP512 => 'sha512', self::COSE_ALGORITHM_ESB256 => 'sha256', self::COSE_ALGORITHM_ESB320 => 'sha384', self::COSE_ALGORITHM_ESB384 => 'sha384', self::COSE_ALGORITHM_ESB512 => 'sha512', self::COSE_ALGORITHM_RS256 => 'sha256', self::COSE_ALGORITHM_RS384 => 'sha384', self::COSE_ALGORITHM_RS512 => 'sha512', self::COSE_ALGORITHM_PS256 => 'sha256', self::COSE_ALGORITHM_PS384 => 'sha384', self::COSE_ALGORITHM_PS512 => 'sha512', self::COSE_ALGORITHM_RS1 => 'sha1', ]; /** * The digest to hand to openssl_sign() / openssl_verify() for an ECDSA or RSASSA-PKCS1-v1_5 identifier. * * @see self::COSE_ALGORITHM_MAP for the identifiers this covers, and for those it deliberately does not * * @param bool $acknowledgeInsecureAlgorithm see Cose\Algorithm\Signature\RSA\RS1: SHA-1 is only handed out * against an explicit acknowledgement, as creating the RS1 algorithm itself is * * @throws InvalidArgumentException when the identifier is not one this map describes */ public static function getOpensslAlgorithmFor( int $algorithmIdentifier, bool $acknowledgeInsecureAlgorithm = false ): int { if (! array_key_exists($algorithmIdentifier, self::COSE_ALGORITHM_MAP)) { throw new InvalidArgumentException('The specified algorithm identifier is not supported'); } self::checkInsecureAlgorithm($algorithmIdentifier, $acknowledgeInsecureAlgorithm); return self::COSE_ALGORITHM_MAP[$algorithmIdentifier]; } /** * The name of the digest an identifier uses, as hash() spells it. * * @see self::COSE_HASH_MAP for the identifiers this covers, and for those it deliberately does not * * @param bool $acknowledgeInsecureAlgorithm see Cose\Algorithm\Signature\RSA\RS1: SHA-1 is only handed out * against an explicit acknowledgement, as creating the RS1 algorithm itself is * * @throws InvalidArgumentException when the identifier is not one this map describes */ public static function getHashAlgorithmFor( int $algorithmIdentifier, bool $acknowledgeInsecureAlgorithm = false ): string { if (! array_key_exists($algorithmIdentifier, self::COSE_HASH_MAP)) { throw new InvalidArgumentException('The specified algorithm identifier is not supported'); } self::checkInsecureAlgorithm($algorithmIdentifier, $acknowledgeInsecureAlgorithm); return self::COSE_HASH_MAP[$algorithmIdentifier]; } /** * The RS1 class refuses to be created without an acknowledgement, but the two accessors above expose the very * same primitive - SHA-1 with PKCS #1 v1.5 padding - without any object ever being built, and their result is * handed straight to openssl_verify(). The policy therefore has to hold here too, otherwise the operator's * decision not to register RS1 has no effect on the code paths that go through these maps. * * Unlike RS1::__construct(), this fires per verification rather than once at configuration time, which is * precisely what makes an unacknowledged SHA-1 verification visible in the logs. */ private static function checkInsecureAlgorithm(int $algorithmIdentifier, bool $acknowledgeInsecureAlgorithm): void { if ($algorithmIdentifier === self::COSE_ALGORITHM_RS1 && ! $acknowledgeInsecureAlgorithm) { // As of v5.0.0, this will throw an InvalidArgumentException instead of warning. trigger_error(RS1::INSECURE_ALGORITHM_MESSAGE, E_USER_WARNING); } } }