1. 程式人生 > >openssl 自簽發證書及ssl 原理簡介(一)

openssl 自簽發證書及ssl 原理簡介(一)

提綱:

1、使用openssl自簽發證書

2、配置apache

3、配置客戶端

4、使用自簽發證書訪問apache

一、使用Openssl自簽發證書

首先裝openssl 。編輯配置檔案/etc/ssl/openssl.conf ,配置TSA root dir 等以下配置

[ tsa_config1 ]

# These are used by the TSA reply generation only.
dir     =  ./demoCA     # TSA root directory
serial      = $dir/serial   # The current serial number (mandatory)
crypto_device   = builtin       # OpenSSL engine to use for signing
signer_cert = $dir/cacert.pem   # The TSA signing certificate
                    # (optional)
certs       = $dir/cacert.pem   # Certificate chain to include in reply
                    # (optional)
signer_key  = $dir/private/cakey.pem # The TSA private key (optional)

然後就可以生成CA以及server、user的證書了。

1、建立cert檔案結構。

<pre name="code" class="html">[email protected]:~/certs# mkdir -p ./demoCA/{private,newcerts}
[email protected]:~/certs# touch ./demoCA/index.txt
[email protected]:~/certs# echo 01 > ./demoCA/serial

 2、生成CA私鑰與CA證書。

openssl genrsa -des3 -out ./demoCA/private/cakey.pem 2048
Generating RSA private key, 2048 bit long modulus
.+++
............+++
e is 65537 (0x10001)
Enter pass phrase for ./demoCA/private/cakey.pem:
Verifying - Enter pass phrase for ./demoCA/private/cakey.pem:
[email protected]
:~/certs# openssl req -new -x509 -days 365 -key ./demoCA/private/cakey.pem -out ./demoCA/cacert.pem Enter pass phrase for ./demoCA/private/cakey.pem: You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:cn State or Province Name (full name) [Some-State]:Asia Locality Name (eg, city) []:bj Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []:lyw Email Address []:


4、生成user 私鑰、證書。

[email protected]:~/certs# openssl genrsa -des3 -out userkey.pem
Generating RSA private key, 2048 bit long modulus
................................................+++
...+++
e is 65537 (0x10001)
Enter pass phrase for userkey.pem:
Verifying - Enter pass phrase for userkey.pem:
[email protected]:~/certs# openssl req -new -days 365 -key userkey.pem -out userreq.pem
Enter pass phrase for userkey.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:Asia
Locality Name (eg, city) []:BJ
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:abc
Common Name (e.g. server FQDN or YOUR name) []:abc
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[email protected]:~/certs# openssl ca -in userreq.pem -out usercert.pem
Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jul 17 19:14:18 2015 GMT
            Not After : Jul 16 19:14:18 2016 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = Asia
            organizationName          = Internet Widgits Pty Ltd
            organizationalUnitName    = abc
            commonName                = abc
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                DE:90:9F:7B:A3:58:0C:D4:32:5A:2E:E0:40:BC:6B:25:3B:F9:3D:28
            X509v3 Authority Key Identifier: 
                keyid:DD:E5:B1:AA:36:1F:F2:95:A8:0A:DD:51:2E:3D:BB:9A:F3:7B:8C:57

Certificate is to be certified until Jul 16 19:14:18 2016 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

5. 生成server 私鑰與證書
openssl genrsa -des3 -out serverkey.pem
Generating RSA private key, 2048 bit long modulus
.........................................................................................+++
....+++
e is 65537 (0x10001)
Enter pass phrase for serverkey.pem:
Verifying - Enter pass phrase for serverkey.pem:
[email protected]:~/certs# openssl req -new -days 365 -key serverkey.pem -out serverreq.pem
Enter pass phrase for serverkey.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,

If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:cnState or Province Name (full name) [Some-State]:AsiaLocality Name (eg, city) []:BJOrganization Name (eg, company) [Internet Widgits Pty Ltd]:Organizational Unit Name (eg, section) []:Common Name (e.g. server FQDN or YOUR name) []:lywEmail Address []:Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:[email protected]:~/certs# openssl ca -in serverreq.pem -out servercert.pemUsing configuration from /usr/lib/ssl/openssl.cnfEnter pass phrase for ./demoCA/private/cakey.pem:Check that the request matches the signatureSignature okCertificate Details: Serial Number: 2 (0x2) Validity Not Before: Jul 17 19:25:24 2015 GMT Not After : Jul 16 19:25:24 2016 GMT Subject: countryName = cn stateOrProvinceName = Asia organizationName = Internet Widgits Pty Ltd commonName = lyw X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 5D:F4:01:21:6A:2D:28:58:24:0E:DC:66:10:4D:65:40:72:C4:04:22 X509v3 Authority Key Identifier: keyid:DD:E5:B1:AA:36:1F:F2:95:A8:0A:DD:51:2E:3D:BB:9A:F3:7B:8C:57Certificate is to be certified until Jul 16 19:25:24 2016 GMT (365 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated

