aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssl/src/ssl_certificate.erl
blob: 686e90a70cc870ff891da2712454fd6a925f7e3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2007-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%----------------------------------------------------------------------
%% Purpose: Help funtions for handling certificat verification.
%% The path validation defined in ssl_handshake.erl that mainly
%% calls functions in this module is described in RFC 3280. 
%%----------------------------------------------------------------------

-module(ssl_certificate).

-include("ssl_handshake.hrl").
-include("ssl_alert.hrl").
-include("ssl_internal.hrl").
-include("ssl_debug.hrl").
-include_lib("public_key/include/public_key.hrl"). 

-export([trusted_cert_and_path/3, 
	 certificate_chain/2, 
	 file_to_certificats/1,
	 validate_extensions/6]).
 
%%====================================================================
%% Internal application API
%%====================================================================

trusted_cert_and_path(CertChain, CertDbRef, Verify) ->
    [Cert | RestPath] = lists:reverse(CertChain),
    {ok, OtpCert} = public_key:pkix_decode_cert(Cert, otp),
    IssuerAnPath = 
	case public_key:pkix_is_self_signed(OtpCert) of
	    true ->
		{ok, IssuerId} = public_key:pkix_issuer_id(OtpCert, self),
		{IssuerId, RestPath};
	    false  ->
		case public_key:pkix_issuer_id(OtpCert, other) of
		    {ok, IssuerId} ->
			{IssuerId, [Cert | RestPath]};
		    {error, issuer_not_found} ->
			case find_issuer(OtpCert, no_candidate) of
			    {ok, IssuerId} ->
				{IssuerId, [Cert | RestPath]};
			    Other ->
				{Other, RestPath}
			end
		end
	end,
    
    case IssuerAnPath of
	{{error, issuer_not_found}, _ } ->
	    %% The root CA was not sent and can not be found, we fail if verify = true
	    not_valid(?ALERT_REC(?FATAL, ?UNKNOWN_CA), Verify, {Cert, RestPath});
	{{SerialNr, Issuer}, Path} ->
	    case ssl_certificate_db:lookup_trusted_cert(CertDbRef, 
							SerialNr, Issuer) of
		{ok, {BinCert,_}} ->
		    {BinCert, Path, []};
		_ ->
		    %%  Fail if verify = true
		    not_valid(?ALERT_REC(?FATAL, ?UNKNOWN_CA),
			     Verify,  {Cert, RestPath})
	    end
    end.


certificate_chain(undefined, _CertsDbRef) ->
    {error, no_cert};
certificate_chain(OwnCert, CertsDbRef) ->
    {ok, ErlCert} = public_key:pkix_decode_cert(OwnCert, otp),
    certificate_chain(ErlCert, OwnCert, CertsDbRef, [OwnCert]).

file_to_certificats(File) ->
    {ok, List} = ssl_manager:cache_pem_file(File),
    [Bin || {cert, Bin, not_encrypted} <- List].


%% Validates ssl/tls specific extensions
validate_extensions([], ValidationState, UnknownExtensions, _, AccErr, _) -> 
    {UnknownExtensions, ValidationState, AccErr};

validate_extensions([#'Extension'{extnID = ?'id-ce-extKeyUsage',
				  extnValue = KeyUse,
				  critical = true} | Rest], 
		    ValidationState, UnknownExtensions, Verify, AccErr0, Role) ->
    case is_valid_extkey_usage(KeyUse, Role) of
	true ->
	    validate_extensions(Rest, ValidationState, UnknownExtensions, 
				Verify, AccErr0, Role);
	false ->
	    AccErr = 
		not_valid_extension({bad_cert, invalid_ext_key_usage}, Verify, AccErr0),
	    validate_extensions(Rest, ValidationState, UnknownExtensions, Verify, AccErr, Role)
    end;

validate_extensions([Extension | Rest],  ValidationState, UnknownExtensions, 
		    Verify, AccErr, Role) ->
    validate_extensions(Rest, ValidationState, [Extension | UnknownExtensions],
		       Verify, AccErr, Role).
    
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
certificate_chain(OtpCert, _Cert, CertsDbRef, Chain) ->    
    IssuerAndSelfSigned = 
	case public_key:pkix_is_self_signed(OtpCert) of
	    true ->
		{public_key:pkix_issuer_id(OtpCert, self), true};
	    false  ->
		{public_key:pkix_issuer_id(OtpCert, other), false}
	end,
    
    case IssuerAndSelfSigned of 
	{_, true = SelfSigned} ->
	    certificate_chain(CertsDbRef, Chain, ignore, ignore, SelfSigned);
	{{error, issuer_not_found}, SelfSigned} ->
	    case find_issuer(OtpCert, no_candidate) of
		{ok, {SerialNr, Issuer}} ->
		    certificate_chain(CertsDbRef, Chain, 
				      SerialNr, Issuer, SelfSigned);
		_ ->
		    %% Guess the the issuer must be the root
		    %% certificate. The verification of the
		    %% cert chain will fail if guess is
		    %% incorrect.
		    {ok, lists:reverse(Chain)}
	    end;
	{{ok, {SerialNr, Issuer}}, SelfSigned} -> 
	    certificate_chain(CertsDbRef, Chain, SerialNr, Issuer, SelfSigned)
    end.
  
certificate_chain(_CertsDbRef, Chain, _SerialNr, _Issuer, true) ->
    {ok, lists:reverse(Chain)};

certificate_chain(CertsDbRef, Chain, SerialNr, Issuer, _SelfSigned) ->
    case ssl_certificate_db:lookup_trusted_cert(CertsDbRef, 
						SerialNr, Issuer) of
	{ok, {IssuerCert, ErlCert}} ->
	    {ok, ErlCert} = public_key:pkix_decode_cert(IssuerCert, otp),
	    certificate_chain(ErlCert, IssuerCert, 
			      CertsDbRef, [IssuerCert | Chain]);
	_ ->
	    %% The trusted cert may be obmitted from the chain as the
	    %% counter part needs to have it anyway to be able to
	    %% verify it.  This will be the normal case for servers
	    %% that does not verify the clients and hence have not
	    %% specified the cacertfile.
	    {ok, lists:reverse(Chain)}		      
    end.

find_issuer(OtpCert, PrevCandidateKey) ->
    case ssl_certificate_db:issuer_candidate(PrevCandidateKey) of
 	no_more_candidates ->
 	    {error, issuer_not_found};
 	{Key, {_Cert, ErlCertCandidate}} ->
	    case public_key:pkix_is_issuer(OtpCert, ErlCertCandidate) of
		true ->
		    public_key:pkix_issuer_id(ErlCertCandidate, self);
		false ->
		    find_issuer(OtpCert, Key)
	    end
    end.

not_valid(Alert, true, _) ->
    throw(Alert);
not_valid(_, false, {ErlCert, Path}) ->
    {ErlCert, Path, [{bad_cert, unknown_ca}]}.

is_valid_extkey_usage(KeyUse, client) ->
    %% Client wants to verify server
    is_valid_key_usage(KeyUse,?'id-kp-serverAuth');
is_valid_extkey_usage(KeyUse, server) ->
    %% Server wants to verify client
    is_valid_key_usage(KeyUse, ?'id-kp-clientAuth').

is_valid_key_usage(KeyUse, Use) ->
    lists:member(Use, KeyUse).

not_valid_extension(Error, true, _) ->
    throw(Error);
not_valid_extension(Error, false, AccErrors) ->
    [Error | AccErrors].