SHA256
119 lines
6.1 KiB
Java
119 lines
6.1 KiB
Java
package server.archive.registration;
|
|
|
|
import blockchain.Ans104DataItem;
|
|
import blockchain.ShineProtocolTags;
|
|
import utils.crypto.Ed25519Util;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.text.Normalizer;
|
|
import java.util.Arrays;
|
|
import java.util.Base64;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
/** Строгое формирование и проверка двух подписей нового независимого DataItem `t=reg`. */
|
|
public final class FreeRegistrationCertificateV1 {
|
|
private static final byte[] DOMAIN = "SHiNE_FREE_REG_V1\0".getBytes(StandardCharsets.UTF_8);
|
|
private static final List<Ans104DataItem.Tag> TAGS = ShineProtocolTags.CORE_REGISTRATION_TAGS;
|
|
|
|
public record Verified(String login, byte[] rootKey, byte[] blockchainKey,
|
|
byte[] clientKey, byte[] innerSignature,
|
|
byte[] emailSha256, byte[] owner32, String idBase64Url) {}
|
|
|
|
private FreeRegistrationCertificateV1() {}
|
|
|
|
public static String validateLogin(String login) {
|
|
if (login == null || !login.matches("[A-Za-z0-9_]+@[a-z0-9_]+") || login.length() > 30)
|
|
throw new IllegalArgumentException("Full login must use ASCII letters (any local-part case), lowercase namespace, <= 30 bytes");
|
|
return login;
|
|
}
|
|
|
|
public static byte[] identityFields(String login, byte[] root32, byte[] blockchain32, byte[] client32) {
|
|
byte[] name = validateLogin(login).getBytes(StandardCharsets.UTF_8);
|
|
require32(root32); require32(blockchain32); require32(client32);
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
out.write(1); out.write(0); out.write(name.length); out.writeBytes(name);
|
|
out.writeBytes(root32); out.writeBytes(blockchain32); out.writeBytes(client32);
|
|
return out.toByteArray();
|
|
}
|
|
|
|
public static byte[] innerMessage(byte[] identity) {
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
out.writeBytes(DOMAIN); out.writeBytes(identity);
|
|
return out.toByteArray();
|
|
}
|
|
|
|
public static String normalizeEmail(String email) {
|
|
if (email == null) throw new IllegalArgumentException("Email is required");
|
|
String value = Normalizer.normalize(email.strip(), Normalizer.Form.NFC).toLowerCase(Locale.ROOT);
|
|
if (value.isBlank() || value.indexOf('@') < 1 || value.contains(" "))
|
|
throw new IllegalArgumentException("Invalid email");
|
|
return value;
|
|
}
|
|
|
|
public static byte[] emailHash(String email) {
|
|
return sha256(normalizeEmail(email).getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
|
|
public static byte[] issue(String login, byte[] root32, byte[] blockchain32,
|
|
byte[] client32, String email, RegistrarSigner signer) {
|
|
return issueWithEmailHash(login, root32, blockchain32, client32, emailHash(email), signer);
|
|
}
|
|
|
|
public static byte[] issueWithEmailHash(String login, byte[] root32, byte[] blockchain32,
|
|
byte[] client32, byte[] emailHash, RegistrarSigner signer) {
|
|
require32(emailHash);
|
|
byte[] fields = identityFields(login, root32, blockchain32, client32);
|
|
byte[] inner = signer.sign(innerMessage(fields));
|
|
if (inner.length != 64) throw new IllegalStateException("Invalid inner signature length");
|
|
ByteArrayOutputStream payload = new ByteArrayOutputStream();
|
|
payload.writeBytes(fields);
|
|
payload.writeBytes(inner);
|
|
payload.writeBytes(emailHash);
|
|
byte[] data = payload.toByteArray();
|
|
byte[] owner = signer.publicKey32();
|
|
require32(owner);
|
|
byte[] outer = signer.sign(Ans104DataItem.buildSigningMessage(owner, TAGS, data));
|
|
byte[] raw = Ans104DataItem.serializeEd25519(outer, owner, TAGS, data);
|
|
verify(raw, owner);
|
|
return raw;
|
|
}
|
|
|
|
public static Verified verify(byte[] signedDataItem, byte[] registrarPublicKey32) {
|
|
require32(registrarPublicKey32);
|
|
Ans104DataItem item = new Ans104DataItem(signedDataItem);
|
|
if (!item.ownerEquals(registrarPublicKey32) || !item.verifySignature())
|
|
throw new IllegalArgumentException("Invalid outer ANS-104 registrar signature");
|
|
if (!item.tags().equals(TAGS) || item.target().length != 0 || item.anchor().length != 0)
|
|
throw new IllegalArgumentException("Unexpected registration tags/target/anchor");
|
|
byte[] data = item.data();
|
|
if (data.length < 3 + 96 + 64 + 32 || data[0] != 1 || data[1] != 0)
|
|
throw new IllegalArgumentException("Invalid certificate version/length");
|
|
int len = data[2] & 0xff;
|
|
if (data.length != 3 + len + 96 + 64 + 32)
|
|
throw new IllegalArgumentException("Invalid certificate byte length");
|
|
String login = validateLogin(new String(data, 3, len, StandardCharsets.UTF_8));
|
|
int offset = 3 + len;
|
|
byte[] root = Arrays.copyOfRange(data, offset, offset+32); offset += 32;
|
|
byte[] blockchain = Arrays.copyOfRange(data, offset, offset+32); offset += 32;
|
|
byte[] client = Arrays.copyOfRange(data, offset, offset+32); offset += 32;
|
|
byte[] innerSig = Arrays.copyOfRange(data, offset, offset+64); offset += 64;
|
|
byte[] emailSha = Arrays.copyOfRange(data, offset, offset+32);
|
|
byte[] fields = Arrays.copyOfRange(data, 0, 3 + len + 96);
|
|
if (!Ed25519Util.verify(innerMessage(fields), innerSig, registrarPublicKey32))
|
|
throw new IllegalArgumentException("Invalid inner certificate signature");
|
|
return new Verified(login, root, blockchain, client, innerSig, emailSha,
|
|
item.owner32(), Base64.getUrlEncoder().withoutPadding().encodeToString(item.id32()));
|
|
}
|
|
|
|
private static void require32(byte[] key) {
|
|
if (key == null || key.length != 32) throw new IllegalArgumentException("Expected 32-byte public key");
|
|
}
|
|
private static byte[] sha256(byte[] data) {
|
|
try { return MessageDigest.getInstance("SHA-256").digest(data); }
|
|
catch (Exception e) { throw new IllegalStateException("SHA-256 unavailable", e); }
|
|
}
|
|
}
|