>From d2b23bf9df2c48dbaa61ac03c6ad1beb918c01f1 Mon Sep 17 00:00:00 2001
From: Andrew Wnuk <awnuk@redhat.com>
Date: Thu, 12 Jan 2012 12:49:08 -0800
Subject: [PATCH] Big numbers fix for CA and DRM.

This patch resolves multiple issues related to use of big numbers on CA and DRM
It also provides a fix for incomplete recovery requests causing null pointer exception.

Bugs: 756133, 758505.
---
 .../com/netscape/cms/servlet/cert/DoRevoke.java    |   16 ++---
 .../com/netscape/cms/servlet/cert/DoRevokeTPS.java |    5 +-
 .../com/netscape/cms/servlet/cert/DoUnrevoke.java  |    4 +-
 .../netscape/cms/servlet/cert/DoUnrevokeTPS.java   |    4 +-
 .../cms/servlet/cert/GetCertFromRequest.java       |    3 +-
 .../cms/servlet/key/ConfirmRecoverBySerial.java    |   12 ++--
 .../netscape/cms/servlet/key/DisplayBySerial.java  |   10 +--
 .../servlet/key/DisplayBySerialForRecovery.java    |   10 +--
 .../netscape/cms/servlet/key/KeyRecordParser.java  |    5 +-
 .../netscape/cms/servlet/key/RecoverBySerial.java  |    8 +-
 .../netscape/cms/servlet/request/CheckRequest.java |    3 +-
 .../cms/servlet/request/ProcessCertReq.java        |   23 +++----
 .../netscape/cms/servlet/request/ProcessReq.java   |   18 +++---
 .../com/netscape/cms/servlet/request/QueryReq.java |   70 +++++++++++--------
 .../netscape/cms/servlet/request/ReqParser.java    |    5 +-
 15 files changed, 98 insertions(+), 98 deletions(-)

diff --git a/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevoke.java b/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevoke.java
index 4c11d477d1b558a68516d08dc853195d4e5bf9a1..97a70fac3c33386d2b403995e0c51b6d2097d02c 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevoke.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevoke.java
@@ -24,6 +24,7 @@ import java.util.Date;
 import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Vector;
