Skip to content

Commit

Permalink
Check Oscore Identity of server at client side
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Mar 18, 2022
1 parent fa9c632 commit 48b1d60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.californium.cose.AlgorithmID;
import org.eclipse.californium.cose.CoseException;
import org.eclipse.californium.elements.Connector;
import org.eclipse.californium.elements.EndpointContext;
import org.eclipse.californium.elements.auth.RawPublicKeyIdentity;
import org.eclipse.californium.elements.config.Configuration;
import org.eclipse.californium.elements.util.CertPathUtil;
Expand All @@ -52,6 +53,7 @@
import org.eclipse.leshan.client.servers.ServerInfo;
import org.eclipse.leshan.core.CertificateUsage;
import org.eclipse.leshan.core.SecurityMode;
import org.eclipse.leshan.core.californium.EndpointContextUtil;
import org.eclipse.leshan.core.californium.EndpointFactory;
import org.eclipse.leshan.core.californium.oscore.cf.InMemoryOscoreContextDB;
import org.eclipse.leshan.core.californium.oscore.cf.OscoreParameters;
Expand Down Expand Up @@ -357,7 +359,8 @@ public synchronized Endpoint getEndpoint(ServerIdentity server) {
return null;
}

public synchronized ServerIdentity getServerIdentity(Endpoint endpoint, InetSocketAddress serverAddress) {
public synchronized ServerIdentity getServerIdentity(Endpoint endpoint, InetSocketAddress serverAddress,
EndpointContext endpointContext) {
// TODO support multi server

// knowing used CoAP endpoint we should be able to know the server identity because :
Expand All @@ -370,6 +373,19 @@ public synchronized ServerIdentity getServerIdentity(Endpoint endpoint, InetSock
&& !currentServer.getIdentity().getPeerAddress().equals(serverAddress)) {
return null;
}
// For OSCORE, be sure OSCORE is used.
if (currentServer.getIdentity().isOSCORE()) {
Identity foreignPeerIdentity = EndpointContextUtil.extractIdentity(endpointContext);
if (!foreignPeerIdentity.isOSCORE() //
// we also check OscoreIdentity but this is probably not useful
// because we are using static OSCOREstore which holds only 1 OscoreParameter,
// so if the request was successfully decrypted and OSCORE is used, this MUST be the right
// server.
|| !foreignPeerIdentity.getOscoreIdentity()
.equals(currentServer.getIdentity().getOscoreIdentity())) {
return null;
}
}
return currentServer;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected ServerIdentity getServerOrRejectRequest(CoapExchange exchange) {
* @throws IllegalStateException if we are not able to extract {@link ServerIdentity}.
*/
protected ServerIdentity extractIdentity(CoapExchange exchange) {
return endpointsManager.getServerIdentity(exchange.advanced().getEndpoint(), exchange.getSourceSocketAddress());
return endpointsManager.getServerIdentity(exchange.advanced().getEndpoint(), exchange.getSourceSocketAddress(),
exchange.advanced().getRequest().getSourceContext());
}
}

0 comments on commit 48b1d60

Please sign in to comment.