java keystore ca_PKCS12 Java Keystore from CA and User certificate in java

问题

I've recently been put in charge of mocking up an Apple product (iPhone Configuration Utility) in Java. One of the sections I've been a bit stuck on is a part about Exchange ActiveSync. In there, it allows you to select a certificate from your Keychain to use as credentials for your EAS account. After some research, I found that it's actually creating a PKCS12 keystore, inserting the private key of the certificate I selected, and encoding that into XML. So far not a big deal. If I create a .p12 file with Keychain Access it uploads without a problem. But I run into a problem when I try to bring that over to Java.

Say I export one of those certs that I had used earlier with the .p12 file as a .cer file (this is what we are expecting to get in the environment). Now when I upload it into Java I get a Certificate object as follows...

KeyStore ks = java.security.KeyStore.getInstance("PKCS12");

ks.load(null, "somePassword".toCharArray());

CertificateFactory cf = CertificateFactory.getInstance("X.509", new BouncyCastleProvider());

java.security.cert.Certificate userCert = cf.generateCertificate(new FileInputStream("/Users/me/Desktop/RecentlyExportedCert.cer"));

But when I try...

ks.setCertificateEntry("SomeAlias", userCert);

I get the exception...

java.security.KeyStoreException: TrustedCertEntry not supported

So from certs I move onto keys. But with those Certificates (I got the CA Cert as well), I'm only able to access the public key, not the private. And if I attempt to add the public key like so...

java.security.cert.Certificate[] chain = {CACert};

ks.setKeyEntry("SomeAlias", userCert.getPublicKey().getEncoded(), chain);

I get...

java.security.KeyStoreException: Private key is not stored as PKCS#8 EncryptedPrivateKeyInfo: java.io.IOException: DerValue.getOctetString, not an Octet String: 3

So now I'm here. Does anyone have any idea how to get a private key from a .cer file into a PKCS12 keystore in Java? Am I even on the right track?

Thanks in advance!

回答1:

The PKCS#12 format is intended for storing a private key associated with a certificate chain, and both are required (although you might not need the whole chain).

Although the PKCS12 keystore type does a good job for mapping this format to a Java KeyStore, not everything is supported for this reason.

What you're trying to do in your first attempt is storing a certificate on its own, which won't work.

What you're trying to do in your second attempt (ks.setKeyEntry("SomeAlias", userCert.getPublicKey().getEncoded(), chain)) is to for a public key in place of what should be a private key (see KeyStore#setKeyEntry).

.cer file tend to be just for certificates not private keys (although of course, the extension is ultimately just an indication). If you export your .cer file from Keychain Access.app, you won't get the private key with it (that's what the .p12 export format is for).

EDIT about KeychainStore:

If the reason you're trying to do this conversion is ultimately to access private keys and certificates that are already in the keychain you could load them from the KeychainStore directly:

KeyStore ks = KeyStore.getInstance("KeychainStore", "Apple");

ks.load(null, "-".toCharArray());

A couple of notes for this:

Any non-null, non-empty password will do to use the private key (e.g. "-".toCharArray()), as access will be prompted by the OS's security service (like it would in other applications).

As far as I'm aware, there is still a bug and it only allows access to one private key/certificate pair (even if a number of pairs of private key/certificate pairs are present in the keychain)

回答2:

http://www.docjar.com/html/api/org/bouncycastle/jce/examples/PKCS12Example.java.html

This is how to add a certificate with a associating private key to a PKCS12 keystore.

When you are using client authentication, the keystore needs to contain also the private key, in that case you use KeyStore.getInstance("PKCS12").

When youre not using client authentication but only server authentication(and private key will not be added to keystore, since it belongs to the server), its better to use

KeyStore.getInstance("JKS"), than you can add multiple certificates with an alias to that one keystore.

When you are using PKCS12, as far as I know, you can only add 1 certificate(you have to add the whole certificate chain) associated with the private key, you want to use for that certificate.

回答3:

I'm a couple years late to the party but this took me a few hours to get working correctly,

so I thought it was worth posting a working solution.

This solution uses 1) A .p12 / PKCS12 certificate

and 2) a CA not in the default TrustManager (and you want to add it programatically rather than adding to the default TrustManager). 3) No third party cryptography libraries, just HttpClient to bring it all together.

I've also added a few helpful keytool and openssl commands in the JavaDoc for working with certificates, as that is an art in itself.

// Stitch it all together with HttpClient

CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(getSSLSocketFactory()).build();

private SSLConnectionSocketFactory getSSLSocketFactory() {

try {

SSLContext sslContext = SSLContext.getInstance("TLS");

KeyManager[] keyManager = getKeyManager("pkcs12", "path/to/cert.p12"), "p12_password"));

TrustManager[] trustManager = getTrustManager("jks", "path/to/CA.truststore", "trust_store_password"));

sslContext.init(keyManager, trustManager, new SecureRandom());

