BEP 35 - Torrent Signing

This allows you to attach a certificate to 'signatures' field to prove authenticity of that media within the torrent file. Editing the file will remove the verification as it collects a checksum of the files and produces a certificate field, and a sha1 field of infoDict + Length.

OpenSSL 1.1.0 or greater - PHP 5.6, 7, 8

This expects a DER encoded X.509 certificate and private key file generated when you created your CSR (Ask your host).

I've managed to pull it off based on this:
https://github.com/bittorrent/ut-signing-tool

Spec:
http://bittorrent.org/beps/bep_0035.html


// Open torrent file for reading
$parse = file_get_contents('in.torrent');
$array = Bencode::decode($parse);

// BEP_35 - Torrent Signing
if (!isset($array['signatures'])) {
   $certFile      = file_get_contents('certificate.pem');
   $keyFile       = file_get_contents('private.key');
   $result        = openssl_x509_parse($certFile);
   $keyPassphrase = 'Your_Private_Key_Passphrase';
   $privatekey    = openssl_get_privatekey($keyFile, $keyPassphrase);
   openssl_sign($parse, $signature, $privatekey);
   $signatures = array(
       'signatures' => array(
           $result['subject']['CN'] => array(
               'certificate' => null, // <--- we need to now deal with this
               'info' => 'Optional',
               'signature' => $signature
           )
       )
   );
   $array      = array_merge($array, $signatures);
   array_merge($result, $array);
   file_put_contents('out.torrent', Bencode::encode($array));
} else {
   echo 'torrent is already signed.';
}
// Test
echo '<pre>';
print_r($array);
echo '</pre>';


Its nearly finished, as I realize the binary (unix executable) just dumps the parsed X509 certificates array into the bencoded data and from there it Bencode::encode($array) like the program in the link.

So I converted it to PHP native functions and Im nearly finished. All the update needs is a change to your upload script, and a small change to download.php - optionally you can showcase the signed feature on your torrent details page.

Using the link above, you cant change algorithms, add options or even control the data - because its written in pure C++.
So to avoid that (using it for 2 yrs now), I wanted to replicate it in PHP.

It provides the exact same processing as the program in the link. Its the best I can do 😂
Last edited by STorrents2020 on 2022-02-15 01:20:50