AcmePhp\Ssl\Parser\KeyParser::parse PHP Method

parse() public method

Parse the key.
public parse ( Key $key ) : ParsedKey
$key AcmePhp\Ssl\Key
return AcmePhp\Ssl\ParsedKey
    public function parse(Key $key)
    {
        try {
            $resource = $key->getResource();
        } catch (KeyFormatException $e) {
            throw new KeyParsingException('Fail to load resource for key', 0, $e);
        }
        $rawData = openssl_pkey_get_details($resource);
        if (!is_array($rawData)) {
            throw new KeyParsingException(sprintf('Fail to parse key with error: %s', openssl_error_string()));
        }
        foreach (['type', 'key', 'bits'] as $requiredKey) {
            if (!isset($rawData[$requiredKey])) {
                throw new KeyParsingException(sprintf('Missing expected key "%s" in OpenSSL key', $requiredKey));
            }
        }
        $details = [];
        if ($rawData['type'] === OPENSSL_KEYTYPE_RSA) {
            $details = $rawData['rsa'];
        } elseif ($rawData['type'] === OPENSSL_KEYTYPE_DSA) {
            $details = $rawData['dsa'];
        } elseif ($rawData['type'] === OPENSSL_KEYTYPE_DH) {
            $details = $rawData['dh'];
        }
        return new ParsedKey($key, $rawData['key'], $rawData['bits'], $rawData['type'], $details);
    }

Usage Example

Example #1
0
 public function test parse PublicKey returns instance of ParsedKey()
 {
     $result = $this->service->parse($this->getPublicKey());
     $this->assertInstanceOf(ParsedKey::class, $result);
     $this->assertInstanceOf(Key::class, $result->getSource());
     $this->assertEquals(OPENSSL_KEYTYPE_RSA, $result->getType());
     $this->assertEquals(4096, $result->getBits());
     $this->assertInternalType('array', $result->getDetails());
     $this->assertEquals(trim($this->getPublicKey()->getPEM()), trim($result->getKey()));
 }
All Usage Examples Of AcmePhp\Ssl\Parser\KeyParser::parse
KeyParser