今天尝试将一个websocket服务升级为wss,在调试过程中需要自己生成证书。Google之后发现很多方案混杂在一起,而且很多例子对于新版的Chrome浏览器并不适用。因此找到两篇比较靠谱的文章,整理出本篇博客,文中提到的两种方法均亲测可用。 在浏览器兼容方面,主要是新版本对域名的认证放弃了CommonName,因此本文介绍的方法全部都使用Subject Alternative Name (SAN) , 从而支持目前的主流浏览器。
HOME = . RANDFILE = $ENV::HOME/.rnd #################################################################### [ ca ] default_ca = CA_default # The default ca section [ CA_default ] default_days = 1000 # how long to certify for default_crl_days = 30 # how long before next CRL default_md = sha256 # use public key default MD preserve = no # keep passed DN ordering x509_extensions = ca_extensions # The extensions to add to the cert email_in_dn = no # Don't concat the email in the DN copy_extensions = copy # Required to copy SANs from CSR to cert
#====Following 7 lines are for signing other certs, not for making the CA cert.==== base_dir = . certificate = $base_dir/cacert.pem # The CA certifcate private_key = $base_dir/cakey.pem # The CA private key new_certs_dir = $base_dir # Location for new certs after signing database = $base_dir/index.txt # Database index file serial = $base_dir/serial.txt # The current serial number unique_subject = no # Set to 'no' to allow creation of several certificates with same subject.
#################################################################### [ req ] default_bits = 4096 default_keyfile = cakey.pem distinguished_name = ca_distinguished_name x509_extensions = ca_extensions string_mask = utf8only #################################################################### [ ca_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = CN stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Heilongjiang localityName = Locality Name (eg, city) localityName_default = Harbin organizationName = Organization Name (eg, company) organizationName_default = BY2HIT organizationalUnitName = Organizational Unit (eg, division) organizationalUnitName_default = R&D commonName = Common Name (e.g. server FQDN or YOUR name) commonName_default = Test CA emailAddress = Email Address emailAddress_default = test@example.com #################################################################### [ ca_extensions ] subjectKeyIdentifier = hash authorityKeyIdentifier = keyid:always, issuer basicConstraints = critical, CA:true keyUsage = keyCertSign, cRLSign
#====All lines below are for signing other certs, not for making the CA cert.======
[ server_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = CN stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Heilongjiang localityName = Locality Name (eg, city) localityName_default = Harbin organizationName = Organization Name (eg, company) organizationName_default = BY2HIT commonName = Common Name (e.g. server FQDN or YOUR name) commonName_default = by2hit.net emailAddress = Email Address emailAddress_default = test@example.com