From 646db39ffa685557c2efb1dbf574f9fd7873a6af Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Tue, 28 Feb 2012 00:05:53 -0600
Subject: [PATCH] Fixed DRM REST interface to use BigInteger.

The Key ID and request ID previously were Strings. They have been
converted into BigIntegers.

Ticket #94
---
 .../cms/servlet/key/KeyResourceService.java        |   12 ++++++++----
 .../com/netscape/cms/servlet/key/KeysResource.java |    8 ++++----
 .../com/netscape/cms/servlet/key/model/KeyDAO.java |   11 +++++------
 .../servlet/request/KeyRequestResourceService.java |    4 +++-
 .../cms/servlet/request/KeyRequestsResource.java   |   16 ++++++++--------
 .../request/KeyRequestsResourceService.java        |   19 ++++++++++++-------
 .../cms/servlet/request/model/KeyRequestDAO.java   |   13 +++++++------
 .../servlet/request/model/RecoveryRequestData.java |   17 +++++++++--------
 .../netscape/cmscore/cert/PrettyPrintFormat.java   |    3 +++
 .../netscape/cms/servlet/test/DRMRestClient.java   |    9 +++++----
 .../src/com/netscape/cms/servlet/test/DRMTest.java |   12 +++++++-----
 .../util/src/netscape/security/util/BigInt.java    |    4 ++--
 12 files changed, 73 insertions(+), 55 deletions(-)

diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java
index 4888d609f6feb63d39c6d1ad3f26a9fb811d9d4d..748c3dbdd48d6e8516673bdd27eacd78e81252ab 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java
@@ -34,6 +34,9 @@ import com.netscape.cms.servlet.request.model.RecoveryRequestData;
 import com.netscape.certsrv.request.IRequest;
 import com.netscape.certsrv.request.RequestStatus;
 import com.netscape.certsrv.base.EBaseException;
