1 13 14 package org.ejbca.core.ejb.ca.store; 15 16 import java.security.cert.Certificate ; 17 import java.security.cert.CertificateEncodingException ; 18 import java.security.cert.CertificateException ; 19 import java.security.cert.X509Certificate ; 20 import java.util.Date ; 21 22 import javax.ejb.CreateException ; 23 24 import org.apache.log4j.Logger; 25 import org.ejbca.core.ejb.BaseEntityBean; 26 import org.ejbca.core.model.SecConst; 27 import org.ejbca.core.model.ca.crl.RevokedCertInfo; 28 import org.ejbca.util.Base64; 29 import org.ejbca.util.CertTools; 30 31 32 33 108 public abstract class CertificateDataBean extends BaseEntityBean { 109 110 111 public static final int CERT_UNASSIGNED = 0; 112 113 114 public static final int CERT_INACTIVE = 10; 115 116 117 public static final int CERT_ACTIVE = 20; 118 119 121 public static final int CERT_NOTIFIEDABOUTEXPIRATION = 21; 122 123 124 public static final int CERT_TEMP_REVOKED = 30; 125 126 127 public static final int CERT_REVOKED = 40; 128 129 130 public static final int CERT_EXPIRED = 50; 131 132 133 public static final int CERT_ARCHIVED = 60; 134 135 137 public static final int CERT_TYPE_ENCRYPTION = 0x1; 138 139 140 public static final int CERT_TYPE_SIGNATURE = 0x2; 141 142 143 public static final int CERT_TYPE_ENCSIGN = 0x3; 144 145 147 public static final int CERTTYPE_ENDENTITY = 0x1; 148 149 public static final int CERTTYPE_SUBCA = 0x2; 150 151 public static final int CERTTYPE_ROOTCA = 0x8; 152 153 public static final int CERTTYPE_HARDTOKEN = 0x16; 154 155 159 public static final int digitalSignature = (1 << 7); 160 public static final int nonRepudiation = (1 << 6); 161 public static final int keyEncipherment = (1 << 5); 162 public static final int dataEncipherment = (1 << 4); 163 public static final int keyAgreement = (1 << 3); 164 public static final int keyCertSign = (1 << 2); 165 public static final int cRLSign = (1 << 1); 166 public static final int encipherOnly = (1 << 0); 167 public static final int decipherOnly = (1 << 15); 168 169 private static final Logger log = Logger.getLogger(CertificateDataBean.class); 170 171 178 public abstract String getIssuerDN(); 179 180 187 public abstract void setIssuerDN(String issuerDN); 188 189 196 public abstract String getSubjectDN(); 197 198 205 public abstract void setSubjectDN(String subjectDN); 206 207 215 public abstract String getFingerprint(); 216 217 222 public abstract void setFingerprint(String fingerprint); 223 224 231 public abstract String getCaFingerprint(); 232 233 239 public abstract void setCaFingerprint(String caFingerprint); 240 241 248 public abstract int getStatus(); 249 250 256 public abstract void setStatus(int status); 257 258 265 public abstract int getType(); 266 267 273 public abstract void setType(int type); 274 275 282 public abstract String getSerialNumber(); 283 284 290 public abstract void setSerialNumber(String serialNumber); 291 292 299 public abstract long getExpireDate(); 300 301 307 public abstract void setExpireDate(long expireDate); 308 309 316 public abstract long getRevocationDate(); 317 318 324 public abstract void setRevocationDate(long revocationDate); 325 326 333 public abstract int getRevocationReason(); 334 335 341 public abstract void setRevocationReason(int revocationReason); 342 343 350 public abstract String getBase64Cert(); 351 352 358 public abstract void setBase64Cert(String base64Cert); 359 360 367 public abstract String getUsername(); 368 369 377 public abstract void setUsername(String username); 378 379 383 389 public Certificate getCertificate() { 390 X509Certificate cert = null; 391 try { 392 cert = CertTools.getCertfromByteArray(Base64.decode(getBase64Cert().getBytes())); 393 } catch (CertificateException ce) { 394 log.error("Can't decode certificate.", ce); 395 return null; 396 } 397 return cert; 398 } 399 400 406 public void setCertificate(Certificate incert) { 407 try { 408 String b64Cert = new String (Base64.encode(incert.getEncoded())); 409 setBase64Cert(b64Cert); 410 411 X509Certificate tmpcert = (X509Certificate ) incert; 412 String fp = CertTools.getFingerprintAsString(tmpcert); 413 setFingerprint(fp); 414 setSubjectDN(CertTools.getSubjectDN(tmpcert)); 415 setIssuerDN(CertTools.getIssuerDN(tmpcert)); 416 setSerialNumber(tmpcert.getSerialNumber().toString()); 417 } catch (CertificateEncodingException cee) { 418 log.error("Can't extract DER encoded certificate information.", cee); 419 } 420 } 421 422 428 public void setIssuer(String dn) { 429 setIssuerDN(CertTools.stringToBCDNString(dn)); 430 } 431 432 438 public void setSubject(String dn) { 439 setSubjectDN(CertTools.stringToBCDNString(dn)); 440 } 441 442 448 public void setExpireDate(Date expireDate) { 449 if (expireDate == null) { 450 setExpireDate(-1L); 451 } else { 452 setExpireDate(expireDate.getTime()); 453 } 454 } 455 456 462 public void setRevocationDate(Date revocationDate) { 463 if (revocationDate == null) { 464 setRevocationDate(-1L); 465 } else { 466 setRevocationDate(revocationDate.getTime()); 467 } 468 } 469 470 474 486 public CertificateDataPK ejbCreate(Certificate incert) 487 throws CreateException { 488 X509Certificate tmpcert; 490 491 try { 492 String b64Cert = new String (Base64.encode(incert.getEncoded())); 493 setBase64Cert(b64Cert); 494 tmpcert = (X509Certificate ) incert; 495 496 String fp = CertTools.getFingerprintAsString(tmpcert); 497 setFingerprint(fp); 498 499 setSubjectDN(CertTools.getSubjectDN(tmpcert)); 501 setIssuerDN(CertTools.getIssuerDN(tmpcert)); 502 log.debug("Creating certdata, subject=" + getSubjectDN() + ", issuer=" + getIssuerDN()); 503 setSerialNumber(tmpcert.getSerialNumber().toString()); 504 505 setStatus(CERT_UNASSIGNED); 507 setType(SecConst.USER_INVALID); 508 setCaFingerprint(null); 509 setExpireDate(tmpcert.getNotAfter()); 510 setRevocationDate(-1L); 511 setRevocationReason(RevokedCertInfo.NOT_REVOKED); 512 } catch (CertificateEncodingException cee) { 513 log.error("Can't extract DER encoded certificate information.", cee); 514 } 516 return null; 517 } 518 519 524 public void ejbPostCreate(Certificate incert) { 525 } 527 } 528 | Popular Tags |