2016-09-01 51 views
1

當談到使用證書進行身份驗證時,我是一個新手。如果我的問題沒有意義,請糾正我。我在哪裏可以找到我在本地創建的X.509證書的詳細信息?

我在本地創建了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。爲此,我使用tenant id和其他信息創建了一個Azure應用程序。現在上面的代碼會引發下面的異常。

com.microsoft.aad.adal4j.AuthenticationException:{ 「ERROR_DESCRIPTION」:「AADSTS70002:錯誤驗證憑據AADSTS50012:。客戶端斷言包含無效的簽名[原因 - 鍵沒有被發現,關鍵的指紋客戶使用:'H6383KO9763C6E4KIE8363032D6',配置的密鑰:[]] \ r \ nTrace ID:76YT3GG-7b8b-JDU73-afeb-JDUEY7372 \ r \ n相關性ID:7H3Y743-a5b7-KD98-88ba-HDUYE7663 \ r \ n時間戳:2016 -08-31 23:56:50Z「,」error「:」invalid_client「}

原因是因爲我沒有在服務器端上傳證書(即在Azure AD應用程序上)。我跟着this tutorial,發現一個解決方案,顯示我必須下載清單文件,使用證書對其進行編輯,然後將其上載到Azure服務器。

問題是我不知道從證書中獲取以下密鑰的值。你能幫我找到customKeyIdentifier,keyIdvalue嗎?

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

您開發的平臺是什麼(我假設它不是Windows,因爲您鏈接的工具向您展示瞭如何使用PowerShell獲取這些值)。 –

+0

定義'本地創建'。 – EJP

+0

@PhilippeSignoret我正在構建一個將調用O365 API的Java Batch Job。我在Mac上開發,但批處理將在Unix服務器之一中運行。 EJP,我使用Openssl在本地開發機器上創建了證書,並且它是自簽名的。這僅用於開發目的。 – WowBow

回答

1

我發現以下源代碼來生成我正在尋找的keyCredentials中的鍵/值。儘管您需要首先生成證書。然後運行代碼,您的keyCredentials內容應該位於keycredentials.txt文件中。爲ce​​rtCustomKeyId和certValue

@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"); 
    } 
} 
+0

你可以(也應該)標記您自己的答案的答案。 :) –

+0

我還剩7個小時。大聲笑 – WowBow

0

較短C#代碼:

字符串certFile中= 「/etc/abc/server2.crt」; X509Certificate cert = new X509Certificate();

cert.Import(certFile);

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

Console.WriteLine(「Cert value:」+ certValue);

String certCustomKeyId = Convert.ToBase64String(cert.GetCertHash()); Console.WriteLine(「customKeyIdentifier:」+ certCustomKeyId); (「keyId:」+ System.Guid.NewGuid());

0

我得到這個錯誤的原因(無效簽名...關鍵是沒有找到),是我用錯了客戶機/應用程序ID當我在做這樣的事情:

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

一切是好的,下面this procedure後(從步驟1.1)

開始
相關問題