CacheKeyStore.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace ACES\Common;
  3. abstract class KStoreType {
  4. const ENC_STORE = 0;
  5. const DEC_STORE = 1;
  6. }
  7. final class CacheKeyStore {
  8. private $encKeyStore;
  9. private $decKeyStore;
  10. private $futureKeyIds;
  11. public function __construct() {
  12. $this->encKeyStore = array();
  13. $this->decKeyStore = array();
  14. $this->futureKeyIds = array();
  15. }
  16. /**
  17. * Search master key by index
  18. * @param $mkIndex
  19. * @return Mkey|null
  20. */
  21. public function searchDeckey($mkIndex){
  22. if(array_key_exists(base64_encode($mkIndex), $this->decKeyStore)){
  23. return $this->decKeyStore[base64_encode($mkIndex)];
  24. }else{
  25. return NULL;
  26. }
  27. }
  28. /* Return number of specified key store type
  29. * @param KeyStoreType $kstoreType
  30. *
  31. * @return int
  32. */
  33. public function numOfKeys($kstoreType) {
  34. if($kstoreType == KStoreType::ENC_STORE){
  35. return sizeof($this->encKeyStore);
  36. }else{
  37. return sizeof($this->decKeyStore);
  38. }
  39. }
  40. /* Get encrypt keys by version
  41. * @param int $keyVersion
  42. *
  43. * @return Mkey
  44. */
  45. public function getEncKeyByVersion($keyVersion) {
  46. foreach ($this->encKeyStore as $key => $value ){
  47. if($value->getVersion() == $keyVersion)
  48. return $value;
  49. }
  50. return null;
  51. }
  52. /* Update keys in keystore cache
  53. * @param string $b64Index
  54. * @param Mkey $mkey
  55. * @KStoreType $kstoreType
  56. *
  57. * @return void
  58. */
  59. public function updateKey($b64Index, $mkey, $kstoreType) {
  60. if($kstoreType == KStoreType::ENC_STORE){
  61. // update it when key is new to cache or status has been changed
  62. if(!array_key_exists($b64Index, $this->encKeyStore)){
  63. $this->encKeyStore[$b64Index] = $mkey;
  64. }else{
  65. if($this->encKeyStore[$b64Index]->getKeyStatus() != $mkey->getKeyStatus()){
  66. $this->encKeyStore[$b64Index] = $mkey;
  67. }
  68. }
  69. }else{
  70. if(!array_key_exists($b64Index, $this->decKeyStore)){
  71. $this->decKeyStore[$b64Index] = $mkey;
  72. }else{
  73. if($this->decKeyStore[$b64Index]->getKeyStatus() != $mkey->getKeyStatus()){
  74. $this->decKeyStore[$b64Index] = $mkey;
  75. }
  76. }
  77. }
  78. }
  79. /* Remove all keys in keystore cache
  80. *
  81. * @return void
  82. */
  83. public function removeAllMKeys() {
  84. $this->encKeyStore = array();
  85. $this->decKeyStore = array();
  86. }
  87. /* Remove keys via list
  88. * @param array $target
  89. * @param KStoreType $kstoreType
  90. *
  91. * @return void
  92. */
  93. public function removeKeysViaList($target, $kstoreType) {
  94. foreach ($target as $t){
  95. if($kstoreType == KStoreType::ENC_STORE){
  96. unset($this->encKeyStore[$t]);
  97. }else{
  98. unset($this->decKeyStore[$t]);
  99. }
  100. }
  101. }
  102. /* Get key id list by key store type
  103. * @param KStoreType $kstoreType
  104. *
  105. * @return array
  106. */
  107. public function getKeyIDList($kstoreType) {
  108. if($kstoreType == KStoreType::ENC_STORE){
  109. return array_keys($this->encKeyStore);
  110. }else{
  111. return array_keys($this->decKeyStore);
  112. }
  113. }
  114. /* Clear futurekeyids
  115. *
  116. * @return void
  117. */
  118. public function resetFutureKeyIDs() {
  119. $this->futureKeyIds = array();
  120. }
  121. /* Update local futurekeyids cache
  122. * @param string $service
  123. * @param int maxVer
  124. *
  125. * @return void
  126. */
  127. public function updateFutureKeyIDs($service, $maxVer) {
  128. ;
  129. }
  130. /* If local futurekeyids cache contains specified keyid
  131. * @param byte[] $keyid
  132. *
  133. * @return bool
  134. */
  135. public function hasFutureKeyID($keyid) {
  136. return array_key_exists(base64_encode($keyid), $this->futureKeyIds);
  137. }
  138. }