*/ private static function config() { $config = require ACHLAAI_DIR . 'config/trust-anchors.php'; return is_array( $config ) ? $config : array(); } /** @return array */ public static function ownership() { $config = self::config(); return self::valid_config( $config ) ? $config['ownership'] : array(); } /** @return array */ public static function release() { $config = self::config(); return self::valid_config( $config ) ? $config['release'] : array(); } /** @return bool */ private static function valid_config( $config ) { if ( ! isset( $config['package_mode'] ) || 'production' !== $config['package_mode'] || ! isset( $config['ownership'], $config['release'], $config['rotation'] ) || ! is_array( $config['ownership'] ) || ! is_array( $config['release'] ) || ! is_array( $config['rotation'] ) || ! isset( $config['trust_policy'] ) || ! self::valid_policy_metadata( $config['trust_policy'] ) ) { return false; } foreach ( array( 'ownership', 'release' ) as $family ) { if ( empty( $config['rotation'][ $family ]['current'] ) || empty( $config['rotation'][ $family ]['next'] ) ) { return false; } $current = $config['rotation'][ $family ]['current']; $next = $config['rotation'][ $family ]['next']; if ( ! is_array( $current ) || ! is_array( $next ) || array_intersect( $current, $next ) ) { return false; } $expected = array_values( array_unique( array_merge( $current, $next ) ) ); $actual = array_keys( $config[ $family ] ); sort( $expected, SORT_STRING ); sort( $actual, SORT_STRING ); if ( $expected !== $actual ) { return false; } foreach ( $config[ $family ] as $kid => $key ) { if ( ! is_string( $kid ) || 1 !== preg_match( '/^[A-Za-z0-9._-]{1,64}$/', $kid ) || ! self::valid_key( $family, $key ) ) { return false; } } } return true; } /** @return bool */ private static function valid_policy_metadata( $policy ) { if ( ! is_array( $policy ) ) { return false; } $keys = array_keys( $policy ); sort( $keys, SORT_STRING ); if ( array( 'expires_at', 'policy_id', 'protocol', 'signer_kid' ) !== $keys ) { return false; } return 'achla-cms-trust-policy-v1' === $policy['protocol'] && is_string( $policy['policy_id'] ) && 1 === preg_match( '/^[A-Za-z0-9][A-Za-z0-9._-]{2,127}$/', $policy['policy_id'] ) && null !== Protocol::parse_canonical_time( $policy['expires_at'] ) && is_string( $policy['signer_kid'] ) && 1 === preg_match( '/^[A-Za-z0-9._-]{1,64}$/', $policy['signer_kid'] ); } /** @return bool */ private static function valid_key( $family, $key ) { if ( ! is_string( $key ) ) { return false; } if ( 'ownership' === $family ) { $decoded = Crypto::base64url_decode( $key ); return false !== $decoded && 32 === strlen( $decoded ) && Crypto::base64url_encode( $decoded ) === $key; } if ( ! function_exists( 'openssl_pkey_get_public' ) || ! defined( 'OPENSSL_KEYTYPE_EC' ) ) { return false; } $resource = openssl_pkey_get_public( $key ); if ( false === $resource ) { return false; } $details = openssl_pkey_get_details( $resource ); return is_array( $details ) && OPENSSL_KEYTYPE_EC === $details['type'] && isset( $details['ec']['curve_name'] ) && 'prime256v1' === $details['ec']['curve_name']; } }