|
| 1 | +package dev.kastle.netty.util.nethernet; |
| 2 | + |
| 3 | +import org.jose4j.jwk.EcJwkGenerator; |
| 4 | +import org.jose4j.jwk.EllipticCurveJsonWebKey; |
| 5 | +import org.jose4j.jws.AlgorithmIdentifiers; |
| 6 | +import org.jose4j.jws.JsonWebSignature; |
| 7 | +import org.jose4j.jwt.JwtClaims; |
| 8 | +import org.jose4j.jwt.NumericDate; |
| 9 | +import org.jose4j.keys.EllipticCurves; |
| 10 | +import org.jose4j.lang.JoseException; |
| 11 | + |
| 12 | +import java.security.PrivateKey; |
| 13 | +import java.security.PublicKey; |
| 14 | +import java.time.Instant; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Base64; |
| 18 | +import java.util.List; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +/** |
| 22 | + * Produces the server-side identity assertion for each SDP answer |
| 23 | + * |
| 24 | + * @see <a href="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/Mojang/bedrock-protocol-docs/blob/7330880ab78ef001cad0b9cdfedb3aa3eaa6d4af/NetherNetOnboardingGuide.md#52-producing-the-server-assertion-in-the-answer">NetherNet onboarding guide, section 5.2</a> |
| 25 | + */ |
| 26 | +public class ServerIdentity { |
| 27 | + private static final String ALG = AlgorithmIdentifiers.ECDSA_USING_P384_CURVE_AND_SHA384; // ES384 / P-384 |
| 28 | + |
| 29 | + private final PrivateKey privateKey; |
| 30 | + private final String domain; |
| 31 | + private final String token; |
| 32 | + |
| 33 | + public ServerIdentity(PrivateKey privateKey, PublicKey publicKey, Instant expiry, String domain) throws JoseException { |
| 34 | + this.privateKey = privateKey; |
| 35 | + this.domain = domain; |
| 36 | + this.token = buildToken(publicKey, expiry); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Generate a brand-new server identity that is not stored |
| 41 | + * |
| 42 | + * @param domain The domain name for the server identity |
| 43 | + * @return A new ServerIdentity instance |
| 44 | + * @throws JoseException If there is an error creating the JWT |
| 45 | + */ |
| 46 | + public static ServerIdentity generate(String domain) throws JoseException { |
| 47 | + EllipticCurveJsonWebKey jwk = EcJwkGenerator.generateJwk(EllipticCurves.P384); |
| 48 | + return new ServerIdentity(jwk.getPrivateKey(), jwk.getPublicKey(), null, domain); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Build a JWT token with the given public key and expiry. |
| 53 | + * |
| 54 | + * @param publicKey The public key to include in the token |
| 55 | + * @param expiry The expiration time of the token |
| 56 | + * @return The signed JWT token |
| 57 | + * @throws JoseException If there is an error signing the token |
| 58 | + */ |
| 59 | + private String buildToken(PublicKey publicKey, Instant expiry) throws JoseException { |
| 60 | + JwtClaims claims = new JwtClaims(); |
| 61 | + claims.setClaim("cpk", Base64.getEncoder().encodeToString(publicKey.getEncoded())); // Custom claim required by the NetherNet spec |
| 62 | + claims.setIssuedAtToNow(); |
| 63 | + |
| 64 | + // If we have a domain set it as the isser as it could be shown to the user |
| 65 | + if (domain != null && !domain.isBlank()) { |
| 66 | + claims.setIssuer(domain); |
| 67 | + } |
| 68 | + |
| 69 | + // Mirror the certificate expiry if set |
| 70 | + if (expiry != null) { |
| 71 | + claims.setExpirationTime(NumericDate.fromMilliseconds(expiry.toEpochMilli())); |
| 72 | + } |
| 73 | + |
| 74 | + return sign(claims.toJson()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Sign the payload with the private key and return the compact JWS serialization. |
| 79 | + * |
| 80 | + * @param payload The payload to sign |
| 81 | + * @return The compact JWS serialization |
| 82 | + * @throws JoseException If there is an error signing the payload |
| 83 | + */ |
| 84 | + private String sign(String payload) throws JoseException { |
| 85 | + JsonWebSignature jws = new JsonWebSignature(); |
| 86 | + jws.setPayload(payload); |
| 87 | + jws.setKey(privateKey); |
| 88 | + jws.setAlgorithmHeaderValue(ALG); |
| 89 | + return jws.getCompactSerialization(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Generate the identity value as base64 for this answer SDP |
| 94 | + * |
| 95 | + * @param answerSdp The SDP to generate the identity value for |
| 96 | + * @return The base64 identity value |
| 97 | + * @throws JoseException If there is an error signing the identity value |
| 98 | + */ |
| 99 | + public String identityValue(String answerSdp) throws JoseException { |
| 100 | + // Generate and sign the fingerprint |
| 101 | + String[] fingerprintParts = sign(getCanonicalFingerprintJson(answerSdp)).split("\\."); |
| 102 | + String fingerprints = fingerprintParts[0] + ".." + fingerprintParts[2]; |
| 103 | + |
| 104 | + Identity.Assertion assertion = new Identity.Assertion(token, fingerprints); |
| 105 | + Identity.Idp idp = new Identity.Idp(domain, "default"); |
| 106 | + return new Identity(idp, assertion).toBase64(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Insert the identity into the answer SDP |
| 111 | + * The specific placement is a strange requirement for the spec but we will follow it |
| 112 | + * |
| 113 | + * @param answerSdp The SDP to insert the identity into |
| 114 | + * @return The SDP with the identity inserted |
| 115 | + * @throws JoseException If there is an error signing the identity value |
| 116 | + */ |
| 117 | + public String augmentAnswer(String answerSdp) throws JoseException { |
| 118 | + String line = "a=identity:" + identityValue(answerSdp); |
| 119 | + String eol = answerSdp.contains("\r\n") ? "\r\n" : "\n"; |
| 120 | + |
| 121 | + String[] lines = answerSdp.split("\r\n|\n", -1); |
| 122 | + List<String> out = new ArrayList<>(lines.length + 1); |
| 123 | + |
| 124 | + boolean inserted = false; |
| 125 | + for (String current : lines) { |
| 126 | + if (!inserted && current.startsWith("m=")) { |
| 127 | + out.add(line); |
| 128 | + inserted = true; |
| 129 | + } |
| 130 | + out.add(current); |
| 131 | + } |
| 132 | + |
| 133 | + if (!inserted) { |
| 134 | + out.add(line); |
| 135 | + } |
| 136 | + |
| 137 | + return String.join(eol, out); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get the canonical fingerprint JSON from the SDP offer |
| 142 | + * |
| 143 | + * @param sdpOffer The SDP offer to extract fingerprints from |
| 144 | + * @return The canonical fingerprint JSON |
| 145 | + */ |
| 146 | + private static String getCanonicalFingerprintJson(String sdpOffer) { |
| 147 | + String prefix = "a=fingerprint:"; |
| 148 | + return Arrays.stream(sdpOffer.split("\n")) |
| 149 | + .filter(line -> line.startsWith(prefix)) |
| 150 | + .map(line -> line.substring(prefix.length()).trim()) |
| 151 | + .map(line -> { |
| 152 | + String[] parts = line.split(" "); |
| 153 | + if (parts.length != 2) { |
| 154 | + throw new IllegalArgumentException("Invalid fingerprint line: " + line); |
| 155 | + } |
| 156 | + return "{\"algorithm\":\"" + parts[0] + "\",\"digest\":\"" + parts[1] + "\"}"; |
| 157 | + }) |
| 158 | + .collect(Collectors.joining(",", "{\"fingerprint\":[", "]}")); |
| 159 | + } |
| 160 | +} |
0 commit comments