I'm having trouble writing V3 certificates to a
.cer file. I have no trouble creating a certificate from a file and adding
a V3 extension. Here's my code to add the extension:
public void addCustomExtension()
{
try
{
byte[] randomBytes = (initialize a
bag of bytes here);
OCTET_STRING
verificationBytes =
new OCTET_STRING(randomBytes);
V3Extension
randomExtension = new UnknownExtension(new
ObjectID("RANDOM_BYTES"));
randomExtension.setCritical(true);
randomExtension.init(randomBytes);
m_cert.addExtension(randomExtension);
System.out.println("version = " +
m_cert.getVersion());
}
catch
(X509ExtensionException xee)
{
// some
handling
xee.printStackTrace();
}
catch
(CxEnigmaException cee)
{
// more
handling
cee.printStackTrace();
}
}
The System.out.println call at the end returns
"version = 3", indicating that the extension has been added successfully.
But when I write the revised certificate to a file with the following
code:
public void writeToFile(String i_fileName,
X509Certificate i_cert)
{
FileOutputStream fos = null;
try
{
File
certificateFile = new File(i_fileName);
fos =
new FileOutputStream(certificateFile);
fos.write(i_cert.getEncoded());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fos.flush();
fos.close();
}
catch (IOException
ioe)
{
// handle it
here
}
}
}
it writes ok (without an error), but I end up
with V1 certificate, not a V3. In other words it appears to ignore my
added extension. Any help would be greatly appreciated.
Aaron
Haspel