BouncyCastle Library in ColdFusion 10

心不动则不痛 提交于 2019-12-06 05:53:04

While I'm still unclear why two machines with the same version of CF with the same version of the same java package would have a discrepancy in the classes included in that package, I did find a workaround.

The org.bouncycastle.util.io.pem.PemReader class in the BouncyCastle Provider package is a replacement for the deprecated org.bouncycastle.openssl.PEMReader class in the BouncyCastle PKIX/OpenSSL package. Since CF10 comes with a relatively old version of BC (v1.39-jdk1.4), it still includes the deprecated class. Replacing the code above with the following code corrected the issue...

var privateKeyFile = createObject("java", "java.io.FileReader").init("myPrivateKey.pem");
var privateKey = createObject("java", "org.bouncycastle.openssl.PEMReader").init( privateKeyFile ).readObject().getPrivate().getEncoded();
privateKeyFile.close();

It also has the benefit of explicitly closing the file (though that was always possible).

Note: The machines that had the earlier issue also weren't loading the BouncyCastle security provider. It seems to be available on every machine, but not always loaded so I had to explicitly load it if it wasn't already:

var securityProviders = createObject("java", "java.security.Security").getProviders();
var providerInstalled = false;
for( var provider IN securityProviders ){
    if( provider.getName() eq "BC" ){
        providerInstalled = true;
        break;
    }
}
if( not providerInstalled )
    createObject("java", "java.security.Security")
        .addProvider( createObject("java", "org.bouncycastle.jce.provider.BouncyCastleProvider").init() );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!