@@ -1097,34 +1097,7 @@ private MetadataBLOB verifyBlob(ParseResult parseResult, X509Certificate trustRo
10971097 InvalidAlgorithmParameterException ,
10981098 FidoMetadataDownloaderException {
10991099 final MetadataBLOBHeader header = parseResult .blob .getHeader ();
1100-
1101- final List <X509Certificate > certChain ;
1102- if (header .getX5u ().isPresent ()) {
1103- final URL x5u = header .getX5u ().get ();
1104- if (blobUrl != null
1105- && (!(x5u .getHost ().equals (blobUrl .getHost ())
1106- && x5u .getProtocol ().equals (blobUrl .getProtocol ())
1107- && x5u .getPort () == blobUrl .getPort ()))) {
1108- throw new IllegalArgumentException (
1109- String .format (
1110- "x5u in BLOB header must have same origin as the URL the BLOB was downloaded from. Expected origin of: %s ; found: %s" ,
1111- blobUrl , x5u ));
1112- }
1113- List <X509Certificate > certs = new ArrayList <>();
1114- for (String pem :
1115- new String (download (x5u ).getBytes (), StandardCharsets .UTF_8 )
1116- .trim ()
1117- .split ("\\ n+-----END CERTIFICATE-----\\ n+-----BEGIN CERTIFICATE-----\\ n+" )) {
1118- X509Certificate x509Certificate = CertificateParser .parsePem (pem );
1119- certs .add (x509Certificate );
1120- }
1121- certChain = certs ;
1122- } else if (header .getX5c ().isPresent ()) {
1123- certChain = header .getX5c ().get ();
1124- } else {
1125- certChain = Collections .singletonList (trustRootCertificate );
1126- }
1127-
1100+ final List <X509Certificate > certChain = fetchHeaderCertChain (trustRootCertificate , header );
11281101 final X509Certificate leafCert = certChain .get (0 );
11291102
11301103 final Signature signature ;
@@ -1209,4 +1182,35 @@ private static class ParseResult {
12091182 private ByteArray jwtPayload ;
12101183 private ByteArray jwtSignature ;
12111184 }
1185+
1186+ /** Parse the header cert chain and download any certificates as necessary. */
1187+ List <X509Certificate > fetchHeaderCertChain (
1188+ X509Certificate trustRootCertificate , MetadataBLOBHeader header )
1189+ throws IOException , CertificateException {
1190+ if (header .getX5u ().isPresent ()) {
1191+ final URL x5u = header .getX5u ().get ();
1192+ if (blobUrl != null
1193+ && (!(x5u .getHost ().equals (blobUrl .getHost ())
1194+ && x5u .getProtocol ().equals (blobUrl .getProtocol ())
1195+ && x5u .getPort () == blobUrl .getPort ()))) {
1196+ throw new IllegalArgumentException (
1197+ String .format (
1198+ "x5u in BLOB header must have same origin as the URL the BLOB was downloaded from. Expected origin of: %s ; found: %s" ,
1199+ blobUrl , x5u ));
1200+ }
1201+ List <X509Certificate > certs = new ArrayList <>();
1202+ for (String pem :
1203+ new String (download (x5u ).getBytes (), StandardCharsets .UTF_8 )
1204+ .trim ()
1205+ .split ("\\ n+-----END CERTIFICATE-----\\ n+-----BEGIN CERTIFICATE-----\\ n+" )) {
1206+ X509Certificate x509Certificate = CertificateParser .parsePem (pem );
1207+ certs .add (x509Certificate );
1208+ }
1209+ return certs ;
1210+ } else if (header .getX5c ().isPresent ()) {
1211+ return header .getX5c ().get ();
1212+ } else {
1213+ return Collections .singletonList (trustRootCertificate );
1214+ }
1215+ }
12121216}
0 commit comments