>From 7cf2ad70d17c93ff0597145c41c94c2c72512b65 Mon Sep 17 00:00:00 2001
From: Andrew Wnuk <awnuk@redhat.com>
Date: Wed, 14 Aug 2013 19:12:30 -0400
Subject: [PATCH] PKCS10Client enhancement

This patch provides enhancement to PKCS10Client allowing to control encoding for components of the subject name.

Ticket #677.
---
 .../src/com/netscape/cmstools/PKCS10Client.java    | 68 +++++++++++++++++++---
 1 file changed, 61 insertions(+), 7 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/PKCS10Client.java b/base/java-tools/src/com/netscape/cmstools/PKCS10Client.java
index cbe5f3f..b963133 100644
--- a/base/java-tools/src/com/netscape/cmstools/PKCS10Client.java
+++ b/base/java-tools/src/com/netscape/cmstools/PKCS10Client.java
@@ -30,11 +30,15 @@ import netscape.security.x509.X500Name;
 import netscape.security.x509.X509Key;
 
 import org.mozilla.jss.CryptoManager;
+import org.mozilla.jss.asn1.BMPString;
 import org.mozilla.jss.asn1.INTEGER;
 import org.mozilla.jss.asn1.OBJECT_IDENTIFIER;
 import org.mozilla.jss.asn1.OCTET_STRING;
 import org.mozilla.jss.asn1.PrintableString;
 import org.mozilla.jss.asn1.SET;
+import org.mozilla.jss.asn1.TeletexString;
+import org.mozilla.jss.asn1.UTF8String;
+import org.mozilla.jss.asn1.UniversalString;
 import org.mozilla.jss.crypto.CryptoToken;
 import org.mozilla.jss.crypto.KeyPairAlgorithm;
 import org.mozilla.jss.crypto.KeyPairGenerator;
@@ -326,6 +330,30 @@ public class PKCS10Client {
 
     }
 
+    static Name addNameElement (Name name, OBJECT_IDENTIFIER oid, int n, String elementValue) {
+        try {
+            String encodingType = (n > 0)? elementValue.substring(0, n): null;
+            String nameValue = (n > 0)? elementValue.substring(n+1): null;
+            if (encodingType != null && encodingType.length() > 0 &&
+                nameValue != null && nameValue.length() > 0) {
+                if (encodingType.equals("UTF8String")) {
+                    name.addElement( new AVA(oid, new UTF8String(nameValue)));
+                } else if (encodingType.equals("PrintableString")) {
+                    name.addElement( new AVA(oid, new PrintableString(nameValue)));
+                } else if (encodingType.equals("BMPString")) {
+                    name.addElement( new AVA(oid, new BMPString(nameValue)));
+                } else if (encodingType.equals("TeletexString")) {
+                    name.addElement( new AVA(oid, new TeletexString(nameValue)));
+                } else if (encodingType.equals("UniversalString")) {
+                    name.addElement( new AVA(oid, new UniversalString(nameValue)));
+                }
+            }
+        }  catch (Exception e)  {
+            System.out.println("PKCS10Client: Error adding name element: " + elementValue + " Error: "  + e.toString());
+        }
+        return name;
+    }
+
     static Name getJssName(String dn) {
 
         X500Name x5Name = null;
@@ -353,11 +381,17 @@ public class PKCS10Client {
 
             if (split.length != 2)
                 continue;
+            int n = split[1].indexOf(':');
 
             try {
                 if (split[0].equals("UID")) {
-                    ret.addElement(new AVA(new OBJECT_IDENTIFIER("0.9.2342.19200300.100.1.1"), new PrintableString(
-                            split[1])));
+                    if (n > 0) {
+                        ret = addNameElement(ret, new OBJECT_IDENTIFIER("0.9.2342.19200300.100.1.1"),
+                                             n, split[1]);
+                    } else {
+                        ret.addElement(new AVA(new OBJECT_IDENTIFIER("0.9.2342.19200300.100.1.1"),
+                                               new PrintableString(split[1])));
+                    }
                     //                 System.out.println("UID found : " + split[1]);
                 }
 
@@ -368,31 +402,51 @@ public class PKCS10Client {
                 }
 
                 if (split[0].equals("CN")) {
-                    ret.addCommonName(split[1]);
+                    if (n > 0) {
+                        ret = addNameElement (ret, Name.commonName, n, split[1]);
+                    } else {
+                        ret.addCommonName(split[1]);
+                    }
                     //                  System.out.println("CN found : " + split[1]);
                     continue;
                 }
 
                 if (split[0].equals("L")) {
-                    ret.addLocalityName(split[1]);
+                    if (n > 0) {
+                        ret = addNameElement (ret, Name.localityName, n, split[1]);
+                    } else {
+                        ret.addLocalityName(split[1]);
+                    }
                     //                 System.out.println("L found : " + split[1]);
                     continue;
                 }
 
                 if (split[0].equals("O")) {
-                    ret.addOrganizationName(split[1]);
+                    if (n > 0) {
+                        ret = addNameElement (ret, Name.organizationName, n, split[1]);
+                    } else {
+                        ret.addOrganizationName(split[1]);
+                    }
                     //                System.out.println("O found : " + split[1]);
                     continue;
                 }
 
                 if (split[0].equals("ST")) {
-                    ret.addStateOrProvinceName(split[1]);
+                    if (n > 0) {
+                        ret = addNameElement (ret, Name.stateOrProvinceName, n, split[1]);
+                    } else {
+                        ret.addStateOrProvinceName(split[1]);
+                    }
                     //               System.out.println("ST found : " + split[1]);
                     continue;
                 }
 
                 if (split[0].equals("OU")) {
-                    ret.addOrganizationalUnitName(split[1]);
+                    if (n > 0) {
+                        ret = addNameElement (ret, Name.organizationalUnitName, n, split[1]);
+                    } else {
+                        ret.addOrganizationalUnitName(split[1]);
+                    }
                     //              System.out.println("OU found : " + split[1]);
                     continue;
                 }
-- 
1.8.1.4

