https://centossrv.com/apache-client-authentication.shtml
を参考に(というかほぼそのまま)、クライアント証明書によるアクセス制限を試みています。
書いてある通りにやったのですが、証明書をインストールしていないのに相変わらずアクセスができてしまいます。
特に何かエラーが出ているとかでもなく、お手上げ状態です。
何を確認すればいいかとか、どんな情報が必要だとかアドバイスを頂ければ幸いです。
ConoHa VPS
CentOS Linux 7.6.1810
Apache/2.4.6 (CentOS)
Let's Encryptを使用してhttpsでのアクセスは確立済。
AllowOverride をしました。
etc/httpd/conf/httpd.conf を編集し、
<Directory "/var/www/html">のAllowOverrideをNoneからAllにしたところ、証明書を要求するとこまではいきました。
しかし正しい証明書を選択しているはずなのにアクセスできず、
このサイトは安全に接続できません
vps.*****.com でログイン証明書が承認されなかったか、ログイン証明書が提示されていない可能性があります。
システム管理者にお問い合わせください。
となります。
原因として、AllowOverrideについてご指摘頂く前に見ていたのですが、
BASIC認証用ユーザーデータベース(例:/etc/httpd/conf/.htpasswd)がない場合=1件目の場合 [root@centos ~]# htpasswd -bcm /etc/httpd/conf/.htpasswd `openssl x509 -noout -subject -in /etc/pki/CA/certs/ユーザー名.crt |sed -e 's/subject= ([^ ]*)/\1/p' -e d` password ← クライアント証明書をBASIC認証用ユーザーデータベース(例:/etc/httpd/conf/.htpasswd)へ登録 BASIC認証用ユーザーデータベース(例:/etc/httpd/conf/.htpasswd)がある場合=2件目以降の場合 [root@centos ~]# htpasswd -bm /etc/httpd/conf/.htpasswd `openssl x509 -noout -subject -in /etc/pki/CA/certs/ユーザー名.crt |sed -e 's/subject= ([^ ]*)/\1/p' -e d` password ← クライアント証明書をBASIC認証用ユーザーデータベース(例:/etc/httpd/conf/.htpasswd)へ登録
この部分がうまくいっていない気がします。
-bcmのほうでやっても.htpasswdは作成されず、一旦空の.htpasswdを作成してから-bmで行っても空のままです。
その際に表示されるエラー?メッセージは下記です。
[root@******* ~]# htpasswd -bcm /etc/httpd/conf/.htpasswd `openssl x509 -noout -subject -in /etc/pki/CA/certs/********.crt |sed -e 's/subject= ([^ ]*)/\1/p' -e d` password Usage: htpasswd [-cimBdpsDv] [-C cost] passwordfile username htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password htpasswd -n[imBdps] [-C cost] username htpasswd -nb[mBdps] [-C cost] username password -c Create a new file. -n Don't update file; display results on stdout. -b Use the password from the command line rather than prompting for it. -i Read password from stdin without verification (for script usage). -m Force MD5 encryption of the password (default). -B Force bcrypt encryption of the password (very secure). -C Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31). -d Force CRYPT encryption of the password (8 chars max, insecure). -s Force SHA encryption of the password (insecure). -p Do not encrypt the password (plaintext, insecure). -D Delete the specified user. -v Verify password for the specified user. On other systems than Windows and NetWare the '-p' flag will probably not work. The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
####htpasswdコマンド
[root@******* ~]# htpasswd -bcm /etc/httpd/conf/.htpasswd hoge hogehoge Adding password for user hoge
これは普通に通りましたので、
`openssl x509 -noout -subject -in /etc/pki/CA/certs/********.crt |sed -e 's/subject= ([^ ]*)/\1/p' -e d` password
この部分が原因と思われます。
####opensslコマンド
[root@******* ~]# openssl x509 -noout -subject -in /etc/pki/CA/certs/*********.crt subject= /C=JP/ST=*****/L=******/O=********/CN=*********/emailAddress=****@****.com
これは正常に動いているようなので、sed以下の部分なんですが・・・正規表現?よくわかりません。。
####htpasswd生成成功
頂いたアドバイスに従い、
htpasswd -bm /etc/httpd/conf/.htpasswd "/C=JP/ST=*****/L=******/O=********/CN=*********/emailAddress=****@****.com" password
で.htpasswdの生成に成功しましたが、証明書を読み込むとエラーで接続できません。
####クライアント証明書の verify
[root@****** ~]# openssl verify -CAfile /etc/pki/CA/cacert.pem ./newcert.pem ./newcert.pem: OK
####curl コマンドでクライアント証明書認証
[root@****** ~]# curl -v --cacert /etc/pki/CA/cacert.pem --key ./newkey.pem --cert ./newcert.pem https://******.com * About to connect() to vps.******.com port 443 (#0) * Trying **.**.**.**... * Connected to vps.******.com (**.**.**.**) port 443 (#0) * Initializing NSS with certpath: sql:/etc/pki/nssdb * CAfile: /etc/pki/CA/cacert.pem CApath: none * unable to load client key: -8178 (SEC_ERROR_BAD_KEY) * NSS error -8178 (SEC_ERROR_BAD_KEY) * Peer's public key is invalid. * Closing connection 0 curl: (58) unable to load client key: -8178 (SEC_ERROR_BAD_KEY)