2020 */
2121public final class TlsContextOptions extends CrtResource {
2222
23+ /**
24+ * Identifies the source of the mTLS certificate/private key configured on a
25+ * {@link TlsContextOptions}. Used internally by the IoT Device SDK metrics
26+ * layer to encode feature "I" of the SDK metrics string. Values are set
27+ * automatically by the {@code createWithMtls*} factory methods (and their
28+ * {@code withMtls*} equivalents).
29+ */
30+ public enum CertificateSource {
31+ /** PEM cert + key files (in-memory or on-disk). */
32+ CERTIFICATE_FILES ,
33+ /** Hardware security module via PKCS#11. */
34+ PKCS11 ,
35+ /** Windows certificate store. */
36+ WINDOWS_CERT_STORE ,
37+ /** Java keystore. */
38+ JAVA_KEYSTORE ,
39+ /** PKCS#12 (.p12 / .pfx) file. */
40+ PKCS12_FILE ,
41+ }
42+
2343 public enum TlsVersions {
2444 /**
2545 * SSL v3. This should almost never be used.
@@ -90,7 +110,7 @@ public enum TlsVersions {
90110 * On Linux (s2n), this disables validation of OCSP stapled responses provided by the server.
91111 *
92112 * On Apple platforms, this is a no-op as revocation checking is not enabled by default.
93- *
113+ *
94114 * Default is false (revocation checking enabled where available).
95115 */
96116 public boolean noCertificateRevocation = false ;
@@ -107,6 +127,7 @@ public enum TlsVersions {
107127 private TlsContextPkcs11Options pkcs11Options ;
108128 private TlsContextCustomKeyOperationOptions customKeyOperations ;
109129 private String windowsCertStorePath ;
130+ private CertificateSource certificateSource ;
110131
111132 /**
112133 * Creates a new set of options that can be used to create a {@link TlsContext}
@@ -187,6 +208,7 @@ public void setCipherPreference(TlsCipherPreference cipherPref) {
187208 public void initMtlsFromPath (String certificatePath , String privateKeyPath ) {
188209 this .certificatePath = certificatePath ;
189210 this .privateKeyPath = privateKeyPath ;
211+ this .certificateSource = CertificateSource .CERTIFICATE_FILES ;
190212 }
191213
192214 /**
@@ -203,6 +225,7 @@ public void initMtls(String certificate, String privateKey) throws IllegalArgume
203225
204226 this .privateKey = PemUtils .cleanUpPem (privateKey );
205227 PemUtils .sanityCheck (privateKey , 1 , "PRIVATE KEY" );
228+ this .certificateSource = CertificateSource .CERTIFICATE_FILES ;
206229 }
207230
208231 /**
@@ -218,6 +241,7 @@ public void initMtlsPkcs12(String pkcs12Path, String pkcs12Password) {
218241 }
219242 this .pkcs12Path = pkcs12Path ;
220243 this .pkcs12Password = pkcs12Password ;
244+ this .certificateSource = CertificateSource .PKCS12_FILE ;
221245 }
222246
223247 /**
@@ -414,6 +438,7 @@ public static TlsContextOptions createWithMtlsJavaKeystore(
414438 }
415439 options .initMtls (certificate , privateKey );
416440 options .verifyPeer = true ;
441+ options .certificateSource = CertificateSource .JAVA_KEYSTORE ;
417442 return options ;
418443 }
419444
@@ -517,6 +542,7 @@ public TlsContextOptions withMtlsPkcs12(String pkcs12Path, String pkcs12Password
517542 public TlsContextOptions withMtlsPkcs11 (TlsContextPkcs11Options pkcs11Options ) {
518543 swapReferenceTo (this .pkcs11Options , pkcs11Options );
519544 this .pkcs11Options = pkcs11Options ;
545+ this .certificateSource = CertificateSource .PKCS11 ;
520546 return this ;
521547 }
522548
@@ -544,6 +570,7 @@ public TlsContextOptions withMtlsCustomKeyOperations(TlsContextCustomKeyOperatio
544570 */
545571 public TlsContextOptions withMtlsWindowsCertStorePath (String certificatePath ) {
546572 this .windowsCertStorePath = certificatePath ;
573+ this .certificateSource = CertificateSource .WINDOWS_CERT_STORE ;
547574 return this ;
548575 }
549576
@@ -577,6 +604,14 @@ public TlsContextOptions withNoCertificateRevocation() {
577604 return this ;
578605 }
579606
607+ /**
608+ * @return the {@link CertificateSource} of the configured mTLS, or {@code null}
609+ * if no mTLS source has been set (or the source has no defined metrics mapping).
610+ */
611+ CertificateSource getCertificateSource () {
612+ return certificateSource ;
613+ }
614+
580615 /*******************************************************************************
581616 * native methods
582617 ******************************************************************************/
0 commit comments