Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit d4bffa0

Browse files
author
Rodrigo Reis
committed
DE388462- Special character handling on CSR generation
1 parent fc2efcd commit d4bffa0

File tree

4 files changed

+26
-40
lines changed

4 files changed

+26
-40
lines changed

mas-foundation/src/androidTest/java/com/ca/mas/foundation/MASRegistrationTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,10 @@ protected MockResponse registerDeviceResponse(RecordedRequest request) {
287287
assertNotNull(getRecordRequest(GatewayDefaultDispatcher.CONNECT_DEVICE_RENEW));
288288
}
289289

290-
290+
@Test
291+
public void testWithSpecialCharacterUserName() throws ExecutionException, InterruptedException {
292+
MASCallbackFuture<MASUser> callback = new MASCallbackFuture<>();
293+
MASUser.login("admin!#$%&'*+-/=?^_`{|}~@ca.com\"", "test".toCharArray(), callback);
294+
assertNotNull(callback.get());
295+
}
291296
}

mas-foundation/src/main/java/com/ca/mas/core/cert/CertUtils.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
import java.io.ByteArrayInputStream;
1515
import java.io.ByteArrayOutputStream;
1616
import java.io.IOException;
17-
import java.security.PrivateKey;
18-
import java.security.PublicKey;
19-
import java.security.Signature;
2017
import java.security.cert.Certificate;
2118
import java.security.cert.CertificateException;
2219
import java.security.cert.CertificateFactory;
@@ -25,9 +22,6 @@
2522
import java.util.Collection;
2623
import java.util.List;
2724

28-
import sun.security.pkcs.PKCS10;
29-
import sun.security.x509.X500Signer;
30-
3125
import static com.ca.mas.foundation.MAS.DEBUG;
3226
import static com.ca.mas.foundation.MAS.TAG;
3327

@@ -72,37 +66,6 @@ public static X509Certificate decodeCertFromPem(String certificateText) throws I
7266
}
7367
}
7468

75-
76-
/**
77-
* Generate a PKCS#10 certificate signing request from the specified parameters.
78-
*
79-
* @param commonName the username. Required.
80-
* @param deviceId the device ID. Required.
81-
* @param deviceName the device name. Required.
82-
* @param organization the organization. Required.
83-
* @param publicKey the client's public key. Required.
84-
* @param privateKey the client's private key. Required.
85-
* @return a signed PKCS#10 CertificationRequest structure in binary DER format. Never null.
86-
* @throws CertificateException if a CSR cannot be created
87-
*/
88-
public static byte[] generateCertificateSigningRequest(String commonName,
89-
String deviceId, String deviceName, String organization,
90-
PublicKey publicKey, PrivateKey privateKey) throws CertificateException {
91-
try {
92-
PKCS10 pkcs10 = new PKCS10(publicKey);
93-
Signature signature = Signature.getInstance("SHA256withRSA");
94-
signature.initSign(privateKey);
95-
sun.security.x509.X500Name x500Name = new sun.security.x509.X500Name("cn=" + commonName + ", ou=" + deviceId + ", dc=" + deviceName + ", o=" + organization);
96-
97-
pkcs10.encodeAndSign(new X500Signer(signature, x500Name));
98-
return pkcs10.getEncoded();
99-
} catch (Exception t) {
100-
if (DEBUG) Log.e(TAG, "Unable to generate certificate signing request: " + t, t);
101-
throw new CertificateException("Unable to generate certificate signing request: " + t);
102-
}
103-
}
104-
105-
10669
/**
10770
* Convert the specified Certificate array into an X509Certificate array.
10871
*

mas-foundation/src/main/java/com/ca/mas/core/security/AndroidJellyBeanKeyRepository.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ public void deleteCertificateChain(String alias) {
103103
@Override
104104
public byte[] generateCertificateSigningRequest(String commonName, String deviceId, String deviceName, String organization, PrivateKey privateKey, PublicKey publicKey) throws CertificateException {
105105
try {
106-
X500Principal subject = new X500Principal("cn=" + commonName + ", ou=" + deviceId + ", dc=" + deviceName + ", o=" + organization);
106+
commonName = commonName.replace("\"", "\\\"");
107+
deviceId = deviceId.replace("\"", "\\\"");
108+
deviceName = deviceName.replace("\"", "\\\"");
109+
organization = organization.replace("\"", "\\\"");
110+
111+
X500Principal subject = new X500Principal("cn=\"" + commonName +
112+
"\", ou=\"" + deviceId +
113+
"\", dc=\"" + deviceName +
114+
"\", o=\"" + organization + "\"");
107115
ASN1Set attrs = new DERSet(new ASN1EncodableVector());
108116
PKCS10CertificationRequest csr = new PKCS10CertificationRequest("SHA1withRSA", subject, publicKey, attrs, privateKey, null);
109117
return csr.getEncoded();

mas-foundation/src/main/java/com/ca/mas/core/security/AndroidKeyStoreRepository.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,17 @@ public byte[] generateCertificateSigningRequest(String commonName, String device
156156
PKCS10 pkcs10 = new PKCS10(publicKey);
157157
Signature signature = Signature.getInstance("SHA256withRSA");
158158
signature.initSign(privateKey);
159-
sun.security.x509.X500Name x500Name = new sun.security.x509.X500Name("cn=" + commonName + ", ou=" + deviceId + ", dc=" + deviceName + ", o=" + organization);
159+
160+
commonName = commonName.replace("\"", "\\\"");
161+
deviceId = deviceId.replace("\"", "\\\"");
162+
deviceName = deviceName.replace("\"", "\\\"");
163+
organization = organization.replace("\"", "\\\"");
164+
165+
sun.security.x509.X500Name x500Name = new sun.security.x509.X500Name(
166+
"cn=\"" + commonName +
167+
"\", ou=\"" + deviceId +
168+
"\", dc=\"" + deviceName +
169+
"\", o=\"" + organization + "\"");
160170

161171
pkcs10.encodeAndSign(new X500Signer(signature, x500Name));
162172
return pkcs10.getEncoded();

0 commit comments

Comments
 (0)