return new SSLConnectionSocketFactory(sslContext);

} catch (Exception e) {

throw new RuntimeException("Unable to setup keystore and truststore", e);

}

}

/**

* Some useful commands for looking at the client certificate and private key:

* keytool -keystore certificate.p12 -list -storetype pkcs12 -v

* openssl pkcs12 -info -in certificate.p12

*/

private KeyManager[] getKeyManager(String keyStoreType, String keyStoreFile, String keyStorePassword) throws Exception {

KeyStore keyStore = KeyStore.getInstance(keyStoreType);

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

keyStore.load(this.getClass().getClassLoader().getResourceAsStream(keyStoreFile), keyStorePassword.toCharArray());

kmf.init(keyStore, keyStorePassword.toCharArray());

return kmf.getKeyManagers();

}

/**

* Depending on what format (pem / cer / p12) you have received the CA in, you will need to use a combination of openssl and keytool

* to convert it to JKS format in order to be loaded into the truststore using the method below.

*

* You could of course use keytool to import this into the JREs TrustStore - my situation mandated I create it on the fly.

*

* Useful command to look at the CA certificate:

* keytool -keystore root_ca.truststore -list -storetype jks -v

*

*/

private TrustManager[] getTrustManager(String trustStoreType, String trustStoreFile, String trustStorePassword) throws Exception {

KeyStore trustStore = KeyStore.getInstance(trustStoreType);

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

trustStore.load(this.getClass().getClassLoader().getResourceAsStream(trustStoreFile), trustStorePassword.toCharArray());

tmf.init(trustStore);

return tmf.getTrustManagers();

}

来源:https://stackoverflow.com/questions/3614239/pkcs12-java-keystore-from-ca-and-user-certificate-in-java

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/487873.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

干货|六维力和力矩传感器的技术与应用

来源:机器人大讲堂六维力和力矩传感器是一种提供三轴力和三轴力矩反馈的设备,并广泛应用在机器人控制、力学实验和科研等不同的场景中。不同于其他常见的测力仪器,六维力和力矩传感器可以测量完整的六自由度环境力数据,从而使其适…

Spark笔记——技术点汇总

Spark笔记——技术点汇总 目录 概况 手工搭建集群 引言 安装Scala 配置文件 启动与测试 应用部署 部署架构 应用程序部署 核心原理 RDD概念 RDD核心组成 RDD依赖关系 DAG图 RDD故障恢复机制 Standalone模式的Spark架构 YARN模式的Spark架构 应用程序资源构建…

人工智能正在推动芯片的复兴

来源丨Forbes作者丨Rob Toews编译丨科技行者半导体是数字时代的一项基础技术。美国硅谷的名字正是源自于此。过去半个世纪以来,计算技术的革命改变着社会的方方面面,而半导体技术正是这场革命的核心。自英特尔1971年推出全球第一个微处理器以来&#xff…

java integer 包_java之学习基本类型包装类的概述及Integer类的概述和构造方法

基本类型包装类的概述A:为什么会有基本类型包装类* 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。* B:常用操作* 常用的操作之一:用于基本数据类型与字符串之间的转换。* C:基本类型和包装类的对应byte Byteshort …

UVA3942 Remember the Word

题意&#xff1a;输入一个字符串(l<300000)&#xff0c;一个n(n<4000)&#xff0c;接下来n个字符串(l<100)&#xff0c;问能分解成几种字符串 题解&#xff1a;直接计算复杂度高&#xff0c;考虑DP&#xff0c;dp[i]代表以第i个字符开头的字符串分解的方案数&#xff…

MEMS为何独领风骚?看完这篇秒懂!

显微镜下的MEMS结构&#xff08;注意图片上的刻度&#xff0c;是微米哦&#xff09;文章来源&#xff1a;硬件十万个为什么卡西欧早年就以电机马达、继电器等机电产品闻名。如今的卡西欧本身就是MEMS的制造商之一&#xff0c;用MEMS的思路解决传统机械手表遇到的问题是卡西欧的…

如何把我的Java程序变成exe文件?

JAVA是一种“跨平台”的语言&#xff0c;拥有“一次编写&#xff0c;处处运行”的特点&#xff0c;让它成为当今IT行业&#xff0c;必不可少的一门编程语言。每一个软件开发完成之后&#xff0c;应该大家都需要打包程序并发送给客户&#xff0c;常见的方式&#xff1a;java程序…

福布斯:混合现实未来的八大应用场景

来源&#xff1a;AR工业应用近日&#xff0c;福布斯发布了一篇关于混合现实未来的八个值得关注的领域和应用场景。文中提到&#xff1a;随着时间的推移&#xff0c;技术永远在不断进展&#xff0c;而未来技术中很可能是AR和VR的结合形式。有报告显示&#xff1a;2024年&#xf…

php怎么实现简单的mvc框架,php实现简单的MVC框架实例

