From 53b7f59d14a0f1afa33be6aefd123f2c5f741181 Mon Sep 17 00:00:00 2001 From: "Taymon A. Beal" Date: Fri, 22 Sep 2017 19:30:30 -0400 Subject: [PATCH] Fix Safari 11 PKCS bug --- src/service/crypto-impl.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/service/crypto-impl.js b/src/service/crypto-impl.js index e1e58f42e26b..20c82650c54a 100644 --- a/src/service/crypto-impl.js +++ b/src/service/crypto-impl.js @@ -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; @@ -149,8 +160,8 @@ export class Crypto { return /** @type {!Promise} */ ( 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), @@ -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 */