+
+import java.math.BigInteger;
+
 /**
  * @author alee
  * 
@@ -50,7 +53,7 @@ public class KeyResourceService extends CMSResourceService implements KeyResourc
      */
     public KeyData retrieveKey(RecoveryRequestData data) {
         // auth and authz
-        String keyId = validateRequest(data);
+        BigInteger keyId = validateRequest(data);
         KeyDAO dao = new KeyDAO();
         KeyData keyData;
         try {
@@ -73,10 +76,10 @@ public class KeyResourceService extends CMSResourceService implements KeyResourc
         return retrieveKey(data);
     }
     
-    private String validateRequest(RecoveryRequestData data) {
+    private BigInteger validateRequest(RecoveryRequestData data) {
         
         // confirm request exists
-        String reqId = data.getRequestId();
+        BigInteger reqId = data.getRequestId();
         if (reqId == null) {
             // log error
             throw new WebApplicationException(Response.Status.BAD_REQUEST);
@@ -121,7 +124,8 @@ public class KeyResourceService extends CMSResourceService implements KeyResourc
         }
         
         String keyURL = reqInfo.getKeyURL();
-        return keyURL.substring(keyURL.lastIndexOf("/") + 1);
+        String id = keyURL.substring(keyURL.lastIndexOf("/") + 1);
+        return new BigInteger(id);
     }
 
 }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java
index 35cd5ccbe45ab0b78e550c20837de6b8de0e91e1..c93ffa4c913c84958d4b59adcaa70d805caf6359 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResource.java
@@ -10,14 +10,14 @@ import com.netscape.cms.servlet.key.model.KeyDataInfos;
  
 @Path("/keys")
 public interface KeysResource {
-    public static final String DEFAULT_MAXTIME = "10";
-    public static final String DEFAULT_MAXRESULTS = "100";
+    public static final int DEFAULT_MAXTIME = 10;
+    public static final int DEFAULT_MAXRESULTS = 100;
     
     @GET
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
     public KeyDataInfos listKeys(@QueryParam("clientID") String clientID,
                                  @QueryParam("status") String status,
-                                 @DefaultValue(DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
-                                 @DefaultValue(DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
+                                 @DefaultValue(""+DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
+                                 @DefaultValue(""+DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
     
 }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java b/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java
index 6a90a7c8f6c27a769a955494e26d5d6cd54380d4..f5a1e496ab9b5be2bc3f7d1db112612ffc8b4169 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/model/KeyDAO.java
@@ -85,23 +85,22 @@ public class KeyDAO {
         return ret;
     }
     
-    public KeyData getKey(String keyId, RecoveryRequestData data) throws EBaseException {
+    public KeyData getKey(BigInteger keyId, RecoveryRequestData data) throws EBaseException {
         KeyData keyData;
-        BigInteger serial = new BigInteger(keyId);
-        
-        String rId = data.getRequestId();
+
+        BigInteger rId = data.getRequestId();
 
         String transWrappedSessionKey;
         String sessionWrappedPassphrase;
 
-        IRequest  request = queue.findRequest(new RequestId(rId));
+        IRequest request = queue.findRequest(new RequestId(rId.toString()));
 
         if (request == null) {
             return null;
         }
 
      // get wrapped key
-        IKeyRecord rec = repo.readKeyRecord(serial);
+        IKeyRecord rec = repo.readKeyRecord(keyId);
         if (rec == null) {
             return null;  
         }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java
index e18407727f78a4e4e87a8c488d11dd0f70b452e5..e6489d3b5831fd7685841fb8be5a984c5d493bd4 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java
@@ -18,6 +18,8 @@
 
 package com.netscape.cms.servlet.request;
 
+import java.math.BigInteger;
+
 import javax.ws.rs.PathParam;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
@@ -48,7 +50,7 @@ public class KeyRequestResourceService extends CMSResourceService implements Key
         KeyRequestDAO dao = new KeyRequestDAO();
         KeyRequestInfo info;
         try {
-            info = dao.getRequest(id, uriInfo);
+            info = dao.getRequest(new BigInteger(id), uriInfo);
         } catch (EBaseException e) {
             // log error
             e.printStackTrace();
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResource.java b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResource.java
index 0f8fbc78fd664ecb69d2fa407b0217fff5694227..bae764436bd199e4a94d93f33f1a3d647aa55e12 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResource.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResource.java
@@ -11,10 +11,10 @@ import com.netscape.cms.servlet.request.model.KeyRequestInfos;
 @Path("/keyrequests")
 public interface KeyRequestsResource {
 
-    public static final String DEFAULT_START = "0";
-    public static final String DEFAULT_PAGESIZE = "20";
-    public static final String DEFAULT_MAXRESULTS = "100";
-    public static final String DEFAULT_MAXTIME = "10";
+    public static final int DEFAULT_START = 0;
+    public static final int DEFAULT_PAGESIZE = 20;
+    public static final int DEFAULT_MAXRESULTS = 100;
+    public static final int DEFAULT_MAXTIME = 10;
 
     /**
      * Used to generate list of key requests based on the search parameters
@@ -24,9 +24,9 @@ public interface KeyRequestsResource {
     public KeyRequestInfos listRequests(@QueryParam("requestState") String requestState,
                                             @QueryParam("requestType") String requestType,
                                             @QueryParam("clientID") String clientID,
-                                            @DefaultValue(DEFAULT_START) @QueryParam("start") String start_s,
-                                            @DefaultValue(DEFAULT_PAGESIZE) @QueryParam("pageSize") int pageSize,
-                                            @DefaultValue(DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
-                                            @DefaultValue(DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
+                                            @DefaultValue(""+DEFAULT_START) @QueryParam("start") String start_s,
+                                            @DefaultValue(""+DEFAULT_PAGESIZE) @QueryParam("pageSize") int pageSize,
+                                            @DefaultValue(""+DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
+                                            @DefaultValue(""+DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
 
 }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java
index 290868ab5574bafbcdcad6fc7d5f38d3d04085e1..15e41f41b1217ac8fbd22669bc8e680015d4368a 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java
@@ -28,7 +28,9 @@ import com.netscape.certsrv.base.EBaseException;
 import com.netscape.cms.servlet.base.CMSResourceService;
 import com.netscape.cms.servlet.request.model.KeyRequestDAO;
 import com.netscape.cms.servlet.request.model.KeyRequestInfos;
- 
+
+import java.math.BigInteger;
+
 /**
  * @author alee
  *
@@ -50,15 +52,18 @@ public class KeyRequestsResourceService extends CMSResourceService implements Ke
         String filter = createSearchFilter(requestState, requestType, clientID);
         CMS.debug("listRequests: filter is " + filter);
        
-        
         // get start marker
-        int start = Integer.parseInt(KeyRequestsResource.DEFAULT_START);
-        if (start_s != null) {
+        BigInteger start;
+
+        if (start_s == null) {
+            start = BigInteger.valueOf(KeyRequestsResource.DEFAULT_START);
+        } else {
             try {
-                if (start_s.trim().startsWith("0x")) {
-                    start = Integer.parseInt(start_s.trim().substring(2), 16);
+                start_s = start_s.trim();
+                if (start_s.startsWith("0x")) {
+                    start = new BigInteger(start_s.substring(2), 16);
                 } else {
-                    start = Integer.parseInt(start_s.trim());
+                    start = new BigInteger(start_s);
                 }
             } catch (NumberFormatException e) {
                 CMS.debug("listRequests: NumberformatException: Invalid value for start " + start_s);
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/model/KeyRequestDAO.java b/pki/base/common/src/com/netscape/cms/servlet/request/model/KeyRequestDAO.java
index 8a32f5d8feb9e11e48641dfb1fc0104b56fd85d6..7f60a01114d2096f3af4667784ed4681b73da069 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/model/KeyRequestDAO.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/model/KeyRequestDAO.java
@@ -17,6 +17,7 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cms.servlet.request.model;
 
+import java.math.BigInteger;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Hashtable;
@@ -86,7 +87,7 @@ public class KeyRequestDAO {
      * @return collection of key request info
      * @throws EBaseException
      */
-    public KeyRequestInfos listRequests(String filter, int start, int pageSize, int maxResults, int maxTime, 
+    public KeyRequestInfos listRequests(String filter, BigInteger start, int pageSize, int maxResults, int maxTime,
             UriInfo uriInfo) throws EBaseException {
         List <KeyRequestInfo> list = new ArrayList<KeyRequestInfo>();
         List <Link> links = new ArrayList<Link>();
@@ -94,7 +95,7 @@ public class KeyRequestDAO {
         int current = 0;
         
         if (isVLVSearch(filter)) {
-            RequestId id = new RequestId(Integer.toString(start));
+            RequestId id = new RequestId(start.toString());
             IRequestVirtualList vlvlist = queue.getPagedRequestsByFilter(id, false, filter, 
                                                                          pageSize +1 , "requestId");
             totalSize = vlvlist.getSize();
@@ -117,7 +118,7 @@ public class KeyRequestDAO {
                 return null;
             }
             while (requests.hasMoreElements()) {
-                RequestId rid = (RequestId) requests.nextElement();
+                RequestId rid = requests.nextElement();
                 IRequest request = queue.findRequest(rid);
                 if (request != null) {
                     list.add(createKeyRequestInfo(request, uriInfo));
@@ -165,8 +166,8 @@ public class KeyRequestDAO {
      * @return info for specific request
      * @throws EBaseException 
      */
-    public KeyRequestInfo getRequest(String id, UriInfo uriInfo) throws EBaseException {
-        IRequest request = queue.findRequest(new RequestId(id));
+    public KeyRequestInfo getRequest(BigInteger id, UriInfo uriInfo) throws EBaseException {
+        IRequest request = queue.findRequest(new RequestId(id.toString()));
         if (request == null) {
             return null;
         }
@@ -218,7 +219,7 @@ public class KeyRequestDAO {
 
         IRequest request = queue.newRequest(IRequest.SECURITY_DATA_RECOVERY_REQUEST);
 
-        String keyId = data.getKeyId();
+        BigInteger keyId = data.getKeyId();
 
         Hashtable<String, Object> requestParams;
         requestParams = kra.createVolatileRequest(request.getRequestId());
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/model/RecoveryRequestData.java b/pki/base/common/src/com/netscape/cms/servlet/request/model/RecoveryRequestData.java
index ae84175424d8a8bad6cda578b39888d6ae96e24e..631399bfa9813399b3b1363dd5d2b70bee67e1d3 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/model/RecoveryRequestData.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/model/RecoveryRequestData.java
@@ -26,6 +26,7 @@ import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
+import java.math.BigInteger;
 
 /**
  * @author alee
@@ -42,10 +43,10 @@ public class RecoveryRequestData {
     private static final String NONCE_DATA = "nonceData";
 
     @XmlElement
-    protected String keyId;
+    protected BigInteger keyId;
     
     @XmlElement
-    protected String requestId;
+    protected BigInteger requestId;
     
     @XmlElement
     protected String transWrappedSessionKey;
@@ -61,8 +62,8 @@ public class RecoveryRequestData {
     }
     
     public RecoveryRequestData(MultivaluedMap<String, String> form) {
-        keyId = form.getFirst(KEY_ID);
-        requestId = form.getFirst(REQUEST_ID);
+        keyId = new BigInteger(form.getFirst(KEY_ID));
+        requestId = new BigInteger(form.getFirst(REQUEST_ID));
         transWrappedSessionKey = form.getFirst(TRANS_WRAPPED_SESSION_KEY);
         sessionWrappedPassphrase = form.getFirst(SESSION_WRAPPED_PASSPHRASE);
         nonceData = form.getFirst(NONCE_DATA);
@@ -71,28 +72,28 @@ public class RecoveryRequestData {
     /**
      * @return the keyId
      */
-    public String getKeyId() {
+    public BigInteger getKeyId() {
         return keyId;
     }
 
     /**
      * @param keyId the keyId to set
      */
-    public void setKeyId(String keyId) {
+    public void setKeyId(BigInteger keyId) {
         this.keyId = keyId;
     }
 
     /**
      * @return the requestId
      */
-    public String getRequestId() {
+    public BigInteger getRequestId() {
         return requestId;
     }
 
     /**
      * @param requestId the requestId to set
      */
-    public void setRequestId(String requestId) {
+    public void setRequestId(BigInteger requestId) {
         this.requestId = requestId;
     }
 
diff --git a/pki/base/common/src/com/netscape/cmscore/cert/PrettyPrintFormat.java b/pki/base/common/src/com/netscape/cmscore/cert/PrettyPrintFormat.java
index fb97a5ccb2a62c8244cd2d2fa161461950ac8d9b..6692005752516270e74bfadf7ac313d54b7515e5 100644
--- a/pki/base/common/src/com/netscape/cmscore/cert/PrettyPrintFormat.java
+++ b/pki/base/common/src/com/netscape/cmscore/cert/PrettyPrintFormat.java
@@ -104,6 +104,9 @@ public class PrettyPrintFormat implements IPrettyPrintFormat {
 
     public String toHexString(byte[] in, int indentSize,
             int lineLen, String separator) {
+
+        if (in == null) return "";
+
         StringBuffer sb = new StringBuffer();
         int hexCount = 0;
         char c[];
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
index eaf6b98dd1283fc50ddce30443ff5f24e269b89b..6ca227e654831f18019faa58f80d0cad4b4fe82f 100644
--- a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
@@ -1,5 +1,6 @@
 package com.netscape.cms.servlet.test;
 
+import java.math.BigInteger;
 import java.util.Collection;
 import java.util.Iterator;
 
@@ -77,7 +78,7 @@ public class DRMRestClient {
         return null;
     }
     
-    public KeyRequestInfo requestRecovery(String keyId, byte[] rpwd, byte[] rkey, byte[] nonceData) {
+    public KeyRequestInfo requestRecovery(BigInteger keyId, byte[] rpwd, byte[] rkey, byte[] nonceData) {
         // create recovery request
         RecoveryRequestData data = new RecoveryRequestData();
         data.setKeyId(keyId);
@@ -96,11 +97,11 @@ public class DRMRestClient {
         return info;
     }
     
-    public void approveRecovery(String recoveryId) {
-        keyRequestClient.approveRequest(recoveryId);
+    public void approveRecovery(BigInteger recoveryId) {
+        keyRequestClient.approveRequest(recoveryId.toString());
     }
     
-    public KeyData retrieveKey(String keyId, String requestId, byte[] rpwd, byte[] rkey, byte[] nonceData) {
+    public KeyData retrieveKey(BigInteger keyId, BigInteger requestId, byte[] rpwd, byte[] rkey, byte[] nonceData) {
         // create recovery request
         RecoveryRequestData data = new RecoveryRequestData();
         data.setKeyId(keyId);
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
index 368cb72822b0bd453cbb6d6abc0ef3618901f7f9..42ec1ac3fe1125d86602327dee39bbb27da8498b 100644
--- a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
@@ -17,6 +17,7 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cms.servlet.test;
 
+import java.math.BigInteger;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Iterator;
@@ -134,9 +135,9 @@ public class DRMTest {
         String recoveredKey = null;
 
         // various ids used in recovery/archival operations
-        String keyId = null;
+        BigInteger keyId = null;
         String clientId = null;
-        String recoveryRequestId = null;
+        BigInteger recoveryRequestId = null;
 
         // Variables for data structures from calls
         KeyRequestInfo requestInfo = null;
@@ -228,7 +229,7 @@ public class DRMTest {
 
         log("Getting key ID for symmetric key");
         keyInfo = client.getKeyData(clientId, "active");
-        String keyId2 = getId(keyInfo.getKeyURL());
+        BigInteger keyId2 = getId(keyInfo.getKeyURL());
         if (keyId2 == null) {
             log("No archived key found");
         } else {
@@ -453,8 +454,9 @@ public class DRMTest {
         System.out.println(string);
     }
 
-    private static String getId(String link) {
-        return link.substring(link.lastIndexOf("/") + 1);
+    private static BigInteger getId(String link) {
+        String id = link.substring(link.lastIndexOf("/") + 1);
+        return new BigInteger(id);
     }
 
     private static void printRequestInfo(KeyRequestInfo info) {
diff --git a/pki/base/util/src/netscape/security/util/BigInt.java b/pki/base/util/src/netscape/security/util/BigInt.java
index 8f7b0b872ec0036c74cfd77625385ea4bc164956..9210648f124baee90e0e2aaa0a0357b97233552b 100644
--- a/pki/base/util/src/netscape/security/util/BigInt.java
+++ b/pki/base/util/src/netscape/security/util/BigInt.java
@@ -47,7 +47,7 @@ public final class BigInt {
      *            first. CONSUMED.
      */
     public BigInt(byte[] data) {
-        places = (byte[]) data.clone();
+        places = data.clone();
     }
 
     /**
@@ -154,7 +154,7 @@ public final class BigInt {
             zero[0] = (byte) 0;
             return zero;
         } else {
-            return (byte[]) places.clone();
+            return places.clone();
         }
     }
 
-- 
1.7.7.4

