I suggest you the following reading about Using the hardware real-time clock (RTC) in STM32 F0, F2, F3, F4 and L1 series of MCUs. Synchronizing the RTC part starts at page 19.
Just to highlight the basics from the document:
The RTC calendar can be synchronized to a more precise clock, “remote
clock”, using the RTC shift feature. After reading the RTC sub-second
field, a calculation of the precise offset between the time being
maintained by the remote clock and the RTC can be made. The RTC can
be adjusted by removing this offset with a fine adjustment using the
shift register control.
Correcting the RTC calendar time
If the RTC clock is advanced compared to the remote clock by n
fractions of seconds, the offset value must be written in SUBFS,
which will be added to the synchronous prescaler’s counter. As this
counter counts down, this operation effectively subtracts from
(delays) the clock by:
Delay (seconds) = SUBFS / (PREDIV_S + 1)
If the RTC is delayed compared to the remote clock by n fractions of
seconds, the offset value can effectively be added to the clock
(advancing the clock) when the ADD1S function is used in conjunction
with SUBFS, effectively advancing the clock by:
Advance (seconds) = (1 - (SUBFS / (PREDIV_S + 1))).