<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Welcome to my world. &#187; Security</title>
	<atom:link href="http://www.marceble.com/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marceble.com</link>
	<description>Marc E. Eble</description>
	<lastBuildDate>Thu, 22 Apr 2010 20:47:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>JAVA Security Provider Error</title>
		<link>http://www.marceble.com/2009/08/java-security-providers/</link>
		<comments>http://www.marceble.com/2009/08/java-security-providers/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 22:56:39 +0000</pubDate>
		<dc:creator>Marc</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[JAVA]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.marceble.com/?p=147</guid>
		<description><![CDATA[Just a quick note about a simple but annoying error I encountered when porting an app to glassfish; The code in question was as follows:   ...      Charset charset = Charset.forName("UTF-8");     CharsetEncoder encoder = charset.newEncoder();     ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(str));   SecretKeySpec key = new SecretKeySpec( KEY_BASE, "DES" );     private static Cipher [...]]]></description>
			<content:encoded><![CDATA[<p id="top" />Just a quick note about a simple but annoying error I encountered when porting an app to glassfish; The code in question was as follows:</p>
<pre> 
     ... 
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(str));
    SecretKeySpec key = new SecretKeySpec( KEY_BASE, "DES" );

    private static Cipher ecipher = Cipher.getInstance( "DES");
    ecipher.init( Cipher.ENCRYPT_MODE, key ); 
    byte[] enc = ecipher.doFinal( bbuf.array() );
     ...</pre>
<pre><span style="font-family: Georgia; line-height: 19px; white-space: normal;">The code above just converts a string (stored as str here) to UTF-8 then encrypts it using DES. Equivalently you could use str.getBytes() for the UTF conversion. The code above worked fine on JBOSS, and worked fine stand alone. It even worked on my glassfish test server, however when deployed on glassfish enterprise I started getting the following stacktrace:<span id="more-147"></span>
</span></pre>
<pre>    java.security.ProviderException: update() failed
        at  sun.security.pkcs11.P11Cipher.implUpdate(P11Cipher.java:557)
        at  sun.security.pkcs11.P11Cipher.engineUpdate(P11Cipher.java:457)
        at  sun.security.pkcs11.P11Cipher.engineDoFinal(P11Cipher.java:485)
        at  sun.security.pkcs11.P11Cipher.engineDoFinal(P11Cipher.java:471)
        at  javax.crypto.Cipher.doFinal(DashoA13*..)
    ...
    Caused  by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_DEVICE_ERROR
        at  sun.security.pkcs11.wrapper.PKCS11.C_EncryptUpdate(Native Method)
        at  sun.security.pkcs11.P11Cipher.implUpdate(P11Cipher.java:510)</pre>
<p>After much trial and error I found the issue at hand was caused by the security provider being used. Adding the following code segment I output the provider being used and ran it both stand alone and inside glassfish:</p>
<pre>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            System.out.println("--------" + ecipher.getProvider().getName());</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            System.out.println(ecipher.getProvider().getInfo());</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">            System.out.println(ecipher.getProvider().getVersion())            System.out.println("--------" + ecipher.getProvider().getName());</div>
</pre>
<pre>    System.out.println(ecipher.getProvider().getName());
    System.out.println(ecipher.getProvider().getInfo());
    System.out.println(ecipher.getProvider().getVersion());</pre>
<p>When running stand alone I got the following output:</p>
<pre style="padding-left: 30px; ">SunJCE
SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, 
                 ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC)
1.6</pre>
<p>Running in glassfish I got:</p>
<pre style="padding-left: 30px; ">SunPKCS11-__SUN_SJSAS_internal
SunPKCS11-__SUN_SJSAS_internal using library C:\Sun\AppServer\lib\softokn3.dll</pre>
<p>I tried prioritizing SunJCE in java.security however the above results did not change. Finally I just specified SunJCE in my Cipher.getInstance so:</p>
<pre style="padding-left: 30px; ">Cipher.getInstance("DES", "SunJCE");</pre>
<p>After that all worked well. A very trivial issue to resolve, however annoying nonetheless.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marceble.com/2009/08/java-security-providers/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
