Skip to content

Commit

Permalink
Fix Safari 11 PKCS bug (#11396)
Browse files Browse the repository at this point in the history
  • Loading branch information
taymonbeal authored and camelburrito committed Sep 25, 2017
1 parent 3287c42 commit 773880f
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/service/crypto-impl.js
Expand Up @@ -30,11 +30,22 @@ export class Crypto {
/** @private {!Window} */
this.win_ = win;

let subtle = null;
let isLegacyWebkit = false;
if (win.crypto) {
if (win.crypto.subtle) {
subtle = win.crypto.subtle;
} else if (win.crypto.webkitSubtle) {
subtle = win.crypto.webkitSubtle;
isLegacyWebkit = true;
}
}

/** @private @const {?webCrypto.SubtleCrypto} */
this.subtle_ = getSubtle(win);
this.subtle_ = subtle;

/** @private @const {boolean} */
this.isWebkit_ = this.subtle_ && win.crypto && 'webkitSubtle' in win.crypto;
this.isLegacyWebkit_ = isLegacyWebkit;

/** @private {?Promise<{sha384: function((string|Uint8Array))}>} */
this.polyfillPromise_ = null;
Expand Down Expand Up @@ -149,8 +160,8 @@ export class Crypto {
return /** @type {!Promise<!webCrypto.CryptoKey>} */ (
this.subtle_.importKey(
'jwk',
this.isWebkit_ ?
// WebKit wants this as an ArrayBufferView.
this.isLegacyWebkit_ ?
// Safari 10 and earlier want this as an ArrayBufferView.
utf8EncodeSync(JSON.stringify(
/** @type {!JsonObject} */ (jwk))) :
/** @type {!webCrypto.JsonWebKey} */ (jwk),
Expand Down Expand Up @@ -190,13 +201,6 @@ export class Crypto {
}
}

function getSubtle(win) {
if (!win.crypto) {
return null;
}
return win.crypto.subtle || win.crypto.webkitSubtle || null;
}

/**
* @param {!Window} win
*/
Expand Down

0 comments on commit 773880f

Please sign in to comment.