Search in sources :

Example 1 with RawPublicKeyIdentity

use of org.eclipse.californium.elements.auth.RawPublicKeyIdentity in project leshan by eclipse.

the class EndpointContextSerDes method serialize.

public static JsonObject serialize(EndpointContext context) {
    JsonObject peer = Json.object();
    peer.set(KEY_ADDRESS, context.getPeerAddress().getHostString());
    peer.set(KEY_PORT, context.getPeerAddress().getPort());
    Principal principal = context.getPeerIdentity();
    if (principal != null) {
        if (principal instanceof PreSharedKeyIdentity) {
            peer.set(KEY_ID, principal.getName());
        } else if (principal instanceof RawPublicKeyIdentity) {
            PublicKey publicKey = ((RawPublicKeyIdentity) principal).getKey();
            peer.set(KEY_RPK, Hex.encodeHexString(publicKey.getEncoded()));
        } else if (principal instanceof X500Principal || principal instanceof X509CertPath) {
            peer.set(KEY_DN, principal.getName());
        }
    }
    /**
     * copy the attributes *
     */
    Map<String, String> attributes = context.entries();
    if (!attributes.isEmpty()) {
        JsonObject attContext = Json.object();
        for (String key : attributes.keySet()) {
            attContext.set(key, attributes.get(key));
        }
        peer.set(KEY_ATTRIBUTES, attContext);
    }
    return peer;
}
Also used : PublicKey(java.security.PublicKey) RawPublicKeyIdentity(org.eclipse.californium.elements.auth.RawPublicKeyIdentity) JsonObject(com.eclipsesource.json.JsonObject) X500Principal(javax.security.auth.x500.X500Principal) PreSharedKeyIdentity(org.eclipse.californium.elements.auth.PreSharedKeyIdentity) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal) X509CertPath(org.eclipse.californium.elements.auth.X509CertPath)

Example 2 with RawPublicKeyIdentity

use of org.eclipse.californium.elements.auth.RawPublicKeyIdentity in project leshan by eclipse.

the class EndpointContextUtil method extractIdentity.

public static Identity extractIdentity(EndpointContext context) {
    InetSocketAddress peerAddress = context.getPeerAddress();
    Principal senderIdentity = context.getPeerIdentity();
    if (senderIdentity != null) {
        if (senderIdentity instanceof PreSharedKeyIdentity) {
            return Identity.psk(peerAddress, senderIdentity.getName());
        } else if (senderIdentity instanceof RawPublicKeyIdentity) {
            PublicKey publicKey = ((RawPublicKeyIdentity) senderIdentity).getKey();
            return Identity.rpk(peerAddress, publicKey);
        } else if (senderIdentity instanceof X500Principal || senderIdentity instanceof X509CertPath) {
            // Extract common name
            String x509CommonName = extractCN(senderIdentity.getName());
            return Identity.x509(peerAddress, x509CommonName);
        }
        throw new IllegalStateException("Unable to extract sender identity : unexpected type of Principal");
    }
    return Identity.unsecure(peerAddress);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) PublicKey(java.security.PublicKey) RawPublicKeyIdentity(org.eclipse.californium.elements.auth.RawPublicKeyIdentity) X500Principal(javax.security.auth.x500.X500Principal) PreSharedKeyIdentity(org.eclipse.californium.elements.auth.PreSharedKeyIdentity) Principal(java.security.Principal) X500Principal(javax.security.auth.x500.X500Principal) X509CertPath(org.eclipse.californium.elements.auth.X509CertPath)

Example 3 with RawPublicKeyIdentity

use of org.eclipse.californium.elements.auth.RawPublicKeyIdentity in project leshan by eclipse.

the class EndpointContextSerDes method deserialize.

public static EndpointContext deserialize(JsonObject peer) {
    String address = peer.get(KEY_ADDRESS).asString();
    int port = peer.get(KEY_PORT).asInt();
    InetSocketAddress socketAddress = new InetSocketAddress(address, port);
    Principal principal = null;
    JsonValue value = peer.get(KEY_ID);
    if (value != null) {
        principal = new PreSharedKeyIdentity(value.asString());
    } else if ((value = peer.get(KEY_RPK)) != null) {
        try {
            byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
            X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
            PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
            principal = new RawPublicKeyIdentity(publicKey);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
            throw new IllegalStateException("Invalid security info content", e);
        }
    } else if ((value = peer.get(KEY_DN)) != null) {
        principal = new X500Principal(value.asString());
    }
    EndpointContext endpointContext;
    value = peer.get(KEY_ATTRIBUTES);
    if (value == null) {
        endpointContext = new AddressEndpointContext(socketAddress, principal);
    } else {
        int index = 0;
        String[] attributes = new String[value.asObject().size() * 2];
        for (Member member : value.asObject()) {
            attributes[index++] = member.getName();
            attributes[index++] = member.getValue().asString();
        }
        endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
    }
    return endpointContext;
}
Also used : AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) PublicKey(java.security.PublicKey) RawPublicKeyIdentity(org.eclipse.californium.elements.auth.RawPublicKeyIdentity) JsonValue(com.eclipsesource.json.JsonValue) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) X500Principal(javax.security.auth.x500.X500Principal) PreSharedKeyIdentity(org.eclipse.californium.elements.auth.PreSharedKeyIdentity) Member(com.eclipsesource.json.JsonObject.Member) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Aggregations

Principal (java.security.Principal)3 PublicKey (java.security.PublicKey)3 X500Principal (javax.security.auth.x500.X500Principal)3 PreSharedKeyIdentity (org.eclipse.californium.elements.auth.PreSharedKeyIdentity)3 RawPublicKeyIdentity (org.eclipse.californium.elements.auth.RawPublicKeyIdentity)3 InetSocketAddress (java.net.InetSocketAddress)2 X509CertPath (org.eclipse.californium.elements.auth.X509CertPath)2 JsonObject (com.eclipsesource.json.JsonObject)1 Member (com.eclipsesource.json.JsonObject.Member)1 JsonValue (com.eclipsesource.json.JsonValue)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 AddressEndpointContext (org.eclipse.californium.elements.AddressEndpointContext)1 EndpointContext (org.eclipse.californium.elements.EndpointContext)1 MapBasedEndpointContext (org.eclipse.californium.elements.MapBasedEndpointContext)1