Class PublicKeySources


  • public class PublicKeySources
    extends Object

    Static factory methods used to create instances of PublicKeySource.

    PublicKeySources created by the static factory methods of this class perform public key validation. They use the validation method described in Section 5.6.2.3 of Recommendation for Pair-Wise Key Establishment Schemes Using Discrete Logarithm Cryptography - NIST Special Publication 800-56A Revision 2.

    Examples:

    The following is an example of commands that uses OpenSSL to generate a file that can be handled with PublicKeySources.

     openssl ecparam -genkey -name prime256v1 -noout -out my-private.pem
     openssl ec -in my-private.pem -pubout -conv_form uncompressed -out my-pub.pem
     

    If you want to generate in DER format, you can also do as follows.

     openssl ec -in my-private.pem -pubout -conv_form uncompressed -outform der -out my-pub.der
     

    The examples of using these files to create PublicKeySources are as follows.

     Path pemPath = new File("my-pub.pem").toPath();
     Path derPath = new File("my-pub.der").toPath();
    
     PublicKeySource pemSource = PublicKeySources.ofPEMFile(pemPath);
     PublicKeySource derSource = PublicKeySources.ofDERFile(derPath);
    
     byte[] pemBytes = Files.readAllBytes(pemPath);
     String pemText = new String(pemBytes, StandardCharsets.UTF_8);
     PublicKeySource pemSource2 = PublicKeySources.ofPEMText(pemText);
    
     byte[] derBytes = Files.readAllBytes(derPath);
     PublicKeySource derSource2 = PublicKeySources.ofX509Bytes(derBytes);
    
     
    Thread Safety:

    Instances obtained through a factory method of this class are NOT thread-safe.

    Author:
    Tomoki Sato
    See Also:
    PublicKeySource, InvalidECPublicKeyException, PrivateKeySource, PrivateKeySources
    • Method Detail

      • ofUncompressedBytes

        public static PublicKeySource ofUncompressedBytes​(byte[] uncompressedBytes)
        Create a new PublicKeySource with the given octet sequence representing a public key on the P-256 curve encoded in the uncompressed form[X9.62].
        Parameters:
        uncompressedBytes - the octet sequence representing a public key.
        Returns:
        a new PublicKeySource.
        Throws:
        MalformedUncompressedBytesException - if the given octet sequence doesn't start with 0x04 or the length isn't 65 bytes.
      • ofX509Bytes

        public static PublicKeySource ofX509Bytes​(byte[] x509Bytes)
        Creates a new PublicKeySource with the given octet sequence that is assumed to be encoded according to the X.509 standard.
        Parameters:
        x509Bytes - the octet sequence representing a public key.
        Returns:
        a new PublicKeySource.
        See Also:
        X509EncodedKeySpec
      • ofPEMText

        public static PublicKeySource ofPEMText​(String pemText)

        Creates a new PublicKeySource with the given PEM-encoded text. The underlying octet sequence is assumed to be encoded according to the X.509 standard.

        The PEM-encoded text is assumed to contain a public key data that starts with '-----BEGIN PUBLIC KEY-----' and ends with '-----END PUBLIC KEY-----'.

        Parameters:
        pemText - the PEM-encoded text representing a public key.
        Returns:
        a new PublicKeySource.
        Throws:
        MalformedPEMException - if the given text cannot be parsed as a valid PEM format.
        See Also:
        X509EncodedKeySpec
      • ofPEMText

        public static PublicKeySource ofPEMText​(String pemText,
                                                PEMParser parser)

        Creates a new PublicKeySource with the given PEM-encoded text and the given PEMParser. The underlying octet sequence is assumed to be encoded according to the X.509 standard.

        The PEM-encoded text is parsed by the given PEMParser.

        Parameters:
        pemText - the PEM-encoded text representing a public key.
        parser - the parser used to parse the PEM-encoded text.
        Returns:
        a new PublicKeySource.
        Throws:
        MalformedPEMException - if the given text cannot be parsed as a valid PEM format.
        See Also:
        X509EncodedKeySpec
      • ofPEMFile

        public static PublicKeySource ofPEMFile​(Path path)
                                         throws IOException

        Creates a new PublicKeySource with the PEM formatted file specified by the given path. The underlying octet sequence is assumed to be encoded according to the X.509 standard.

        The PEM formatted file is assumed to contain a public key data that starts with '-----BEGIN PUBLIC KEY-----' and ends with '-----END PUBLIC KEY-----'.

        Parameters:
        path - the path to a PEM formatted file.
        Returns:
        a new PublicKeySource.
        Throws:
        IOException - if an I/O error occurs.
        MalformedPEMException - if the content of the given file cannot be parsed as a valid PEM format.
      • ofPEMFile

        public static PublicKeySource ofPEMFile​(Path path,
                                                PEMParser parser)
                                         throws IOException

        Creates a new PublicKeySource with the PEM formatted file specified by the given path. The underlying octet sequence is assumed to be encoded according to the X.509 standard.

        The content of the PEM file is parsed by the given PEMParser.

        Parameters:
        path - the path to a PEM formatted file.
        parser - the parser used to parse the content of the PEM file.
        Returns:
        a new PublicKeySource.
        Throws:
        IOException - if an I/O error occurs.
        MalformedPEMException - if the content of the given file cannot be parsed as a valid PEM format.
      • ofDERFile

        public static PublicKeySource ofDERFile​(Path path)
                                         throws IOException
        Creates a new PublicKeySource with the DER file specified by the given path. Its octet sequence is assumed to be encoded according to the X.509 standard.
        Parameters:
        path - the path to a DER file.
        Returns:
        a new PublicKeySource.
        Throws:
        IOException - if an I/O error occurs.