19

I'd like to access SSL certificate information from a Google Chrome extension.

I took a look at the APIs here: http://code.google.com/chrome/extensions/api_index.html, but didn't see anything that would get the job done.

Ideally I'd like to get access to Issuer, Validity Period, Subject, Serial Number, etc...

This seems to be possible in Mozilla/Firefox:

https://developer.mozilla.org/En/How_to_check_the_security_state_of_an_XMLHTTPRequest_over_SSL

http://www.sslshopper.com/article-perspectives-extension-to-change-how-firefox-handles-ssl-certificates.html

Dominik
  • 2,283
  • 1
  • 25
  • 37
Tom Hennen
  • 4,746
  • 7
  • 34
  • 45
  • possible duplicate of [Within a web browser, is it possible for JavaScript to obtain information about the SSL Certificate being used for the current page?](http://stackoverflow.com/questions/2402121/within-a-web-browser-is-it-possible-for-javascript-to-obtain-information-about-t) – Pablo Fernandez Jul 04 '11 at 01:54
  • Hi @tom, I've added working code to get this working under webextensions (this uses the same API as Chrome extensions). Currently this works in Firefox only, but it may soon work in Chrome. If that answer helps, maybe mark it as an accepted answer? – mikemaccana Jun 22 '18 at 23:02
  • See [Chrome bug #628819](https://bugs.chromium.org/p/chromium/issues/detail?id=628819), opened in July 2016. As of September 2022, it's **still not fixed**. – JamesTheAwesomeDude Sep 22 '22 at 21:44

3 Answers3

15

2018 answer: webextensions (which use the Chrome extension API) can do this in Firefox 62

You'll need to make a WebExtension, which is also called a browser extension.

See accessing security information on MDN

You can also check out the docs for:

You'll need Firefox 62.

Here's a working background.js

var log = console.log.bind(console)

log(`\n\nTLS browser extension loaded`)

// https://developer.chrome.com/extensions/match_patterns
var ALL_SITES = { urls: ['<all_urls>'] }

// Mozilla doesn't use tlsInfo in extraInfoSpec 
var extraInfoSpec = ['blocking']; 

// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onHeadersReceived
browser.webRequest.onHeadersReceived.addListener(async function(details){
    log(`\n\nGot a request for ${details.url} with ID ${details.requestId}`)

    // Yeah this is a String, even though the content is a Number
    var requestId = details.requestId

    var securityInfo = await browser.webRequest.getSecurityInfo(requestId, {
        certificateChain: true,
        rawDER: false
    });

    log(`securityInfo: ${JSON.stringify(securityInfo, null, 2)}`)

}, ALL_SITES, extraInfoSpec) 

log('Added listener')

manifest.json:

{
    "manifest_version": 2,
    "name": "Test extension",
    "version": "1.0",
    "description": "Test extension.",
    "icons": {
        "48": "icons/border-48.png"
    },
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": [
        "webRequest",
        "webRequestBlocking",
        "<all_urls>"
    ]
}

enter image description here

It also may be implemented in Chromium once this code is merged.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
2

It is currently not available, but there's the Chromium API proposal webRequest SSL Hooks (from 02/27/2012) which treats this topic.

Dominik
  • 2,283
  • 1
  • 25
  • 37
  • +1, thanks for the link, [I've written a **more complete answer** to another question over here at SO after looking into the Chromium API proposal](http://stackoverflow.com/a/21541463/1122270) that you've brought to my attention here, thanks! – cnst Feb 04 '14 at 01:44
  • The link is not working anymore. It shows page not found. – Reem Aug 13 '20 at 16:16
-1

You can use a NPAPI plugin to do that.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 1
    This answer seems to suggest you can't. Unless something has changed... http://stackoverflow.com/questions/4077060/ssl-certificate-context-how-to-get-it-using-npapi – Tom Hennen Aug 08 '11 at 18:47
  • Additionally, Chrome is deprecating support for NPAPI in 2014: http://www.chromium.org/developers/npapi-deprecation –  Jul 27 '14 at 06:50