Class DSSJsonUtils

java.lang.Object
eu.europa.esig.dss.jades.DSSJsonUtils

public class DSSJsonUtils extends Object
Utility class for working with JSON objects
  • Field Details

    • MIME_TYPE_APPLICATION_PREFIX

      public static final String MIME_TYPE_APPLICATION_PREFIX
      The MimeType application prefix
      See Also:
    • HTTP_HEADER_DIGEST

      public static final String HTTP_HEADER_DIGEST
      The HttpHeader defining the Digest value of a signed message body
      See Also:
    • CONTENT_ENCODING_BINARY

      public static final String CONTENT_ENCODING_BINARY
      The binary content encoding (RFC 2045)
      See Also:
  • Method Details

    • getAsciiBytes

      public static byte[] getAsciiBytes(String str)
      Returns ASCII-encoded array
      Parameters:
      str - String to encode to ASCII
      Returns:
      byte array
    • toBase64Url

      public static String toBase64Url(byte[] binary)
      Returns a base64Url encoded string
      Parameters:
      binary - a byte array to encode
      Returns:
      base64Url encoded String
    • toBase64Url

      public static String toBase64Url(DSSDocument document)
      Returns a base64Url encoded string
      Parameters:
      document - DSSDocument to encode
      Returns:
      base64Url encoded String
    • toBase64Url

      public static String toBase64Url(Object object)
      Returns a base64Url encoded string from the provided JSON Object or JSON Array
      Parameters:
      object - JSON Object or JSON Array to encode
      Returns:
      base64Url encoded String
    • fromBase64Url

      public static byte[] fromBase64Url(String base64UrlEncoded)
      Returns the decoded binary for a base64url encoded string
      Parameters:
      base64UrlEncoded - the String to be decoded
      Returns:
      the decoded binary
    • isBase64UrlEncoded

      public static boolean isBase64UrlEncoded(String str)
      Checks if the provided string is base64Url encoded
      Parameters:
      str - String to check
      Returns:
      TRUE if the String is base64Url encoded, FALSE otherwise
    • isBase64UrlEncoded

      public static boolean isBase64UrlEncoded(byte b)
      Checks if the byte is Base64Url encoded
      Parameters:
      b - a byte to check
      Returns:
      TRUE if the byte is Base64Url encoded, FALSE otherwise
    • isUrlSafePayload

      public static boolean isUrlSafePayload(String payloadString)
      Checks if the payload is JWS URL safe See RFC 7797 : 5.2. Unencoded JWS Compact Serialization Payload
      Parameters:
      payloadString - String representing a payload
      Returns:
      TRUE if the payload is URL safe, FALSE otherwise
    • isUrlSafe

      public static boolean isUrlSafe(byte b)
      Checks if the given byte is url safe See RFC 7797 : 5.2. Unencoded JWS Compact Serialization Payload
      Parameters:
      b - a byte to check
      Returns:
      TRUE if the byte is url-safe, FALSE otherwise
    • isUtf8

      public static boolean isUtf8(byte[] binaries)
      Checks if the binaries contain a UTF-8 encoded string
      Parameters:
      binaries - byte array to check
      Returns:
      TRUE if binaries contain a UTF-8 encoded string, FALSE otherwise
    • concatenate

      public static String concatenate(String... strings)
      Concatenates the given strings with a '.' (dot) between. Example: "xxx", "yyy", "zzz" to "xxx.yyy.zzz"
      Parameters:
      strings - a list of Strings to concatenate
      Returns:
      a concatenation string result
    • getSupportedProtectedCriticalHeaders

      public static Set<String> getSupportedProtectedCriticalHeaders()
      Returns set of supported protected critical headers
      Returns:
      set of supported protected critical header strings
    • isCriticalHeaderException

      public static boolean isCriticalHeaderException(String headerName)
      Checks if the given headerName is a critical header exception (shall not be incorporated within 'crit' header). See RFC 7515
      Parameters:
      headerName - String header name to check
      Returns:
      TRUE if the header is critical header exception, FALSE otherwise
    • getDigest

      public static Digest getDigest(Map<?,?> digestValueAndAlgo)
      Creates a Digest object from a JSON structure
      Parameters:
      digestValueAndAlgo - a Map with digAlg and digVal values
      Returns:
      an instance of Digest or null
    • getOidObject

      public static JsonObject getOidObject(ObjectIdentifier objectIdentifier)
      Creates an 'oid' LinkedJSONObject according to EN 119-182 ch. 5.4.1 The oId data type
      Parameters:
      objectIdentifier - ObjectIdentifier to create an 'oid' from
      Returns:
      'oid' JsonObject
    • getUriOrUrnOid

      public static String getUriOrUrnOid(ObjectIdentifier objectIdentifier)
      Returns URI if present, otherwise URN encoded OID (see RFC 3061) Returns NULL if non of them is present
      Parameters:
      objectIdentifier - ObjectIdentifier used to build an object of 'oid' type
      Returns:
      String URI
    • getOidObject

      public static JsonObject getOidObject(String uri, String desc, String[] docRefs)
      Creates an 'oid' JsonObject according to EN 119-182 ch. 5.4.1 The oId data type
      Parameters:
      uri - String URI defining the object. The property is REQUIRED.
      desc - String the object description. The property is OPTIONAL.
      docRefs - an array of String URIs containing any other additional information about the object. The property is OPTIONAL.
      Returns:
      'oid' JsonObject
    • getTstContainer

      public static JsonObject getTstContainer(List<TimestampBinary> timestampBinaries, String canonicalizationMethodUri)
      Creates a 'tstContainer' JsonObject according to EN 119-182 ch. 5.4.3.3 The tstContainer type
      Parameters:
      timestampBinaries - a list of TimestampBinarys to incorporate
      canonicalizationMethodUri - a canonicalization method (OPTIONAL, e.g. shall not be present for content timestamps)
      Returns:
      'tstContainer' JsonObject
    • concatenateDSSDocuments

      public static byte[] concatenateDSSDocuments(List<DSSDocument> documents, boolean isBase64UrlEncoded)
      Concatenates document octets to a single byte array
      Parameters:
      documents - a list of DSSDocuments to concatenate
      isBase64UrlEncoded - defines whether the document octets shall be base64url-encoded
      Returns:
      a byte array of document octets
    • writeDocumentsDigest

      public static void writeDocumentsDigest(List<DSSDocument> documents, boolean isBase64UrlEncoded, DSSMessageDigestCalculator digestCalculator)
      Writes digest on a concatenated binaries of provided documents
      Parameters:
      documents - list of DSSDocuments to be concatenated
      isBase64UrlEncoded - defines whether the document octets shall be base64url-encoded
      digestCalculator - DSSMessageDigestCalculator to compute message-digest with
    • getDocumentOctets

      public static byte[] getDocumentOctets(DSSDocument document, boolean isBase64UrlEncoded)
      This method returns binaries of the document to be used for payload computation, depending on the isBase64UrlEncoded parameter. When isBase64UrlEncoded is set to TRUE, returns base64url-encoded binaries of the document. When isBase64UrlEncoded is set to FALSE, returns original octets of the document.
      Parameters:
      document - DSSDocument to get octets from
      isBase64UrlEncoded - defines whether return base64url-encoded octets
      Returns:
      octets of the provided DSSDocument
    • isJsonDocument

      public static boolean isJsonDocument(DSSDocument document)
      Checks if the provided document is JSON document
      Parameters:
      document - DSSDocument to check
      Returns:
      TRUE of the document is JSON, FALSE otherwise
    • isAllowedSignatureDocumentType

      public static boolean isAllowedSignatureDocumentType(DSSDocument document)
      Checks if the signature document has an allowed type (bytes can be extracted)
      Parameters:
      document - DSSDocument to check
      Returns:
      TRUE if the document is of an allowed signature type, FALSE otherwise
    • getEtsiU

      public static List<Object> getEtsiU(JWS jws)
      This method returns the etsiU container with the unsigned properties or an empty List
      Parameters:
      jws - the signature
      Returns:
      etsiU content or an empty List
    • getUnsignedPropertiesWithHeaderName

      public static List<EtsiUComponent> getUnsignedPropertiesWithHeaderName(JAdESEtsiUHeader etsiUHeader, String headerName)
      Returns a list of unsigned 'etsiU' properties matching the headerName from the jws
      Parameters:
      etsiUHeader - JAdESEtsiUHeader to extract values from
      headerName - String name of the unsigned header
      Returns:
      a list of EtsiUComponents
    • getDate

      public static Date getDate(String dateTimeString)
      Parses a IETF RFC 3339 dateTime String
      Parameters:
      dateTimeString - String in the RFC 3339 format to parse
      Returns:
      Date
    • getIssuerSerial

      public static org.bouncycastle.asn1.x509.IssuerSerial getIssuerSerial(String value)
      Parses the 'kid' header value as in IETF RFC 5035
      Parameters:
      value - String IssuerSerial to parse
      Returns:
      IssuerSerial
    • generateKid

      public static String generateKid(CertificateToken signingCertificate)
      Generates the 'kid' value as in IETF RFC 5035
      Parameters:
      signingCertificate - CertificateToken representing the singing certificate
      Returns:
      String 'kid' header value
    • extractJAdESCounterSignature

      public static JAdESSignature extractJAdESCounterSignature(EtsiUComponent cSigAttribute, JAdESSignature masterSignature)
      Extracts a counter signature from 'cSig' value with respect to the found format
      Parameters:
      cSigAttribute - an attribute containing the 'cSig' element
      masterSignature - JAdESSignature the master signature
      Returns:
      JAdESSignature
    • validateAgainstJAdESSchema

      public static List<String> validateAgainstJAdESSchema(JWS jws)
      Validates JWS against a JAdES schema (ETSI TS 119 182-1)
      Parameters:
      jws - JWS to validate
      Returns:
      a list of Strings containing validation errors, empty list if no error occurred
    • checkComponentsUnicity

      public static boolean checkComponentsUnicity(List<Object> components)
      Checks if all components have one type (strings or objects)
      Parameters:
      components - a list of objects to check
      Returns:
      TRUE if all components are uniform (strings or objects), FALSE otherwise
    • isStringFormat

      public static boolean isStringFormat(Object object)
      Checks of the object is an instance of a String class
      Parameters:
      object - to check
      Returns:
      TRUE if the object is an instance of String class, FALSE otherwise
    • areAllBase64UrlComponents

      public static boolean areAllBase64UrlComponents(List<Object> components)
      Checks if the all components are base64Url encoded
      Parameters:
      components - a list of components to check
      Returns:
      TRUE if all of the components are base64Url encoded, FALSE otherwise
    • parseEtsiUComponent

      public static Map<String,Object> parseEtsiUComponent(Object etsiUComponent)
      Parses 'etsiU' component as it is (base64url-encoded or JSON), and returns the resulting Map
      Parameters:
      etsiUComponent - object to parse (base64url-encoded or JSON)
      Returns:
      map representing the object
    • parseSPDocSpecification

      public static SpDocSpecification parseSPDocSpecification(Object spDocSpecificationObject)
      This method builds SpDocSpecification from the provided JSON object element
      Parameters:
      spDocSpecificationObject - Object json object
      Returns:
      SpDocSpecification
    • toJWSJsonSerializationObject

      public static JWSJsonSerializationObject toJWSJsonSerializationObject(JWS jws)
      Converts the JWS to JWSJsonSerializationObject
      Parameters:
      jws - JWS to convert
      Returns:
      JWSJsonSerializationObject
    • getSigningInputBytes

      public static byte[] getSigningInputBytes(JWS jws)
      This method computes the signing input bytes for a JWS signature
      Parameters:
      jws - JWS to get signing input for
      Returns:
      signing input bytes
    • extractJOSEHeaderMembersSet

      public static Set<String> extractJOSEHeaderMembersSet(JWS jws)
      This method extracts a key set used within a JOSE Header (protected + unprotected)
      Parameters:
      jws - JWS to extract keys from
      Returns:
      a set of String keys
    • getAsBoolean

      public static Boolean getAsBoolean(Map<?,?> map, String key)
      Gets a value from the map under the given key as Boolean
      Parameters:
      map - Map to extract the value from
      key - String key
      Returns:
      Boolean value when found, null otherwise
    • toBoolean

      public static Boolean toBoolean(Object object, String headerName)
      Method safely converts Object to Boolean if possible. The method also provides a user-friendly message explaining the origin of the unexpected variable.
      Parameters:
      object - Object to convert
      headerName - String name of the header attribute with the extracted value
      Returns:
      Boolean if able to convert, null value otherwise
    • getAsString

      public static String getAsString(Map<?,?> map, String key)
      Gets a value from the map under the given key as String
      Parameters:
      map - Map to extract the value from
      key - String key
      Returns:
      String value when found, empty string otherwise
    • toString

      public static String toString(Object object)
      Method safely converts Object to String if possible
      Parameters:
      object - Object to convert
      Returns:
      String if able to convert, empty string otherwise
    • toString

      public static String toString(Object object, String headerName)
      Method safely converts Object to String if possible. The method also provides a user-friendly message explaining the origin of the unexpected variable.
      Parameters:
      object - Object to convert
      headerName - String name of the header attribute with the extracted value
      Returns:
      String if able to convert, empty string otherwise
    • getAsMap

      public static Map<?,?> getAsMap(Map<?,?> map, String key)
      Gets a value from the map under the given key as Map
      Parameters:
      map - Map to extract the value from
      key - String key
      Returns:
      Map value when found, empty map otherwise
    • toMap

      public static Map<?,?> toMap(Object object)
      Method safely converts Object to Map if possible.
      Parameters:
      object - Object to convert
      Returns:
      Map if able to convert, empty map otherwise
    • toMap

      public static Map<?,?> toMap(Object object, String headerName)
      Method safely converts Object to Map if possible. The method also provides a user-friendly message explaining the origin of the unexpected variable.
      Parameters:
      object - Object to convert
      headerName - String name of the header attribute with the extracted value
      Returns:
      Map if able to convert, empty map otherwise
    • getAsList

      public static List<?> getAsList(Map<?,?> map, String key)
      Gets a value from the map under the given key as List
      Parameters:
      map - Map to extract the value from
      key - String key
      Returns:
      List value when found, empty list otherwise
    • toList

      public static List<?> toList(Object object)
      Method safely converts Object to List if possible.
      Parameters:
      object - Object to convert
      Returns:
      List if able to convert, empty map otherwise
    • toList

      public static List<?> toList(Object object, String headerName)
      Method safely converts Object to List if possible. The method also provides a user-friendly message explaining the origin of the unexpected variable.
      Parameters:
      object - Object to convert
      headerName - String name of the header attribute with the extracted value
      Returns:
      List if able to convert, empty map otherwise
    • toListOfStrings

      public static List<String> toListOfStrings(List<?> list)
      Converts a list of objects to a list of Strings
      Parameters:
      list - a list of Objects
      Returns:
      list of Strings
    • toListOfNumbers

      public static List<Number> toListOfNumbers(List<?> list)
      Converts a list of objects to a list of Numbers
      Parameters:
      list - a list of Objects
      Returns:
      list of Numbers