Package COSE

Class ASN1


  • public class ASN1
    extends Object
    Author:
    Jim
    • Field Detail

      • Oid_secp256r1

        public static final byte[] Oid_secp256r1
      • Oid_secp384r1

        public static final byte[] Oid_secp384r1
      • Oid_secp521r1

        public static final byte[] Oid_secp521r1
      • oid_ecPublicKey

        public static final byte[] oid_ecPublicKey
      • Oid_X25519

        public static final byte[] Oid_X25519
      • Oid_X448

        public static final byte[] Oid_X448
      • Oid_Ed25519

        public static final byte[] Oid_Ed25519
      • Oid_Ed448

        public static final byte[] Oid_Ed448
      • Oid_rsaEncryption

        public static final byte[] Oid_rsaEncryption
    • Constructor Detail

      • ASN1

        public ASN1()
    • Method Detail

      • EncodeSubjectPublicKeyInfo

        public static byte[] EncodeSubjectPublicKeyInfo​(byte[] algorithm,
                                                        byte[] keyBytes)
                                                 throws CoseException
        Encode a subject public key info structure from an OID and the data bytes for the key This function assumes that we are encoding an EC Public key.d
        Parameters:
        algorithm - - encoded Object Identifier
        keyBytes - - encoded key bytes
        Returns:
        - encoded SPKI
        Throws:
        CoseException - - ASN encoding error.
      • EncodeEcPrivateKey

        public static byte[] EncodeEcPrivateKey​(byte[] oid,
                                                byte[] keyBytes,
                                                byte[] spki)
                                         throws CoseException
        Encode an EC Private key
        Parameters:
        oid - - curve to use
        keyBytes - - bytes of the key
        spki - - optional SPKI
        Returns:
        encoded private key
        Throws:
        CoseException - - from lower level
      • DecodeCompound

        public static ASN1.TagValue DecodeCompound​(int offset,
                                                   byte[] encoding)
                                            throws CoseException
        Decode an array of bytes which is supposed to be an ASN.1 encoded structure. This code does the decoding w/o any reference to a schema for what is being decoded so it returns type and value pairs rather than converting the values to the correct underlying data type. One oddity that needs to be observed is that Object Identifiers do not have the type and length removed from them. This is because we do a byte wise comparison and started doing the entire item rather than just the value portion. M00BUG - we should check that we don't overflow during the decoding process.
        Parameters:
        offset - - starting offset in array to begin decoding
        encoding - - bytes of the ASN.1 encoded value
        Returns:
        Decoded structure
        Throws:
        CoseException - - ASN.1 encoding errors
      • EncodePKCS8

        public static byte[] EncodePKCS8​(byte[] algorithm,
                                         byte[] keyBytes,
                                         byte[] spki)
                                  throws CoseException
        Encode a private key into a PKCS#8 private key structure.
        Parameters:
        algorithm - - EC curve OID
        keyBytes - - raw bytes of the key
        spki - - optional subject public key info structure to include
        Returns:
        byte array of encoded bytes
        Throws:
        CoseException - - ASN.1 encoding errors
      • DecodePKCS8Structure

        public static ArrayList<ASN1.TagValue> DecodePKCS8Structure​(byte[] encodedData)
                                                             throws CoseException
        Decode a PKCS#8 private key structure, leaving the private key as an octetstring.
        Parameters:
        encodedData - bytes containing the private key
        Returns:
        tag/value from the decoded object
        Throws:
        CoseException - - ASN.1 encoding errors
      • AlgorithmIdentifier

        public static byte[] AlgorithmIdentifier​(byte[] oid,
                                                 byte[] params)
                                          throws CoseException
        Throws:
        CoseException