class PrivateKey
{
/**
- * OpenSSL pkey resource
- * @var resource
+ * Private key
+ * @var string
*/
private $key;
+
+ /**
+ * Public key
+ * @var string
+ */
+ private $pub;
/**
* Read a private key
* @throws \Exception
*/
function __construct($file, $password) {
- $this->key = openssl_pkey_get_private("file://$file", $password);
- if (!is_resource($this->key)) {
+ /* there appears to be a bug with refcount handling of this
+ * resource; when the resource is stored as property, it cannot be
+ * "coerced to a private key" on openssl_sign() alter in another method
+ */
+ $key = openssl_pkey_get_private("file://$file", $password);
+ if (!is_resource($key)) {
throw new \Exception("Could not load private key");
}
+ openssl_pkey_export($key, $this->key);
+ $this->pub = openssl_pkey_get_details($key)["key"];
}
/**
* @throws \Exception
*/
function exportPublicKey($file) {
- if (!file_put_contents("$file.tmp", openssl_pkey_get_details($this->key)["key"])
- || !rename("$file.tmp", $file)
- ) {
+ if (!file_put_contents("$file.tmp", $this->pub) || !rename("$file.tmp", $file)) {
throw new \Exception(error_get_last()["message"]);
}
}
$this->info("Creating phar %s ...%s", $pkgtemp, $this->args->verbose ? "\n" : " ");
try {
$package = new Phar($pkgtemp);
-
+
if ($this->args->sign) {
$this->info("\nUsing private key to sign phar ... \n");
$privkey = new Openssl\PrivateKey(realpath($this->args->sign), $this->askpass());