First draw the box with depth buffering, then draw the solid lines with depth testing, only the ones in front or beside the box are rendered.
Now disable depth testing and enable line stippling, render all lines again. Done.
The parts which were previously occluded by the box are rendered stippled, the parts where the lines have already been rendered solid can’t change because they either fail the depth test and are rendered in the same color anyway.
Cross your fingers that the implementation of the solid and stippled lines is accurate enough to hit the same pixels, otherwise the outside parts might get two pixels thick, solid and stippled.
Important: Use the exact identical rendering order. Rendering a line from A to B is not the same as rendering from B to A due to the diamond exit rasterization rule for lines on OpenGL.
To avoid the problems mentioned above, you can use the stencil buffer which is more elegant as well (fewer pixels rendered).
The box would mask the area where stippled lines are drawn inside the stencil. The zPass and zFail tests in the stencil test would be used to let either the solid or the stippled lines render.
BTW, the stippled white outline on the top left and top right edges of the box, are depth buffer bleeding artefacts. To resolve those the application would need to use polygon offset for the box’ faces and push them into the scene.