Package com.github.lucadruda.iotc.device
Class SSLContextBuilder
java.lang.Object
com.github.lucadruda.iotc.device.SSLContextBuilder
Helper class that demonstrates how to build an SSLContext for x509
authentication from your public and private certificates, or how to build an
SSLContext for SAS authentication from the default IoT Hub public
certificates
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic SSLContext
Build the default SSLContext.static SSLContext
buildSSLContext(String publicKeyCertificateString, String privateKeyString)
Create an SSLContext instance with the provided public and private keys that also trusts the base iot hub certificatesstatic RSAPrivateKey
parsePrivateKeyString(String privateKeyPEM)
static X509Certificate[]
parsePublicKeyCertificateString(String pemString)
static RSAPrivateKey
readPrivateKeyPKCS1PEM(String content)
-
Constructor Details
-
SSLContextBuilder
public SSLContextBuilder()
-
-
Method Details
-
buildSSLContext
public static SSLContext buildSSLContext(String publicKeyCertificateString, String privateKeyString) throws GeneralSecurityException, IOExceptionCreate an SSLContext instance with the provided public and private keys that also trusts the base iot hub certificates- Parameters:
publicKeyCertificateString
- the public key to use for x509 authentication. Does not need to include the default Iot Hub trusted certificate as it will be added automaticallyprivateKeyString
- The private key to use for x509 authentication- Returns:
- The created SSLContext that uses the provided public key and private key
- Throws:
GeneralSecurityException
- If the certificate creation fails, or if the SSLContext creation using those certificates failsIOException
- If the certificates cannot be read
-
buildSSLContext
public static SSLContext buildSSLContext() throws NoSuchAlgorithmException, KeyManagementException, CertificateException, KeyStoreException, IOExceptionBuild the default SSLContext. Trusts the iot hub base certificates, but can only be used for sas auth- Returns:
- the default SSLContext
- Throws:
NoSuchAlgorithmException
- If the SSLContext cannot be created because of a missing algorithmKeyManagementException
- If the SSLContext cannot be initiatedCertificateException
- If certificate creation failsKeyStoreException
- If the keystore operations failIOException
- If the default certificate fails to be read
-
parsePrivateKeyString
public static RSAPrivateKey parsePrivateKeyString(String privateKeyPEM) throws IOException, GeneralSecurityException- Throws:
IOException
GeneralSecurityException
-
readPrivateKeyPKCS1PEM
public static RSAPrivateKey readPrivateKeyPKCS1PEM(String content) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException -
parsePublicKeyCertificateString
public static X509Certificate[] parsePublicKeyCertificateString(String pemString) throws GeneralSecurityException- Throws:
GeneralSecurityException
-