public static Identity deserialize(JsonObject peer) { String address = peer.get(KEY_ADDRESS).asString(); int port = peer.get(KEY_PORT).asInt(); JsonValue jpsk = peer.get(KEY_ID); if (jpsk != null) { return Identity.psk(new InetSocketAddress(address, port), jpsk.asString()); } JsonValue jrpk = peer.get(KEY_RPK); if (jrpk != null) { try { byte[] rpk = Hex.decodeHex(jrpk.asString().toCharArray()); X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk); PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec); return Identity.rpk(new InetSocketAddress(address, port), publicKey); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new IllegalStateException("Invalid security info content", e); } } JsonValue jcn = peer.get(KEY_CN); if (jcn != null) { return Identity.x509(new InetSocketAddress(address, port), jcn.asString()); } return Identity.unsecure(new InetSocketAddress(address, port)); } }
public static Identity deserialize(JsonObject peer) { String address = peer.get(KEY_ADDRESS).asString(); int port = peer.get(KEY_PORT).asInt(); JsonValue jpsk = peer.get(KEY_ID); if (jpsk != null) { return Identity.psk(new InetSocketAddress(address, port), jpsk.asString()); } JsonValue jrpk = peer.get(KEY_RPK); if (jrpk != null) { try { byte[] rpk = Hex.decodeHex(jrpk.asString().toCharArray()); X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk); PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec); return Identity.rpk(new InetSocketAddress(address, port), publicKey); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new IllegalStateException("Invalid security info content", e); } } JsonValue jcn = peer.get(KEY_CN); if (jcn != null) { return Identity.x509(new InetSocketAddress(address, port), jcn.asString()); } return Identity.unsecure(new InetSocketAddress(address, port)); } }
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, ((PreSharedKeyIdentity) senderIdentity).getIdentity()); } 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); }
StaticPskStore staticPskStore = new StaticPskStore(serverInfo.pskId, serverInfo.pskKey); newBuilder.setPskStore(staticPskStore); serverIdentity = Identity.psk(serverInfo.getAddress(), serverInfo.pskId); } else if (serverInfo.secureMode == SecurityMode.RPK) {