使用 Java 的 OpenSSL
我必须在 Java Web 项目中使用 OpenSSL,但我对“OpenSSL”一无所知。
如何将 OpenSSL 与我的项目集成?有没有什么好的基础教程可以学习这个?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我必须在 Java Web 项目中使用 OpenSSL,但我对“OpenSSL”一无所知。
如何将 OpenSSL 与我的项目集成?有没有什么好的基础教程可以学习这个?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
在提出任何建议之前,您需要回答几个重要问题
1)您真的想从JAVA中调用C(本机)实现吗?
2) OpenSSL 中有哪些功能是 JCE 和 BouncyCastle 无法解决的
3) 范围是否仅限于使用 OpenSSL 生成的证书,解密 OpenSSL 生成的文件?
You need to answer a few important questions before any suggestions
1) Do you really want to call C(native) implementation form JAVA?
2) What are the features in OpenSSL which cannot be solved by JCE and BouncyCastle
3) Is the scope just limited to using certificates generated by OpenSSL, decrypting files generated by OpenSSL?
您可以使用
java\jdk\jre\bin
目录中的 keytool java 工具。You can use keytool java tool in your
java\jdk\jre\bin
directory.Apache Tomcat Native Library 就是解决方案。
https://github.com/apache/tomcat-native
它使用 OpenSSL 来实现 TLS/SSL 功能。您可以将它用作独立库(就像我所做的那样)或连接您的 Tomcat。它是一个开源项目,具有详细记录的 Java 代码。
为什么要原生tomcat?
JSSE 速度慢且难以使用。
在我的项目中,为了获得最佳性能,我们决定为 OpenSSL 查找/编写 JNI 包装器,因为动态升级证书的可能性是一个问号,而且密钥存储太复杂。
由于 Bouncy Castle Crypto API 是用 Java 编写的,因此您不能指望获得最佳效率。
Tomcat Native 是一个包装器,因此您只能使用 OpenSSL 版本的功能。
Apache Tomcat Native Library is the solution.
https://github.com/apache/tomcat-native
It uses OpenSSL for TLS/SSL capabilities. You can use it as standalone library (as I did) or connect your Tomcat. It is open source project with well documented Java code.
Why tomcat native?
JSSE is slow and hard to use.
In my project to get best performance we've decided to find/write JNI wrapper for OpenSSL as possibility for upgrading certificates on-the-fly is a question mark and Key Stores are too complex.
As Bouncy Castle Crypto APIs is written in Java you cannot expect best efficiency.
Tomcat Native is a wrapper so you are limited only to capabilities of your OpenSSL version.
每个人都在谈论 BouncyCastle,但在我们的用例中,Gnu 加密库赢得了胜利。原生java。
对于某些客户来说,我们的数据库 (aerospike) 至少花费 10% 的时间在 java 中计算哈希值,这仅仅是因为这些实现速度很慢。我欢迎每台 Linux 机器上都有可用的加密库的本机实现。我认为一些 Java7 VM 将包含更多算法,但我还没有看到它们。
Everyone talks about BouncyCastle, but in our use case Gnu Crypto library won the day. Native java.
Our database ( aerospike ) spends at least 10% of its time computing hashes in java, for some customers, simply because these implementations are slow. I welcome when there's a native implementation of the crypto libraries available on every Linux machine. I thought some of the Java7 VMs were going to include more algorithms, but I haven't seen them yet.
最佳解决方案:使用 Java 的内置安全性来执行简单任务,或使用 BouncyCastle 来执行更高级的任务。
如果您必须从 Java 使用 OpenSSL,您有 2 个选择:
这些都不是真正好的方法。
Best solution: Use Java's built-in security for simple tasks or use BouncyCastle for more advanced ones.
If you HAVE TO use OpenSSL from Java you have 2 choices:
None of those are really good ways of doing it.
有很多用于加密的 Java 本机库。然而,它们通常不能与 OpenSSL 完全互操作,有时速度明显慢(请参阅下面网站上的指标),并且并非所有平台都支持。 OpenSSL 几乎在所有平台上都受到支持,并且通常具有高性能。
话虽这么说,使用基于虚拟机的加密有一些安全优势。这也应该是一个考虑因素。
Apache 小组已经为 Java 构建了一个库,该库使用 JNI 访问 openssl 以进行 AES 加密。我认为这是使用 JNI 访问 openssl 的最佳公开示例,您可以使用 maven 轻松引用它。
https://github.com/apache/commons-crypto
如果你愿意,你可以拉取出库的 JNI 绑定部分并实现您需要的功能。
此 makefile 展示了如何使用 javah 从 .class 中获取构建 .c 代码所需的内容:
https://github.com/apache/commons-crypto/blob/master/Makefile
具体来说,Makefile 中的这一行调用 javah:
$(JAVAH) -force -classpath $ (TARGET)/classes -o $@ org.apache.commons.crypto.cipher.OpenSslNative
基于 OpenSslNative.class 生成正确的“OpenSslNative.h”文件。https://github.com/apache/commons-crypto/blob/master/src/main/java/org/apache/commons/crypto/cipher/OpenSslNative.java
注意如何
导入java.nio .ByteBuffer;
用于允许 C 输出缓冲区。相关的.c程序在这里:
https://github.com/apache/commons-crypto/blob/master/src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c
这是编写时考虑到了跨平台支持,并且是一个很好的例子。每个导出的函数都必须以 JNIEXPORT 开头,您可以看到每个函数名称中的完整类路径。
我见过很多不好的 JNI 绑定、传递字符串等。从坚实的基础开始,对于在 Java 中构建良好的 OpenSSL 集成大有帮助。
There are lots of Java native libraries for crypto. However they are generally not fully interoperable with OpenSSL, are sometimes significantly slower (see the metrics on the site below), and aren't supported on all platforms. OpenSSL is definitely supported on nearly every platform and is, generally, performant.
That being said, there are some security advantages to using VM-based crypto. This should also be a consideration.
The Apache group has built a library for Java that uses JNI to access openssl for AES encryption. I think it's the best public example of using JNI to access openssl, and you can reference it easily using maven.
https://github.com/apache/commons-crypto
If you want, you can pull out the JNI binding portion of the libary and implement the functions you need.
This makefile shows how to use javah to get what you need from the .class to build the .c code:
https://github.com/apache/commons-crypto/blob/master/Makefile
Specifically, this line in the Makefile calls javah:
$(JAVAH) -force -classpath $(TARGET)/classes -o $@ org.apache.commons.crypto.cipher.OpenSslNative
to produce the correct "OpenSslNative.h" file based on the OpenSslNative.class.https://github.com/apache/commons-crypto/blob/master/src/main/java/org/apache/commons/crypto/cipher/OpenSslNative.java
Note how
import java.nio.ByteBuffer;
is used to allow for C output buffers.The associated .c program is here:
https://github.com/apache/commons-crypto/blob/master/src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c
It's written with cross-platform support in mind, and is a good example. Every exported function must begin with
JNIEXPORT
, and you can see how the full class path is in each function name.I've seen a lot of bad JNI bindings, passing strings around, etc. Starting with a solid base goes a long way toward building good OpenSSL integration in Java.
首先:你需要图书馆做什么?
喜欢这些属性:
First of all: what do you need the library for?
Like these properties: