001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2025 Richard "Shred" Körber 005 * http://acme4j.shredzone.org 006 * 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 013 */ 014package org.shredzone.acme4j.challenge; 015 016import static org.shredzone.acme4j.toolbox.AcmeUtils.*; 017 018import java.io.Serial; 019import java.net.URL; 020import java.util.Arrays; 021import java.util.Locale; 022 023import org.shredzone.acme4j.Identifier; 024import org.shredzone.acme4j.Login; 025import org.shredzone.acme4j.toolbox.JSON; 026 027/** 028 * Implements the {@value TYPE} challenge. It requires a specific DNS record for domain 029 * validation. See the acme4j documentation for a detailed explanation. 030 * 031 * @draft This class is currently based on an RFC draft. It may be changed or removed 032 * without notice to reflect future changes to the draft. SemVer rules do not apply here. 033 * @since 4.0.0 034 */ 035public class DnsAccount01Challenge extends TokenChallenge { 036 @Serial 037 private static final long serialVersionUID = -1098129409378900733L; 038 039 /** 040 * Challenge type name: {@value} 041 */ 042 public static final String TYPE = "dns-account-01"; 043 044 /** 045 * Creates a new generic {@link DnsAccount01Challenge} object. 046 * 047 * @param login 048 * {@link Login} the resource is bound with 049 * @param data 050 * {@link JSON} challenge data 051 */ 052 public DnsAccount01Challenge(Login login, JSON data) { 053 super(login, data); 054 } 055 056 /** 057 * Converts a domain identifier to the Resource Record name to be used for the DNS TXT 058 * record. 059 * 060 * @param identifier 061 * {@link Identifier} to be validated 062 * @return Resource Record name (e.g. 063 * {@code _ujmmovf2vn55tgye._acme-challenge.example.org.}, note the trailing full stop 064 * character). 065 */ 066 public String getRRName(Identifier identifier) { 067 return getRRName(identifier.getDomain()); 068 } 069 070 /** 071 * Converts a domain identifier to the Resource Record name to be used for the DNS TXT 072 * record. 073 * 074 * @param domain 075 * Domain name to be validated 076 * @return Resource Record name (e.g. 077 * {@code _ujmmovf2vn55tgye._acme-challenge.example.org.}, note the trailing full stop 078 * character). 079 */ 080 public String getRRName(String domain) { 081 return getPrefix(getLogin().getAccount().getLocation()) + '.' + domain + '.'; 082 } 083 084 /** 085 * Returns the digest string to be set in the domain's TXT record. 086 */ 087 public String getDigest() { 088 return base64UrlEncode(sha256hash(getAuthorization())); 089 } 090 091 /** 092 * Returns the prefix of an account location. 093 */ 094 private String getPrefix(URL accountLocation) { 095 var urlHash = sha256hash(accountLocation.toExternalForm()); 096 var hash = base32Encode(Arrays.copyOfRange(urlHash, 0, 10)); 097 return "_" + hash.toLowerCase(Locale.ENGLISH) 098 + "._acme-challenge"; 099 } 100 101 @Override 102 protected boolean acceptable(String type) { 103 return TYPE.equals(type); 104 } 105 106}