rfc:secure_serialization

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:secure_serialization [2015/12/30 20:03] yohgakirfc:secure_serialization [2018/03/01 23:18] (current) – RFC is Under Discussion carusogabriel
Line 3: Line 3:
   * Date: 2015-12-30   * Date: 2015-12-30
   * Author: Yasuo Ohgaki <yohgaki@php.net>   * Author: Yasuo Ohgaki <yohgaki@php.net>
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/secure_serialization   * First Published at: http://wiki.php.net/rfc/secure_serialization
  
Line 20: Line 20:
 ===== Proposal ===== ===== Proposal =====
  
-  * Add secure_serialize() and secure_unserialize() supports message authentication code generation/validation and expiration.+  * Add serialize_mhac() and unserialize_mhac() supports message authentication code generation/validation and expiration.
  
 <code php> <code php>
-  string secure_serialize(mixed $data_to_be_serialized , string $secret_key [, int $ttl=1800 [,bool $session_only=TRUE]]) +  string serialize_mhac(mixed $data_to_be_serialized , string $secret_key [, int $ttl=1800 [,bool $session_only=TRUE]]) 
-  mixed secure_unserialize(mixed $data_to_be_unserialized , mixed $secret_keys)+  mixed unserialize_mhac(mixed $data_to_be_unserialized , mixed $secret_keys)
 </code> </code>
  
  
-==== How secure_serialize() works ====+==== How serialize_mhac() works ====
  
 Pseudo code Pseudo code
Line 37: Line 37:
     return FALSE;     return FALSE;
   }   }
-  if ($ttl <0) {+  if ($ttl < 0) {
     trigger_error('Invalid TTL');     trigger_error('Invalid TTL');
     return FALSE;     return FALSE;
   }   }
-  $ttl = time() + $ttl; +  $ttl = $ttl ? time() + $ttl : 0
   $session_only = $session_only ? TRUE : FALSE;   $session_only = $session_only ? TRUE : FALSE;
   // Use random key to randomize $mac   // Use random key to randomize $mac
Line 69: Line 69:
  
  
-==== How secure_unserialize() works ====+==== How unserialize_mhac() works ====
  
 Pseudo code Pseudo code
 <code php> <code php>
-function secure_unserialize(string $data_to_be_unserialized, mixed $secret_key) : mixed {+function unserialize_mhac(string $data_to_be_unserialized, mixed $secret_key) : mixed {
   if (strlen($secret_key) < 32) {   if (strlen($secret_key) < 32) {
     trigger_error('Too short secret key');     trigger_error('Too short secret key');
Line 81: Line 81:
   // Unserialize special format   // Unserialize special format
   $tmp = __unserialize__($data_to_be_unserialized);   $tmp = __unserialize__($data_to_be_unserialized);
-  if ($tmp['ttl'] < time() {+  if ($tmp['ttl'] && $tmp['ttl'] < time() {
     // Serialized data is expired     // Serialized data is expired
     return FALSE;     return FALSE;
Line 93: Line 93:
       $mac = hash_hmac(       $mac = hash_hmac(
         'sha256',         'sha256',
-        $tmp['ttl'].$tmp['key'].sha256($secret_key.session_id()).$tmp['data'],+        $tmp['ttl'].$tmp['key'].sha256($k.session_id()).$tmp['data'],
         $k);         $k);
     } else {     } else {
Line 101: Line 101:
         $k);         $k);
     }     }
 +    if ($mac !== $tmp['mac']) {
 +       continue;
 +    }
 +    // Unserialize data normally and return
 +    return unserialize($tmp['data']);
   }   }
-   +  return FALSE;
-  if ($mac !== $tmp['mac']) { +
-    return FALSE+
-  } +
-  // Unserialize data normally and return +
-  return unserialize($tmp['data'])+
 } }
 </code> </code>
Line 158: Line 158:
 If session module stores old session ID, automatic fallback to old session ID may be supported. If session module stores old session ID, automatic fallback to old session ID may be supported.
  
-Compatibility functions for older releases may be implemented as PHP script.+Encryption is more secure than authentication codeImplement serialize_crypt/unserialize_crypt when standard encryption module is introduced. 
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
rfc/secure_serialization.1451505801.txt.gz · Last modified: 2017/09/22 13:28 (external edit)