(f) The records of the Department of State and of diplomatic and consular offices of the United States pertaining to the issuance or refusal of visas or permits to enter the United States shall be considered confidential and shall be used only for the formulation, amendment, administration, or enforcement of the immigration, nationality, and other laws of the United States, 1a/ except that--
(1) in the discretion of the Secretary of State certified copies of such records may be made available to a court which certifies that the information contained in such records is needed by the court in the interest of the ends of justice in a case pending before the court.
(2) the Secretary of State, in the Secretary's discretion and on the basis of reciprocity, may provide to a foreign government information in the Department of State's computerized visa lookout database and, when necessary and appropriate, other records covered by this section related to information in the database--
(A) with regard to individual aliens, at any time on a case-by-case basis for the purpose of preventing, investigating, or punishing acts that would constitute a crime in the United States, including, but not limited to, terrorism or trafficking in controlled substances, persons, or illicit weapons; or
(B) with regard to any or all aliens in the database, pursuant to such conditions as the Secretary of State shall establish in an agreement with the foreign government in which that government agrees to use such information and records for the purposes described in subparagraph (A) or to deny visas to persons who would be inadmissible to the United States.