二、apache伺服器。

首先裝apache。更改配置檔案 /usr/local/apache2/conf/extra/httpd-ssl.conf

更改:

SSLCertificateFile "/root/certs/server.crt"
SSLCertificateKeyFile "/root/certs/server.key"
SSLCACertificatePath "/root/certs/demoCA/"
SSLCACertificateFile "/root/certs/demoCA/cacert.pem"
啟動apache伺服器。
/usr/local/apache2/bin/apachectl start
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
Apache/2.2.29 mod_ssl/2.2.29 (Pass Phrase Dialog)
Some of your private key files are encrypted for security reasons.
In order to read them you have to provide the pass phrases.

Server lyw:443 (RSA)
Enter pass phrase:

OK: Pass Phrase Dialog successful.

為客戶生成可以匯入瀏覽器的格式。
openssl pkcs12 -export -clcerts -in usercert.pem -inkey userkey.pem  -out user.p12
Enter pass phrase for userkey.pem:
Enter Export Password:
Verifying - Enter Export Password:

不匯入證書時訪問。


將user.p12匯入瀏覽器。


此時可以檢視證書的資訊,和生成證書請求時填的資訊一樣,點選確定。

由於此證書的頒發者不是瀏覽器信任的CA,所以會有這個告警,點選高階,繼續前往。


至此,已經可以使用自己生成的CA、客戶端證書訪問伺服器了。

當然要設定本地Host檔案。

下一節,我們根據ssl訪問過程分析SSL原理。

參考:http://netkiller.github.io/cryptography/openssl/email.html

相關推薦

openssl 簽發證書ssl 原理簡介

提綱: 1、使用openssl自簽發證書 2、配置apache 3、配置客戶端 4、使用自簽發證書訪問apache 一、使用Openssl自簽發證書 首先裝openssl 。編輯配置檔案/etc/ssl/openssl.conf ,配置TSA root dir 等以下配置

數據結構——圖算法簡介

森林 後繼 鄰接矩陣 數組 如果 結點 無重復 鄰接表 稀疏 圖(graph):是一種較線性表和樹更為復雜的數據結構,圖形結構中,結點之間的關系可以是任意的,圖中任意兩個數據元素之間都可能相關 線性表:數據元素之間僅有線性關系,每個數據元素只有一個直接前驅和一個直接後繼 樹

Golang併發原理GPM排程策略

其實從一開始瞭解到go的goroutine概念就應該想到,其實go應該就是在核心級執行緒的基礎上做了一層邏輯上的虛擬執行緒(使用者級執行緒)+ 執行緒排程系統,如此分析以後,goroutine也就不再那麼神祕了。 併發≠並行 假如我們有一段CPU密集型任務,我們建立2000個gorountine是否真的可

Camera Calibration 相機標定:原理簡介

5 基於2D標定物的標定方法 基於2D標定物的標定方法,原理與基於3D標定物相同,只是通過相機對一個平面進行成像,就可得到相機的標定引數,由於標定物為平面,本身所具有的約束條機,相對後者標定更為簡單。經典演算法為Z. Zhang(PAMI, 2000) A F

Linux平臺下使用者基本管理機制原理剖析

Linux平臺下使用者基本管理機制及原理剖析(一)  Linux是一個多使用者、多工的作業系統,具有很好的安全性和穩定性。 使用者和群組存在的意義 使用者的存在是為了使你的工作環境更加安全,就是有一個隱私空間,這個空間只有使用者本人有許可權。 而群組存在的意義是為了讓大家有

μc/os-II原理簡介筆記

第一章 1、實時作業系統必須是多工系統,任務的切換時間應與系統中的任務數無關,並且中斷延遲的時間應該可預知並儘可能短。 第二章 3.1.1 1、從任務的儲存結構上看,μc/os-II的任務由:任務程式程式碼、任務堆疊和任務控制塊組成。 2、μc/os-II是所有的任務都是執

scikit-learn機器學習常用算法原理編程實戰

sed 數據 super 結構化 得到 人工智 計算機程序 語音 數值 機器學習介紹 機器學習的概念 機器學習要解決的問題分類 使用機器學習解決問題的一般性步驟 什麽是機器學習 機器學習是一個計算機程序,針對某個特定的任務 ,從經驗中學習,並且越做越好。 誰掌握的數據

