Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load certificates from systems keychain on darwin #8844

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/public_key/src/pubkey_os_cacerts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

-include("public_key.hrl").
-include_lib("kernel/include/file.hrl").
-include_lib("kernel/include/logger.hrl").
-export([load/0, load/1, get/0, clear/0, format_error/2]).

-on_load(on_load/0).
Expand Down Expand Up @@ -171,11 +172,27 @@ load_win32() ->
store(lists:foldl(Dec, [], os_cacerts())).

load_darwin() ->
SystemRootsKeyChainFile = "/System/Library/Keychains/SystemRootCertificates.keychain",
case get_darwin_certs(SystemRootsKeyChainFile) of
{ok, Bin1} ->
SystemKeyChainFile = "/Library/Keychains/System.keychain",
case get_darwin_certs(SystemKeyChainFile) of
{ok, Bin2} ->
decode_result(<<Bin1/binary, Bin2/binary>>);
Err ->
starbelly marked this conversation as resolved.
Show resolved Hide resolved
?LOG_WARNING(
"Unable to load additional OS certificates from System.keychain : ~p~n", [Err]),
decode_result(Bin1)
end;
Err ->
Err
end.

get_darwin_certs(KeyChainFile) ->
%% Could/should probably be re-written to use Keychain Access API
KeyChainFile = "/System/Library/Keychains/SystemRootCertificates.keychain",
Args = ["export", "-t", "certs", "-f", "pemseq", "-k", KeyChainFile],
try run_cmd("/usr/bin/security", Args) of
{ok, Bin} -> decode_result(Bin);
{ok, _} = Res -> Res;
Err -> Err
catch error:Reason ->
{error, {eopnotsupp, Reason}}
Expand Down
Loading