001/* 002 * acme4j - Java ACME client 003 * 004 * Copyright (C) 2018 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.sha256hash; 017 018import java.io.IOException; 019import java.io.Serial; 020import java.security.KeyPair; 021import java.security.cert.X509Certificate; 022 023import org.shredzone.acme4j.Identifier; 024import org.shredzone.acme4j.Login; 025import org.shredzone.acme4j.toolbox.JSON; 026import org.shredzone.acme4j.util.CertificateUtils; 027 028/** 029 * Implements the {@value TYPE} challenge. It requires a specific certificate that can be 030 * retrieved from the domain via HTTPS request. See the acme4j documentation for a 031 * detailed explanation. 032 * 033 * @since 2.1 034 */ 035public class TlsAlpn01Challenge extends TokenChallenge { 036 @Serial 037 private static final long serialVersionUID = -5590351078176091228L; 038 039 /** 040 * Challenge type name: {@value} 041 */ 042 public static final String TYPE = "tls-alpn-01"; 043 044 /** 045 * OID of the {@code acmeValidation} extension. 046 */ 047 public static final String ACME_VALIDATION_OID = "1.3.6.1.5.5.7.1.31"; 048 049 /** 050 * {@code acme-tls/1} protocol. 051 */ 052 public static final String ACME_TLS_1_PROTOCOL = "acme-tls/1"; 053 054 /** 055 * Creates a new generic {@link TlsAlpn01Challenge} object. 056 * 057 * @param login 058 * {@link Login} the resource is bound with 059 * @param data 060 * {@link JSON} challenge data 061 */ 062 public TlsAlpn01Challenge(Login login, JSON data) { 063 super(login, data); 064 } 065 066 /** 067 * Returns the value that is to be used as {@code acmeValidation} extension in 068 * the test certificate. 069 */ 070 public byte[] getAcmeValidation() { 071 return sha256hash(getAuthorization()); 072 } 073 074 /** 075 * Creates a self-signed {@link X509Certificate} for this challenge. The certificate 076 * is valid for 7 days. 077 * 078 * @param keypair 079 * A domain {@link KeyPair} to be used for the challenge 080 * @param id 081 * The {@link Identifier} that is to be validated 082 * @return Created certificate 083 * @since 3.0.0 084 */ 085 public X509Certificate createCertificate(KeyPair keypair, Identifier id) { 086 try { 087 return CertificateUtils.createTlsAlpn01Certificate(keypair, id, getAcmeValidation()); 088 } catch (IOException ex) { 089 throw new IllegalArgumentException("Bad certificate parameters", ex); 090 } 091 } 092 093 @Override 094 protected boolean acceptable(String type) { 095 return TYPE.equals(type); 096 } 097 098}