001/*
002 * acme4j - Java ACME client
003 *
004 * Copyright (C) 2015 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.base64UrlEncode;
017import static org.shredzone.acme4j.toolbox.AcmeUtils.sha256hash;
018
019import java.io.Serial;
020
021import org.shredzone.acme4j.Identifier;
022import org.shredzone.acme4j.Login;
023import org.shredzone.acme4j.toolbox.JSON;
024
025/**
026 * Implements the {@value TYPE} challenge. It requires a specific DNS record for domain
027 * validation. See the acme4j documentation for a detailed explanation.
028 */
029public class Dns01Challenge extends TokenChallenge {
030    @Serial
031    private static final long serialVersionUID = 6964687027713533075L;
032
033    /**
034     * Challenge type name: {@value}
035     */
036    public static final String TYPE = "dns-01";
037
038    /**
039     * The prefix of the domain name to be used for the DNS TXT record.
040     */
041    public static final String RECORD_NAME_PREFIX = "_acme-challenge";
042
043    /**
044     * Creates a new generic {@link Dns01Challenge} object.
045     *
046     * @param login
047     *         {@link Login} the resource is bound with
048     * @param data
049     *         {@link JSON} challenge data
050     */
051    public Dns01Challenge(Login login, JSON data) {
052        super(login, data);
053    }
054
055    /**
056     * Converts a domain identifier to the Resource Record name to be used for the DNS TXT
057     * record.
058     *
059     * @param identifier
060     *         Domain {@link Identifier} of the domain to be validated
061     * @return Resource Record name (e.g. {@code _acme-challenge.www.example.org.}, note
062     * the trailing full stop character).
063     * @since 4.0.0
064     */
065    public String getRRName(Identifier identifier) {
066        return getRRName(identifier.getDomain());
067    }
068
069    /**
070     * Converts a domain identifier to the Resource Record name to be used for the DNS TXT
071     * record.
072     *
073     * @param domain
074     *         Domain name to be validated
075     * @return Resource Record name (e.g. {@code _acme-challenge.www.example.org.}, note
076     * the trailing full stop character).
077     * @since 4.0.0
078     */
079    public String getRRName(String domain) {
080        return RECORD_NAME_PREFIX + '.' + domain + '.';
081    }
082
083    /**
084     * Returns the digest string to be set in the domain's {@code _acme-challenge} TXT
085     * record.
086     */
087    public String getDigest() {
088        return base64UrlEncode(sha256hash(getAuthorization()));
089    }
090
091    @Override
092    protected boolean acceptable(String type) {
093        return TYPE.equals(type);
094    }
095
096}