-
Notifications
You must be signed in to change notification settings - Fork 818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: peer cert as username #775
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46397ae
feat: peer cert as username
MikeDombo f4e2431
chore: address parts of PR feedback for formatting
MikeDombo 307d4c9
test: add test with real tls for peer cert as username
MikeDombo 0c70e8f
test: extract new cert as username test
MikeDombo e14a8d5
chore: address PR comment
MikeDombo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
broker/src/main/java/io/moquette/broker/security/PemUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2023 The original author or authors | ||
* ------------------------------------------------------ | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* The Apache License v2.0 is available at | ||
* http://www.opensource.org/licenses/apache2.0.php | ||
* | ||
* You may elect to redistribute this code under either of these licenses. | ||
*/ | ||
|
||
package io.moquette.broker.security; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.util.Base64; | ||
|
||
public final class PemUtils { | ||
private static final String certificateBoundaryType = "CERTIFICATE"; | ||
|
||
public static String certificatesToPem(Certificate... certificates) | ||
throws CertificateEncodingException, IOException { | ||
try (StringWriter str = new StringWriter(); | ||
PemWriter pemWriter = new PemWriter(str)) { | ||
for (Certificate certificate : certificates) { | ||
pemWriter.writeObject(certificateBoundaryType, certificate.getEncoded()); | ||
} | ||
pemWriter.close(); | ||
return str.toString(); | ||
} | ||
} | ||
|
||
/** | ||
* Copyright (c) 2000 - 2021 The Legion of the Bouncy Castle Inc. (https://www.bouncycastle.org) | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* <p>A generic PEM writer, based on RFC 1421 | ||
* From: https://javadoc.io/static/org.bouncycastle/bcprov-jdk15on/1.62/org/bouncycastle/util/io/pem/PemWriter.html</p> | ||
*/ | ||
public static class PemWriter extends BufferedWriter { | ||
private static final int LINE_LENGTH = 64; | ||
private final char[] buf = new char[LINE_LENGTH]; | ||
|
||
/** | ||
MikeDombo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Base constructor. | ||
* | ||
* @param out output stream to use. | ||
*/ | ||
public PemWriter(Writer out) { | ||
super(out); | ||
} | ||
|
||
/** | ||
MikeDombo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Writes a pem encoded string. | ||
* | ||
* @param type key type. | ||
* @param bytes encoded string | ||
* @throws IOException IO Exception | ||
*/ | ||
public void writeObject(String type, byte[] bytes) throws IOException { | ||
writePreEncapsulationBoundary(type); | ||
writeEncoded(bytes); | ||
writePostEncapsulationBoundary(type); | ||
} | ||
MikeDombo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private void writeEncoded(byte[] bytes) throws IOException { | ||
bytes = Base64.getEncoder().encode(bytes); | ||
for (int i = 0; i < bytes.length; i += buf.length) { | ||
int index = 0; | ||
while (index != buf.length) { | ||
if ((i + index) >= bytes.length) { | ||
break; | ||
} | ||
buf[index] = (char) bytes[i + index]; | ||
index++; | ||
} | ||
this.write(buf, 0, index); | ||
this.newLine(); | ||
} | ||
} | ||
MikeDombo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private void writePreEncapsulationBoundary(String type) throws IOException { | ||
this.write("-----BEGIN " + type + "-----"); | ||
this.newLine(); | ||
} | ||
MikeDombo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private void writePostEncapsulationBoundary(String type) throws IOException { | ||
this.write("-----END " + type + "-----"); | ||
this.newLine(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But
pemWriter
isn't closed automatically by thetry
statement?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is closed automatically yes, however it needs to be closed before calling
str.toString()
or else thestr
will not contain the correct contents due to the buffered nature ofBufferedWriter
whichPemWriter
extends.