俺々SSLサーバー証明書の作り方
- 自己署名証明書の基本的な作り方について解説する。
- これは俺々認証局を作るのにも必要な基本的な技術であるためである。
- もちろん実運用的には、イントラネット内に閉じた環境のSSLサーバー証明書の運用など、実務でも使うことがある。
- 対インターネット向けなら、Let's Encryptはもちろんのこと、年額1000円~2000円程度の認証局があるので、そちらを利用されたし。
パッチ
- FreeBSD 10.3-RELEASEのopensslコマンド(1.0.1s)にパッチを当てる。
このパッチはreq(1)に-startdateと-enddateオプションを与えるパッチである。
パッチを当てられないほとんどの環境では-startdate ~ -enddate ~オプションの代わりに-days ~オプションを指定すること。
1 Index: crypto/openssl/apps/req.c
2 ===================================================================
3 --- crypto/openssl/apps/req.c (revision 298785)
4 +++ crypto/openssl/apps/req.c (working copy)
5 @@ -126,6 +126,8 @@
6 * -x509 - output a self signed X509 structure instead.
7 * -asn1-kludge - output new certificate request in a format that some CA's
8 * require. This format is wrong
9 + * -startdate - notBefore field
10 + * -enddate - notAfter field
11 */
12
13 static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn,
14 @@ -179,6 +181,7 @@
15 int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0;
16 char *infile, *outfile, *prog, *keyfile = NULL, *template =
17 NULL, *keyout = NULL;
18 + char *startdate=NULL,*enddate=NULL;
19 #ifndef OPENSSL_NO_ENGINE
20 char *engine = NULL;
21 #endif
22 @@ -364,6 +367,14 @@
23 if (--argc < 1)
24 goto bad;
25 req_exts = *(++argv);
26 + } else if (strcmp(*argv,"-startdate") == 0) {
27 + if (--argc < 1)
28 + goto bad;
29 + startdate= *(++argv);
30 + } else if (strcmp(*argv,"-enddate") == 0) {
31 + if (--argc < 1)
32 + goto bad;
33 + enddate= *(++argv);
34 } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) {
35 /* ok */
36 digest = md_alg;
37 @@ -428,6 +439,10 @@
38 BIO_printf(bio_err,
39 " -days number of days a certificate generated by -x509 is valid for.\n");
40 BIO_printf(bio_err,
41 + " -startdate certificate validity notBefore - YYMMDDHHMMSSZ.\n");
42 + BIO_printf(bio_err,
43 + " -enddate certificate validity notAfter - YYMMDDHHMMSSZ.\n");
44 + BIO_printf(bio_err,
45 " -set_serial serial number to use for a certificate generated by -x509.\n");
46 BIO_printf(bio_err,
47 " -newhdr output \"NEW\" in the header lines\n");
48 @@ -796,13 +811,26 @@
49 if (!rand_serial(NULL, X509_get_serialNumber(x509ss)))
50 goto end;
51 }
52 -
53 + if (startdate == NULL) {
54 + if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
55 + goto end;
56 + } else {
57 + if (!ASN1_UTCTIME_set_string(X509_get_notBefore(x509ss), startdate)) {
58 + BIO_printf(bio_err, "start date is invalid, it should be YYMMDDHHMMSSZ\n");
59 + goto end;
60 + }
61 + }
62 + if (enddate == NULL) {
63 + if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
64 + goto end;
65 + } else {
66 + if (!ASN1_UTCTIME_set_string(X509_get_notAfter(x509ss), enddate)) {
67 + BIO_printf(bio_err, "end date is invalid, it should be YYMMDDHHMMSSZ\n");
68 + goto end;
69 + }
70 + }
71 if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req)))
72 goto end;
73 - if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0))
74 - goto end;
75 - if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL))
76 - goto end;
77 if (!X509_set_subject_name
78 (x509ss, X509_REQ_get_subject_name(req)))
79 goto end;
ディスティングイッシュ名を決める
- 自らが作りたい、俺々SSLサーバー証明書のコモンネーム(CN)以外は本質的に必要ない。
- 管理責任等の明確化のために、組織名(O)や部門名(OU)を加えるとなおよしで、その場合、国名(C)、都道府県(ST)、市区町村(L)を含めるのは当然かと。
- また、ワイルドカード証明書を作りたい場合も、同一手順で問題無いが、SAN(Subject Alternative Name)を行いたい場合は、もう少し手順が必要となる。
- ここでは自身のドメイン(例では ninth-nine.com のこと)をSANしてみる:-)。
項目名 |
略号 |
設定例 |
備考 |
国名(Country name) |
C |
JP |
|
都道府県名(STate or province name) |
ST |
|
|
市区町村(Locality name) |
L |
|
|
組織名(Organization name) |
O |
|
|
部門名(Organization Unit name) |
OU |
|
|
コモンネーム(Common Name) |
CN |
*.ninth-nine.com |
|
メールアドレス(emailAddress) |
emailAddress |
|
|
上記をまとめたのが下記の通りとなる。省略した項目は項目名から省略する(一切残さない)。
/C=JP/CN=*.ninth-nine.com
openssl.cnfの準備
自己署名証明書の作り方
openssl req -new -newkey ec:<(openssl ecparam -name prime256v1) -sha256 -nodes -subj '/C=JP/CN=*.ninth-nine.com' -out '/ssl/*.ninth-nine.com/*.ninth-nine.com,prime256v1-sha256,201601-201912,0.crt -keyout '/ssl/*.ninth-nine.com/*.ninth-nine.com,prime256v1-sha256,201601-201912,0.key -x509 -startdate '160101000000Z' -enddate '191231235959Z' -extensions exts