/* * This file is part of cert-signer * Copyright (c) 2024 Paolo Lulli. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package net.lulli.certsigner; import net.lulli.certsigner.service.CertificateSigningService; import net.lulli.certsigner.util.CSRManager; import net.lulli.certsigner.util.Serde; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.Security; import java.util.Base64; import java.util.Objects; public class Client { private static final String CERTIFICATES_HOME = System.getenv("HOME") + "/.config/tlscerts"; private final String serviceName; private final String clientName; public Client(String serviceName, String clientName) { Objects.requireNonNull(serviceName); Objects.requireNonNull(clientName); this.serviceName = serviceName; this.clientName = clientName; init(); } private void init() { var home = new File(getCertificatesPath()); if (!home.exists()) { home.mkdirs(); } } private String getCertificatesPath() { return CERTIFICATES_HOME + "/" + this.serviceName + "/" + this.clientName; } public String createCsr() { try { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance(Settings.KEY_ALGORITHM); keyGen.initialize(Settings.KEYSIZE); KeyPair keypair = keyGen.genKeyPair(); Serde.toFile(keypair.getPrivate(), getCertificatesPath() + "/" + this.clientName + ".key"); Serde.toFile(keypair.getPublic(), getCertificatesPath() + "/" + this.clientName + ".key.pub"); var subject = "CN=" + this.clientName + ", O=" + this.serviceName + ""; var csrManager = CSRManager.with(subject, keypair.getPrivate(), keypair.getPublic()); var csrFileName = getCertificatesPath() + "/" + this.clientName + ".csr"; saveCsrAs(csrManager.pem(), csrFileName); return csrManager.pem(); } catch (Exception e) { throw new IllegalStateException(e.getMessage()); } } private void saveCsrAs(String content, String filename) { System.out.println("Writing content to file: " + filename); try { Files.write(Paths.get(filename), content.getBytes()); } catch (Exception e) { throw new IllegalStateException(e); } } public String requestCertificate() { try { var client = new Client(serviceName, clientName); var csr = client.createCsr(); var base64Csr = Base64.getEncoder().encodeToString(csr.getBytes()); var certificateSigningService = new CertificateSigningService(serviceName); var certificate = certificateSigningService.sign(clientName, base64Csr); Files.write(Paths.get(CERTIFICATES_HOME + "/" + serviceName + "/" + clientName + "/" + clientName + ".pem"), certificate.getBytes()); return certificate; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } } public static void main(String[] args) { if (args.length != 2){ throw new IllegalStateException("Missing parameters"); } var serviceName = args[0]; var clientName = args[1]; var client = new Client(serviceName, clientName); var certificate = client.requestCertificate(); System.out.printf("Certificate data:\n%s", certificate); } }