詞法分析器——哈工大編譯原理課程

mina == 原理 技術分享 after 文件 編碼 exe warn 詞法分析器——哈工大編譯原理課程(一) 程序輸入:從code.txt文件中讀取內容 程序輸出:識別出的單詞序列,格式為:(種別碼,屬性值)      ①對於關鍵字

【java】簡介

編碼 http ... 設計 適合 不能 高度 代碼格式 操作系統 應用:web後端開發、android-app開發、大數據應用開發 學習:java會過時,但程序設計的思想不會過時 特點:1、面向對象,跨平臺,語法比c++簡單     2、以字節碼的形式運行在虛擬機上   

Linux簡介——

組成 mac os x 入門 不同 單機 默認 自己 dha 軟件 1。 常見操作系統 - 服務端操作系統 : linux、unix、windows server - 單機操作系統 : windows(dos 、ucdos、win95、win

我們一起學習Spring之Spring簡介

邏輯 style 發的 nfa 不同的 構建 john 局限 認識   首先聲明,我是一個spring初學者,寫這篇blog的目的是為了能和大家交流。文中不當之處還望大佬指出,不勝感激!   好了,現在我們開始進入正題。   很多小夥伴在學習Java的時候都會有人建議你去學

DCOM分布式組件對象模型原理淺析

com dcom 分布式系統 底層開發 組件技術 (分布式組件對象模型)是一系列微軟的概念和程序接口,利用這個接口,客戶端程序對象能夠請求來自網絡中另一臺計算機上的服務器程序對象。DCOM基於組件對象模型(COM),COM提供了一套允許同一臺計算機上的客戶端和服務器之間進行通信的

SpringCloud微服務簡介

一起 轉發 例如 sset 雲服務 心跳檢測 因此 靈活性 dubbo Spring Cloud簡單認識  微服務英文名稱Microservice,Microservice架構模式就是將整個Web應用組織為一系列小的Web服務。這些小的Web服務可以獨立地編譯及

flume簡介

gpo body events emp 簡單實例 buffers bind 通過 .sh 緒論:   本文的內容包括flume的背景、數據流模型、常見的數據流操作、flume agent啟動和flume agent簡單實例。參考文檔為flume官網的flume1.8.0 F

PowerPC中斷系統簡介

PowerPC 中斷1. PowerPC中斷系統簡介PowerPC處理器的中斷系統由兩部分組成,一是內核的中斷及異常的處理;二是中斷控制器。以P2020處理器為例,包括E500內核中斷及異常處理系統和OpenPIC中斷控制器。在E500內核中,包含兩種可以暫時中止處理器運行當前指令的事件、中斷和異常。其中,異

PCI簡介

PCIX86處理器系統簡介X86主板布局PCI是Peripheral Component Interconnect(外設部件互連標準)的縮寫,本文介紹x86處理器系統下PCI相關知識。x86主板圖如圖 1.1所示:圖 1.1 X86主板布局從圖中可以看出,靠近CPU的是北橋芯片(North Bridge),北

CentOS7.4下DNS服務器軟件BIND安裝相關的配置

dns服務器 bind-chroot nslookup (一)DNS簡介:DNS,全稱Domain Name System,即域名解析系統。域名是通過DNS來實現的,每個域名代表一個IP,DNS就是用來在ip地址與域名之間進行轉換的服務。DNS幫助用戶在互聯網上尋找路徑。在互聯網上的每一個計算機都

Java並發AQS原理分析

jpg 子類 ole success ces || pro 同步 無法 我們說的AQS就是AbstractQueuedSynchronizer,他在java.util.concurrent.locks包下,這個類是Java並發的一個核心類。第一次知道有這個類是在看可重入鎖R

萬能的搜索--之簡介

log 問題 暴力 編程 深度優先 分數 分享 圖片 搜索問題 * 搜索可能是在 NOIP 中使用範圍最廣的算法,大部分的題目,在想不到正解的情況下,總能寫個搜索(我們稱之為的暴力)來得到部分的分數。 * 搜索,嚴格說不上是一個算法,是一種編程的思路:通過窮舉所有的可能性,

WSGIgunicorn指北

pointer multiple pro 協議 一場 nic tip point ise 作為一個Python Web 開發工程師,pyg0每天都喜滋滋的寫著基於各種web框架的業務代碼。 突然有一天,技術老大過來跟pyg0說,嘿,我們要新上線一個服務,你來幫我部署一下吧。