>From 0f802c4aef67478ada3ceb96beb953299ce11908 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Wed, 5 Feb 2014 10:26:10 -0500
Subject: [PATCH 191/192] Add strength and algorithm to KeyData and KeyInfo
 classes

Make sure these are updated so that clients can get this information
when accessing a symmetric key.  Also allow a default for generation
requests (but not for archival requests).
---
 .../src/com/netscape/certsrv/key/KeyData.java      | 34 ++++++++++++++++++++++
 .../src/com/netscape/certsrv/key/KeyInfo.java      | 10 +++----
 .../src/com/netscape/cmstools/key/KeyCLI.java      |  2 +-
 .../src/com/netscape/cms/servlet/test/DRMTest.java | 13 ++++++++-
 .../netscape/cms/servlet/key/KeyRequestDAO.java    | 25 +++++++++++-----
 .../com/netscape/cms/servlet/key/KeyService.java   | 13 ++++++++-
 6 files changed, 81 insertions(+), 16 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/key/KeyData.java b/base/common/src/com/netscape/certsrv/key/KeyData.java
index b71861470966b7d5900e965cf2c27fce588b3999..82e23ed9e882800a98264a8c3d71491cba2195e2 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyData.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyData.java
@@ -42,6 +42,12 @@ public class KeyData {
     @XmlElement
     String p12Data;
 
+    @XmlElement
+    String algorithm;
+
+    @XmlElement
+    Integer strength;
+
     public KeyData() {
         // required for JAXB (defaults)
     }
@@ -89,4 +95,32 @@ public class KeyData {
     public void setP12Data(String p12Data) {
         this.p12Data = p12Data;
     }
+
+    /**
+     * @return the algorithm
+     */
+    public String getAlgorithm() {
+        return algorithm;
+    }
+
+    /**
+     * @param algorithm the algorithm to set
+     */
+    public void setAlgorithm(String algorithm) {
+        this.algorithm = algorithm;
+    }
+
+    /**
+     * @return the strength
+     */
+    public Integer getStrength() {
+        return strength;
+    }
+
+    /**
+     * @param strength the strength to set
+     */
+    public void setStrength(Integer strength) {
+        this.strength = strength;
+    }
 }
diff --git a/base/common/src/com/netscape/certsrv/key/KeyInfo.java b/base/common/src/com/netscape/certsrv/key/KeyInfo.java
index a4f4e62f3cf5c7ee4d4b9ca2cedb09b2de8ff2ea..897fc3b1df34a88a99db347677f1f8c6681313ba 100644
--- a/base/common/src/com/netscape/certsrv/key/KeyInfo.java
+++ b/base/common/src/com/netscape/certsrv/key/KeyInfo.java
@@ -49,7 +49,7 @@ public class KeyInfo {
     protected String algorithm;
 
     @XmlElement
-    protected Integer size;
+    protected Integer strength;
 
     @XmlElement
     protected String ownerName;
@@ -110,12 +110,12 @@ public class KeyInfo {
         this.algorithm = algorithm;
     }
 
-    public Integer getSize() {
-        return size;
+    public Integer getStrength() {
+        return strength;
     }
 
-    public void setSize(Integer size) {
-        this.size = size;
+    public void setStrength(Integer strength) {
+        this.strength = strength;
     }
 
     public String getOwnerName() {
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
index f205506aa428bd83f86f6880b7d171fa8eb2b48a..03e545f02d0bd3fd25779be2fed54b6fef1634fe 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
@@ -66,7 +66,7 @@ public class KeyCLI extends CLI {
         if (info.getClientID() != null) System.out.println("  Client ID: "+info.getClientID());
         if (info.getStatus() != null) System.out.println("  Status: "+info.getStatus());
         if (info.getAlgorithm() != null) System.out.println("  Algorithm: "+info.getAlgorithm());
-        if (info.getSize() != null) System.out.println("  Size: "+info.getSize());
+        if (info.getStrength() != null) System.out.println("  Size: "+info.getStrength());
         if (info.getOwnerName() != null) System.out.println("  Owner: "+info.getOwnerName());
     }
 
diff --git a/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java b/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
index 05995f614c160875985b781571d42c4e1e02cf49..42ab77c906af6cc48347f49a947794cb20e5b9c9 100644
--- a/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
+++ b/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
@@ -269,6 +269,7 @@ public class DRMTest {
 
         log("Getting key ID for symmetric key");
         keyInfo = client.getKeyData(clientId, "active");
+        printKeyInfo(keyInfo);
         KeyId keyId2 = keyInfo.getKeyId();
         if (keyId2 == null) {
             log("No archived key found");
@@ -378,6 +379,7 @@ public class DRMTest {
         //Test 13: Get keyId for active passphrase with client ID
         log("Getting key ID for passphrase");
         keyInfo = client.getKeyData(clientId, "active");
+        printKeyInfo(keyInfo);
         keyId2 = keyInfo.getKeyId();
         if (keyId2 == null) {
             log("No archived key found");
@@ -572,6 +574,7 @@ public class DRMTest {
         // test 29: Get keyId for active key with client ID
         log("Getting key ID for symmetric key");
         keyInfo = client.getKeyData(clientId, "active");
+        printKeyInfo(keyInfo);
         keyId2 = keyInfo.getKeyId();
         if (keyId2 == null) {
             log("No archived key found");
@@ -661,6 +664,7 @@ public class DRMTest {
         //Test 37: Get keyId for active key with client ID
         log("Getting key ID for symmetric key");
         keyInfo = client.getKeyData(clientId, "active");
+        printKeyInfo(keyInfo);
         keyId2 = keyInfo.getKeyId();
         if (keyId2 == null) {
             log("No archived key found");
@@ -713,8 +717,15 @@ public class DRMTest {
 
     }
 
+    private static void printKeyInfo(KeyInfo keyInfo) {
+        log("Printing keyInfo:");
+        log("ClientID:  " + keyInfo.getClientID());
+        log("Key URL:   " + keyInfo.getKeyURL());
+        log("Algorithm: " + keyInfo.getAlgorithm());
+        log("Strength:  " + keyInfo.getStrength());
+    }
+
     private static void log(String string) {
-        // TODO Auto-generated method stub
         System.out.println(string);
     }
 
diff --git a/base/server/cms/src/com/netscape/cms/servlet/key/KeyRequestDAO.java b/base/server/cms/src/com/netscape/cms/servlet/key/KeyRequestDAO.java
index 7d45420a4eba6624799e09801da4e4916b934d23..ac728d5931eab4ce1529ac8a595c3bebedfaa61c 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/key/KeyRequestDAO.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/key/KeyRequestDAO.java
@@ -218,8 +218,8 @@ public class KeyRequestDAO extends CMSRequestDAO {
         int size = data.getKeySize();
         List<String> usages = data.getUsages();
 
-        if (StringUtils.isBlank(clientId) || StringUtils.isBlank(algName) || (size<=0)) {
-            throw new BadRequestException("Invalid key generation request. Missing parameters");
+        if (StringUtils.isBlank(clientId)) {
+            throw new BadRequestException("Invalid key generation request. Missing clientId");
         }
 
         boolean keyExists = doesKeyExist(clientId, "active", uriInfo);
@@ -227,13 +227,22 @@ public class KeyRequestDAO extends CMSRequestDAO {
             throw new BadRequestException("Can not archive already active existing key!");
         }
 
-        KeyGenAlgorithm alg = KeyRequestService.KEYGEN_ALGORITHMS.get(algName);
-        if (alg == null) {
-            throw new BadRequestException("Invalid Algorithm");
-        }
+        if (StringUtils.isBlank(algName)) {
+            if (size != 0) {
+                throw new BadRequestException(
+                        "Invalid request.  Must specify key algorithm if size is specified");
+            }
+            algName = KeyRequestResource.AES_ALGORITHM;
+            size = 128;
+        } else {
+            KeyGenAlgorithm alg = KeyRequestService.KEYGEN_ALGORITHMS.get(algName);
+            if (alg == null) {
+                throw new BadRequestException("Invalid Algorithm");
+            }
 
-        if (!alg.isValidStrength(size)) {
-            throw new BadRequestException("Invalid key size for this algorithm");
+            if (!alg.isValidStrength(size)) {
+                throw new BadRequestException("Invalid key size for this algorithm");
+            }
         }
 
         IRequest request = queue.newRequest(IRequest.SYMKEY_GENERATION_REQUEST);
diff --git a/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java b/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java
index 56e4f3c4d42d63ff9b77494478d74eaddd474954..c91c3157a1d271951d812e01e2bf183bd0ae65ac 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/key/KeyService.java
@@ -245,6 +245,17 @@ public class KeyService extends PKIService implements KeyResource {
             keyData.setNonceData(nonceData);
         }
 
+        String algorithm = rec.getAlgorithm();
+        Integer keySize = rec.getKeySize();
+
+        if (algorithm != null) {
+            keyData.setAlgorithm(algorithm);
+        }
+
+        if (keySize != null) {
+            keyData.setStrength(keySize);
+        }
+
         kra.destroyVolatileRequest(request.getRequestId());
 
         queue.markAsServiced(request);
@@ -372,7 +383,7 @@ public class KeyService extends PKIService implements KeyResource {
         ret.setClientID(rec.getClientId());
         ret.setStatus(rec.getKeyStatus());
         ret.setAlgorithm(rec.getAlgorithm());
-        ret.setSize(rec.getKeySize());
+        ret.setStrength(rec.getKeySize());
         ret.setOwnerName(rec.getOwnerName());
 
         Path keyPath = KeyResource.class.getAnnotation(Path.class);
-- 
1.8.4.2

