LoginSignup
5
5

More than 1 year has passed since last update.

OpenSSLで楕円曲線暗号(Elliptic Curve Cryptography: ECC)を使用した自己証明書を作成する方法

Posted at

OpenSSLで楕円曲線暗号(Elliptic Curve Cryptography: ECC)を使用した自己証明書を作成する手順です。

環境

  • OS:CentOS Linux release 7.8.2003
[root@CENTOS7 test4]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@CENTOS7 test4]#
  • openssl:OpenSSL 1.0.2k-fips
[root@CENTOS7 test4]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017
[root@CENTOS7 test4]#

手順

1. 使用可能な楕円曲線暗号の種類を確認

以下のコマンドで、使用可能な楕円曲線暗号の種類を確認します。

openssl ecparam -list_curves

実行結果
[root@CENTOS7 test4]# openssl ecparam -list_curves
  secp256k1 : SECG curve over a 256 bit prime field
  secp384r1 : NIST/SECG curve over a 384 bit prime field
  secp521r1 : NIST/SECG curve over a 521 bit prime field
  prime256v1: X9.62/SECG curve over a 256 bit prime field
[root@CENTOS7 test4]#

ここではprime256v1を使用してみます。

2. 秘密鍵の作成

以下のコマンドで楕円曲線暗号(prime256v1)を使用して秘密鍵を作成します。

openssl ecparam -name <暗号名> -genkey -out <秘密鍵のファイル名>

実行結果
[root@CENTOS7 test4]# openssl ecparam -name prime256v1 -genkey -out curves_key.pem
[root@CENTOS7 test4]# ls -l
合計 4
-rw-r--r-- 1 root root 302  7月 15 20:37 curves_key.pem
[root@CENTOS7 test4]#

以下のコマンドで秘密鍵の内容を確認します。

openssl ec -text -noout -in <秘密鍵のファイル名>

実行結果
[root@CENTOS7 test4]# openssl ec -text -noout -in curves_key.pem
read EC key
Private-Key: (256 bit)
priv:
    59:a6:8e:00:67:10:6c:9d:9b:ee:83:5d:58:63:57:
    e8:46:ab:ad:65:10:7c:1f:fe:a9:55:10:9c:73:b1:
    10:a8
pub:
    04:a2:15:f8:93:0a:fd:f3:5e:b8:f4:40:3a:4c:31:
    9a:b1:38:b2:e6:c8:20:4d:cd:61:39:69:e0:5f:75:
    93:b4:b5:43:84:b8:e9:37:e6:2d:e6:ce:69:bb:b8:
    e2:96:1c:c0:59:42:dd:65:16:8a:b8:3d:36:b0:ef:
    81:f7:ce:22:6f
ASN1 OID: prime256v1
NIST CURVE: P-256
[root@CENTOS7 test4]#

3. CSR(証明書署名要求)ファイルの作成

以下のコマンドでCSR(証明書署名要求)ファイルを作成します。

openssl req -new -key <秘密鍵のファイル名> -out <CSRのファイル名> -subj "/C=<国コード>/ST=<都道府県>/L=<市町村>/O=<組織の名称>/OU=<組織の部局の名前>/CN=<サーバの FQDN>"

実行結果
[root@CENTOS7 test4]# openssl req -new -key curves_key.pem -out curves_csr.pem -subj "/C=JP/ST=KANAGAWA/L=YOKOHAMA/O=SAMPLE CORP/OU=DEV1/CN=yasushi.local"
[root@CENTOS7 test4]# ls -l
合計 8
-rw-r--r-- 1 root root 481  7月 15 20:43 curves_csr.pem
-rw-r--r-- 1 root root 302  7月 15 20:37 curves_key.pem
[root@CENTOS7 test4]#

以下のコマンドでCSR(証明書署名要求)ファイルの内容を確認します。

openssl req -text -noout -in <CSRのファイル名>

