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