+import java.math.BigInteger;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -1161,31 +1162,28 @@ public class DoRevoke extends CMSServlet {
 
             // find out if the value is hex or decimal
 
-            int value = -1;
+            BigInteger value = BigInteger.ONE.negate();
 
             //try int 
             try {
-                value = Integer.parseInt(serialNumber, 10);
+                value = new BigInteger(serialNumber, 10);
             } catch (NumberFormatException e) {
             }
 
             //try hex
-            if (value == -1) {
+            if (value.compareTo(BigInteger.ONE.negate()) == 0) {
                 try {
-                    value = Integer.parseInt(serialNumber, 16);
-
+                    value = new BigInteger(serialNumber, 16);
                 } catch (NumberFormatException e) {
                 }
             }
             // give up if it isn't hex or dec
-            if (value == -1) {
+            if (value.compareTo(BigInteger.ONE.negate()) == 0) {
                 throw new NumberFormatException();
             }
 
             // convert it to hexadecimal
-            serialNumber = "0x"
-                    + Integer.toHexString(
-                            value);
+            serialNumber = "0x" + value.toString(16);
         } else {
             serialNumber = ILogger.SIGNED_AUDIT_EMPTY_VALUE;
         }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevokeTPS.java b/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevokeTPS.java
index f17e576d657d824f2eadae33c29e67eaee88127a..075f3218b8f45b2e17e8607e347d54899aaac4a4 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevokeTPS.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/cert/DoRevokeTPS.java
@@ -23,6 +23,7 @@ import java.util.Date;
 import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Vector;
+import java.math.BigInteger;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -917,9 +918,7 @@ public class DoRevokeTPS extends CMSServlet {
             serialNumber = eeSerialNumber.trim();
 
             // convert it to hexadecimal
-            serialNumber = "0x"
-                    + Integer.toHexString(
-                            Integer.valueOf(serialNumber).intValue());
+            serialNumber = "0x" + (new BigInteger(serialNumber)).toString(16);
         } else {
             serialNumber = ILogger.SIGNED_AUDIT_EMPTY_VALUE;
         }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevoke.java b/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevoke.java
index 656d36f858441a189b44f2e8d39d395393a2e8ba..edf04b1baccc4427fef6bb0e92217091c7407a48 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevoke.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevoke.java
@@ -662,9 +662,7 @@ public class DoUnrevoke extends CMSServlet {
             serialNumber = eeSerialNumber.trim();
 
             // convert it to hexadecimal
-            serialNumber = "0x"
-                    + Integer.toHexString(
-                            Integer.valueOf(serialNumber).intValue());
+            serialNumber = "0x" + (new BigInteger(serialNumber)).toString(16);
         } else {
             serialNumber = ILogger.SIGNED_AUDIT_EMPTY_VALUE;
         }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevokeTPS.java b/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevokeTPS.java
index 3af743a95c927c9894216b7e0e5e57e726031020..edc40613713c938d16e37fe1f7c28c19fb04dc4c 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevokeTPS.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/cert/DoUnrevokeTPS.java
@@ -615,9 +615,7 @@ public class DoUnrevokeTPS extends CMSServlet {
             serialNumber = eeSerialNumber.trim();
 
             // convert it to hexadecimal
-            serialNumber = "0x"
-                    + Integer.toHexString(
-                            Integer.valueOf(serialNumber).intValue());
+            serialNumber = "0x" + (new BigInteger(serialNumber)).toString(16);
         } else {
             serialNumber = ILogger.SIGNED_AUDIT_EMPTY_VALUE;
         }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/cert/GetCertFromRequest.java b/pki/base/common/src/com/netscape/cms/servlet/cert/GetCertFromRequest.java
index 67481e7df5a356e98ab01bc1efda6d93e5635b43..71b0004cd682f8241d6cd81871bcfe5f2efe861b 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/cert/GetCertFromRequest.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/cert/GetCertFromRequest.java
@@ -19,6 +19,7 @@ package com.netscape.cms.servlet.cert;
 
 import java.io.IOException;
 import java.util.Locale;
+import java.math.BigInteger;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -169,7 +170,7 @@ public class GetCertFromRequest extends CMSServlet {
         }
         // check if request Id is valid.
         try {
-            Integer.parseInt(requestId);
+            new BigInteger(requestId);
         } catch (NumberFormatException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSGW_INVALID_REQ_ID_FORMAT", requestId));
             throw new EBaseException(
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/ConfirmRecoverBySerial.java b/pki/base/common/src/com/netscape/cms/servlet/key/ConfirmRecoverBySerial.java
index d7c3ffaefeaf2dc38baa47f57c71421f0358c4f3..15e73179e94f171cfd14047649ab482d83145727 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/ConfirmRecoverBySerial.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/ConfirmRecoverBySerial.java
@@ -130,12 +130,11 @@ public class ConfirmRecoverBySerial extends CMSServlet {
         IArgBlock fixed = CMS.createArgBlock();
         CMSTemplateParams argSet = new CMSTemplateParams(header, fixed);
 
-        int seqNum = -1;
+        BigInteger seqNum = BigInteger.ZERO;
 
         try {
             if (req.getParameter(IN_SERIALNO) != null) {
-                seqNum = Integer.parseInt(
-                            req.getParameter(IN_SERIALNO));
+                seqNum = new BigInteger(req.getParameter(IN_SERIALNO));
             }
 
             // make sure this page, which contains password
@@ -167,11 +166,11 @@ public class ConfirmRecoverBySerial extends CMSServlet {
      * Requests for a list of agent passwords.
      */
     private void process(CMSTemplateParams argSet,
-            IArgBlock header, int seq,
+            IArgBlock header, BigInteger seq,
             HttpServletRequest req, HttpServletResponse resp,
             Locale locale) {
         try {
-            header.addIntegerValue(OUT_SERIALNO, seq);
+            header.addBigIntegerValue(OUT_SERIALNO, seq, 10);
             header.addIntegerValue(OUT_M,
                     mRecoveryService.getNoOfRequiredAgents());
             header.addStringValue(OUT_OP,
@@ -179,8 +178,7 @@ public class ConfirmRecoverBySerial extends CMSServlet {
             header.addStringValue(OUT_SERVICE_URL,
                     req.getRequestURI());
 
-            IKeyRecord rec = (IKeyRecord) mKeyDB.readKeyRecord(new BigInteger(
-                        Integer.toString(seq)));
+            IKeyRecord rec = (IKeyRecord) mKeyDB.readKeyRecord(seq);
 
             KeyRecordParser.fillRecordIntoArg(rec, header);
         } catch (EBaseException e) {
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerial.java b/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerial.java
index a3490d893ac1a01b47d292e1847f323cdda13b5c..8876d935056490b228a5cbe458c5f9a77291138f 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerial.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerial.java
@@ -147,12 +147,11 @@ public class DisplayBySerial extends CMSServlet {
         IArgBlock header = CMS.createArgBlock();
         IArgBlock fixed = CMS.createArgBlock();
         CMSTemplateParams argSet = new CMSTemplateParams(header, fixed);
-        int seqNum = -1;
+        BigInteger seqNum = BigInteger.ZERO;
 
         try {
             if (req.getParameter(IN_SERIALNO) != null) {
-                seqNum = Integer.parseInt(
-                            req.getParameter(IN_SERIALNO));
+                seqNum = new BigInteger(req.getParameter(IN_SERIALNO));
             }
             process(argSet, header, seqNum, req, resp, locale[0]);
         } catch (NumberFormatException e) {
@@ -177,7 +176,7 @@ public class DisplayBySerial extends CMSServlet {
      * Display information about a particular key.
      */
     private void process(CMSTemplateParams argSet,
-            IArgBlock header, int seq,
+            IArgBlock header, BigInteger seq,
             HttpServletRequest req, HttpServletResponse resp,
             Locale locale) {
         try {
@@ -185,8 +184,7 @@ public class DisplayBySerial extends CMSServlet {
                     req.getParameter(OUT_OP));
             header.addStringValue(OUT_SERVICE_URL,
                     req.getRequestURI());
-            IKeyRecord rec = (IKeyRecord) mKeyDB.readKeyRecord(new
-                    BigInteger(Integer.toString(seq)));
+            IKeyRecord rec = (IKeyRecord) mKeyDB.readKeyRecord(seq);
 
             KeyRecordParser.fillRecordIntoArg(rec, header);
         } catch (EBaseException e) {
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerialForRecovery.java b/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerialForRecovery.java
index 1ef0ba40b7bfe8db3f7a40a5b91201524f3b7b16..29cc2b3b30a9deac6180673efd07c3cefab75160 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerialForRecovery.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/DisplayBySerialForRecovery.java
@@ -150,12 +150,11 @@ public class DisplayBySerialForRecovery extends CMSServlet {
         IArgBlock fixed = CMS.createArgBlock();
         CMSTemplateParams argSet = new CMSTemplateParams(header, fixed);
 
-        int seqNum = -1;
+        BigInteger seqNum = BigInteger.ZERO;
 
         try {
             if (req.getParameter(IN_SERIALNO) != null) {
-                seqNum = Integer.parseInt(
-                            req.getParameter(IN_SERIALNO));
+                seqNum = new BigInteger(req.getParameter(IN_SERIALNO));
             }
             process(argSet, header,
                     req.getParameter("publicKeyData"),
@@ -185,7 +184,7 @@ public class DisplayBySerialForRecovery extends CMSServlet {
      * Display information about a particular key.
      */
     private synchronized void process(CMSTemplateParams argSet,
-            IArgBlock header, String publicKeyData, int seq,
+            IArgBlock header, String publicKeyData, BigInteger seq,
             HttpServletRequest req, HttpServletResponse resp,
             Locale locale) {
         try {
@@ -201,8 +200,7 @@ public class DisplayBySerialForRecovery extends CMSServlet {
                 header.addStringValue("publicKeyData",
                         publicKeyData);
             }
-            IKeyRecord rec = (IKeyRecord) mKeyDB.readKeyRecord(new
-                    BigInteger(Integer.toString(seq)));
+            IKeyRecord rec = (IKeyRecord) mKeyDB.readKeyRecord(seq);
 
             KeyRecordParser.fillRecordIntoArg(rec, header);
 
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeyRecordParser.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeyRecordParser.java
index 1171236b8e3dc5cd71e52593717d341f4e32a013..d6bc57fcf0233232e4d7e4e2ec1c4b94fc5c53a7 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeyRecordParser.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeyRecordParser.java
@@ -17,6 +17,7 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cms.servlet.key;
 
+import java.math.BigInteger;
 import java.util.Date;
 
 import com.netscape.certsrv.apps.CMS;
@@ -54,8 +55,8 @@ public class KeyRecordParser {
                 rec.getState().toString());
         rarg.addStringValue(OUT_OWNER_NAME,
                 rec.getOwnerName());
-        rarg.addIntegerValue(OUT_SERIALNO,
-                rec.getSerialNumber().intValue());
+        rarg.addBigIntegerValue(OUT_SERIALNO,
+                rec.getSerialNumber(), 10);
         rarg.addStringValue(OUT_KEY_ALGORITHM,
                 rec.getAlgorithm());
         // Possible Enhancement: sun's BASE64Encode is not 
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/RecoverBySerial.java b/pki/base/common/src/com/netscape/cms/servlet/key/RecoverBySerial.java
index 8abafa155dd17a27be61690d473d93ee7bd08ec8..6bb013b1c8abaf0a81644ce15042580318ca43a7 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/RecoverBySerial.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/RecoverBySerial.java
@@ -271,7 +271,7 @@ public class RecoverBySerial extends CMSServlet {
         }
         X509CertImpl x509cert = null;
 
-        if (cert == null) {
+        if (cert == null || cert.trim().length() == 0) {
             header.addStringValue(OUT_ERROR, "certificate not found");
             return;
         } else {
@@ -335,7 +335,7 @@ public class RecoverBySerial extends CMSServlet {
         }
         X509CertImpl x509cert = null;
 
-        if (cert == null) {
+        if (cert == null || cert.trim().length() == 0) {
             // perform recovery
             header.addStringValue(OUT_ERROR, "certificate not found");
             return null;
@@ -407,8 +407,8 @@ public class RecoverBySerial extends CMSServlet {
 
                 header.addStringValue(OUT_OP,
                         req.getParameter(OUT_OP));
-                header.addIntegerValue(OUT_SERIALNO,
-                        Integer.parseInt(seq));
+                header.addBigIntegerValue(OUT_SERIALNO,
+                        new BigInteger(seq), 10);
                 header.addStringValue(OUT_SERVICE_URL,
                         req.getRequestURI());
                 byte pkcs12[] = mService.doKeyRecovery(
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/CheckRequest.java b/pki/base/common/src/com/netscape/cms/servlet/request/CheckRequest.java
index b53413dcf1cd4c22e4328c3a46c410ef1a04f57c..b0a70ffc15dc8ae0540cab2ddb7e833927a75dee 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/CheckRequest.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/CheckRequest.java
@@ -27,6 +27,7 @@ import java.security.NoSuchAlgorithmException;
 import java.security.cert.X509Certificate;
 import java.util.Locale;
 import java.util.StringTokenizer;
+import java.math.BigInteger;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -279,7 +280,7 @@ public class CheckRequest extends CMSServlet {
             throw new ECMSGWException(CMS.getUserMessage("CMS_GW_NO_REQUEST_ID_PROVIDED"));
         }
         try {
-            Integer.parseInt(requestId);
+            new BigInteger(requestId);
         } catch (NumberFormatException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("BASE_INVALID_NUMBER_FORMAT_1", requestId));
             throw new EBaseException(
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/ProcessCertReq.java b/pki/base/common/src/com/netscape/cms/servlet/request/ProcessCertReq.java
index 6e782f5ab147607009fe2c6c43760bc4a6b786cf..cbed8ae6b60173d3389db48aea6e2cbe30f5f31f 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/ProcessCertReq.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/ProcessCertReq.java
@@ -18,6 +18,7 @@
 package com.netscape.cms.servlet.request;
 
 import java.io.IOException;
+import java.math.BigInteger;
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.Certificate;
 import java.security.cert.CertificateEncodingException;
@@ -263,7 +264,7 @@ public class ProcessCertReq extends CMSServlet {
         String signatureAlgorithm = null;
         long notValidBefore = 0;
         long notValidAfter = 0;
-        int seqNum = -1;
+        BigInteger seqNum = BigInteger.ONE.negate();
         EBaseException error = null;
 
         HttpServletRequest req = cmsReq.getHttpReq();
@@ -289,7 +290,7 @@ public class ProcessCertReq extends CMSServlet {
             if (req.getParameter(SEQNUM) != null) {
                 CMS.debug(
                         "ProcessCertReq: parameter seqNum " + req.getParameter(SEQNUM));
-                seqNum = Integer.parseInt(req.getParameter(SEQNUM));
+                seqNum = new BigInteger(req.getParameter(SEQNUM));
             }
             String notValidBeforeStr = req.getParameter("notValidBefore");
 
@@ -311,20 +312,19 @@ public class ProcessCertReq extends CMSServlet {
 
             IRequest r = null;
 
-            if (seqNum > -1) {
-                r = mQueue.findRequest(new RequestId(
-                            Integer.toString(seqNum)));
+            if (seqNum.compareTo(BigInteger.ONE.negate()) > 0) {
+                r = mQueue.findRequest(new RequestId(seqNum.toString()));
             }
 
-            if (seqNum > -1 && r != null) {
+            if (seqNum.compareTo(BigInteger.ONE.negate()) > 0 && r != null) {
                 processX509(cmsReq, argSet, header, seqNum, req, resp,
                         toDo, signatureAlgorithm, subject,
                         notValidBefore, notValidAfter, locale[0], startTime);
             } else {
-                log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSGW_INVALID_REQUEST_ID_1", String.valueOf(seqNum)));
+                log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSGW_INVALID_REQUEST_ID_1", seqNum.toString()));
                 error = new ECMSGWException(
                         CMS.getUserMessage("CMS_GW_INVALID_REQUEST_ID",
-                                String.valueOf(seqNum)));
+                                seqNum.toString()));
             }
         } catch (EBaseException e) {
             error = e;
@@ -399,7 +399,7 @@ public class ProcessCertReq extends CMSServlet {
      */
     private void processX509(CMSRequest cmsReq,
             CMSTemplateParams argSet, IArgBlock header,
-            int seqNum, HttpServletRequest req,
+            BigInteger seqNum, HttpServletRequest req,
             HttpServletResponse resp,
             String toDo, String signatureAlgorithm,
             String subject,
@@ -423,8 +423,7 @@ public class ProcessCertReq extends CMSServlet {
         }
 
         try {
-            IRequest r = mQueue.findRequest(new RequestId(
-                        Integer.toString(seqNum)));
+            IRequest r = mQueue.findRequest(new RequestId(seqNum.toString()));
 
             if (r != null) {
                 // overwrite "auditRequesterID" if and only if "id" != null
@@ -1264,7 +1263,7 @@ public class ProcessCertReq extends CMSServlet {
             if (CMS.getSubsystem("ra") != null)
                 header.addStringValue("localra", "yes");
 
-            header.addIntegerValue("seqNum", seqNum);
+            header.addBigIntegerValue("seqNum", seqNum, 10);
             mParser.fillRequestIntoArg(locale, r, argSet, header);
             String rid = r.getExtDataInString(IRequest.REMOTE_REQID);
 
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/ProcessReq.java b/pki/base/common/src/com/netscape/cms/servlet/request/ProcessReq.java
index 78f047d2cd45c027680f0e482a40d99a98f240b9..93457a2816832b4698e78caf3bf8f06ad7e39680 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/ProcessReq.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/ProcessReq.java
@@ -19,6 +19,7 @@ package com.netscape.cms.servlet.request;
 
 import java.io.IOException;
 import java.util.Locale;
+import java.math.BigInteger;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -130,7 +131,7 @@ public class ProcessReq extends CMSServlet {
      * @param cmsReq the object holding the request and response information
      */
     public void process(CMSRequest cmsReq) throws EBaseException {
-        int seqNum = -1;
+        BigInteger seqNum = BigInteger.ONE.negate();
 
         HttpServletRequest req = cmsReq.getHttpReq();
         HttpServletResponse resp = cmsReq.getHttpResp();
@@ -158,11 +159,11 @@ public class ProcessReq extends CMSServlet {
 
         try {
             if (req.getParameter(SEQNUM) != null) {
-                seqNum = Integer.parseInt(req.getParameter(SEQNUM));
+                seqNum = new BigInteger(req.getParameter(SEQNUM));
             }
             doAssign = req.getParameter(DO_ASSIGN);
 
-            if (seqNum > -1) {
+            if (seqNum.compareTo(BigInteger.ONE.negate()) > 0) {
                 // start authorization
                 AuthzToken authzToken = null;
 
@@ -237,15 +238,14 @@ public class ProcessReq extends CMSServlet {
      * returns whether there was an error or not.
      */
     private void process(CMSTemplateParams argSet, IArgBlock header,
-            int seqNum, HttpServletRequest req,
+            BigInteger seqNum, HttpServletRequest req,
             HttpServletResponse resp,
             String doAssign, Locale locale)
             throws EBaseException {
 
-        header.addIntegerValue("seqNum", seqNum);
+        header.addBigIntegerValue("seqNum", seqNum, 10);
 
-        IRequest r =
-                mQueue.findRequest(new RequestId(Integer.toString(seqNum)));
+        IRequest r = mQueue.findRequest(new RequestId(seqNum.toString()));
 
         if (r != null) {
             if (doAssign != null) {
@@ -324,10 +324,10 @@ public class ProcessReq extends CMSServlet {
 
             mParser.fillRequestIntoArg(locale, r, argSet, header);
         } else {
-            log(ILogger.LL_FAILURE, "Invalid sequence number " + seqNum);
+            log(ILogger.LL_FAILURE, "Invalid sequence number " + seqNum.toString());
             throw new ECMSGWException(
                     CMS.getUserMessage("CMS_GW_INVALID_REQUEST_ID",
-                            String.valueOf(seqNum)));
+                            seqNum.toString()));
         }
 
         return;
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/QueryReq.java b/pki/base/common/src/com/netscape/cms/servlet/request/QueryReq.java
index 3cb2704666b11967815c9de748ba9ea4ddddd498..f86b030b3b0237afe3bfd002957f3750b879a339 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/QueryReq.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/QueryReq.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.util.Enumeration;
 import java.util.Locale;
 import java.util.Vector;
+import java.math.BigInteger;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -291,7 +292,8 @@ public class QueryReq extends CMSServlet {
             direction = req.getParameter("direction").trim();
         }
 
-        int top = 0, bottom = 0;
+        BigInteger top = BigInteger.ZERO;
+        BigInteger bottom = BigInteger.ZERO;
 
         try {
             String top_s = req.getParameter(OUT_FIRST_ENTRY_ON_PAGE);
@@ -303,14 +305,14 @@ public class QueryReq extends CMSServlet {
                 bottom_s = "0";
 
             if (top_s.trim().startsWith("0x")) {
-                top = Integer.parseInt(top_s.trim().substring(2), 16);
+                top = new BigInteger(top_s.trim().substring(2), 16);
             } else {
-                top = Integer.parseInt(top_s.trim());
+                top = new BigInteger(top_s.trim());
             }
             if (bottom_s.trim().startsWith("0x")) {
-                bottom = Integer.parseInt(bottom_s.trim().substring(2), 16);
+                bottom = new BigInteger(bottom_s.trim().substring(2), 16);
             } else {
-                bottom = Integer.parseInt(bottom_s.trim());
+                bottom = new BigInteger(bottom_s.trim());
             }
 
         } catch (NumberFormatException e) {
@@ -362,18 +364,19 @@ public class QueryReq extends CMSServlet {
      */
 
     private CMSTemplateParams doSearch(Locale l, String filter,
-            int count, String direction, int top, int bottom) {
+            int count, String direction, BigInteger top, BigInteger bottom) {
         CMSTemplateParams ctp = null;
         if (direction.equals("previous")) {
-            ctp = doSearch(l, filter, -count, top - 1);
+            ctp = doSearch(l, filter, -count, top);
         } else if (direction.equals("next")) {
-            ctp = doSearch(l, filter, count, bottom + 1);
+            bottom = bottom.add(BigInteger.ONE);
+            ctp = doSearch(l, filter, count, bottom);
         } else if (direction.equals("begin")) {
-            ctp = doSearch(l, filter, count, 0);
+            ctp = doSearch(l, filter, count, BigInteger.ZERO);
         } else if (direction.equals("first")) {
             ctp = doSearch(l, filter, count, bottom);
         } else { // if 'direction is 'end', default here
-            ctp = doSearch(l, filter, -count, -1);
+            ctp = doSearch(l, filter, -count, BigInteger.ONE.negate());
         }
         return ctp;
     }
@@ -391,7 +394,7 @@ public class QueryReq extends CMSServlet {
             Locale locale,
             String filter,
             int count,
-            int marker) {
+            BigInteger marker) {
 
         IArgBlock header = CMS.createArgBlock();
         IArgBlock context = CMS.createArgBlock();
@@ -406,32 +409,41 @@ public class QueryReq extends CMSServlet {
             header.addStringValue(OUT_REQUESTING_USER, "admin");
 
             boolean jumptoend = false;
-            if (marker == -1) {
-                marker = 0; // I think this is inconsequential
+            if (marker.toString().equals("-1")) {
+                marker = BigInteger.ZERO; // I think this is inconsequential
                 jumptoend = true; // override  to '99' during search 
             }
 
-            RequestId id = new RequestId(Integer.toString(marker));
+            RequestId id = new RequestId(marker.toString());
             IRequestVirtualList list = mQueue.getPagedRequestsByFilter(
                     id,
                     jumptoend,
                     filter,
-                    count + 1,
+                    ((count < 0)? count-1: count+1),
                     "requestId");
 
-            int totalCount = list.getSize() - list.getCurrentIndex();
+            int maxCount = 0;
+            if (count < 0 && jumptoend) {
+                maxCount = -count;
+            } else if (count < 0) {
+                maxCount = -count+1;
+            } else {
+                maxCount = count;
+            }
+            int totalCount = (jumptoend)? maxCount:
+                    (list.getSize() - list.getCurrentIndex());
             header.addIntegerValue(OUT_TOTALCOUNT, totalCount);
             header.addIntegerValue(OUT_CURRENTCOUNT, list.getSize());
 
             int numEntries = list.getSize() - list.getCurrentIndex();
 
-            Vector v = fetchRecords(list, Math.abs(count));
+            Vector v = fetchRecords(list, maxCount);
             v = normalizeOrder(v);
             trim(v, id);
 
             int currentCount = 0;
-            int curNum = 0;
-            int firstNum = -1;
+            BigInteger curNum = BigInteger.ZERO;
+            BigInteger firstNum = BigInteger.ONE.negate();
             Enumeration requests = v.elements();
 
             while (requests.hasMoreElements()) {
@@ -447,10 +459,9 @@ public class QueryReq extends CMSServlet {
                     continue;
                 }
 
-                curNum = Integer.parseInt(
-                        request.getRequestId().toString());
+                curNum = new BigInteger(request.getRequestId().toString());
 
-                if (firstNum == -1) {
+                if (firstNum.equals(BigInteger.ONE.negate())) {
                     firstNum = curNum;
                 }
 
@@ -466,15 +477,14 @@ public class QueryReq extends CMSServlet {
 
             header.addIntegerValue(OUT_CURRENTCOUNT, currentCount);
             header.addStringValue("time", Long.toString(endTime - startTime));
-            header.addIntegerValue(OUT_FIRST_ENTRY_ON_PAGE, firstNum);
-            header.addIntegerValue(OUT_LAST_ENTRY_ON_PAGE, curNum);
+            header.addBigIntegerValue(OUT_FIRST_ENTRY_ON_PAGE, firstNum, 10);
+            header.addBigIntegerValue(OUT_LAST_ENTRY_ON_PAGE, curNum, 10);
 
         } catch (EBaseException e) {
             header.addStringValue(OUT_ERROR, e.toString(locale));
         } catch (Exception e) {
         }
         return argset;
-
     }
 
     /**
@@ -485,10 +495,10 @@ public class QueryReq extends CMSServlet {
      */
     private void trim(Vector v, RequestId marker) {
         int i = v.size() - 1;
-        if (((IRequest) v.elementAt(i)).getRequestId().equals(marker)) {
+        if (((IRequest)v.elementAt(i)).getRequestId().toString().equals(
+                marker.toString())) {
             v.remove(i);
         }
-
     }
 
     /**
@@ -525,12 +535,12 @@ public class QueryReq extends CMSServlet {
      */
     private Vector normalizeOrder(Vector list) {
 
-        int firstrequestnum = Integer.parseInt(((IRequest) list.elementAt(0))
+        BigInteger firstrequestnum = new BigInteger(((IRequest)list.elementAt(0))
                 .getRequestId().toString());
-        int lastrequestnum = Integer.parseInt(((IRequest) list.elementAt(list
+        BigInteger lastrequestnum = new BigInteger(((IRequest)list.elementAt(list
                 .size() - 1)).getRequestId().toString());
         boolean reverse = false;
-        if (firstrequestnum > lastrequestnum) {
+        if (firstrequestnum.compareTo(lastrequestnum) > 0) {
             reverse = true; // if the order is backwards, place items at the beginning
         }
         Vector v = new Vector();
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/ReqParser.java b/pki/base/common/src/com/netscape/cms/servlet/request/ReqParser.java
index 00f95ec2dddc2aa28a307d0e8c37aaf9ccb9e145..230ddb43382997b151e979408dbedeb48a9a81bd 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/ReqParser.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/ReqParser.java
@@ -18,6 +18,7 @@
 package com.netscape.cms.servlet.request;
 
 import java.util.Locale;
+import java.math.BigInteger;
 
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IArgBlock;
@@ -51,8 +52,8 @@ public class ReqParser implements IReqParser {
     public void fillRequestIntoArg(Locale l, IRequest req, CMSTemplateParams argSet, IArgBlock arg)
             throws EBaseException {
         arg.addStringValue(TYPE, req.getRequestType());
-        arg.addLongValue("seqNum",
-                Long.parseLong(req.getRequestId().toString()));
+        arg.addBigIntegerValue("seqNum",
+                new BigInteger(req.getRequestId().toString()), 10);
         arg.addStringValue(STATUS,
                 req.getRequestStatus().toString());
         arg.addLongValue(CREATE_ON,
-- 
1.7.7.5

