Skip to content

Commit

Permalink
Upcast AlgorithmIdentifier typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sop committed Nov 8, 2017
1 parent a3ca085 commit bb23700
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public static function fromASN1Params(UnspecifiedType $params = null)
$idx = 0;
// version is optional in rfc2898
if ($seq->has($idx, Element::TYPE_INTEGER)) {
$version = (int) $seq->at($idx++)
$version = $seq->at($idx++)
->asInteger()
->number();
->intNumber();
$key_bits = self::_versionToEKB($version);
}
// IV is present in all variants
Expand Down
6 changes: 3 additions & 3 deletions lib/CryptoTypes/Asymmetric/EC/ECPrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use ASN1\Type\Primitive\OctetString;
use ASN1\Type\Tagged\ExplicitlyTaggedType;
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\ECPublicKeyAlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
use Sop\CryptoTypes\Asymmetric\PrivateKey;
use Sop\CryptoTypes\Asymmetric\PublicKey;

Expand Down Expand Up @@ -70,7 +70,7 @@ public static function fromASN1(Sequence $seq): self
{
$version = $seq->at(0)
->asInteger()
->number();
->intNumber();
if (1 != $version) {
throw new \UnexpectedValueException("Version must be 1.");
}
Expand Down Expand Up @@ -171,7 +171,7 @@ public function withNamedCurve($named_curve): self
* {@inheritdoc}
*
*/
public function algorithmIdentifier(): AlgorithmIdentifier
public function algorithmIdentifier(): AlgorithmIdentifierType
{
return new ECPublicKeyAlgorithmIdentifier($this->namedCurve());
}
Expand Down
3 changes: 2 additions & 1 deletion lib/CryptoTypes/Asymmetric/EC/ECPublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\ECPublicKeyAlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
use Sop\CryptoTypes\Asymmetric\PublicKey;
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;

Expand Down Expand Up @@ -193,7 +194,7 @@ public function namedCurve(): string
* {@inheritdoc}
*
*/
public function algorithmIdentifier(): AlgorithmIdentifier
public function algorithmIdentifier(): AlgorithmIdentifierType
{
return new ECPublicKeyAlgorithmIdentifier($this->namedCurve());
}
Expand Down
15 changes: 8 additions & 7 deletions lib/CryptoTypes/Asymmetric/OneAsymmetricKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\ECPublicKeyAlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;

/**
* Implements PKCS #8 PrivateKeyInfo / OneAsymmetricKey ASN.1 type.
Expand Down Expand Up @@ -43,7 +44,7 @@ class OneAsymmetricKey
/**
* Algorithm identifier.
*
* @var AlgorithmIdentifier $_algo
* @var AlgorithmIdentifierType $_algo
*/
protected $_algo;

Expand All @@ -57,10 +58,10 @@ class OneAsymmetricKey
/**
* Constructor.
*
* @param AlgorithmIdentifier $algo Algorithm
* @param AlgorithmIdentifierType $algo Algorithm
* @param string $key Private key data
*/
public function __construct(AlgorithmIdentifier $algo, string $key)
public function __construct(AlgorithmIdentifierType $algo, string $key)
{
$this->_version = self::VERSION_1;
$this->_algo = $algo;
Expand All @@ -76,9 +77,9 @@ public function __construct(AlgorithmIdentifier $algo, string $key)
*/
public static function fromASN1(Sequence $seq): self
{
$version = (int) $seq->at(0)
$version = $seq->at(0)
->asInteger()
->number();
->intNumber();
if (!in_array($version, [self::VERSION_1, self::VERSION_2])) {
throw new \UnexpectedValueException(
"Version $version not supported.");
Expand Down Expand Up @@ -141,9 +142,9 @@ public static function fromPEM(PEM $pem): self
/**
* Get algorithm identifier.
*
* @return AlgorithmIdentifier
* @return AlgorithmIdentifierType
*/
public function algorithmIdentifier(): AlgorithmIdentifier
public function algorithmIdentifier(): AlgorithmIdentifierType
{
return $this->_algo;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/CryptoTypes/Asymmetric/PrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Sop\CryptoTypes\Asymmetric;

use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;

/**
* Base class for private keys.
Expand All @@ -15,9 +15,9 @@ abstract class PrivateKey
/**
* Get the private key algorithm identifier.
*
* @return \Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier
* @return AlgorithmIdentifierType
*/
abstract public function algorithmIdentifier(): AlgorithmIdentifier;
abstract public function algorithmIdentifier(): AlgorithmIdentifierType;

/**
* Get public key component of the asymmetric key pair.
Expand Down
6 changes: 3 additions & 3 deletions lib/CryptoTypes/Asymmetric/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Sop\CryptoTypes\Asymmetric;

use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;

/**
* Base class for public keys.
Expand All @@ -15,9 +15,9 @@ abstract class PublicKey
/**
* Get the public key algorithm identifier.
*
* @return \Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier
* @return AlgorithmIdentifierType
*/
abstract public function algorithmIdentifier(): AlgorithmIdentifier;
abstract public function algorithmIdentifier(): AlgorithmIdentifierType;

/**
* Get DER encoding of the public key.
Expand Down
11 changes: 6 additions & 5 deletions lib/CryptoTypes/Asymmetric/PublicKeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\ECPublicKeyAlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;

/**
* Implements X.509 SubjectPublicKeyInfo ASN.1 type.
Expand All @@ -20,7 +21,7 @@ class PublicKeyInfo
/**
* Algorithm identifier.
*
* @var AlgorithmIdentifier $_algo
* @var AlgorithmIdentifierType $_algo
*/
protected $_algo;

Expand All @@ -34,10 +35,10 @@ class PublicKeyInfo
/**
* Constructor.
*
* @param AlgorithmIdentifier $algo Algorithm
* @param AlgorithmIdentifierType $algo Algorithm
* @param string $key Public key data
*/
public function __construct(AlgorithmIdentifier $algo, string $key)
public function __construct(AlgorithmIdentifierType $algo, string $key)
{
$this->_algo = $algo;
$this->_publicKeyData = $key;
Expand Down Expand Up @@ -102,9 +103,9 @@ public static function fromDER(string $data): self
/**
* Get algorithm identifier.
*
* @return AlgorithmIdentifier
* @return AlgorithmIdentifierType
*/
public function algorithmIdentifier(): AlgorithmIdentifier
public function algorithmIdentifier(): AlgorithmIdentifierType
{
return $this->_algo;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/CryptoTypes/Asymmetric/RSA/RSAPrivateKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use ASN1\Type\Constructed\Sequence;
use ASN1\Type\Primitive\Integer;
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\RSAEncryptionAlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
use Sop\CryptoTypes\Asymmetric\PrivateKey;
use Sop\CryptoTypes\Asymmetric\PublicKey;

Expand Down Expand Up @@ -110,7 +110,7 @@ public static function fromASN1(Sequence $seq): self
{
$version = $seq->at(0)
->asInteger()
->number();
->intNumber();
if ($version != 0) {
throw new \UnexpectedValueException("Version must be 0.");
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public function coefficient()
* {@inheritdoc}
*
*/
public function algorithmIdentifier(): AlgorithmIdentifier
public function algorithmIdentifier(): AlgorithmIdentifierType
{
return new RSAEncryptionAlgorithmIdentifier();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/CryptoTypes/Asymmetric/RSA/RSAPublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Asymmetric\RSAEncryptionAlgorithmIdentifier;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;
use Sop\CryptoTypes\Asymmetric\PublicKey;
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;

Expand Down Expand Up @@ -121,7 +122,7 @@ public function publicExponent()
* {@inheritdoc}
*
*/
public function algorithmIdentifier(): AlgorithmIdentifier
public function algorithmIdentifier(): AlgorithmIdentifierType
{
return new RSAEncryptionAlgorithmIdentifier();
}
Expand Down

0 comments on commit bb23700

Please sign in to comment.