anchor.diff:      Default.
   
   handle_incomplete_chain([PeerCert| _] = Chain0, PartialChainHandler, Default, CertDbHandle, CertDbRef) ->
  +    %% We received an incomplete chain, that is not all certs expected to be present are present. 
  +    %% See if we have the certificates to rebuild it. 
       case certificate_chain(PeerCert, CertDbHandle, CertDbRef) of
           {ok, _, [PeerCert | _] = Chain} when Chain =/= Chain0 -> %% Chain candidate found          
               handle_partial_chain(lists:reverse(Chain), PartialChainHandler, CertDbHandle, CertDbRef);
  
positive.code: 
  %%--------------------------------------------------------------------
  -spec trusted_cert_and_paths([der_cert()], db_handle(), certdb_ref(), fun()) ->
  				   [{der_cert() | unknown_ca | selfsigned_peer, [der_cert()]}].
  %%
  %% Description: Extracts the root cert (if not presents tries to 
  %% look it up, if not found {bad_cert, unknown_ca} will be added verification
  %% errors. Returns {RootCert | RootCertRelatedError, Path} Path = lists:reverse(Chain) -- Root
  %%--------------------------------------------------------------------
  trusted_cert_and_paths([Peer] = Chain,  CertDbHandle, CertDbRef, PartialChainHandler) ->
      OtpCert = public_key:pkix_decode_cert(Peer, otp),
  
negative.code:             Default
      end.
  
  extraneous_chains(Certs) ->
      %% If some certs claim to be the same cert that is have the same
      %% subject field we should create a list of possible chain certs
      %% for each such cert. Only one chain, if any, should be
      %% verifiable using available ROOT certs.
      Subjects = [{subject(Cert), Cert} || Cert <- Certs],
      SortedCerts = sort_by_subject(Subjects),
      Extra = extraneous_certs(SortedCerts, []),
      extraneous_chains(Extra, Certs).
  
  extraneous_chains([{CA0, CA1}, {CA2, CA3} | _], Chain) ->
      [Chain -- [CA0, CA2],  
       Chain -- [CA0, CA3], 
       Chain -- [CA1, CA2],
       Chain -- [CA1, CA3]];
  extraneous_chains([{CA0, CA1} | _], Chain) ->
      [Chain -- [CA0],  
       Chain -- [CA1]];					
  extraneous_chains(_, Chain) ->
      %% Let if fail too many extraneous cert
      %% chain alternatives
      [Chain].
                                                   
  extraneous_certs([], Acc) ->
      Acc;
  extraneous_certs([_], Acc) ->
      Acc;
  extraneous_certs([CA0, CA1 | Certs], Acc) ->
      {_, Name1} = public_key:pkix_subject_id(CA0),
      {_, Name2} = public_key:pkix_subject_id(CA1),
      
      case Name1 of
          Name2 -> 
              %% Found extraneous cert
              extraneous_certs([CA1 | Certs], [{CA0, CA1} | Acc]);
          _ ->
              extraneous_certs([CA1 | Certs], Acc)
      end.
  
  sort_by_subject(Subjects) ->
       Sort = lists:keysort(1, Subjects),
       lists:map(fun({_, Cert}) -> Cert end, Sort).
      
  subject(Cert) ->
      {_, Name} = public_key:pkix_subject_id(Cert),
      Name.
  