get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d ORDER BY meta_id ASC LIMIT 1", self::OPTION_KEY, get_current_network_id() ) ); } else { $value = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", self::OPTION_KEY ) ); } // phpcs:enable return is_string( $value ) ? $value : ''; } /** * Create the key exactly once, even when several requests arrive together. * * @return string Base64 key. */ private static function create_stored() { global $wpdb; $got = false; for ( $i = 0; $i < 50 && ! $got; $i++ ) { $got = Lock::acquire( 'encryption_key', 30 ); if ( ! $got ) { usleep( 100000 ); $existing = self::read_stored(); if ( '' !== $existing ) { return $existing; } } } if ( ! $got ) { throw new \RuntimeException( 'Could not create the encryption key; try again.' ); } try { $existing = self::read_stored(); if ( '' !== $existing ) { return $existing; } $new = base64_encode( random_bytes( 32 ) ); // phpcs:disable WordPress.DB.DirectDatabaseQuery if ( is_multisite() ) { $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => get_current_network_id(), 'meta_key' => self::OPTION_KEY, 'meta_value' => $new ) ); // phpcs:ignore WordPress.DB.SlowDBQuery wp_cache_delete( get_current_network_id() . ':' . self::OPTION_KEY, 'site-options' ); } else { $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", self::OPTION_KEY, $new ) ); wp_cache_delete( self::OPTION_KEY, 'options' ); wp_cache_delete( 'notoptions', 'options' ); } // phpcs:enable $saved = self::read_stored(); if ( '' === $saved ) { throw new \RuntimeException( 'The encryption key could not be saved.' ); } return $saved; } finally { Lock::release( 'encryption_key' ); } } /** * @param string $plaintext Plaintext. * @return string */ public static function encrypt( $plaintext ) { $iv = random_bytes( 12 ); $tag = ''; $ciphertext = openssl_encrypt( (string) $plaintext, self::CIPHER, self::key(), OPENSSL_RAW_DATA, $iv, $tag, '', 16 ); if ( false === $ciphertext ) { throw new \RuntimeException( 'Encryption failed.' ); } return self::PREFIX . base64_encode( $iv . $tag . $ciphertext ); } /** * @param string $stored Value from encrypt(). * @return string|null Plaintext, or null when it is not ours or fails authentication. */ public static function decrypt( $stored ) { if ( ! self::is_encrypted( $stored ) ) { return null; } $raw = base64_decode( substr( $stored, strlen( self::PREFIX ) ), true ); if ( false === $raw || strlen( $raw ) < 28 ) { return null; } foreach ( self::decrypt_keys() as $key ) { $plain = openssl_decrypt( substr( $raw, 28 ), self::CIPHER, $key, OPENSSL_RAW_DATA, substr( $raw, 0, 12 ), substr( $raw, 12, 16 ) ); if ( false !== $plain ) { return $plain; } } return null; } /** * @param mixed $value Value. * @return bool */ public static function is_encrypted( $value ) { return is_string( $value ) && 0 === strpos( $value, self::PREFIX ); } /** * Read an SSN stored by any version: 3.0 format, 2.6 base64 AES-CBC, or the * 1.x "value::iv" format. * * @param string $stored Stored value. * @return string Digits, or '' when it cannot be read. */ public static function read_ssn( $stored ) { if ( '' === (string) $stored ) { return ''; } $plain = (string) $stored; // Up to two layers: 2.6's edit link re-saved the already-encrypted value. for ( $layer = 0; $layer < 3; $layer++ ) { $digits = preg_replace( '/\D/', '', $plain ); if ( 9 === strlen( $digits ) && strlen( $plain ) <= 11 ) { return $digits; } $next = self::is_encrypted( $plain ) ? self::decrypt( $plain ) : ( function_exists( 'mapp_decrypt_ssn' ) ? mapp_decrypt_ssn( $plain ) : null ); if ( null === $next || false === $next || '' === $next || $next === $plain ) { return ''; } $plain = (string) $next; } return ''; } /** * Random URL-safe token. * * @param int $bytes Entropy in bytes. * @return string */ public static function token( $bytes = 32 ) { return rtrim( strtr( base64_encode( random_bytes( $bytes ) ), '+/', '-_' ), '=' ); } /** * Keyed hash for storing tokens (the token itself is never stored). * * @param string $token Token. * @return string */ public static function hash_token( $token ) { return Secret::hmac( 'tokens', (string) $token ); } }