Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying certificate content from db

 
cusa patel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I'm retrieving the content of a certificate that has been stored in a database. certficate column is a blob.

Here is part of the code that I started to work on:


<%@ page import="javax.security.cert.X509Certificate"%>

String cert = select convert((hex(certificate)) using ascii) as certificate from dbtable

ResultSet rs = sqlStatement.executeQuery(cert);

Once the data has be retrieved then I...

while (rs.next()) {
byte[] certs = rs.getBytes("certificate");
String cert = X509Certificate.getInstance(certs).toString(); // here is the part that I'm getting a resolved type error. Tried to use different type based on samples that found on the web but still getting resolved type error.
}

thanks
 
Paul Clapham
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cusa patel wrote:here is the part that I'm getting a resolved type error.


It would be helpful if you copied and pasted the actual error message, preferably the top part of the stack trace, rather than a vague description of the message.
 
cusa patel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I made some changes since the last time. Now, got a new error. Here is the latest code:

<%
Statement sqlStatement = conn.createStatement();
String qrycerts = "select convert((hex(certificate)) using ascii) as certificate from dbtable where record_id=1";
ResultSet rs = sqlStatement.executeQuery(qrycerts);
while (rs.next()) {
byte[] certEntryBytes = rs.getBytes("certificate");
InputStream in = new ByteArrayInputStream(certEntryBytes);
CertificateFactory certFactory = CertificateFactory.getInstance("X509");
X509Certificate certs = (X509Certificate)certFactory.generateCertificate(in);
}
%>

===================== Error ===========================

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling
this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page test.jsp at line 40

40: X509Certificate certs = (X509Certificate)certFactory.generateCertificate(in);


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510
)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

javax.servlet.ServletException: java.security.cert.CertificateException: Could not parse
certificate: java.io.IOException: Empty input
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.files.testing_jsp._jspService(testing_jsp.java:115)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.security.cert.CertificateException: Could not parse certificate:
java.io.IOException: Empty input
sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:104)
java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
org.apache.jsp.files.testing_jsp._jspService(testing_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 
Paul Clapham
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.IOException: Empty input


This is the root cause, correct? Do you not have a guess about what it might mean?
 
Paul Clapham
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have an InputStream. Its description isn't useful, though, so printing that doesn't tell you anything. What the error message does tell you is that it's empty. Which means that it doesn't contain any data.

From which it follows that the byte array you created it from is also empty, i.e. doesn't contain any data. You should be able to tell whether that's true with a little bit of debugging; I would suggest you do that before making more random code changes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic