<commit_before>package gov.nih.nci.cagrid.dorian.ca;

import gov.nih.nci.cagrid.common.FaultHelper;
import gov.nih.nci.cagrid.dorian.common.Database;
import gov.nih.nci.cagrid.dorian.common.LoggingObject;
import gov.nih.nci.cagrid.gridca.common.CRLEntry;
import gov.nih.nci.cagrid.gridca.common.CertUtil;
import gov.nih.nci.cagrid.gridca.common.KeyUtil;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jce.PKCS10CertificationRequest;


/**
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @version $Id: ArgumentManagerTable.java,v 1.2 2004/10/15 16:35:16 langella
 *          Exp $
 */
public class DorianCertificateAuthority extends LoggingObject implements CertificateAuthority {

	public static String CA_TABLE = "certificate_authority";

	private Database db;

	private final static String CA_USER = "CA";

	private boolean dbBuilt = false;

	private DorianCertificateAuthorityConf conf;


	public DorianCertificateAuthority(Database db, DorianCertificateAuthorityConf conf) {
		this.db = db;
		this.conf = conf;
	}


	public void setCACredentials(X509Certificate cert, PrivateKey key) throws CertificateAuthorityFault {
		this.buildDatabase();
		try {
			this.deleteCACredentials();
			String keyStr = KeyUtil.writePrivateKey(key, conf.getCaPassword());
			String certStr = CertUtil.writeCertificate(cert);
			db.update("INSERT INTO " + CA_TABLE + " VALUES('" + CA_USER + "','" + certStr + "','" + keyStr + "')");
		} catch (Exception e) {
			logError(e.getMessage(), e);
			CertificateAuthorityFault fault = new CertificateAuthorityFault();
			fault.setFaultString("Unexpected Error, could not add the CA credentials to the CA database.");
			FaultHelper helper = new FaultHelper(fault);
			helper.addFaultCause(e);
			fault = (CertificateAuthorityFault) helper.getFault();
			throw fault;
		}
	}


	private void buildDatabase() throws CertificateAuthorityFault {
		try {
			if (!dbBuilt) {
				db.createDatabaseIfNeeded();
				if (!this.db.tableExists(CA_TABLE)) {
					String users = "CREATE TABLE " + CA_TABLE + " (" + "ID VARCHAR(255) NOT NULL PRIMARY KEY,"
						+ "CERTIFICATE TEXT NOT NULL," + "PRIVATE_KEY TEXT NOT NULL," + "INDEX document_index (ID));";
					db.update(users);

					if (conf.isAutoCreate()) {
						this
							.createCertifcateAuthorityCredentials(conf.getAutoCreateSubject(), conf.getAutoCreateDate());
					}
				}
				this.dbBuilt = true;
			}<commit_msg>*** empty log message ***<commit_after>package gov.nih.nci.cagrid.dorian.ca;

import gov.nih.nci.cagrid.common.FaultHelper;
import gov.nih.nci.cagrid.dorian.common.Database;
import gov.nih.nci.cagrid.dorian.common.LoggingObject;
import gov.nih.nci.cagrid.gridca.common.CRLEntry;
import gov.nih.nci.cagrid.gridca.common.CertUtil;
import gov.nih.nci.cagrid.gridca.common.KeyUtil;

import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.jce.PKCS10CertificationRequest;


/**
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @author <A href="mailto:<EMAIL>"><NAME> </A>
 * @version $Id: ArgumentManagerTable.java,v 1.2 2004/10/15 16:35:16 langella
 *          Exp $
 */
public class DorianCertificateAuthority extends LoggingObject implements CertificateAuthority {

	public static String CA_TABLE = "certificate_authority";

	private Database db;

	private final static String CA_USER = "CA";

	private boolean dbBuilt = false;

	private DorianCertificateAuthorityConf conf;


	public DorianCertificateAuthority(Database db, DorianCertificateAuthorityConf conf) {
		this.db = db;
		this.conf = conf;
	}


	public void setCACredentials(X509Certificate cert, PrivateKey key) throws CertificateAuthorityFault {
		this.buildDatabase();
		try {
			this.deleteCACredentials();
			String keyStr = KeyUtil.writePrivateKey(key, conf.getCaPassword());
			String certStr = CertUtil.writeCertificate(cert);
			db.update("INSERT INTO " + CA_TABLE + " VALUES('" + CA_USER + "','" + certStr + "','" + keyStr + "')");
		} catch (Exception e) {
			logError(e.getMessage(), e);
			CertificateAuthorityFault fault = new CertificateAuthorityFault();
			fault.setFaultString("Unexpected Error, could not add the CA credentials to the CA database.");
			FaultHelper helper = new FaultHelper(fault);
			helper.addFaultCause(e);
			fault = (CertificateAuthorityFault) helper.getFault();
			throw fault;
		}
	}


	private void buildDatabase() throws CertificateAuthorityFault {
		try {
			if (!dbBuilt) {
				db.createDatabaseIfNeeded();
				if (!this.db.tableExists(CA_TABLE)) {
					String users = "CREATE TABLE " + CA_TABLE + " (" + "ID VARCHAR(255) NOT NULL PRIMARY KEY,"
						+ "CERTIFICATE TEXT NOT NULL," + "PRIVATE_KEY TEXT NOT NULL," + "INDEX document_index (ID));";
					db.update(users);
					this.dbBuilt = true;
					if (conf.isAutoCreate()) {
						this
							.createCertifcateAuthorityCredentials(conf.getAutoCreateSubject(), conf.getAutoCreateDate());
					}
				}
				
			}
{'fraction_non_alphanumeric': 0.12643475189828712, 'fraction_numerical': 0.013067278827476603, 'mean_word_length': 4.607920792079208, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3756454', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1848, 'n_words': 388}