RTCCertificate: getFingerprints() 方法
RTCCertificate 介面的 getFingerprints() 方法用於獲取一個證書指紋的陣列。
這可以在應用層程式碼中用於獲取證書指紋,這些指紋是使用瀏覽器支援的各種演算法建立的證書的雜湊。
語法
getFingerprints()
引數
無。
返回值
一個包含指紋值的陣列。每個指紋由一個具有以下屬性的物件表示:
algorithm-
一個字串,指示用於在
value中建立指紋的雜湊函式演算法。允許的值包括:"sha-1"、"sha-224"、"sha-256"、"sha-384"、"sha-512"、"md5"、"md2"。 value-
一個字串,包含證書指紋,以小寫十六進位制字串表示,使用
algorithm雜湊函式計算得出。格式在RFC4572, Section 5 中有更精確的定義。
描述
用於特定 RTCPeerConnection 的 RTCCertificate 例項可以透過 RTCPeerConnection.generateCertificate() 靜態方法建立,或者從 IndexedDB 儲存中獲取,並在建構函式中設定。如果在建構函式中未傳遞任何證書,則會自動建立,在這種情況下,可以使用 RTCPeerConnection.getConfiguration() 獲取使用的證書。
瀏覽器將在 SDP 提議階段自動交換與每個 RTCPeerConnection 關聯的證書和指紋,並將它們用作 DTLS 握手的一部分,以驗證遠端方是否與 SDP 中傳送的證書/端點匹配。這提供了一種低級別的驗證,以確保 WebRTC 通訊是與發起提議的遠端方建立的,但它不提供任何關於通訊使用者身份的驗證。
在某些情況下,應用層將證書指紋帶外共享會很有用。
- 如果兩個瀏覽器之間已建立信任關係,則可以透過儲存證書並在後續會話中(最多一年內)重新使用它們來持久化這種信任。受信任的證書透過其指紋進行標識。
- 想要識別特定使用者的對等方可以傳送指紋,並在“帶外”(即,在瀏覽器管理的 WebRTC 通訊流程之外)驗證關聯的使用者。應用程式可以使用該指紋來識別後續與特定使用者的會話。
- 在某些會議伺服器(“middlebox”)實現中,伺服器可能需要在進行任何提議/應答之前瞭解指紋。
對等方可能支援不同的演算法集。在比較證書時,應匹配對等方支援的演算法集的所有指紋值。
示例
獲取證書指紋
此示例展示瞭如何從本地對等方獲取證書指紋,並將其與遠端對等方的指紋進行比較。
首先,我們建立一個連線並獲取證書及其指紋。我們透過“某種帶外機制”從遠端對等方獲取指紋。
// Get the certificate fingerprints from the local peer.
const rtcPeerConnection = new RTCPeerConnection();
const configuration = rtcPeerConnection.getConfiguration();
const certificates = configuration.certificates;
let fingerprintsFromClient;
if (certificates && certificates.length > 0) {
certificates.forEach((cert) => {
// For purpose of demonstration, just get first certificate
fingerprintsFromClient = cert.getFingerprints();
break;
});
}
// Get the certificate fingerprints from the remote peer for particular certificate (pseudo code)
const fingerprintsFromServer = [
/* … */
];
有多種方法可以比較特定證書的指紋陣列。在這裡,我們將陣列轉換為字典物件,其中演算法名稱是屬性,然後進行比較。這樣做是可行的,因為每種演算法只能有一個指紋值。(還有許多其他方法可以對兩個陣列進行排序和比較)。
let clientFingerprintDict = Object.fromEntries(
fingerprintsFromClient.map((x) => [x.algorithm, x.value]),
);
let serverFingerprintDict = Object.fromEntries(
fingerprintsFromServer.map((x) => [x.algorithm, x.value]),
);
// Function to compare two objects and return true if there are common properties
// and all common properties match.
function compareObjects(obj1, obj2) {
const commonProperties = Object.keys(obj1).filter((prop) =>
Object.hasOwn(obj2, prop),
);
// Return false if there are no common properties
if (Object.keys(commonProperties).length === 0) return false;
// Return false if any common properties don't match
for (const prop of commonProperties) {
if (obj1[prop] !== obj2[prop]) {
return false;
}
}
return true;
}
const matchingFingerprints = compareObjects(
clientFingerprintDict,
serverFingerprintDict,
);
console.log(matchingFingerprints);
規範
| 規範 |
|---|
| WebRTC:瀏覽器中的即時通訊 # dom-rtccertificate-getfingerprints |
瀏覽器相容性
載入中…