|
| 1 | +// mTLS PoP Manual Test — Path 2: Managed Identity (IMDSv2, Windows + VBS) |
| 2 | +// |
| 3 | +// Tests the managed identity mTLS PoP flow end-to-end on an Azure VM with: |
| 4 | +// - System-assigned or user-assigned managed identity |
| 5 | +// - Windows OS with VBS (Virtualization-Based Security) KeyGuard |
| 6 | +// - IMDSv2 endpoint accessible at 169.254.169.254 |
| 7 | +// |
| 8 | +// Usage (from the msal4j-mtls-extensions directory): |
| 9 | +// mvn package -DskipTests |
| 10 | +// mvn exec:java -Dexec.mainClass=com.microsoft.aad.msal4j.mtls.e2e.Path2ManagedIdentity |
| 11 | +// |
| 12 | +// Or with attestation: |
| 13 | +// mvn exec:java -Dexec.mainClass=com.microsoft.aad.msal4j.mtls.e2e.Path2ManagedIdentity -Dexec.args="--attest" |
| 14 | + |
| 15 | +package com.microsoft.aad.msal4j.mtls.e2e; |
| 16 | + |
| 17 | +import com.microsoft.aad.msal4j.mtls.MtlsMsiClient; |
| 18 | +import com.microsoft.aad.msal4j.mtls.MtlsMsiException; |
| 19 | +import com.microsoft.aad.msal4j.mtls.MtlsMsiHelperResult; |
| 20 | +import com.microsoft.aad.msal4j.mtls.MtlsMsiHttpResponse; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.security.cert.CertificateFactory; |
| 24 | +import java.security.cert.X509Certificate; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Base64; |
| 27 | +import java.util.UUID; |
| 28 | + |
| 29 | +/** |
| 30 | + * End-to-end test for mTLS PoP Managed Identity (Path 2). |
| 31 | + * |
| 32 | + * <p>Mirrors msal-go's {@code apps/tests/devapps/mtls-pop/path2_managedidentity/main.go}.</p> |
| 33 | + */ |
| 34 | +public class Path2ManagedIdentity { |
| 35 | + |
| 36 | + private static final String RESOURCE = "https://management.azure.com"; |
| 37 | + |
| 38 | + public static void main(String[] args) throws Exception { |
| 39 | + boolean withAttestation = Arrays.asList(args).contains("--attest"); |
| 40 | + |
| 41 | + System.out.println("=== Path 2: Managed Identity mTLS PoP ==="); |
| 42 | + System.out.println(); |
| 43 | + if (withAttestation) { |
| 44 | + System.out.println("[Attestation mode: ON — requires AttestationClientLib.dll on PATH]"); |
| 45 | + System.out.println(); |
| 46 | + } |
| 47 | + |
| 48 | + MtlsMsiClient client = new MtlsMsiClient(); |
| 49 | + String correlationId = UUID.randomUUID().toString(); |
| 50 | + |
| 51 | + // ── First call: full IMDS flow ────────────────────────────────────────── |
| 52 | + System.out.println("Acquiring mTLS PoP token via IMDSv2 (full flow)..."); |
| 53 | + MtlsMsiHelperResult result1; |
| 54 | + try { |
| 55 | + result1 = client.acquireToken(RESOURCE, "SystemAssigned", null, |
| 56 | + withAttestation, correlationId); |
| 57 | + } catch (MtlsMsiException e) { |
| 58 | + System.out.println(); |
| 59 | + System.err.println("❌ acquireToken failed: " + e.getMessage()); |
| 60 | + System.err.println(); |
| 61 | + // Check for tenant/resource misconfiguration (not a code bug) |
| 62 | + if (e.getMessage() != null && e.getMessage().contains("AADSTS392196")) { |
| 63 | + System.err.println("ℹ️ AADSTS392196: The resource application does not support certificate-bound tokens."); |
| 64 | + System.err.println(" This is a tenant/resource configuration issue (same as MSAL.NET on this VM)."); |
| 65 | + System.err.println(" The mTLS handshake succeeded — the code is working correctly."); |
| 66 | + System.err.println(" To fully test, use a tenant where mTLS PoP is enabled for management.azure.com."); |
| 67 | + } else { |
| 68 | + System.err.println("Common causes:"); |
| 69 | + System.err.println(" - VBS/KeyGuard not running (check msinfo32.exe)"); |
| 70 | + System.err.println(" - IMDSv2 not returning platform metadata"); |
| 71 | + System.err.println(" - VM managed identity not configured"); |
| 72 | + System.err.println(" - 403 from IMDS issuecredential endpoint"); |
| 73 | + System.err.println(" - Tenant not configured for mTLS PoP (AADSTS392196)"); |
| 74 | + } |
| 75 | + System.exit(1); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + System.out.println(); |
| 80 | + printResult("First call (from IMDS)", result1); |
| 81 | + |
| 82 | + // ── Second call: should hit cached binding cert ───────────────────────── |
| 83 | + System.out.println(); |
| 84 | + System.out.println("Acquiring again (expect cert cache hit)..."); |
| 85 | + long t0 = System.currentTimeMillis(); |
| 86 | + MtlsMsiHelperResult result2; |
| 87 | + try { |
| 88 | + result2 = client.acquireToken(RESOURCE, "SystemAssigned", null, |
| 89 | + withAttestation, UUID.randomUUID().toString()); |
| 90 | + } catch (MtlsMsiException e) { |
| 91 | + System.err.println("❌ Second acquireToken failed: " + e.getMessage()); |
| 92 | + System.exit(1); |
| 93 | + return; |
| 94 | + } |
| 95 | + long elapsedMs = System.currentTimeMillis() - t0; |
| 96 | + printResult("Second call (should be cert-cached, ~fast)", result2); |
| 97 | + System.out.printf(" ⏱ Elapsed: %d ms%n", elapsedMs); |
| 98 | + |
| 99 | + // Cert cache check: same cert PEM implies cert was cached. |
| 100 | + if (result1.getBindingCertificate() != null |
| 101 | + && result1.getBindingCertificate().equals(result2.getBindingCertificate())) { |
| 102 | + System.out.println(" ✅ Binding cert cache working: same cert on second call"); |
| 103 | + } else { |
| 104 | + System.out.println(" ⚠️ Different binding cert on second call — may indicate cache miss or cert was expiring"); |
| 105 | + } |
| 106 | + |
| 107 | + // ── Third call: Graph /me to verify token actually works ──────────────── |
| 108 | + System.out.println(); |
| 109 | + System.out.println("Making downstream mTLS call to management.azure.com..."); |
| 110 | + makeDownstreamCall(client, result1, withAttestation); |
| 111 | + |
| 112 | + System.out.println(); |
| 113 | + System.out.println("=== Path 2 Complete ==="); |
| 114 | + } |
| 115 | + |
| 116 | + // ── Downstream mTLS call ────────────────────────────────────────────────── |
| 117 | + |
| 118 | + private static void makeDownstreamCall(MtlsMsiClient client, MtlsMsiHelperResult result, |
| 119 | + boolean withAttestation) { |
| 120 | + // management.azure.com /subscriptions — any auth error is still a TLS success. |
| 121 | + String url = "https://management.azure.com/subscriptions?api-version=2020-01-01"; |
| 122 | + try { |
| 123 | + MtlsMsiHttpResponse resp = client.httpRequest( |
| 124 | + url, "GET", result.getAccessToken(), |
| 125 | + null, null, null, |
| 126 | + RESOURCE, "SystemAssigned", null, |
| 127 | + withAttestation, UUID.randomUUID().toString(), |
| 128 | + false); |
| 129 | + |
| 130 | + System.out.printf(" Downstream HTTP status: %d%n", resp.getStatus()); |
| 131 | + if (resp.getStatus() < 500) { |
| 132 | + System.out.println(" ✅ TLS handshake + token delivery succeeded (HTTP < 500)"); |
| 133 | + } else { |
| 134 | + System.out.println(" ❌ Server error — check token and resource enrollment"); |
| 135 | + } |
| 136 | + if (resp.getStatus() == 200) { |
| 137 | + System.out.println(" ✅ HTTP 200 — full mTLS PoP token accepted by management.azure.com"); |
| 138 | + } else if (resp.getStatus() == 401 || resp.getStatus() == 403) { |
| 139 | + System.out.println(" ℹ️ " + resp.getStatus() + " — TLS OK, authorization depends on subscription/role"); |
| 140 | + } |
| 141 | + } catch (MtlsMsiException e) { |
| 142 | + System.out.println(" ❌ Downstream mTLS call failed: " + e.getMessage()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // ── Print helpers ───────────────────────────────────────────────────────── |
| 147 | + |
| 148 | + private static void printResult(String label, MtlsMsiHelperResult result) { |
| 149 | + System.out.println("[" + label + "]"); |
| 150 | + |
| 151 | + // Print binding cert details. |
| 152 | + if (result.getBindingCertificate() != null) { |
| 153 | + System.out.println(" ✅ BindingCertificate present"); |
| 154 | + try { |
| 155 | + X509Certificate cert = parsePem(result.getBindingCertificate()); |
| 156 | + System.out.println(" Subject: " + cert.getSubjectX500Principal().getName()); |
| 157 | + System.out.println(" Issuer: " + cert.getIssuerX500Principal().getName()); |
| 158 | + System.out.println(" NotBefore: " + cert.getNotBefore()); |
| 159 | + System.out.println(" NotAfter: " + cert.getNotAfter()); |
| 160 | + } catch (Exception e) { |
| 161 | + System.out.println(" (could not parse cert: " + e.getMessage() + ")"); |
| 162 | + } |
| 163 | + } else { |
| 164 | + System.out.println(" ❌ BindingCertificate is null — expected non-null for mTLS PoP"); |
| 165 | + } |
| 166 | + |
| 167 | + System.out.println(" TokenType: " + result.getTokenType()); |
| 168 | + System.out.println(" ExpiresIn: " + result.getExpiresIn() + "s"); |
| 169 | + System.out.println(" TenantId: " + result.getTenantId()); |
| 170 | + System.out.println(" ClientId: " + result.getClientId()); |
| 171 | + |
| 172 | + // Print abbreviated JWT header/claims. |
| 173 | + printTokenSummary(result.getAccessToken()); |
| 174 | + } |
| 175 | + |
| 176 | + private static void printTokenSummary(String jwt) { |
| 177 | + if (jwt == null || jwt.isEmpty()) { |
| 178 | + System.out.println(" ❌ AccessToken is null/empty"); |
| 179 | + return; |
| 180 | + } |
| 181 | + String[] parts = jwt.split("\\."); |
| 182 | + if (parts.length < 2) { |
| 183 | + System.out.println(" AccessToken: (not a JWT — " + jwt.length() + " chars)"); |
| 184 | + return; |
| 185 | + } |
| 186 | + try { |
| 187 | + String header = new String(Base64.getUrlDecoder().decode(pad(parts[0]))); |
| 188 | + String payload = new String(Base64.getUrlDecoder().decode(pad(parts[1]))); |
| 189 | + System.out.println(" AccessToken header: " + header); |
| 190 | + // Print only key claims to keep output readable. |
| 191 | + printClaim(payload, "oid"); |
| 192 | + printClaim(payload, "tid"); |
| 193 | + printClaim(payload, "token_type"); |
| 194 | + printClaim(payload, "cnf"); |
| 195 | + long expEpoch = extractLong(payload, "exp"); |
| 196 | + if (expEpoch > 0) { |
| 197 | + System.out.println(" AccessToken exp: " |
| 198 | + + new java.util.Date(expEpoch * 1000)); |
| 199 | + } |
| 200 | + System.out.println(" ✅ AccessToken present (" + jwt.length() + " chars)"); |
| 201 | + } catch (Exception e) { |
| 202 | + System.out.println(" AccessToken: (could not decode JWT: " + e.getMessage() + ")"); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private static void printClaim(String payload, String key) { |
| 207 | + String val = extractString(payload, key); |
| 208 | + if (val != null) { |
| 209 | + // Truncate long values (e.g. cnf object). |
| 210 | + if (val.length() > 120) val = val.substring(0, 120) + "..."; |
| 211 | + System.out.println(" AccessToken " + key + ": " + val); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private static String extractString(String json, String key) { |
| 216 | + String search = "\"" + key + "\""; |
| 217 | + int idx = json.indexOf(search); |
| 218 | + if (idx < 0) return null; |
| 219 | + int colon = json.indexOf(':', idx + search.length()); |
| 220 | + if (colon < 0) return null; |
| 221 | + int vs = colon + 1; |
| 222 | + while (vs < json.length() && Character.isWhitespace(json.charAt(vs))) vs++; |
| 223 | + if (vs >= json.length()) return null; |
| 224 | + char first = json.charAt(vs); |
| 225 | + if (first == '"') { |
| 226 | + int end = vs + 1; |
| 227 | + while (end < json.length() && json.charAt(end) != '"') end++; |
| 228 | + return json.substring(vs + 1, end); |
| 229 | + } else if (first == '{' || first == '[') { |
| 230 | + // Return the whole nested object/array. |
| 231 | + char close = first == '{' ? '}' : ']'; |
| 232 | + int depth = 0, end = vs; |
| 233 | + while (end < json.length()) { |
| 234 | + char c = json.charAt(end); |
| 235 | + if (c == first) depth++; |
| 236 | + else if (c == close) { if (--depth == 0) { end++; break; } } |
| 237 | + end++; |
| 238 | + } |
| 239 | + return json.substring(vs, end); |
| 240 | + } |
| 241 | + return null; |
| 242 | + } |
| 243 | + |
| 244 | + private static long extractLong(String json, String key) { |
| 245 | + String search = "\"" + key + "\""; |
| 246 | + int idx = json.indexOf(search); |
| 247 | + if (idx < 0) return 0; |
| 248 | + int colon = json.indexOf(':', idx + search.length()); |
| 249 | + if (colon < 0) return 0; |
| 250 | + int vs = colon + 1; |
| 251 | + while (vs < json.length() && Character.isWhitespace(json.charAt(vs))) vs++; |
| 252 | + int ve = vs; |
| 253 | + while (ve < json.length() && (Character.isDigit(json.charAt(ve)) || json.charAt(ve) == '-')) ve++; |
| 254 | + try { return Long.parseLong(json.substring(vs, ve)); } catch (Exception e) { return 0; } |
| 255 | + } |
| 256 | + |
| 257 | + private static String pad(String s) { |
| 258 | + return s + "==".substring(0, (4 - s.length() % 4) % 4); |
| 259 | + } |
| 260 | + |
| 261 | + private static X509Certificate parsePem(String pem) throws Exception { |
| 262 | + String b64 = pem |
| 263 | + .replace("-----BEGIN CERTIFICATE-----", "") |
| 264 | + .replace("-----END CERTIFICATE-----", "") |
| 265 | + .replaceAll("\\s", ""); |
| 266 | + byte[] der = Base64.getDecoder().decode(b64); |
| 267 | + return (X509Certificate) CertificateFactory.getInstance("X.509") |
| 268 | + .generateCertificate(new ByteArrayInputStream(der)); |
| 269 | + } |
| 270 | +} |
0 commit comments