Skip to content

Commit

Permalink
Accept public key data only as a bit string
Browse files Browse the repository at this point in the history
  • Loading branch information
sop committed Aug 11, 2021
1 parent 711dd61 commit 679f6e5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 28 deletions.
5 changes: 3 additions & 2 deletions lib/CryptoTypes/Asymmetric/EC/ECPublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sop\CryptoTypes\Asymmetric\EC;

use Sop\ASN1\Type\Primitive\BitString;
use Sop\ASN1\Type\Primitive\Integer;
use Sop\ASN1\Type\Primitive\OctetString;
use Sop\CryptoEncoding\PEM;
Expand Down Expand Up @@ -200,10 +201,10 @@ public function toDER(): string
*
* @see https://tools.ietf.org/html/rfc5480#section-2.2
*/
public function subjectPublicKeyData(): string
public function subjectPublicKey(): BitString
{
// ECPoint is directly mapped to subjectPublicKey
return $this->_ecPoint;
return new BitString($this->_ecPoint);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/CryptoTypes/Asymmetric/OneAsymmetricKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ public function publicKeyInfo(): PublicKeyInfo
{
// if public key is explicitly defined
if ($this->hasPublicKeyData()) {
return PublicKeyInfo::fromBitString(
$this->_algo, $this->_publicKeyData);
return new PublicKeyInfo($this->_algo, $this->_publicKeyData);
}
// else derive from private key
return $this->privateKey()->publicKey()->publicKeyInfo();
Expand Down
5 changes: 3 additions & 2 deletions lib/CryptoTypes/Asymmetric/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sop\CryptoTypes\Asymmetric;

use Sop\ASN1\Type\Primitive\BitString;
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\AlgorithmIdentifierType;

Expand All @@ -25,9 +26,9 @@ abstract public function toDER(): string;
/**
* Get the public key data for subjectPublicKey in PublicKeyInfo.
*/
public function subjectPublicKeyData(): string
public function subjectPublicKey(): BitString
{
return $this->toDER();
return new BitString($this->toDER());
}

/**
Expand Down
24 changes: 6 additions & 18 deletions lib/CryptoTypes/Asymmetric/PublicKeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class PublicKeyInfo
* Constructor.
*
* @param AlgorithmIdentifierType $algo Algorithm
* @param string $key Public key data
* @param BitString $key Public key data
*/
public function __construct(AlgorithmIdentifierType $algo, string $key)
public function __construct(AlgorithmIdentifierType $algo, BitString $key)
{
$this->_algo = $algo;
$this->_publicKey = new BitString($key);
$this->_publicKey = $key;
}

/**
Expand All @@ -52,27 +52,15 @@ public static function fromASN1(Sequence $seq): self
{
$algo = AlgorithmIdentifier::fromASN1($seq->at(0)->asSequence());
$key = $seq->at(1)->asBitString();
return new self($algo, $key->string());
}

/**
* Initialize from public key data as a bit string.
*/
public static function fromBitString(
AlgorithmIdentifierType $algo, BitString $key): self
{
$obj = new self($algo, '');
$obj->_publicKey = $key;
return $obj;
return new self($algo, $key);
}

/**
* Inititalize from a PublicKey.
*/
public static function fromPublicKey(PublicKey $public_key): self
public static function fromPublicKey(PublicKey $key): self
{
return new self($public_key->algorithmIdentifier(),
$public_key->subjectPublicKeyData());
return new self($key->algorithmIdentifier(), $key->subjectPublicKey());
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/CryptoTypes/Asymmetric/RFC8410/RFC8410PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sop\CryptoTypes\Asymmetric\RFC8410;

use Sop\ASN1\Type\Primitive\BitString;
use Sop\CryptoTypes\Asymmetric\PublicKey;

/**
Expand Down Expand Up @@ -44,8 +45,8 @@ public function toDER(): string
/**
* {@inheritdoc}
*/
public function subjectPublicKeyData(): string
public function subjectPublicKey(): BitString
{
return $this->_publicKey;
return new BitString($this->_publicKey);
}
}
4 changes: 3 additions & 1 deletion test/unit/PublicKeyInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Sop\ASN1\Element;
use Sop\ASN1\Type\Primitive\BitString;
use Sop\ASN1\Type\Primitive\ObjectIdentifier;
use Sop\CryptoEncoding\PEM;
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
Expand Down Expand Up @@ -158,7 +159,8 @@ public function testInvalidAI(PublicKeyInfo $pki)

public function testInvalidECAlgoFail()
{
$pki = new PublicKeyInfo(new PubliceKeyInfoTest_InvalidECAlgo(), '');
$pki = new PublicKeyInfo(
new PubliceKeyInfoTest_InvalidECAlgo(), new BitString(''));
$this->expectException(\UnexpectedValueException::class);
$pki->publicKey();
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/rfc8410/Curve25519Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function testEd25519PubNoDer(Ed25519PublicKey $pub)
*/
public function testEd25519PubKeyData(Ed25519PublicKey $pub)
{
$this->assertIsString($pub->subjectPublicKeyData());
$this->assertInstanceOf(BitString::class, $pub->subjectPublicKey());
}

public function testDecodeX25519(): X25519PrivateKey
Expand Down

0 comments on commit 679f6e5

Please sign in to comment.