Class COSEKey

Direct Known Subclasses:
COSEEC2Key, COSEOKPKey

public class COSEKey extends CBORPairList
COSE Key.

Subclasses should override the following methods.

See Also:
  • Constructor Details

    • COSEKey

      public COSEKey(List<? extends CBORPair> pairs)
      A constructor with key parameters.
      Parameters:
      pairs - Key parameters.
  • Method Details

    • getParameters

      public Map<Object,Object> getParameters()
      Get the set of parameters of this key as a Map.
      Returns:
      The set of parameters of this key.
    • getKty

      public Object getKty()
      Get the value of the "kty (1)" parameter.

      The type of the value is an integer (int, long or BigInteger) or a string (String).

      Returns:
      The value of the "kty (1)" parameter.
      See Also:
    • getKid

      public byte[] getKid()
      Get the value of the "kid (2)" parameter.
      Returns:
      The value of the "kid (2)" parameter. If the parameter is not contained, null is returned.
    • getAlg

      public Object getAlg()
      Get the value of the "alg (3)" parameter.

      The type of the value is an integer (int, long or BigInteger) or a string (String).

      Returns:
      The value of the "alg (3)" parameter. If the parameter is not contained, null is returned.
      See Also:
    • getKeyOps

      public List<Object> getKeyOps()
      Get the value of the "key_ops (4)" parameter.

      The type of elements in the returned list is an integer (int, long or BigInteger) or a string (String).

      Returns:
      The value of the "key_ops (4)" parameter. If the parameter is not contained, null is returned.
    • getBaseIv

      public byte[] getBaseIv()
      Get the value of the "Base IV (5)" parameter.
      Returns:
      The value of the "Base IV (5)" parameter. If the parameter is not contained, null is returned.
    • isPrivate

      public boolean isPrivate()
      Get the flag indicating whether this key contains private parameters.

      Subclasses of the COSEKey class are expected to override this method.

      Returns:
      true if this key contains private parameters.
      Since:
      1.3
    • toPublic

      public COSEKey toPublic() throws COSEException
      Convert this COSE key to a public key.

      When this COSEKey instance is a public key, this method returns this object without any modification. Otherwise, this method creates a new COSEKey instance representing a public key and returns the new instance.

      The default implementation of this method throws an exception. Subclasses should override this method.

      Returns:
      A COSEKey instance representing a public key.
      Throws:
      COSEException
      Since:
      1.15
    • createPrivateKey

      public PrivateKey createPrivateKey() throws COSEException
      Create a PrivateKey instance from this COSE key.

      The default implementation of this method throws an exception. Subclasses should override this method.

      Returns:
      A PrivateKey instance.
      Throws:
      COSEException - The creation failed.
      Since:
      1.15
    • createPublicKey

      public PublicKey createPublicKey() throws COSEException
      Create a PublicKey instance from this COSE key.

      The default implementation of this method throws an exception. Subclasses should override this method.

      Returns:
      A PublicKey instance.
      Throws:
      COSEException - The creation failed.
      Since:
      1.15
    • copy

      public COSEKey copy() throws COSEException
      Copy this COSEKey instance.
      Returns:
      A new COSEKey instance that holds the same content as this COSEKey instance does.
      Throws:
      COSEException - Copying this instance failed.
      Since:
      1.15
    • toJwk

      public Map<String,Object> toJwk()
      Convert this key to a Map instance that represents a JWK (RFC 7517 JSON Web Key (JWK)).

      Subclasses of the COSEKey class should override the addJwkProperties(Map) method to add JWK properties that are specific to them.

      Returns:
      A Map instance that represents a JWK.
      Since:
      1.3
      See Also:
    • addJwkProperties

      protected void addJwkProperties(Map<String,Object> map)
      Add JWK properties to the given map.

      This method is called from within the toJwk() method. Subclasses of the COSEKey class should override this method.

      Parameters:
      map - A map to which JWK properties should be added.
      Since:
      1.3
    • build

      public static COSEKey build(CBORItem item) throws COSEException
      Build an instance of COSEKey or its subclass when possible (e.g. COSEOKPKey and COSEEC2Key) from the given CBOR data item.
      Parameters:
      item - A CBOR data item that represents a COSE key. It must be a CBOR map.
      Returns:
      An instance of COSEKey or its subclass.
      Throws:
      COSEException
    • build

      public static COSEKey build(Map<Object,Object> map) throws COSEException
      Build an instance of COSEKey or its subclass when possible (e.g. COSEOKPKey and COSEEC2Key) from the given map.
      Parameters:
      map - A map that represents a COSE key.
      Returns:
      An instance of COSEKey or its subclass.
      Throws:
      COSEException
    • fromJwk

      public static COSEKey fromJwk(Map<String,Object> jwk) throws COSEException
      Build a COSEKey instance from a map that represents a JWK (JSON Web Key).
      Parameters:
      jwk - A map that represents a JWK.
      Returns:
      An instance of COSEKey or its subclass such as COSEEC2Key.
      Throws:
      COSEException - Failed to create a COSEKey instance of the input map.
      Since:
      1.11