clean up module info

This commit is contained in:
Jörg Prante 2022-11-02 22:01:27 +01:00
parent cdc4639347
commit 3049d51e96
7 changed files with 46 additions and 50 deletions

View file

@ -22,10 +22,11 @@ jar {
}
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xlint:all,-exports')
options.compilerArgs.add('-Xlint:all')
options.encoding = 'UTF-8'
}
javadoc {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}

View file

@ -2,7 +2,7 @@ import org.xbib.net.security.CertificateProvider;
import org.xbib.net.bouncycastle.BouncyCastleCertificateProvider;
module org.xbib.net.bouncycastle {
requires org.xbib.net.security;
requires transitive org.xbib.net.security;
requires org.bouncycastle.pkix;
requires org.bouncycastle.provider;
exports org.xbib.net.bouncycastle;

View file

@ -5,7 +5,6 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.operator.OperatorCreationException;
import org.xbib.net.security.CertificateProvider;
import java.io.IOException;
@ -13,8 +12,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.SecureRandom;
@ -55,12 +52,8 @@ public class BouncyCastleCertificateProvider implements CertificateProvider {
@Override
public Map.Entry<PrivateKey, Collection<? extends X509Certificate>> provideSelfSigned(String fullQualifiedDomainName) throws CertificateException, IOException {
try {
SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate();
selfSignedCertificate.generate(fullQualifiedDomainName, secureRandom, 2048);
return Map.entry(selfSignedCertificate.getPrivateKey(), List.of(selfSignedCertificate.getCertificate()));
} catch (NoSuchProviderException | NoSuchAlgorithmException | OperatorCreationException e) {
throw new IOException(e);
}
SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate();
selfSignedCertificate.generate(fullQualifiedDomainName, secureRandom, 2048);
return Map.entry(selfSignedCertificate.getPrivateKey(), List.of(selfSignedCertificate.getCertificate()));
}
}

View file

@ -70,44 +70,45 @@ public final class SelfSignedCertificate {
* @param fqdn a fully qualified domain name
* @param random the {@link SecureRandom} to use
* @param bits the number of bits of the generated private key
* @throws NoSuchAlgorithmException if algorithm does not exist
* @throws NoSuchProviderException if provider does not exist
* @throws OperatorCreationException if provider does not exist
* @throws IOException if generation fails
*/
public void generate(String fqdn, SecureRandom random, int bits)
throws IOException, NoSuchProviderException, NoSuchAlgorithmException, OperatorCreationException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
keyGen.initialize(bits, random);
KeyPair keypair = keyGen.generateKeyPair();
this.key = keypair.getPrivate();
X500Name name = new X500Name("CN=" + fqdn);
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(keypair.getPublic().getEncoded());
X509v3CertificateBuilder certificateBuilder =
new X509v3CertificateBuilder(name, BigInteger.valueOf(System.currentTimeMillis()),
DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER, name, subjectPublicKeyInfo);
AlgorithmIdentifier sigAlgId =
new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WithRSAEncryption");
AlgorithmIdentifier digestAlgId =
new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
AsymmetricKeyParameter caPrivateKeyParameters = PrivateKeyFactory.createKey(key.getEncoded());
ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digestAlgId)
.build(caPrivateKeyParameters);
this.cert = certificateBuilder.build(contentSigner);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(BEGIN_KEY.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
writeEncoded(key.getEncoded(), outputStream);
outputStream.write(END_KEY.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
this.keyBytes = outputStream.toByteArray();
outputStream = new ByteArrayOutputStream();
outputStream.write(BEGIN_CERT.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
writeEncoded(cert.getEncoded(), outputStream);
outputStream.write(END_CERT.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
this.certBytes = outputStream.toByteArray();
throws IOException {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
keyGen.initialize(bits, random);
KeyPair keypair = keyGen.generateKeyPair();
this.key = keypair.getPrivate();
X500Name name = new X500Name("CN=" + fqdn);
SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(keypair.getPublic().getEncoded());
X509v3CertificateBuilder certificateBuilder =
new X509v3CertificateBuilder(name, BigInteger.valueOf(System.currentTimeMillis()),
DEFAULT_NOT_BEFORE, DEFAULT_NOT_AFTER, name, subjectPublicKeyInfo);
AlgorithmIdentifier sigAlgId =
new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WithRSAEncryption");
AlgorithmIdentifier digestAlgId =
new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
AsymmetricKeyParameter caPrivateKeyParameters = PrivateKeyFactory.createKey(key.getEncoded());
ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digestAlgId)
.build(caPrivateKeyParameters);
this.cert = certificateBuilder.build(contentSigner);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(BEGIN_KEY.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
writeEncoded(key.getEncoded(), outputStream);
outputStream.write(END_KEY.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
this.keyBytes = outputStream.toByteArray();
outputStream = new ByteArrayOutputStream();
outputStream.write(BEGIN_CERT.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
writeEncoded(cert.getEncoded(), outputStream);
outputStream.write(END_CERT.getBytes(StandardCharsets.US_ASCII));
outputStream.write('\n');
this.certBytes = outputStream.toByteArray();
} catch (NoSuchProviderException | NoSuchAlgorithmException | OperatorCreationException e) {
throw new IOException(e);
}
}
/**

View file

@ -11,5 +11,6 @@ module org.xbib.net.security {
exports org.xbib.net.security.eddsa.spec;
exports org.xbib.net.security.signatures;
exports org.xbib.net.security.ssl;
exports org.xbib.net.security.ssl.trustmanager;
exports org.xbib.net.security.util;
}

View file

@ -1,6 +1,6 @@
module org.xbib.net.socket {
requires java.logging;
requires com.sun.jna;
requires transitive com.sun.jna;
exports org.xbib.net.socket;
exports org.xbib.net.socket.v4;
exports org.xbib.net.socket.v4.bsd;

View file

@ -15,7 +15,7 @@ module org.xbib.net {
exports org.xbib.net.util;
requires transitive org.xbib.datastructures.common;
requires java.management;
requires java.logging;
requires transitive java.logging;
uses DataBufferFactory;
provides DataBufferFactory with DefaultDataBufferFactory;
}