Creating self-signed certificates for use on Android

Creating self-signed certificates for use on Android

24 NOVEMBER 2011 // MARCUS KRANTZ

A while ago I started to implement TLS/SSL mutual authentication on Android. How to actually implement the functionality on the Android platform is covered in my other article Android - TLS/SSL Mutual Authentication. Before such implementation can be done, it is important to have the keys and certificates prepared. In this article demonstrate how you can create these. However, this article is not just applicable to Android and should be usable in other scenarios as well.

For this article to be useful, the required tools are: openssl, Java’s Keytool and the BouncyCastle-provider. There are also some resources that I strongly recommend and has been very useful:

One might argue why I don’t use keytool to generate the keys and certificates and use them right away. Well, I was very curious about learning more about openssl and how to deal with various formats of keys and certificates.

1. CREATE PRIVATE KEYS

Let’s start from scratch. First of all we need private keys. We use openssl to create these:

$ openssl genrsa -des3 -out client_key.pem 2048
$ openssl genrsa -des3 -out server_key.pem 2048

This will create the two keys; client.pem and server.pem. We will use these in the next step to sign our certificates with. In normal cases we would create a CA-signing request, that is sent to a CA who will issue your certificates. But since we want to self-sign our certificates this step is redundant.

2. CREATE SELF-SIGNED CERTIFICATES

$ openssl req -new -x509 -key client_key.pem -out client.pem -days 365
$ openssl req -new -x509 -key server_key.pem -out server.pem -days 365

Additionally, instead of being prompted for the certificate’s subject line you can use the -subj parameter and pass it to the openssl req command. What we just did was basically creating a CA signing request using our private keys to sign the outgoing x509-certificates. The certificates will be coded in pem-format and valid for 365 days.

3. CREATE TRUST STORES

In order to use our keys and certificates in Java applications we need to import them into keystores. First of all, we want the client to trust the server certificate. To do this we must create a client trust store and import the server’s certificate.

$ keytool –importcert -trustcacerts –keystore clienttruststore.bks –storetype bks –storepass <truststore_password> -file server.pem -provider org.bouncycastle.jce.provider.BouncyCastleProvider –providerpath <path_to_bcprov_jar>

Note: On the client side, which in our case will be an Android app we use Bouncy Castle as our provider since it is supported on the Android platform.

Create a trust store for the server and import the client’s certificate into it.

$ keytool –importcert -trustcacerts –keystore  servertruststore.jks –storetype jks –storepass <server_truststore_password> -file client.pem

Currently, we have two trust stores one for the server in which we imported the client’s certificate and one for the client in which we imported the server’s certificate.

4. COMBINE KEYS AND CERTIFICATES

A problem with Java’s keytool application is that it won’t let us do such a simple thing as importing an existing private key into a keystore. The workaround to this problem is to combine the private key with the certificate into a pkcs12-file (which is understood by Java’s keytool) and then import this pkcs12 keystore into a regular keystore.

Combine the certificate and the private key for the server and client respectively:

$ openssl pkcs12 –export –inkey  client_key.pem –in client.pem –out  client.p12
$ openssl pkcs12 –export –inkey server_key.pem –in server.pem –out server.p12

5. CONVERT FROM PKCS12

Import the created keystores to new ones with common formats:

$ keytool –importkeystore –srckeystore client.p12 –srcstoretype pkcs12 –destkeystore client.bks –deststoretype bks –provider org.bouncycastle.jce.provider.BouncyCastleProvider –providerpath <path_to_bcprov_jar>
$ keytool –importkeystore –srckeystore server.p12 –srcstoretype pkcs12 –destkeystore server.jks –deststoretype jks

We should now have all files we need for a successful TLS/SSL mutual authentication. The files we move to our Android project will be: clienttruststore.bks and client.bks. The files we move to our server will be: servertruststore.jks and server.jks.

时间: 2024-10-30 21:46:37

Creating self-signed certificates for use on Android的相关文章

创建 Android 上使用的自签名证书(Creating self-signed certificates for use on Android)

创建 Android 上使用的自签名证书 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 创建 Android 上使用的自签名证书 Creating self-signed certificat

【Android平台】 Alljoyn学习笔记四 Android Core API参考

CORE API GUIDE - ANDROID Prerequisites Install dependencies for the Windows platform, or for the Linux platform. A device running Android OS version 2.2 (Froyo) or greater and running a chip based on the ARM 5 (or greater) instruction set. Importing

Android MediaPlayer 音乐播放

主要使用 android.media.MediaPlayer; android.widget.SeekBar;    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Layout01" android:

Android官方文档training中英文翻译目录大全:29篇已翻译,45篇未翻译

Android官方文档training中英文翻译目录大全:29篇已翻译,45篇未翻译   1. Getting Started Building Your First App: 原文: https://developer.android.com/training/basics/firstapp/index.html译文:http://wiki.eoeandroid.com/Building_Your_First_AppAdding the Action Bar:原文:https://develo

appium 测试无法打开 app,请各位大神帮忙

问题描述 appium 测试无法打开 app,请各位大神帮忙 初学appium,使用python写测试脚本,现在可以安装上app,之后无法自动启动app.这是我的脚本:#coding=utf-8from appium import webdriverfrom lib2to3.pgen2.driver import Driverfrom lib2to3.tests.support import driverimport osimport time PATH=lambda p:os.path.abs

Appium入门示例(Java)

一.环境准备: 见我另一篇文章:http://www.cnblogs.com/puresoul/p/4696638.html  二.使用Eclipse直接创建案例工程 1.打开Eclipse,[File]-->[New]-->[Project] 2.选择[Java Project]-->[Next] 3.输入工程名称Appium_demo,点击[Finish] 4.右键点击工程 New-Folder,新建两个文件夹:apps和libs,目录结构如下:   三.导入测试的类库 1.导入Se

iOS开发:服务器到客户端对ATS的适配

  一.简单谈谈ATS(App Transport Security) ATS(App Transport Security)是为了提高App与服务器之间安全传输数据一个特性,这个特性从iOS9和OSX10.11开始出现,它默认需要满足以下几个条件: 服务器TLS版本至少是1.2版本 连接加密只允许几种先进的加密 证书必须使用SHA256或者更好的哈希算法进行签名,要么是2048位或者更长的RSA密钥,要么就是256位或更长的ECC密钥. 如果想了解哪几种先进的加密是被允许的,详情请见官方文档A

docker-gitlab(转)

  Issues Docker is a relatively new project and is active being developed and tested by a thriving community of developers and testers and every release of docker features many enhancements and bugfixes. Given the nature of the development and releas

Nodejs进阶:核心模块https 之 如何优雅的访问12306

本文摘录自<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 这个模块的重要性,基本不用强调了.在网络安全问题日益严峻的今天,网站采用HTTPS是个必然的趋势. 在nodejs中,提供了 https 这个模块来完成 HTTPS 相关功能.从官方文档来看,跟 http 模块用法非常相似. 本文主要包含两部分: 通过客户端.服务端的例子,对https模块进行入门讲解. 如何访问安全证书不受信任的网站.(以 12306 为例子)