···114114 undecodable certificate - while we'd like to stay operational, and ignore
115115 some certificates *)
116116 let d = "-----" in
117117- let sep = d ^ "END CERTIFICATE" ^ d in
118118- let certs = Astring.String.cuts ~sep ~empty:false data in
119119- let cas =
120120- let affix = d ^ "BEGIN CERTIFICATE" ^ d in
117117+ let new_cert = d ^ "BEGIN CERTIFICATE" ^ d
118118+ and end_of_cert = d ^ "END CERTIFICATE" ^ d in
119119+ let len_new = String.length new_cert
120120+ and len_end = String.length end_of_cert in
121121+ let lines = String.split_on_char '\n' data in
122122+ let it, cas =
121123 List.fold_left
122122- (fun acc data ->
123123- if not (Astring.String.is_infix ~affix data) then acc
124124- else
125125- let data = data ^ sep in
126126- match X509.Certificate.decode_pem (Cstruct.of_string data) with
127127- | Ok ca -> ca :: acc
128128- | Error (`Msg msg) ->
129129- Log.warn (fun m -> m "Failed to decode a trust anchor %s." msg);
130130- Log.debug (fun m -> m "Full certificate:@.%s" data);
131131- acc)
132132- [] certs
124124+ (fun (acc, cas) line ->
125125+ match acc with
126126+ | None
127127+ when String.length line >= len_new
128128+ && String.(equal (sub line 0 len_new) new_cert) ->
129129+ (Some [ line ], cas)
130130+ | None ->
131131+ Log.debug (fun m -> m "ignoring line %s" line);
132132+ (None, cas)
133133+ | Some lines
134134+ when String.length line >= len_end
135135+ && String.(equal (sub line 0 len_end) end_of_cert) -> (
136136+ let data = String.concat "\n" (List.rev (line :: lines)) in
137137+ match X509.Certificate.decode_pem (Cstruct.of_string data) with
138138+ | Ok ca -> (None, ca :: cas)
139139+ | Error (`Msg msg) ->
140140+ Log.warn (fun m -> m "Failed to decode a trust anchor %s." msg);
141141+ Log.debug (fun m -> m "Full certificate:@.%s" data);
142142+ (None, cas))
143143+ | Some lines -> (Some (line :: lines), cas))
144144+ (None, []) lines
133145 in
146146+ (match it with
147147+ | None -> ()
148148+ | Some lines ->
149149+ Log.debug (fun m ->
150150+ m "ignoring leftover data: %s" (String.concat "\n" (List.rev lines))));
134151 let cas = List.rev cas in
135152 match cas with
136153 | [] -> Error (`Msg ("ca-certs: empty trust anchors.\n" ^ issue))