[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/secrets/ -> class-wp-secrets-config-key-provider.php (source)

   1  <?php
   2  /**
   3   * Secrets API: WP_Secrets_Config_Key_Provider class
   4   *
   5   * @package WordPress
   6   * @subpackage Secrets
   7   * @since 7.2.0
   8   */
   9  
  10  /**
  11   * The default keyring.
  12   *
  13   * Derives a site key from wp-config.php and uses it to wrap and unwrap the root key
  14   * with authenticated encryption.
  15   *
  16   * Site key derivation:
  17   *
  18   * 1. WP_SECRETS_KEY base64-decodes (strictly) to exactly 32 bytes: those decoded
  19   *    bytes are used raw. This is the recommended form, such as the output of
  20   *    base64_encode( random_bytes( 32 ) ).
  21   * 2. WP_SECRETS_KEY is any other string: the literal constant string is hashed
  22   *    with BLAKE2b to 32 bytes. This is the legacy interpretation – sites arriving
  23   *    from a prior plugin with a constant of arbitrary shape are not locked out of
  24   *    their own credentials by a hard failure here.
  25   *
  26   * There is no fallback. Without a usable WP_SECRETS_KEY, every Secrets API call
  27   * that needs the key returns WP_Error. Deriving a key from the authentication keys
  28   * and salts would make every stored secret unreadable whenever the salts are
  29   * rotated, which is routine advice for them.
  30   *
  31   * The wp-config-sample.php placeholder is not usable, and neither is a value that
  32   * one of the authentication keys and salts also has. A sample that was never
  33   * filled in gives every constant the same phrase, translated in localized samples,
  34   * so the second check rejects the placeholder in any language.
  35   *
  36   * WP_SECRETS_KEY_PREVIOUS follows the same rules and exists only so a site-key
  37   * rotation can unwrap under the old key before wrapping under the new one.
  38   *
  39   * @since 7.2.0
  40   */
  41  final class WP_Secrets_Config_Key_Provider extends WP_Secrets_Keyring {
  42  
  43      /**
  44       * AAD binding the root key's wrapped form to this purpose.
  45       *
  46       * Prevents the wrapped root key from being replayed as some other wrapped value.
  47       *
  48       * @since 7.2.0
  49       * @var string
  50       */
  51      const AAD = 'wp-secrets-root-key-v1';
  52  
  53      /**
  54       * The exact text wp-config-sample.php ships for every key and salt constant.
  55       *
  56       * A site running with this literal value never ran the secret-key generator.
  57       * Treating it as usable would derive a key that any attacker can also derive.
  58       *
  59       * @since 7.2.0
  60       * @var string
  61       */
  62      const KNOWN_PLACEHOLDER = 'put your unique phrase here';
  63  
  64      /**
  65       * WP_SECRETS_KEY is not defined.
  66       *
  67       * @since 7.2.0
  68       * @var string
  69       */
  70      const KEY_STATUS_UNDEFINED = 'undefined';
  71  
  72      /**
  73       * WP_SECRETS_KEY is defined, but empty, the placeholder, or equal to a key or salt.
  74       *
  75       * @since 7.2.0
  76       * @var string
  77       */
  78      const KEY_STATUS_UNUSABLE = 'unusable';
  79  
  80      /**
  81       * WP_SECRETS_KEY is the base64 encoding of 32 bytes, the recommended form.
  82       *
  83       * @since 7.2.0
  84       * @var string
  85       */
  86      const KEY_STATUS_BASE64 = 'base64';
  87  
  88      /**
  89       * WP_SECRETS_KEY is usable but in another form, so it is hashed to 32 bytes.
  90       *
  91       * @since 7.2.0
  92       * @var string
  93       */
  94      const KEY_STATUS_LEGACY = 'legacy';
  95  
  96      /**
  97       * The authentication keys and salts that wp-config-sample.php defines.
  98       *
  99       * @since 7.2.0
 100       * @var string[]
 101       */
 102      const AUTH_CONSTANTS = array(
 103          'AUTH_KEY',
 104          'SECURE_AUTH_KEY',
 105          'LOGGED_IN_KEY',
 106          'NONCE_KEY',
 107          'AUTH_SALT',
 108          'SECURE_AUTH_SALT',
 109          'LOGGED_IN_SALT',
 110          'NONCE_SALT',
 111      );
 112  
 113      /**
 114       * Whether to derive from WP_SECRETS_KEY_PREVIOUS instead of WP_SECRETS_KEY.
 115       *
 116       * @since 7.2.0
 117       * @var bool
 118       */
 119      private $use_previous_key;
 120  
 121      /**
 122       * Constructor.
 123       *
 124       * @since 7.2.0
 125       *
 126       * @param bool $use_previous_key Derive from WP_SECRETS_KEY_PREVIOUS rather than
 127       *                               WP_SECRETS_KEY. Used only during a site-key
 128       *                               rotation, to unwrap under the outgoing key.
 129       */
 130  	public function __construct( bool $use_previous_key = false ) {
 131          $this->use_previous_key = $use_previous_key;
 132      }
 133  
 134      /**
 135       * Wraps raw key material under the derived site key.
 136       *
 137       * @since 7.2.0
 138       *
 139       * @param string $key_material Raw key material to protect.
 140       * @return string|WP_Error
 141       */
 142  	public function wrap(
 143          #[\SensitiveParameter]
 144          string $key_material
 145      ) {
 146          if ( '' === $key_material ) {
 147              return new WP_Error(
 148                  WP_SECRETS_ERROR_INVALID_VALUE,
 149                  __( 'Key material to wrap must not be empty.' )
 150              );
 151          }
 152  
 153          if ( ! function_exists( 'sodium_crypto_aead_xchacha20poly1305_ietf_encrypt' ) ) {
 154              return new WP_Error(
 155                  WP_SECRETS_ERROR_CRYPTO_UNAVAILABLE,
 156                  __( 'No libsodium implementation is available.' )
 157              );
 158          }
 159  
 160          $site_key = $this->get_site_key();
 161  
 162          if ( is_wp_error( $site_key ) ) {
 163              return $site_key;
 164          }
 165  
 166          $nonce      = random_bytes( SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES );
 167          $ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt( $key_material, self::AAD, $nonce, $site_key );
 168  
 169          wp_secrets_memzero( $site_key );
 170  
 171          return base64_encode( $nonce . $ciphertext );
 172      }
 173  
 174      /**
 175       * Unwraps key material previously wrapped by wrap().
 176       *
 177       * @since 7.2.0
 178       *
 179       * @param string $wrapped An opaque value previously returned by wrap().
 180       * @return string|WP_Error
 181       */
 182  	public function unwrap( string $wrapped ) {
 183          if ( '' === $wrapped ) {
 184              return new WP_Error(
 185                  WP_SECRETS_ERROR_KEY_UNAVAILABLE,
 186                  __( 'Nothing to unwrap.' )
 187              );
 188          }
 189  
 190          if ( ! function_exists( 'sodium_crypto_aead_xchacha20poly1305_ietf_decrypt' ) ) {
 191              return new WP_Error(
 192                  WP_SECRETS_ERROR_CRYPTO_UNAVAILABLE,
 193                  __( 'No libsodium implementation is available.' )
 194              );
 195          }
 196  
 197          $raw          = base64_decode( $wrapped, true );
 198          $nonce_length = SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES;
 199  
 200          if ( false === $raw || strlen( $raw ) <= $nonce_length ) {
 201              return new WP_Error(
 202                  WP_SECRETS_ERROR_KEY_UNAVAILABLE,
 203                  __( 'The wrapped key material is malformed.' )
 204              );
 205          }
 206  
 207          $nonce      = substr( $raw, 0, $nonce_length );
 208          $ciphertext = substr( $raw, $nonce_length );
 209  
 210          $site_key = $this->get_site_key();
 211  
 212          if ( is_wp_error( $site_key ) ) {
 213              return $site_key;
 214          }
 215  
 216          $key_material = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt( $ciphertext, self::AAD, $nonce, $site_key );
 217  
 218          wp_secrets_memzero( $site_key );
 219  
 220          if ( ! is_string( $key_material ) ) {
 221              return new WP_Error(
 222                  WP_SECRETS_ERROR_KEY_UNAVAILABLE,
 223                  __( 'The wrapped key material could not be decrypted with the configured site key.' )
 224              );
 225          }
 226  
 227          return $key_material;
 228      }
 229  
 230      /**
 231       * Reports the state of WP_SECRETS_KEY.
 232       *
 233       * Site Health and wp_is_secrets_api_available() use this rather than the text of
 234       * get_key_source(), which is translated.
 235       *
 236       * @since 7.2.0
 237       *
 238       * @return string One of the KEY_STATUS_* constants.
 239       */
 240  	public function get_key_status(): string {
 241          if ( ! defined( 'WP_SECRETS_KEY' ) ) {
 242              return self::KEY_STATUS_UNDEFINED;
 243          }
 244  
 245          if ( ! $this->is_usable_key_value( WP_SECRETS_KEY, $this->get_auth_constant_values() ) ) {
 246              return self::KEY_STATUS_UNUSABLE;
 247          }
 248  
 249          return $this->is_canonical_base64_32( WP_SECRETS_KEY ) ? self::KEY_STATUS_BASE64 : self::KEY_STATUS_LEGACY;
 250      }
 251  
 252      /**
 253       * Describes the active key source, for Site Health.
 254       *
 255       * @since 7.2.0
 256       *
 257       * @return string
 258       */
 259  	public function get_key_source(): string {
 260          switch ( $this->get_key_status() ) {
 261              case self::KEY_STATUS_UNDEFINED:
 262                  /* translators: %s: WP_SECRETS_KEY */
 263                  return sprintf( __( '%s is not defined' ), 'WP_SECRETS_KEY' );
 264  
 265              case self::KEY_STATUS_UNUSABLE:
 266                  /* translators: %s: WP_SECRETS_KEY */
 267                  return sprintf( __( '%s is defined but not usable' ), 'WP_SECRETS_KEY' );
 268  
 269              case self::KEY_STATUS_BASE64:
 270                  /* translators: %s: WP_SECRETS_KEY */
 271                  return sprintf( __( '%s (base64-encoded 32 bytes)' ), 'WP_SECRETS_KEY' );
 272  
 273              default:
 274                  /* translators: %s: WP_SECRETS_KEY */
 275                  return sprintf( __( '%s (legacy interpretation, hashed as an opaque string)' ), 'WP_SECRETS_KEY' );
 276          }
 277      }
 278  
 279      /**
 280       * Derives the 32-byte site key per the rules documented on this class.
 281       *
 282       * @since 7.2.0
 283       *
 284       * @return string|WP_Error
 285       */
 286  	private function get_site_key() {
 287          $constant_name = $this->use_previous_key ? 'WP_SECRETS_KEY_PREVIOUS' : 'WP_SECRETS_KEY';
 288  
 289          if ( ! defined( $constant_name ) ) {
 290              return new WP_Error(
 291                  WP_SECRETS_ERROR_KEY_UNAVAILABLE,
 292                  sprintf(
 293                      /* translators: %s: PHP constant name. */
 294                      __( '%s is not defined.' ),
 295                      $constant_name
 296                  )
 297              );
 298          }
 299  
 300          $raw_constant = constant( $constant_name );
 301  
 302          if ( ! $this->is_usable_key_value( $raw_constant, $this->get_auth_constant_values() ) ) {
 303              return new WP_Error(
 304                  WP_SECRETS_ERROR_KEY_UNAVAILABLE,
 305                  sprintf(
 306                      /* translators: %s: PHP constant name. */
 307                      __( '%s is defined but is not usable: it must be a non-empty string, not the wp-config-sample.php placeholder, and not the value of one of the authentication keys and salts.' ),
 308                      $constant_name
 309                  )
 310              );
 311          }
 312  
 313          $decoded = $this->decode_canonical_base64_32( $raw_constant );
 314  
 315          if ( null !== $decoded ) {
 316              return $decoded;
 317          }
 318  
 319          return sodium_crypto_generichash( $raw_constant, '', SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES );
 320      }
 321  
 322      /**
 323       * Whether a candidate key constant value is usable as key material.
 324       *
 325       * It must be a string that is not empty or only whitespace, must not be the
 326       * wp-config-sample.php placeholder, and must not equal any of the authentication
 327       * keys and salts.
 328       *
 329       * Takes explicit arguments rather than reading the constants directly so the
 330       * decision logic is testable without touching real constants, which – once
 331       * WordPress has bootstrapped once in a process – can never be redefined to a
 332       * different value for a test.
 333       *
 334       * @since 7.2.0
 335       *
 336       * @param mixed    $value       Candidate value of WP_SECRETS_KEY or WP_SECRETS_KEY_PREVIOUS.
 337       * @param string[] $auth_values Values of the authentication keys and salts that are defined.
 338       * @return bool
 339       *
 340       * @phpstan-assert-if-true non-empty-string $value
 341       */
 342  	private function is_usable_key_value( $value, array $auth_values ): bool {
 343          return is_string( $value )
 344              && '' !== trim( $value )
 345              && self::KNOWN_PLACEHOLDER !== $value
 346              && ! in_array( $value, $auth_values, true );
 347      }
 348  
 349      /**
 350       * Returns the values of the authentication keys and salts that are defined.
 351       *
 352       * @since 7.2.0
 353       *
 354       * @return string[]
 355       */
 356  	private function get_auth_constant_values(): array {
 357          $values = array();
 358  
 359          foreach ( self::AUTH_CONSTANTS as $constant_name ) {
 360              if ( defined( $constant_name ) && is_string( constant( $constant_name ) ) ) {
 361                  $values[] = constant( $constant_name );
 362              }
 363          }
 364  
 365          return $values;
 366      }
 367  
 368      /**
 369       * Whether $value is the canonical base64 encoding of exactly 32 bytes.
 370       *
 371       * Uses strict-mode decoding plus a re-encode comparison, rather than a bare
 372       * base64_decode(), so that a malformed or non-canonically-padded constant falls
 373       * through to the legacy (hashed) interpretation instead of being silently
 374       * accepted as 32 bytes of something else.
 375       *
 376       * @since 7.2.0
 377       *
 378       * @param mixed $value Candidate value.
 379       * @return bool
 380       */
 381  	private function is_canonical_base64_32( $value ): bool {
 382          return null !== $this->decode_canonical_base64_32( $value );
 383      }
 384  
 385      /**
 386       * Decodes $value if it is the canonical base64 encoding of exactly 32 bytes.
 387       *
 388       * @since 7.2.0
 389       *
 390       * @param mixed $value Candidate value.
 391       * @return string|null The 32 decoded bytes, or null if $value is anything else.
 392       */
 393  	private function decode_canonical_base64_32( $value ): ?string {
 394          if ( ! is_string( $value ) ) {
 395              return null;
 396          }
 397  
 398          $decoded = base64_decode( $value, true );
 399  
 400          if ( false === $decoded || SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES !== strlen( $decoded ) ) {
 401              return null;
 402          }
 403  
 404          return base64_encode( $decoded ) === $value ? $decoded : null;
 405      }
 406  }


Generated : Mon Nov 16 08:20:01 2026 Cross-referenced by PHPXref