【问题标题】:Self-signed certificates and Nativescript Android Apps自签名证书和 Nativescript Android 应用程序
【发布时间】:2019-01-31 21:58:34
【问题描述】:

我正在开发一个在 Android 模拟器中调试的 Nativescript 应用程序。我在该应用程序中有一项服务,它调用通过 HTTPS 访问的 ASP.Net Core API。我已使用以下 Powershell 脚本生成了一个自签名证书并将其添加到我的个人和受信任的商店中

$selfSignedCert = New-SelfSignedCertificate `
-Subject "CN=10.0.2.2" `
-KeyExportPolicy Exportable `
-FriendlyName "My self-signed certificate" `
-DnsName "localhost","10.0.2.2" `
-HashAlgorithm sha256 `
-KeyLength 2048 `
-NotAfter (Get-Date).AddYears(1) `
-CertStoreLocation "Cert:\CurrentUser\My" `
-TextExtension @("2.5.29.19 ={critical} {text}ca=true")

$thumbprint = $selfSignedCert.Thumbprint

Export-Certificate -Cert cert:\CurrentUser\my\$thumbprint -FilePath 
c:\Temp\MyCert.cer -force

Import-Certificate -filePath c:\Temp\MyCert.cer -CertStoreLocation 
"cert:\CurrentUser\Root"

然后我更新了 API 的 application.json 以从我的商店提供此证书,即,

{
    "Kestrel": {
      "Endpoints": {
        "HttpsInlineCertStore": {
          "Url": "https://localhost:5001",
            "Certificate": {
              "Subject": "10.0.2.2",
              "Store": "My",
              "Location": "CurrentUser"
            }
          }
        }
      }
 }

我已将相同的证书上传到我的 Android 模拟器,并在此处看到它安装在受信任的凭据中

这里是用户凭据

我已更新我的 AndroidManifest.xml 文件以包含 network_security_config,即,

<application
    android:name="com.tns.NativeScriptApplication"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:debuggable="true"
    android:networkSecurityConfig="@xml/network_security_config">

network_security_config 在哪里

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <debug-overrides>
        <trust-anchors>
            <certificates src="user"/>
        </trust-anchors>
    </debug-overrides>
</network-security-config>

但是,当我尝试从我的 Nativescript 应用程序中调用我的 API 时,我收到以下错误

JS:     "originalStack": "Error: javax.net.ssl.SSLPeerUnverifiedException: Hostname 10.0.2.2 not verified:\n    certificate: sha1/Ls/XF5mOCTPFkwc7SY//8DELFQU=\n    DN: CN=10.0.2.2\n    subjectAltNames: [localhost, 10.0.2.2]\n    at new ZoneAwareError (file:///data/data/org.nativescript.koffi/files/app/vendor.js:80939:33)\n    at onRequestComplete (file:///data/data/org.nativescript.koffi/files/app/vendor.js:100228:34)\n
   at Object.onComplete (file:///data/data/org.nativescript.koffi/files/app/vendor.js:100220:13)",
JS:     "zoneAwareStack": "Error: javax.net.ssl.SSLPeerUnverifiedException: Hostname 10.0.2.2 not verified:\n    certificate: sha1/Ls/XF5mOCTPFkwc7SY//8DELFQU=\n    DN: CN=10.0.2.2\n    subjectAltNames: [localhost, 10.0.2.2...

这是因为证书是自签名的,而不是来自受信任的 CA,即使我的 XML 配置指向 user 的信任锚?开发 Android 应用时是否可以使用自签名证书?

【问题讨论】:

  • 您解决了这个问题吗?

标签: android ssl nativescript


【解决方案1】:

通用名称 (CN) 不应是 IP 地址。您必须将备用名称 (SAN) 设置为 IP 地址 - 在您的情况下为 10.0.2.2。

请在您的脚本中添加参数“-SAN ”。 有关 powershell 脚本,请参阅此处的“SubjectAlternativeName”部分:https://www.sysadmins.lv/blog-en/self-signed-certificate-creation-with-powershell.aspx

您也可以使用其他工具生成证书/密钥,例如 openssl 和 keytool。我发现生成的最简单方法是使用 keytool: https://medium.com/@piraveenaparalogarajah/ssl-issues-when-integrating-android-applications-with-local-is-server-7355b681dbc9

【讨论】:

    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2016-03-23
    • 2021-10-29
    相关资源
    最近更新 更多