Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Fix sans defaults etc#4

Merged
tr1ck3r merged 5 commits intomasterfrom
fix-sans-defaults-etc
Sep 19, 2019
Merged

Fix sans defaults etc#4
tr1ck3r merged 5 commits intomasterfrom
fix-sans-defaults-etc

Conversation

@tr1ck3r
Copy link
Copy Markdown
Member

@tr1ck3r tr1ck3r commented Sep 13, 2019

No description provided.

tr1ck3r and others added 5 commits June 8, 2019 11:11
Fix defaults for keyType, keyLength, keyCurve, signatureAlgorithm
… handlers don't properly deal with platform differences (e.g. /C:/ path). Fixed bugs in certificate and other generators because bouncy castle doesn't properly handle whitespace per RFC7468. Fixed typos in comments. Set default signature algorithm to SHA256.
Copy link
Copy Markdown

@djivko djivko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review seems good. There are a few suggestions regarding the code style - common methods etc.

ECPublicKey reqEcPublicKey = (ECPublicKey) keyPair.getPublic();

// https://stackoverflow.com/questions/24121801/how-to-verify-if-the-private-key-matches-with-the-certificate
java.security.spec.ECParameterSpec certSpec = certEcPublicKey.getParams(), csrSpec = reqEcPublicKey.getParams();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check and the one bellow are identical worth extracting common method for those as they are not trivial.

String pem = null;
if (!Objects.isNull(this.certificate)) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(outputStream))) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract a common method - private String toPem(String type, byte[] encoded) { .... }. The same code is repeated through out the class.

.stream()
.filter(Objects::nonNull)
.map(entry -> new SANItem().type(type).name(entry.toString()))
.map(entry -> new SANItem().type(type).name( type == 7 ? ((InetAddress)entry).getHostAddress() : entry.toString()) )
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about - entry instanceof InetAddress ? ((InetAddress)entry).getHostAddress() : entry.toString.

request.keyCurve(EllipticCurve.ellipticCurveDefault());
}
if (request.signatureAlgorithm() == SignatureAlgorithm.UnknownSignatureAlgorithm) {
request.signatureAlgorithm(SignatureAlgorithm.ECDSAWithSHA256);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about having defaultECAlg and defaultRSAAlg methods in SignatureAlgorithm class. This will allow us to easily change defaults if ever needed.


default:
if (request.keyLength() < 2048) {
request.keyLength(2048);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some common place to define the default Length

@@ -74,47 +93,14 @@ public void updateCertificateRequest(CertificateRequest request) {
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break in case of match

import java.util.Base64;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x509.X509Extension;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and import below seems to be deprecated, so probably better to replace with org.bouncycastle.asn1.x509.Extension

import org.bouncycastle.asn1.eac.ECDSAPublicKey;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.jce.PKCS10CertificationRequest;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems deprecated and should be imported from org.bouncycastle.pkcs

}

GeneralNames names = new GeneralNames(sans.toArray(new GeneralName[] {}));
Vector oids = new Vector();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vector is a raw type and better to be parameterized. Aplpy for the line below also.

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(outputStream))) {
pemWriter.writeObject(new PemObject("CERTIFICATE", this.certificate.getEncoded()));
} catch (CertificateEncodingException e) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can handle both exception here, i.e catch (CertificateEncodingException | IOException e)

@tr1ck3r tr1ck3r merged commit b7475fb into master Sep 19, 2019
@tr1ck3r tr1ck3r deleted the fix-sans-defaults-etc branch September 19, 2019 16:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants