File tree 1 file changed +20
-20
lines changed
1 file changed +20
-20
lines changed Original file line number Diff line number Diff line change 1
- #GOLANG搭建单、双向自认证HTTPS服务器
2
- ###前言
1
+ # GOLANG搭建单、双向自认证HTTPS服务器
2
+ ### 前言
3
3
> 2015年双11期间淘宝、天猫实现了全站式https安全传输,web安全问题已经成了人们关注的话题,那什么是https呢?如何实现单、双向自认证https服务器呢?接下来我们将一一介绍。
4
4
5
- ##一、HTTPS相关概念已经认证流程
6
- ######基本概念:
5
+ ## 一、HTTPS相关概念已经认证流程
6
+ ###### 基本概念:
7
7
[ ** HTTPS** ] ( http://baike.baidu.com/link?url=XuEFqp8HTAIWBO12QMzj54K1iIBGPL6VJGPEn85nyCirdG8LE104hMYvOeDgfucyMf3gu1zPLap3i0BKb-SKHa ) (全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。 它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https: URL 表明它使用了HTTP,但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。这个系统的最初研发由网景公司(Netscape)进行,并内置于其浏览器Netscape Navigator中,提供了身份验证与加密通讯方法。现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。关于https详细介绍请见:[ 大型网站的HTTPS实践] ( http://studygolang.com/articles/2984 ) 。
8
8
9
9
** SSL** (Secure Socket Layer):是Netscape公司设计的主要用于WEB的安全传输协议。从名字就可以看出它在https协议栈中负责实现上面提到的加密层。
31
31
##二、自认证根证书
32
32
1 . 创建根证书密钥文件(自己做CA)root.key:
33
33
34
- ```shell
34
+ ``` shell
35
35
$openssl genrsa -des3 -out root.key 2048
36
36
```
37
37
38
38
需要输入两次私钥密码
39
39

40
40
2 . 创建根证书的申请文件root.csr:
41
41
42
- ```shell
42
+ ``` shell
43
43
$openssl req -new -key root.key -out root.csr
44
44
```
45
45
48
48
49
49
3 . 创建根证书root.crt:
50
50
51
- ```shell
51
+ ``` shell
52
52
$openssl x509 -req -days 3650 -sha256 -extensions v3_ca -signkey root.key -in root.csr -out root.crt
53
53
```
54
54
58
58
##三、SSL单向认证
59
59
1 . 创建服务器证书秘钥
60
60
61
- ```shell
61
+ ``` shell
62
62
$openssl genrsa –des3 -out server.key 2048
63
63
```
64
64
65
65
需要输入两次私钥密码
66
66

67
67
2 . 去除key口令
68
68
69
- ```shell
69
+ ``` shell
70
70
$openssl rsa -in server.key -out server.key
71
71
```
72
72
73
73
需要输入私钥密码
74
74

75
75
3 . 创建服务器证书申请文件server.csr
76
76
77
- ```shell
77
+ ``` shell
78
78
$openssl req -new -key server.key -out server.csr
79
79
```
80
80
81
81
"Common Name"最好跟网站的域名一致
82
82

83
83
4 . 创建服务器证书server.crt
84
84
85
- ```shell
85
+ ``` shell
86
86
$openssl x509 -req -days 365 -sha256 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt
87
87
```
88
88
100
100

101
101
6 . golang实现简单的https服务器
102
102
103
- ```Go
103
+ ``` Go
104
104
package main
105
105
106
106
import (
130
130
131
131
1 . 创建客户端证书私钥
132
132
133
- ```shell
133
+ ``` shell
134
134
$openssl genrsa -des3 -out client.key 2048
135
135
```
136
136
137
137
需要输入两次私钥密码
138
138

139
139
2 . 去除key口令
140
140
141
- ```shell
141
+ ``` shell
142
142
$openssl rsa -in client.key -out client.key
143
143
```
144
144
145
145
需要输入私钥密码
146
146

147
147
3 . 创建客户端证书申请文件client.csr
148
148
149
- ```shell
149
+ ``` shell
150
150
$openssl req -new -key client.key -out client.csr
151
151
```
152
152
153
153

154
154
155
155
3 . 创建客户端证书文件client.crt
156
156
157
- ```shell
157
+ ``` shell
158
158
$openssl x509 -req -days 365 -sha256 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt
159
159
```
160
160
161
161

162
162
4 . 将客户端证书文件client.crt和客户端证书密钥文件client.key合并成客户端证书安装包client.pfx
163
163
164
- ```shell
164
+ ``` shell
165
165
$openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx
166
166
```
167
167
173
173

174
174
3 . 修改服务器代码
175
175
176
- ```Go
176
+ ``` Go
177
177
package main
178
178
179
179
import (
222
222

223
223
5 . 使用golang访问https服务器
224
224
225
- ```Go
225
+ ``` Go
226
226
package main
227
227
228
228
import (
269
269
270
270
```
271
271
272
- ###结语
272
+ ### 结语
273
273
希望通过这次实例能让大家更好的理解、应用https,谢谢观看。
You can’t perform that action at this time.
0 commit comments