Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/pr/8844' into maint
Browse files Browse the repository at this point in the history
OTP-19375

* upstream/pr/8844:
  Log warning when we can not load certs from System.keychain
  Load certificates from systems keychain on darwin
  • Loading branch information
dgud committed Nov 27, 2024
2 parents 9b80a14 + e461e03 commit 1f83f01
Showing 1 changed file with 19 additions and 2 deletions.
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 @@ -179,11 +180,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 ->
?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

0 comments on commit 1f83f01

Please sign in to comment.