PackageSigner   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 3
c 3
b 1
f 0
lcom 0
cbo 2
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createPackageSignature() 0 32 3
1
<?php
2
3
namespace JWage\APNS\Safari;
4
5
use ErrorException;
6
use JWage\APNS\Certificate;
7
use RuntimeException;
8
9
class PackageSigner
10
{
11
    /**
12
     * Creates a package signature using the given certificate and package directory.
13
     *
14
     * @param \JWage\APNS\Certificate $certificate
15
     * @param \JWage\APNS\Safari\Package $package
16
     *
17
     * @return string Path of signature
18
     */
19
    public function createPackageSignature(Certificate $certificate, Package $package)
20
    {
21
        $pkcs12 = $certificate->getCertificateString();
22
        $certPassword = $certificate->getPassword();
23
24
        $certs = array();
25
26
        if (!openssl_pkcs12_read($pkcs12, $certs, $certPassword)) {
27
            throw new RuntimeException('Failed to create signature.');
28
        }
29
30
        $signaturePath = sprintf('%s/signature', $package->getPackageDir());
31
        $manifestJsonPath = sprintf('%s/manifest.json', $package->getPackageDir());
32
33
        // Sign the manifest.json file with the private key from the certificate
34
        $certData = openssl_x509_read($certs['cert']);
35
        $privateKey = openssl_pkey_get_private($certs['pkey'], $certPassword);
36
        openssl_pkcs7_sign($manifestJsonPath, $signaturePath, $certData, $privateKey, array(), PKCS7_BINARY | PKCS7_DETACHED);
37
38
        // Convert the signature from PEM to DER
39
        $signaturePem = file_get_contents($signaturePath);
40
        $matches = array();
41
42
        if (!preg_match('~Content-Disposition:[^\n]+\s*?([A-Za-z0-9+=/\r\n]+)\s*?-----~', $signaturePem, $matches)) {
43
            throw new ErrorException('Failed to extract content from signature pem.');
44
        }
45
46
        $signatureDer = base64_decode($matches[1]);
47
        file_put_contents($signaturePath, $signatureDer);
48
49
        return $signaturePath;
50
    }
51
}
52