Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import static google.registry.util.DateTimeUtils.minusDays;
import static google.registry.util.DateTimeUtils.plusYears;
import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import static google.registry.util.DomainNameUtils.ALLOWED_CHARS;
import static java.util.stream.Collectors.joining;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -160,10 +160,6 @@ public class DomainFlowUtils {
+ "delegated. Please visit https://www.icann.org/namecollision for more information on "
+ "name collision.";

/** Strict validator for ascii lowercase letters, digits, and "-", allowing "." as a separator */
private static final CharMatcher ALLOWED_CHARS =
CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('0', '9').or(CharMatcher.anyOf("-.")));

/** Default validator used to determine if an IDN name can be provisioned on a TLD. */
private static final IdnLabelValidator IDN_LABEL_VALIDATOR = new IdnLabelValidator();

Expand Down
93 changes: 93 additions & 0 deletions core/src/main/java/google/registry/model/registrar/Registrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy;
import static google.registry.util.CollectionUtils.nullToEmptyImmutableSortedCopy;
import static google.registry.util.DateTimeUtils.START_INSTANT;
import static google.registry.util.DomainNameUtils.canonicalizeAndValidateHostname;
import static google.registry.util.PasswordUtils.SALT_SUPPLIER;
import static google.registry.util.PasswordUtils.hashPassword;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
Expand Down Expand Up @@ -185,6 +186,12 @@ public enum State {
/** Regex for E.164 phone number format. */
private static final Pattern E164_PATTERN = Pattern.compile("\\+[0-9]{1,3}\\.[0-9]{1,14}");

/** Maximum number of hostnames in the EPP client certificate allowlist. */
public static final int MAX_EPP_CLIENT_CERTIFICATE_HOSTNAMES = 20;

/** Maximum number of revoked client certificate hashes. */
public static final int MAX_REVOKED_CLIENT_CERTIFICATE_HASHES = 20;

/** Regex for telephone support passcode (5 digit string). */
public static final Pattern PHONE_PASSCODE_PATTERN = Pattern.compile("\\d{5}");

Expand Down Expand Up @@ -295,6 +302,27 @@ public enum State {
/** Base64 encoded SHA256 hash of {@link #failoverClientCertificate}. */
String failoverClientCertificateHash;

/**
* Hostnames for which a client certificate issued by a publicly trusted CA is accepted at EPP
* login, as an alternative to the pinned {@link #clientCertificate}.
*
* <p>Entries are stored in canonical A-label (punycode) lowercase form without a trailing dot.
* They are only consulted while the {@code ALLOW_EPP_CLIENT_CERT_HOSTNAME_AUTH} feature flag is
* active.
*/
@GetterDelegate(methodName = "getAllowedEppClientCertificateHostnames")
@Expose
Set<String> allowedEppClientCertificateHostnames;

/**
* Base64 encoded SHA256 hashes of client certificates the registrar has revoked, in the same form
* as {@link #clientCertificateHash}. A certificate with one of these hashes is rejected at EPP
* login regardless of which mechanism would otherwise have accepted it.
*/
@GetterDelegate(methodName = "getRevokedClientCertificateHashes")
@Expose
Set<String> revokedClientCertificateHashes;

/** An allow list of netmasks (in CIDR notation) which the client is allowed to connect from. */
@org.hibernate.annotations.Type(CidrBlockListUserType.class)
@Column(columnDefinition = "text[]")
Expand Down Expand Up @@ -540,6 +568,14 @@ public ImmutableList<CidrAddressBlock> getIpAddressAllowList() {
return nullToEmptyImmutableCopy(ipAddressAllowList);
}

public ImmutableSortedSet<String> getAllowedEppClientCertificateHostnames() {
return nullToEmptyImmutableSortedCopy(allowedEppClientCertificateHostnames);
}

public ImmutableSortedSet<String> getRevokedClientCertificateHashes() {
return nullToEmptyImmutableSortedCopy(revokedClientCertificateHashes);
}

public RegistrarAddress getLocalizedAddress() {
return localizedAddress;
}
Expand Down Expand Up @@ -662,6 +698,9 @@ public Map<String, Object> toJsonMap() {
.put("clientCertificateHash", clientCertificateHash)
.put("failoverClientCertificate", failoverClientCertificate)
.put("failoverClientCertificateHash", failoverClientCertificateHash)
.putListOfStrings(
"allowedEppClientCertificateHostnames", getAllowedEppClientCertificateHostnames())
.putListOfStrings("revokedClientCertificateHashes", getRevokedClientCertificateHashes())
.put("localizedAddress", localizedAddress)
.put("internationalizedAddress", internationalizedAddress)
.put("phoneNumber", phoneNumber)
Expand Down Expand Up @@ -853,6 +892,60 @@ public Builder setLastPocVerificationDate(Instant now) {
return this;
}

/**
* Sets the hostnames for which CA-issued client certificates are accepted at EPP login.
*
* <p>Each entry is canonicalized to lowercase A-label (punycode) form and validated as a
* multi-label DNS name; duplicates collapse to one entry.
*/
public Builder setAllowedEppClientCertificateHostnames(Iterable<String> hostnames) {
checkArgumentNotNull(hostnames, "EPP client certificate hostnames cannot be null");
ImmutableSortedSet<String> validated =
stream(hostnames)
.map(
hostname ->
canonicalizeAndValidateHostname(
checkArgumentNotNull(
hostname, "EPP client certificate hostname cannot be null")))
.collect(toImmutableSortedSet(Comparator.naturalOrder()));
checkArgument(
validated.size() <= MAX_EPP_CLIENT_CERTIFICATE_HOSTNAMES,
"At most %s EPP client certificate hostnames may be configured, got %s",
MAX_EPP_CLIENT_CERTIFICATE_HOSTNAMES,
validated.size());
getInstance().allowedEppClientCertificateHostnames = validated;
return this;
}

/** Sets the hashes of client certificates that are rejected at EPP login. */
public Builder setRevokedClientCertificateHashes(Iterable<String> hashes) {
checkArgumentNotNull(hashes, "Revoked client certificate hashes cannot be null");
ImmutableSortedSet<String> validated =
stream(hashes)
.map(
hash -> {
checkArgumentNotNull(hash, "Revoked client certificate hash cannot be null");
checkArgument(
base64().omitPadding().canDecode(hash),
"Invalid base64 SHA-256 certificate hash: %s",
hash);
byte[] decoded = base64().omitPadding().decode(hash);
checkArgument(
decoded.length == 32 && base64().omitPadding().encode(decoded).equals(hash),
"Invalid base64 SHA-256 certificate hash: %s",
hash);
return hash;
})
.collect(toImmutableSortedSet(Comparator.naturalOrder()));
checkArgument(
validated.size() <= MAX_REVOKED_CLIENT_CERTIFICATE_HASHES,
"At most %s revoked client certificates may be configured, got %s",
MAX_REVOKED_CLIENT_CERTIFICATE_HASHES,
validated.size());
getInstance().revokedClientCertificateHashes = validated;
return this;
}
Comment thread
CydeWeys marked this conversation as resolved.
Resolved

private static String calculateHash(String clientCertificate) {
if (clientCertificate == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import static jakarta.servlet.http.HttpServletResponse.SC_OK;

import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import google.registry.flows.certs.CertificateChecker;
import google.registry.flows.certs.CertificateChecker.InsecureCertificateException;
import google.registry.model.console.ConsolePermission;
Expand All @@ -30,6 +33,7 @@
import google.registry.model.registrar.Registrar;
import google.registry.request.Action;
import google.registry.request.Action.Service;
import google.registry.request.OptionalJsonPayload;
import google.registry.request.Parameter;
import google.registry.request.auth.Auth;
import google.registry.request.auth.AuthenticatedRegistrarAccessor;
Expand All @@ -50,7 +54,7 @@ public class SecurityAction extends ConsoleApiAction {
static final String PATH = "/console-api/settings/security";
private final String registrarId;
private final AuthenticatedRegistrarAccessor registrarAccessor;
private final Optional<Registrar> registrar;
private final Optional<JsonElement> payload;
private final CertificateChecker certificateChecker;

@Inject
Expand All @@ -59,18 +63,19 @@ public SecurityAction(
CertificateChecker certificateChecker,
AuthenticatedRegistrarAccessor registrarAccessor,
@Parameter("registrarId") String registrarId,
@Parameter("registrar") Optional<Registrar> registrar) {
@OptionalJsonPayload Optional<JsonElement> payload) {
super(consoleApiParams);
this.registrarId = registrarId;
this.registrarAccessor = registrarAccessor;
this.registrar = registrar;
this.payload = payload;
this.certificateChecker = certificateChecker;
}

@Override
protected void postHandler(User user) {
checkPermission(user, registrarId, ConsolePermission.EDIT_REGISTRAR_DETAILS);
checkArgument(registrar.isPresent(), "'registrar' parameter is not present");
checkArgument(payload.isPresent(), "'registrar' parameter is not present");
checkArgument(payload.get().isJsonObject(), "'registrar' parameter must be a JSON object");

Registrar savedRegistrar;
try {
Expand All @@ -84,18 +89,45 @@ protected void postHandler(User user) {
}

private void setResponse(Registrar savedRegistrar) {
Registrar registrarParameter = registrar.get();
JsonObject jsonObject = payload.get().getAsJsonObject();
Registrar.Builder updatedRegistrarBuilder = savedRegistrar.asBuilder();
StringJoiner updates = new StringJoiner(",");

if (!savedRegistrar
.getIpAddressAllowList()
.equals(registrarParameter.getIpAddressAllowList())) {
updatedRegistrarBuilder.setIpAddressAllowList(registrarParameter.getIpAddressAllowList());
updates.add("IP_CHANGE");
}

boolean hasHostnames = isFieldPresent(jsonObject, "allowedEppClientCertificateHostnames");
boolean hasRevokedHashes = isFieldPresent(jsonObject, "revokedClientCertificateHashes");
try {
if (hasHostnames) {
checkStringArrayElements(
jsonObject,
"allowedEppClientCertificateHostnames",
"EPP client certificate hostname cannot be null");
}
if (hasRevokedHashes) {
checkStringArrayElements(
jsonObject,
"revokedClientCertificateHashes",
"Revoked client certificate hash cannot be null");
}
Registrar registrarParameter = consoleApiParams.gson().fromJson(jsonObject, Registrar.class);

if (!savedRegistrar
.getIpAddressAllowList()
.equals(registrarParameter.getIpAddressAllowList())) {
updatedRegistrarBuilder.setIpAddressAllowList(registrarParameter.getIpAddressAllowList());
updates.add("IP_CHANGE");
}
if (hasHostnames) {
checkArgument(
!registrarParameter.getAllowedEppClientCertificateHostnames().isEmpty()
|| savedRegistrar.getAllowedEppClientCertificateHostnames().isEmpty(),
"Cannot clear allowed EPP client certificate hostnames once configured");
updatedRegistrarBuilder.setAllowedEppClientCertificateHostnames(
registrarParameter.getAllowedEppClientCertificateHostnames());
}
if (hasRevokedHashes) {
updatedRegistrarBuilder.setRevokedClientCertificateHashes(
registrarParameter.getRevokedClientCertificateHashes());
}
if (!savedRegistrar
.getClientCertificate()
.equals(registrarParameter.getClientCertificate())) {
Expand All @@ -116,12 +148,22 @@ private void setResponse(Registrar savedRegistrar) {
updates.add("FAILOVER_SSL_CERT_CHANGE");
}
}
} catch (InsecureCertificateException e) {
} catch (InsecureCertificateException | IllegalArgumentException | JsonSyntaxException e) {
setFailedResponse(e.getMessage(), SC_BAD_REQUEST);
return;
}

Registrar updatedRegistrar = updatedRegistrarBuilder.build();
if (!savedRegistrar
.getAllowedEppClientCertificateHostnames()
.equals(updatedRegistrar.getAllowedEppClientCertificateHostnames())) {
updates.add("EPP_CERT_HOSTNAMES_CHANGE");
}
if (!savedRegistrar
.getRevokedClientCertificateHashes()
.equals(updatedRegistrar.getRevokedClientCertificateHashes())) {
updates.add("REVOKED_CERTS_CHANGE");
}
tm().put(updatedRegistrar);
finishAndPersistConsoleUpdateHistory(
new ConsoleUpdateHistory.Builder()
Expand All @@ -134,4 +176,21 @@ private void setResponse(Registrar savedRegistrar) {
EmailInfo.create(savedRegistrar, updatedRegistrar, ImmutableSet.of(), ImmutableSet.of()));
consoleApiParams.response().setStatus(SC_OK);
}

private static boolean isFieldPresent(JsonObject jsonObject, String fieldName) {
return jsonObject.has(fieldName) && !jsonObject.get(fieldName).isJsonNull();
}

private static void checkStringArrayElements(
JsonObject jsonObject, String fieldName, String nullElementErrorMessage) {
JsonElement field = jsonObject.get(fieldName);
checkArgument(field.isJsonArray(), "'%s' must be a JSON array", fieldName);
for (JsonElement element : field.getAsJsonArray()) {
checkArgument(!element.isJsonNull(), nullElementErrorMessage);
checkArgument(
element.isJsonPrimitive() && element.getAsJsonPrimitive().isString(),
"'%s' elements must be strings",
fieldName);
}
}
}
Loading
Loading