这篇文章主要介绍了php实现简单的MVC框架,较为详细的分析了php实现MVC框架的相关实现技巧与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下本文实例讲述了php实现简单的MVC框架。分享给大家供大家参考。具体如下&#xff1a;在开始之前需要知道的知识1.php基础知识2.单一…

演化树,靠不住?挑战物种出现与灭绝速率的估算方法

来源&#xff1a;Nature自然科研原文作者&#xff1a;Mark Pagel以演化树形式绘制的图表能够显示物种之间的系统发育关系&#xff0c;这类图表被广泛用于估算新物种出现以及现存物种灭绝的速率&#xff0c;但一项新的研究对这种方法提出了质疑。科学家经常试图对生物的过去是什…

Windows中使用wget整站下载

weget wget安装 Windows下载 点击下载 https://eternallybored.org/misc/wget/会跳转到wget的下载页&#xff0c;根据自己电脑选择下载的文件&#xff0c;我下载的版本为wget-1.19.1-win64.zip下载完成后解压文件,并且双击exe使用wget wget使用 在需要下载到网站的位置新建文…

java中事件的概念,事件中心上IoTHub数据的概念

我是IoTHub的新手 . 我有一个关于数据如何发送到IoTHub \ EventHub的基本查询 . 从我的阅读中我看到 - 和 IoT Hub and Event Hub - 作为不同的实体 .My Requirement我需要开发一个可以将数据从Raspberry Pi3设备发送到IoT Hub的应用程序另一个Web应用程序&#xff0c;用于订阅…

Android媒体解码MediaCodec,MediaExtractor

Android提供了MediaPlayer播放器播放媒体文件&#xff0c;其实MediaPlyer只是对Android Media包下的MediaCodec和MediaExtractor进行了包装&#xff0c;方便使用。但是最好理解下Android媒体文件的解码&#xff0c;编码和渲染流程。 Shape Of My Heart.mp4 <source src"…

2020五大技术趋势一览!超自动化、人类增强技术、无人驾驶发展、机器视觉崛起、区块链实用化...

文章原载于 RichardLiu自动驾驶技术的发展近年来&#xff0c;自动驾驶技术一直在发展&#xff0c;特斯拉、英特尔等大公司在这一领域取得了长足的进展。虽然我们还没有达到L4级或L5级自动驾驶汽车的水平&#xff0c;但我们已经很接近了。为了解释每个级别的含义&#xff0c;下…

ansible执行mysql命令,Ansible常用命令(ad-hoc 命令)

Ansible提供两种方式去完成任务&#xff1a;1、ad-hoc命令2、写Ansible playbook脚本前者可以解决一些简单的任务&#xff0c;后者解决较复杂的任务(ad-hoc命令和ansible playbook的关系类似于在命令行敲入shell命令和写shell scripts两者之间的关系)。ansible命令应用基础语法…

Facebook 开源聊天机器人Blender,经94 亿个参数强化训练,更具“人情味”

来源&#xff1a;AI前线作者 | Kyle Wiggers编译 | Sambodhi策划 & 编辑 | 刘燕不久前&#xff0c;Facebook 开源了号称是全球最强大的聊天机器人 Blender&#xff0c;它标志着 Facebook 在 AI 领域的新进展&#xff1a;新的聊天机器人不仅解决了此前聊天机器人的固有缺点&…

微服务go还是java,Java微服务 vs Go微服务,究竟谁更强!?

前言Java微服务能像Go微服务一样快吗&#xff1f;这是我最近一直在思索地一个问题。去年8月份的the Oracle Groundbreakers Tour 2020 LATAM大会上&#xff0c;Mark Nelson和Peter Nagy就对此做过一系列基础的的测试用以比较。接下来就给大家介绍下。在程序员圈子里&#xff0c…

php类中双冒号和-的区别

就是为了区分对象的方法和属性&#xff0c;和是访问类的静态方法和静态变量&#xff0c;类的静态方法和静态变量是类公用的&#xff0c;不需要实例化也能访问&#xff0c;而对象的方法和属性是每个对象特有的&#xff0c;因此必须先实例化。其他语言如C,JAVA等也是一样的&#…

java用循环语法在窗体中显示文字,如果子窗体在窗体视图中,则访问2003循环当前页面不起作用...

我有一个包含多个子表单的表单 .我想有以下行为&#xff1a;按主窗体的最后一个字段中的tab键或子窗体的最后一个字段中焦点移动到下一个子窗体或根据定义的Tab顺序返回到主窗体 .为此&#xff0c;所有子表单和主表单都将Cycle属性设置为Current页面 .除了表单视图中显示的子表…

Defi安全-Mono攻击事件分析--etherscan+phalcon

MonoX攻击事件相关信息 在Ethereum和Polygon网络都发生了&#xff0c;攻击手段相同&#xff0c;以Ethereum为例进行分析&#xff1a; 攻击者地址&#xff1a;MonoX Finance Exploiter | Address 0xecbe385f78041895c311070f344b55bfaa953258 | Etherscan 攻击合约&#xff1a…