Ssl.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Ssl
  4. {
  5. public function __construct($field)
  6. {
  7. $this->field = $field;
  8. }
  9. public function encrypt($id, $value)
  10. {
  11. $config = $this->config($id, 1, $value);
  12. if ($config) {
  13. if ($config['type'] == 1) {
  14. # 非对称
  15. openssl_public_encrypt($config['value'], $value, $config['cert'], $config['option']);
  16. } elseif ($config['type'] == 2 && $config['cipher_algo']) {
  17. # 对称
  18. $value = openssl_encrypt($config['value'], $config['cipher_algo'], $config['cert'], $config['option'], $config['iv'], $config['tag'], $config['aad'], $config['tag_len']);
  19. } elseif ($config['type'] == 3 && $config['cipher_algo']) {
  20. # 签名
  21. openssl_sign($config['value'], $value, $config['cert'], $config['cipher_algo']);
  22. }
  23. if ($config['after'] == 2) {
  24. $value = base64_encode($value);
  25. }
  26. }
  27. return $value;
  28. }
  29. public function decrypt($id, $value, $data = '')
  30. {
  31. if (is_array($value)) {
  32. $value = Dever::json_encode($value);
  33. }
  34. $config = $this->config($id, 2, $value);
  35. if ($config) {
  36. if ($config['type'] == 1) {
  37. # 非对称
  38. openssl_public_decrypt($config['value'], $value, $config['cert'], $config['option']);
  39. } elseif ($config['type'] == 2 && $config['cipher_algo']) {
  40. # 对称
  41. $value = openssl_decrypt($config['value'], $config['cipher_algo'], $config['cert'], $config['option'], $config['iv'], $config['tag'], $config['aad']);
  42. } elseif ($config['type'] == 3 && $config['cipher_algo']) {
  43. # 签名验证
  44. $value = openssl_verify($data, $config['value'], $config['cert'], $config['cipher_algo']);
  45. }
  46. }
  47. return $value;
  48. }
  49. protected function config($id, $type, $value)
  50. {
  51. $config = Dever::db('platform_ssl', 'api')->find($id);
  52. if (!$config) {
  53. return false;
  54. }
  55. $config['value'] = $value;
  56. $key = $type == 1 ? 'encrypt' : 'decrypt';
  57. $this->cert($config, $key);
  58. if (!$config['cert']) {
  59. return false;
  60. }
  61. if ($type == 2 && $config['after'] == 2) {
  62. $config['value'] = base64_decode($config['value']);
  63. }
  64. # 对称加密需要特殊处理一下
  65. if ($config['type'] == 2) {
  66. if (!$config['option']) {
  67. $config['option'] = 'OPENSSL_NO_PADDING';
  68. }
  69. $config['option'] = constant($config['option']);
  70. if ($config['option'] === null) {
  71. $config['option'] = OPENSSL_NO_PADDING;
  72. }
  73. $config['iv'] = $this->field->{$config['iv']} ?? $config['iv'];
  74. $config['aad'] = $this->field->{$config['aad']} ?? $config['aad'];
  75. if ($config['tag_len']) {
  76. $config['tag'] = substr($config['value'], -$config['tag_len']);
  77. $config['value'] = substr($config['value'], 0, -$config['tag_len']);
  78. }
  79. if (!$config['tag']) {
  80. $config['tag'] = null;
  81. }
  82. }
  83. return $config;
  84. }
  85. protected function cert(&$config, $key)
  86. {
  87. $config['cert_type'] = $config[$key . '_cert_type'];
  88. $config['cert'] = $config[$key . '_cert'];
  89. $config['cert_id'] = $config[$key . '_cert_id'];
  90. if ($config['cert_type'] == 3) {
  91. # 公钥
  92. $config['cert'] = $this->field->{$config['cert']} ?? $config['cert'];
  93. } else {
  94. $cert = false;
  95. #$set = Dever::db('platform_cert', 'api')->find($config['cert_id']);
  96. # 获取账户里的cert
  97. $project = $this->field->account_project;
  98. $account_id = $this->field->account_id;
  99. if ($project && $account_id) {
  100. $cert = Dever::db('account_cert', $project)->find(array('account_id' => $account_id, 'platform_cert_id' => $config['cert_id']), array('order' => 'edate desc'));
  101. }
  102. if (!$cert) {
  103. $config['cert'] = false;
  104. return $config;
  105. }
  106. $this->field->setNumber($cert['number']);
  107. if ($config['cert_type'] == 2) {
  108. $key = 'private';
  109. $method = 'openssl_get_privatekey';
  110. } else {
  111. $key = 'public';
  112. $method = 'openssl_x509_read';
  113. }
  114. if ($cert[$key]) {
  115. $config['cert'] = $cert[$key];
  116. $config['cert'] = $method($config['cert']);
  117. }
  118. }
  119. }
  120. }