Skip to content

Commit 00bab2d

Browse files
committed
Don't return transports from attestation if unknown or empty
1 parent 595e317 commit 00bab2d

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
== Version 1.9.0 (unreleased) ==
2+
3+
webauthn-server-attestation:
4+
5+
* Fixed that `SimpleAttestationResolver` would return empty transports when
6+
transports are unknown.
7+
8+
19
== Version 1.8.0 ==
210

311
Changes:

webauthn-server-attestation/src/main/java/com/yubico/webauthn/attestation/resolver/SimpleAttestationResolver.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.yubico.internal.util.CertificateParser;
3232
import com.yubico.internal.util.CollectionUtil;
3333
import com.yubico.internal.util.ExceptionUtil;
34+
import com.yubico.internal.util.OptionalUtil;
3435
import com.yubico.webauthn.attestation.Attestation;
3536
import com.yubico.webauthn.attestation.AttestationResolver;
3637
import com.yubico.webauthn.attestation.DeviceMatcher;
@@ -135,9 +136,11 @@ public Optional<Attestation> resolve(
135136
.vendorProperties(Optional.of(vendorProperties))
136137
.deviceProperties(Optional.ofNullable(deviceProperties))
137138
.transports(
138-
Optional.of(
139-
Transport.fromInt(
140-
getTransports(attestationCertificate) | metadataTransports)))
139+
OptionalUtil.zipWith(
140+
getTransports(attestationCertificate),
141+
Optional.of(metadataTransports).filter(t -> t != 0),
142+
(a, b) -> a | b)
143+
.map(Transport::fromInt))
141144
.build();
142145
});
143146
}
@@ -158,11 +161,11 @@ private boolean deviceMatches(
158161
}
159162
}
160163

161-
private static int getTransports(X509Certificate cert) {
164+
private static Optional<Integer> getTransports(X509Certificate cert) {
162165
byte[] extensionValue = cert.getExtensionValue(TRANSPORTS_EXT_OID);
163166

164167
if (extensionValue == null) {
165-
return 0;
168+
return Optional.empty();
166169
}
167170

168171
ExceptionUtil.assure(
@@ -186,14 +189,14 @@ private static int getTransports(X509Certificate cert) {
186189
}
187190
}
188191

189-
return transports;
192+
return Optional.of(transports);
190193
}
191194

192195
@Override
193196
public Attestation untrustedFromCertificate(X509Certificate attestationCertificate) {
194197
return Attestation.builder()
195198
.trusted(false)
196-
.transports(Optional.of(Transport.fromInt(getTransports(attestationCertificate))))
199+
.transports(getTransports(attestationCertificate).map(Transport::fromInt))
197200
.build();
198201
}
199202
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.yubico.internal.util;
2+
3+
import java.util.Optional;
4+
import java.util.function.BinaryOperator;
5+
import lombok.experimental.UtilityClass;
6+
7+
/** Utilities for working with {@link Optional} values. */
8+
@UtilityClass
9+
public class OptionalUtil {
10+
11+
/**
12+
* If both <code>a</code> and <code>b</code> are present, return <code>f(a, b)</code>.
13+
*
14+
* <p>If only <code>a</code> is present, return <code>a</code>.
15+
*
16+
* <p>Otherwise, return <code>b</code>.
17+
*/
18+
public static <T> Optional<T> zipWith(Optional<T> a, Optional<T> b, BinaryOperator<T> f) {
19+
if (a.isPresent() && b.isPresent()) {
20+
return Optional.of(f.apply(a.get(), b.get()));
21+
} else if (a.isPresent()) {
22+
return a;
23+
} else {
24+
return b;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)