/** * @param string $params * @return \Mdanter\Ecc\Curves\NamedCurveFp */ public function parse($params) { $params = str_replace(self::HEADER, '', $params); $params = str_replace(self::FOOTER, '', $params); $oid = ObjectIdentifier::fromBinary(base64_decode($params)); return CurveOidMapper::getCurveFromOid($oid); }
public function format(PublicKeyInterface $key) { if (!$key->getCurve() instanceof NamedCurveFp) { throw new \RuntimeException('Not implemented for unnamed curves'); } $sequence = new Sequence(new Sequence(new ObjectIdentifier(DerPublicKeySerializer::X509_ECDSA_OID), CurveOidMapper::getCurveOid($key->getCurve())), new BitString($this->encodePoint($key->getPoint()))); return $sequence->getBinary(); }
/** * @param string $data * {@inheritDoc} * @see \Mdanter\Ecc\Serializer\PrivateKeySerializerInterface::parse() * @throws \FG\ASN1\Exception\ParserException */ public function parse($data) { $asnObject = Object::fromBinary($data); if (!$asnObject instanceof Sequence || $asnObject->getNumberofChildren() !== 4) { throw new \RuntimeException('Invalid data.'); } $children = $asnObject->getChildren(); $version = $children[0]; if ($version->getContent() != 1) { throw new \RuntimeException('Invalid data: only version 1 (RFC5915) keys are supported.'); } $key = $this->adapter->hexDec($children[1]->getContent()); $oid = $children[2]->getContent(); $generator = CurveOidMapper::getGeneratorFromOid($oid); return $generator->getPrivateKeyFrom($key); }
public function parse($binaryData) { $asnObject = Object::fromBinary($binaryData); if (!$asnObject instanceof Sequence || $asnObject->getNumberofChildren() != 2) { throw new \RuntimeException('Invalid data.'); } $children = $asnObject->getChildren(); $oid = $children[0]->getChildren()[0]; $curveOid = $children[0]->getChildren()[1]; $encodedKey = $children[1]; if ($oid->getContent() !== DerPublicKeySerializer::X509_ECDSA_OID) { throw new \RuntimeException('Invalid data: non X509 data.'); } $generator = CurveOidMapper::getGeneratorFromOid($curveOid); return $this->parseKey($generator, $encodedKey->getContent()); }
/** * @param PointInterface $point * @return string */ public function serialize(PointInterface $point) { $length = CurveOidMapper::getByteSize($point->getCurve()) * 2; if ($this->debug) { error_log('Detected length: ' . $length); error_log('Unpadded:' . $this->adapter->decHex($point->getX())); error_log('Unpadded len:' . strlen($this->adapter->decHex($point->getX()))); error_log('Padded: ' . str_pad($this->adapter->decHex($point->getX()), $length, '0', STR_PAD_LEFT)); } $hexString = '04'; $hexString .= str_pad($this->adapter->decHex($point->getX()), $length, '0', STR_PAD_LEFT); $hexString .= str_pad($this->adapter->decHex($point->getY()), $length, '0', STR_PAD_LEFT); if ($this->debug) { error_log('Resulting length: ' . strlen($hexString)); error_log('Hex: ' . $hexString); } return $hexString; }
/** * @param NamedCurveFp $curve * @param PublicKeyInterface $publicKey * @return Sequence */ public function getSubjectKeyASN(NamedCurveFp $curve, PublicKeyInterface $publicKey) { return new Sequence(new Sequence(new ObjectIdentifier(CertificateSerializer::ECPUBKEY_OID), CurveOidMapper::getCurveOid($curve)), new BitString($this->pubKeySer->getUncompressedKey($publicKey))); }
/** * @param NamedCurveFp $curve * @param PublicKeyInterface $publicKey * @return Sequence */ public function getSubjectKeyASN(NamedCurveFp $curve, PublicKeyInterface $publicKey) { return new Sequence(new Sequence(new ObjectIdentifier('1.2.840.10045.2.1'), CurveOidMapper::getCurveOid($curve)), new BitString($this->pubKeySer->getUncompressedKey($publicKey))); }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(CurveOidMapper::getNames()); }
/** * {@inheritDoc} * @see \Mdanter\Ecc\Serializer\PrivateKeySerializerInterface::serialize() */ public function getPrivateKeyInfo(PrivateKeyInterface $key) { $keyData = $this->serializer->serialize($key); $privateKeyInfo = new Sequence(new Integer(self::VERSION), new Sequence(new ObjectIdentifier(DerPublicKeySerializer::X509_ECDSA_OID), CurveOidMapper::getCurveOid($key->getPoint()->getCurve())), new OctetString(bin2hex($keyData))); return $privateKeyInfo; }