@@ -227,19 +227,16 @@ decode_cert_chain(CertPemBin) ->
227227decode_cert_chain ([], Res ) -> {ok , lists :reverse (Res )};
228228decode_cert_chain ([Cert | Tail ], Res ) ->
229229 case decode_single_certificate (Cert ) of
230- {error , _ } = Err -> Err ;
231- Der -> decode_cert_chain ( Tail , [ Der | Res ])
230+ {ok , Der } -> decode_cert_chain ( Tail , [ Der | Res ]) ;
231+ { error , _ } = Err -> Err
232232 end .
233233
234234decode_single_certificate (CertPemBin ) ->
235235 case do_decode_certificates (CertPemBin ) of
236236 malformed_cert ->
237237 {error , malformed_cert };
238238 [PemEntry ] ->
239- case validate_cert_pem_entry (PemEntry ) of
240- {ok , {'Certificate' , DerCert , not_encrypted }} -> DerCert ;
241- {error , Reason } -> {error , Reason }
242- end ;
239+ validate_cert_pem_entry (PemEntry );
243240 [] ->
244241 {error , malformed_cert };
245242 [_ |_ ] ->
@@ -255,7 +252,7 @@ decode_certificates(CertPemBin) ->
255252 fun (_E , {error , R }) -> {error , R };
256253 (E , {ok , Acc }) ->
257254 case validate_cert_pem_entry (E ) of
258- {ok , Cert } -> {ok , [Cert | Acc ]};
255+ {ok , DerCert } -> {ok , [DerCert | Acc ]};
259256 {error , R } -> {error , R }
260257 end
261258 end , {ok , []}, PemEntries )
@@ -270,8 +267,8 @@ do_decode_certificates(CertPemBin) ->
270267 malformed_cert
271268 end .
272269
273- validate_cert_pem_entry ({'Certificate' , _ , not_encrypted } = Cert ) ->
274- {ok , Cert };
270+ validate_cert_pem_entry ({'Certificate' , Der , not_encrypted }) ->
271+ {ok , Der };
275272validate_cert_pem_entry ({'Certificate' , _ , _ }) ->
276273 {error , encrypted_certificate };
277274validate_cert_pem_entry ({BadType , _ , _ }) ->
@@ -366,17 +363,17 @@ extract_cert_and_pkey(Output) ->
366363 case split_certs (Output ) of
367364 [Cert , PKey ] ->
368365 case decode_single_certificate (Cert ) of
369- {error , Error } ->
370- erlang :exit ({bad_generated_cert , Cert , Error });
371- _ ->
366+ {ok , _ } ->
372367 % % We assume this function is used for self-generated
373368 % % certs only, hence no password is used
374369 case validate_pkey (PKey , fun () -> undefined end ) of
375370 {ok , _ } ->
376371 {Cert , PKey };
377372 Err ->
378373 erlang :exit ({bad_generated_pkey , PKey , Err })
379- end
374+ end ;
375+ {error , Error } ->
376+ erlang :exit ({bad_generated_cert , Cert , Error })
380377 end ;
381378 Parts ->
382379 erlang :exit ({bad_generate_cert_output , Parts })
@@ -440,9 +437,6 @@ convert_date({generalTime, [Y1, Y2, Y3, Y4 | Rest]}) ->
440437 Year = list_to_integer ([Y1 , Y2 , Y3 , Y4 ]),
441438 convert_date (Year , Rest ).
442439
443- get_cert_info ({'Certificate' , DerCert , not_encrypted }) ->
444- get_der_info (DerCert ).
445-
446440get_der_info (DerCert ) ->
447441 Decoded = public_key :pkix_decode_cert (DerCert , otp ),
448442 TBSCert = Decoded # 'OTPCertificate' .tbsCertificate ,
@@ -482,11 +476,10 @@ get_sub_alt_names_by_type(Cert, Type) ->
482476 {error , not_found }
483477 end .
484478
479+ % % Deprecated
485480parse_cluster_ca (CA ) ->
486481 case decode_single_certificate (CA ) of
487- {error , Error } ->
488- {error , Error };
489- RootCertDer ->
482+ {ok , RootCertDer } ->
490483 try
491484 {Subject , NotBefore , NotAfter } = get_der_info (RootCertDer ),
492485 UTC = calendar :datetime_to_gregorian_seconds (
@@ -503,7 +496,9 @@ parse_cluster_ca(CA) ->
503496 ? log_error (" Failed to get certificate info:~n~p~n~p " ,
504497 [RootCertDer , {T , E , S }]),
505498 {error , malformed_cert }
506- end
499+ end ;
500+ {error , Error } ->
501+ {error , Error }
507502 end .
508503
509504% % Deprecated. Can be used in pre-7.1 clusters only.
@@ -665,8 +660,8 @@ decode_and_validate_chain(CAs, Chain) ->
665660
666661get_chain_info (Chain , CA ) when is_binary (Chain ), is_binary (CA ) ->
667662 lists :foldl (
668- fun (Cert , Acc ) ->
669- {NewSub , _ , NewExpiration } = get_cert_info ( Cert ),
663+ fun ({ 'Certificate' , DerCert , not_encrypted } , Acc ) ->
664+ {NewSub , _ , NewExpiration } = get_der_info ( DerCert ),
670665 case Acc of
671666 undefined ->
672667 {NewSub , NewExpiration };
@@ -704,7 +699,8 @@ trusted_CAs(Format) ->
704699 lists :map (
705700 fun (Props ) ->
706701 Pem = proplists :get_value (pem , Props ),
707- decode_single_certificate (Pem )
702+ {ok , Der } = decode_single_certificate (Pem ),
703+ Der
708704 end , SortedCerts )
709705 end .
710706
@@ -714,8 +710,8 @@ trusted_CAs_pre_71(Config) ->
714710
715711 PrepareCertProps =
716712 fun (Id , Type , CAPem ) ->
717- {ok , [CADecoded ]} = decode_certificates (CAPem ),
718- [{id , Id } | cert_props (Type , CADecoded , Extra )]
713+ {ok , [CADer ]} = decode_certificates (CAPem ),
714+ [{id , Id } | cert_props (Type , CADer , Extra )]
719715 end ,
720716
721717 case CertAndPKey of
@@ -990,11 +986,11 @@ add_CAs_txn_fun(Type, Pem, Opts) when is_binary(Pem),
990986 SingleCert = proplists :get_bool (single_cert , Opts ),
991987 ExtraCertProps = proplists :get_value (extra_props , Opts , []),
992988 case decode_certificates (Pem ) of
993- {ok , PemEntries } when SingleCert ,
994- length (PemEntries ) > 1 ->
989+ {ok , DerCerts } when SingleCert ,
990+ length (DerCerts ) > 1 ->
995991 {error , too_many_entries };
996- {ok , PemEntries } ->
997- CAProps = [cert_props (Type , E , ExtraCertProps ) || E <- PemEntries ],
992+ {ok , DerCerts } ->
993+ CAProps = [cert_props (Type , E , ExtraCertProps ) || E <- DerCerts ],
998994 {ok , load_CAs_txn (CAProps , _ )};
999995 {error , Reason } ->
1000996 {error , Reason }
@@ -1121,26 +1117,26 @@ read_ca_file(Path) ->
11211117 case file :read_file (Path ) of
11221118 {ok , CertPemBin } ->
11231119 case decode_certificates (CertPemBin ) of
1124- {ok , PemEntries } ->
1120+ {ok , DerCerts } ->
11251121 Host = misc :extract_node_address (node ()),
11261122 Extras = [{load_host , iolist_to_binary (Host )},
11271123 {load_file , iolist_to_binary (Path )}],
1128- {ok , [cert_props (uploaded , E , Extras )
1129- || E <- PemEntries ]};
1124+ {ok , [cert_props (uploaded , E , Extras ) || E <- DerCerts ]};
11301125 {error , Reason } ->
11311126 {error , Reason }
11321127 end ;
11331128 {error , Reason } ->
11341129 {error , {read , Reason }}
11351130 end .
11361131
1137- cert_props (Type , DecodedCert , Extras ) ->
1138- {Sub , NotBefore , NotAfter } = get_cert_info ( DecodedCert ),
1132+ cert_props (Type , DerCert , Extras ) when is_binary ( DerCert ) ->
1133+ {Sub , NotBefore , NotAfter } = get_der_info ( DerCert ),
11391134 [{subject , iolist_to_binary (Sub )},
11401135 {not_before , NotBefore },
11411136 {not_after , NotAfter },
11421137 {type , Type },
1143- {pem , public_key :pem_encode ([DecodedCert ])}] ++ Extras .
1138+ {pem , public_key :pem_encode ([{'Certificate' , DerCert , not_encrypted }])}]
1139+ ++ Extras .
11441140
11451141get_warnings () ->
11461142 Config = ns_config :get (),
@@ -1213,12 +1209,13 @@ expiration_warnings(CertProps) ->
12131209
12141210is_trusted (CAPem , TrustedCAs ) ->
12151211 case decode_single_certificate (CAPem ) of
1216- {error , _ } -> false ;
1217- Decoded ->
1212+ {ok , Decoded } ->
12181213 lists :any (
12191214 fun (C ) ->
1220- Decoded == decode_single_certificate (C )
1221- end , TrustedCAs )
1215+ {ok , Decoded } == decode_single_certificate (C )
1216+ end , TrustedCAs );
1217+ {error , _ } ->
1218+ false
12221219 end .
12231220
12241221node_cert_warnings (TrustedCAs , NodeCertProps ) ->
0 commit comments