lib/cose/key.rb in cose-0.9.0 vs lib/cose/key.rb in cose-0.10.0
- old
+ new
@@ -6,11 +6,14 @@
require "cose/key/rsa"
require "cose/key/symmetric"
require "openssl"
module COSE
- class UnknownKeyType < StandardError; end
+ class Error < StandardError; end
+ class KeyDeserializationError < Error; end
+ class MalformedKeyError < KeyDeserializationError; end
+ class UnknownKeyType < KeyDeserializationError; end
module Key
def self.serialize(pkey)
from_pkey(pkey).serialize
end
@@ -25,11 +28,11 @@
raise "Unsupported #{pkey.class} object"
end
end
def self.deserialize(data)
- map = CBOR.decode(data)
+ map = cbor_decode(data)
case map[Base::LABEL_KTY]
when COSE::Key::OKP::KTY_OKP
COSE::Key::OKP.from_map(map)
when COSE::Key::EC2::KTY_EC2
@@ -37,12 +40,18 @@
when COSE::Key::RSA::KTY_RSA
COSE::Key::RSA.from_map(map)
when COSE::Key::Symmetric::KTY_SYMMETRIC
COSE::Key::Symmetric.from_map(map)
when nil
- raise UnknownKeyType, "Missing required key type kty label"
+ raise COSE::UnknownKeyType, "Missing required key type kty label"
else
- raise UnknownKeyType, "Unsupported or unknown key type #{map[Base::LABEL_KTY]}"
+ raise COSE::UnknownKeyType, "Unsupported or unknown key type #{map[Base::LABEL_KTY]}"
end
+ end
+
+ def self.cbor_decode(data)
+ CBOR.decode(data)
+ rescue CBOR::MalformedFormatError, EOFError, FloatDomainError, RegexpError, TypeError, URI::InvalidURIError
+ raise COSE::MalformedKeyError, "Malformed CBOR key input"
end
end
end