実行結果
[root@CENTOS7 test4]# openssl req -text -noout -in curves_csr.pem
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=JP, ST=KANAGAWA, L=YOKOHAMA, O=SAMPLE CORP, OU=DEV1, CN=yasushi.local
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:a2:15:f8:93:0a:fd:f3:5e:b8:f4:40:3a:4c:31:
                    9a:b1:38:b2:e6:c8:20:4d:cd:61:39:69:e0:5f:75:
                    93:b4:b5:43:84:b8:e9:37:e6:2d:e6:ce:69:bb:b8:
                    e2:96:1c:c0:59:42:dd:65:16:8a:b8:3d:36:b0:ef:
                    81:f7:ce:22:6f
                ASN1 OID: prime256v1
                NIST CURVE: P-256
        Attributes:
            a0:00
    Signature Algorithm: ecdsa-with-SHA256
         30:44:02:20:3e:9d:3f:ca:8a:9c:8b:dd:58:f3:85:2e:8d:a3:
         79:ff:60:90:56:52:ce:a0:82:18:ae:d8:3a:87:3a:e3:de:c0:
         02:20:6b:0b:b4:9c:ab:a5:65:82:e9:16:54:21:c8:72:19:0c:
         77:1c:db:59:66:8c:0e:17:08:7b:54:7c:fe:bd:43:f2
[root@CENTOS7 test4]#

4. CSRファイルに自己署名して証明書作成

以下のコマンドで、CSRファイルに自己署名して証明書を作成します。

openssl x509 -req -in <CSRのファイル名> -out <証明書のファイル名> -signkey <秘密鍵のファイル名> -days <有効日数> -sha256

実行結果
[root@CENTOS7 test4]# openssl x509 -req -in curves_csr.pem -out curves_cert.pem -signkey curves_key.pem -days 366 -sha256
Signature ok
subject=/C=JP/ST=KANAGAWA/L=YOKOHAMA/O=SAMPLE CORP/OU=DEV1/CN=yasushi.local
Getting Private key
[root@CENTOS7 test4]# ls -l
合計 12
-rw-r--r-- 1 root root 688  7月 15 20:47 curves_cert.pem
-rw-r--r-- 1 root root 481  7月 15 20:43 curves_csr.pem
-rw-r--r-- 1 root root 302  7月 15 20:37 curves_key.pem
[root@CENTOS7 test4]#

以下のコマンドで、自己署名した証明書の内容を確認します。

openssl x509 -text -noout -in <証明書のファイル名>

実行結果
[root@CENTOS7 test4]# openssl x509 -text -noout -in curves_cert.pem
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number:
            e5:0f:07:5c:97:ce:88:84
    Signature Algorithm: ecdsa-with-SHA256
        Issuer: C=JP, ST=KANAGAWA, L=YOKOHAMA, O=SAMPLE CORP, OU=DEV1, CN=yasushi.local
        Validity
            Not Before: Jul 15 11:47:05 2021 GMT
            Not After : Jul 16 11:47:05 2022 GMT
        Subject: C=JP, ST=KANAGAWA, L=YOKOHAMA, O=SAMPLE CORP, OU=DEV1, CN=yasushi.local
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:a2:15:f8:93:0a:fd:f3:5e:b8:f4:40:3a:4c:31:
                    9a:b1:38:b2:e6:c8:20:4d:cd:61:39:69:e0:5f:75:
                    93:b4:b5:43:84:b8:e9:37:e6:2d:e6:ce:69:bb:b8:
                    e2:96:1c:c0:59:42:dd:65:16:8a:b8:3d:36:b0:ef:
                    81:f7:ce:22:6f
                ASN1 OID: prime256v1
                NIST CURVE: P-256
    Signature Algorithm: ecdsa-with-SHA256
         30:44:02:20:2f:19:4e:49:9c:42:5e:ae:1d:a9:6a:d4:05:57:
         a3:f4:8f:17:90:ed:be:ee:13:05:b0:0e:47:15:8a:91:05:b5:
         02:20:1e:a7:94:4b:08:5c:ac:59:93:51:9a:0f:28:c6:1c:05:
         1f:84:c3:5e:bb:8b:fb:0d:f8:c2:3b:68:ef:3f:9c:4f
[root@CENTOS7 test4]#

以上

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5