KeyPair.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (2016-12-12)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\Models;
  20. use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;
  21. /**
  22. * Description of KeyPair
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. * @property int $hosting_id
  27. * @property int $vm_id
  28. * @method static $this ofHostingId($hostingId)
  29. * @method $this ofVmId($id)
  30. */
  31. class KeyPair extends ExtendedEloquentModel
  32. {
  33. /** @var string */
  34. protected $table = 'KeyPair';
  35. protected $fillable = ['id', 'vm_id', 'hosting_id', 'public', 'private'];
  36. public $timestamps = false;
  37. public function getId()
  38. {
  39. return $this->id;
  40. }
  41. public function getHostingId()
  42. {
  43. return $this->hosting_id;
  44. }
  45. public function getPublic()
  46. {
  47. return decrypt($this->public);
  48. }
  49. public function getPrivate()
  50. {
  51. return decrypt($this->private);
  52. }
  53. public function isPublic()
  54. {
  55. return decrypt($this->public) != null;
  56. }
  57. public function isPrivate()
  58. {
  59. return decrypt($this->private) != null;
  60. }
  61. public function setId($id)
  62. {
  63. $this->id = $id;
  64. return $this;
  65. }
  66. public function setHostingId($hostingId)
  67. {
  68. $this->hosting_id = $hostingId;
  69. return $this;
  70. }
  71. public function setPublic($public)
  72. {
  73. $this->public = encrypt($public);
  74. return $this;
  75. }
  76. public function setPrivate($private)
  77. {
  78. $this->private = encrypt($private);
  79. return $this;
  80. }
  81. /**
  82. * @return KeyPair
  83. */
  84. public static function make()
  85. {
  86. $config = ['private_key_type' => OPENSSL_KEYTYPE_RSA];
  87. $res = openssl_pkey_new($config);
  88. openssl_pkey_export($res, $privkey);
  89. $pubkey = openssl_pkey_get_details($res);
  90. $pubKey = self::encodePublicKey($res);
  91. $obj = new KeyPair();
  92. $obj->setPublic($pubKey)
  93. ->setPrivate($privkey);
  94. return $obj;
  95. }
  96. private static function encodePublicKey($privKey)
  97. {
  98. $keyInfo = openssl_pkey_get_details($privKey);
  99. $buffer = pack("N", 7) . "ssh-rsa" .
  100. self::encodeBuffer($keyInfo['rsa']['e']) .
  101. self::encodeBuffer($keyInfo['rsa']['n']);
  102. return "ssh-rsa " . base64_encode($buffer);
  103. }
  104. private static function encodeBuffer($buffer)
  105. {
  106. $len = strlen($buffer);
  107. if (ord($buffer[0]) & 0x80)
  108. {
  109. $len++;
  110. $buffer = "\x00" . $buffer;
  111. }
  112. return pack("Na*", $len, $buffer);
  113. }
  114. public function scopeOfHostingId($query, $hostingId)
  115. {
  116. return $query->where("hosting_id", $hostingId);
  117. }
  118. public function scopeOfVmId($query, $vmId)
  119. {
  120. return $query->where("vm_id", $vmId);
  121. }
  122. }