【问题标题】:Where can I find details of an X.509 certificate I created locally?在哪里可以找到我在本地创建的 X.509 证书的详细信息?
【发布时间】:2017-08-11 23:05:54
【问题描述】:

在使用证书进行身份验证时,我是新手。如果我的问题没有意义,请纠正我。

我在本地创建了 2048 位 X.509 证书。我有 server.crt 、 server.key 、 server.key.org 和 mycert.pfx (mycert.pfx 包含公钥和私钥,我在我的代码中使用该文件)。

现在我有一个包含以下代码的 Java 应用程序:

String tenant="f6377xxx-aeb2-4a8a-be8a-7xxxxa60be3";
String authority = "https://login.windows.net/"+tenant+"/oauth2/authorize";
ExecutorService service=null;
service= Executors.newFixedThreadPool(1);

try
{
    AuthenticationContext authenticationContext =
        new AuthenticationContext(authority,false,service);
    String certFile="/projects/mycert.pfx";
    InputStream pkcs12Cert= new SharedFileInputStream(certFile);

    AsymmetricKeyCredential credential = AsymmetricKeyCredential.create(
        "xxxx-e53c-45b7-432-7b91d93674b6", pkcs12Cert, "password");

    Future<AuthenticationResult> future = authenticationContext.acquireToken(
        "https://outlook.office365.com", credential, null);

    System.out.println("Token Received"+future.get().getAccessToken());
    String token=future.get().getAccessToken();

此代码正在尝试对 Office 365 API 进行身份验证。为此,我在 Azure 上创建了一个包含租户 ID 和其他信息的应用程序。现在上面的代码抛出如下异常。

com.microsoft.aad.adal4j.AuthenticationException: {"error_description":"AADSTS70002:验证凭据时出错。AADSTS50012:客户端断言包含无效签名。[原因 - 找不到密钥。,客户端使用的密钥指纹: 'H6383KO9763C6E4KIE8363032D6', 配置的键: []]\r\n跟踪 ID: 76YT3GG-7b8b-JDU73-afeb-JDUEY7372\r\n相关 ID: 7H3Y743-a5b7-KD98-88ba-HDUYE7663\r\n时间戳: 2016 31 23:56:50Z","错误":"invalid_client"}

原因是我没有在服务器端(即 Azure AD 应用程序)上传证书。我关注this tutorial 并找到了一个解决方案,显示我必须下载 Manifest 文件,使用证书对其进行编辑,然后将其上传到 Azure 服务器。

问题是我不知道从哪里获取证书中以下键的值。你能帮我在哪里找到customKeyIdentifierkeyIdvalue吗?

"keyCredentials": [
    {
        "customKeyIdentifier": "$base64Thumbprint_from_above",
        "keyId": "$keyid_from_above",
        "type": "AsymmetricX509Cert",
        "usage": "Verify",
        "value":  "$base64Value_from_above"
    }
],

【问题讨论】:

  • 您在什么平台上开发(我假设它不是 Windows,因为您链接的说明向您展示了如何使用 PowerShell 获取这些值)。
  • 定义“本地创建”。
  • @PhilippeSignoret 我正在构建一个将调用 O365 API 的 Java 批处理作业。我正在 Mac 上开发,但批处理将在其中一台 Unix 服务器上运行。 EJP,我使用 Openssl 在本地开发机器上创建了证书,它是自签名的。这仅用于开发目的。
  • 因此,当您使用 OpenSSL 工具时,您指定了文件名。所以这就是证书所在的地方。不清楚你在问什么。
  • 谢谢你们。我已经在下面发布了解决方案。

标签: java ssl-certificate office365 azure-active-directory


【解决方案1】:

我找到了以下源代码来生成我正在寻找的 keyCredentials 中的键/值。虽然您需要先生成证书。然后运行代码,您的 keyCredentials 内容应该在 keycredentials.txt 文件中。

@Test
    public void testGenerateKeyCredentials(){

    String certFile = "/etc/abc/server2.crt";
    System.out.printf("Generating keyCredentials entry from %s\n", certFile);


    try {
        FileInputStream certFileIn = new FileInputStream(certFile);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate cert = cf.generateCertificate(certFileIn);

        // Generate base64-encoded version of the cert's data
        // for the "value" property of the "keyCredentials" entry
        byte[] certData = cert.getEncoded();
        String certValue = Base64.getEncoder().encodeToString(certData);
        System.out.println("Cert value: " + certValue);

        // Generate the SHA1-hash of the cert for the "customKeyIdentifier"
        // property of the "keyCredentials" entry
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(certData);
        String certCustomKeyId = Base64.getEncoder().encodeToString(md.digest());
        System.out.println("Cert custom key ID: " + certCustomKeyId);

        FileWriter fw = new FileWriter("keycredentials.txt", false);
        PrintWriter pw = new PrintWriter(fw);

        pw.println("\"keyCredentials\": [");
        pw.println("  {");
        pw.println("    \"customKeyIdentifier\": \"" + certCustomKeyId + "\",");
        pw.println("    \"keyId\": \"" + UUID.randomUUID().toString() + "\",");
        pw.println("    \"type\": \"AsymmetricX509Cert\",");
        pw.println("    \"usage\": \"Verify\",");
        pw.println("    \"value\": \"" + certValue + "\"");
        pw.println("  }");
        pw.println("],");

        pw.close();

        System.out.println("Key credentials written to keycredentials.txt");
    } catch (FileNotFoundException e) {
        System.out.printf("ERROR: Cannot find %s\n", certFile);
    } catch (CertificateException e) {
        System.out.println("ERROR: Cannot instantiate X.509 certificate");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("ERROR: Cannot instantiate SHA-1 algorithm");
    } catch (IOException e) {
        System.out.println("ERROR: Cannot write to keycredentials.txt");
    }
}

【讨论】:

  • 您可以(并且应该)将自己的答案标记为答案。 :)
【解决方案2】:

certCustomKeyId 和 certValue 的 c# 代码更短:

String certFile = "/etc/abc/server2.crt"; X509Certificate cert = new X509Certificate();

cert.Import(certFile);

String certValue = Convert.ToBase64String(cert.GetRawCertData());

Console.WriteLine("证书值:" + certValue);

String certCustomKeyId = Convert.ToBase64String(cert.GetCertHash()); Console.WriteLine("customKeyIdentifier:" + certCustomKeyId);

Console.WriteLine("keyId:" + System.Guid.NewGuid());

【讨论】:

    【解决方案3】:

    我收到此错误(无效签名...密钥未找到)的原因是我使用了错误的客户端/应用程序 ID做类似的事情:

    var adal = require('adal-node');
    var authorityURL = '...';
    var context = new adal.AuthenticationContext(authorityURL);
    context.acquireTokenAsync(resourceURL, clientId, key, thumbprint);
    

    在关注this procedure(从步骤 1.1 开始)之后,其他一切都很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2013-04-09
      • 2017-03-22
      • 2023-03-31
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      相关资源
      最近更新 更多