UserAuthData cainfodata = new UserAuthData("nobody", ca.getSubjectDN(), ca.getSubjectDN().hashCode(), ((X509CA) ca).getSubjectAltName(), null, 0,  ca.getCertificateProfileId());
UserAuthData cainfodata = new UserAuthData("nobody", null, ca.getSubjectDN(), ca.getSubjectDN().hashCode(), ((X509CA) ca).getSubjectAltName(), null, 0,  ca.getCertificateProfileId(), null);
    public void renewCA(Admin admin, int caid, IResponseMessage responsemessage)  throws CADoesntExistsException, AuthorizationDeniedException, CertPathValidatorException, CATokenOfflineException{    	Collection cachain = null;    	Certificate cacertificate = null;    	// check authorization            	try{    		getAuthorizationSession().isAuthorizedNoLog(admin,"/super_administrator");    	}catch(AuthorizationDeniedException e){    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_NOTAUTHORIZEDTORESOURCE,"Administrator isn't authorized to renew CA",e);    		throw new AuthorizationDeniedException("Not authorized to renew CA with caid = " + caid);    	}    	    	// Get CA info.    	CADataLocal cadata = null;    	try{    		cadata = this.cadatahome.findByPrimaryKey(new Integer(caid));    		CA ca = cadata.getCA();    		try{    			// if issuer is insystem CA or selfsigned, then generate new certificate.    			if(ca.getSignedBy() != CAInfo.SIGNEDBYEXTERNALCA){    				if(ca.getSignedBy() == CAInfo.SELFSIGNED){    					// create selfsigned certificate			       					if( ca instanceof X509CA){                   						UserAuthData cainfodata = new UserAuthData("nobody", ca.getSubjectDN(), ca.getSubjectDN().hashCode(), ((X509CA) ca).getSubjectAltName(), null,     								0,  ca.getCertificateProfileId());    						    						CertificateProfile certprofile = getCertificateStoreSession().getCertificateProfile(admin, ca.getCertificateProfileId());														      						cacertificate = ca.generateCertificate(cainfodata, ca.getCAToken().getPublicKey(SecConst.CAKEYPURPOSE_CERTSIGN),-1, ca.getValidity(), certprofile);    					}                                      					// Build Certificate Chain    					cachain = new ArrayList();    					cachain.add(cacertificate);                				     					    				}else{    					// Resign with CA above.              					if(ca.getSignedBy() > CAInfo.SPECIALCAIDBORDER || ca.getSignedBy() < 0){    						// Create CA signed by other internal CA.				       						CADataLocal signcadata = cadatahome.findByPrimaryKey(new Integer(ca.getSignedBy()));       						CA signca = signcadata.getCA();    						// Check validity of signers certificate    						X509Certificate signcert = (X509Certificate) signca.getCACertificate();                      						try{    							signcert.checkValidity();                       						}catch(CertificateExpiredException ce){    							// Signers Certificate has expired.       							signcadata.setStatus(SecConst.CA_EXPIRED);      							getLogSession().log(admin, signcadata.getCAId().intValue(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Signing CA " + signcadata.getSubjectDN() + " has expired",ce);    							throw new EJBException(ce);       						}catch(CertificateNotYetValidException cve){			      							getLogSession().log(admin, signcadata.getCAId().intValue(), LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Signing CA " + signcadata.getSubjectDN() + " is not yet valid",cve);    							throw new EJBException(cve);				        						}    						// Create cacertificate                 					     						if( ca instanceof X509CA){                   							UserAuthData cainfodata = new UserAuthData("nobody", ca.getSubjectDN(), ca.getSubjectDN().hashCode(), ((X509CA) ca).getSubjectAltName(), null,     									0,  ca.getCertificateProfileId());    							    							CertificateProfile certprofile = getCertificateStoreSession().getCertificateProfile(admin, ca.getCertificateProfileId());														      							cacertificate = signca.generateCertificate(cainfodata, ca.getCAToken().getPublicKey(SecConst.CAKEYPURPOSE_CERTSIGN),-1, ca.getValidity(), certprofile);    						}                                      						    						// Build Certificate Chain    						Collection rootcachain = signca.getCertificateChain();    						cachain = new ArrayList();    						cachain.add(cacertificate);    						cachain.addAll(rootcachain);    					}   			  			       				}            			}else{                    				// if external signer then use signed certificate.    				// check the validity of the certificate chain.		        				if(responsemessage instanceof X509ResponseMessage){    					cacertificate = ((X509ResponseMessage) responsemessage).getCertificate();            				}else{    					getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: illegal response message.");     					throw new EJBException(new EjbcaException("Error: illegal response message."));        	    				}                				    				// Check that DN is the equals the request.      				if(!CertTools.getSubjectDN((X509Certificate) cacertificate).equals(CertTools.stringToBCDNString(ca.getSubjectDN()))){    					getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Error: Subject DN of recieved certificate doesn't match request");     					throw new EJBException(new EjbcaException("Error: Subject DN of recieved certificate doesn't match request"));		        				}    				    				cachain = new ArrayList();    				cachain.add(cacertificate);    				cachain.addAll(ca.getRequestCertificateChain());    				    				cachain = createCertChain(cachain);    				    			}       			// Set statuses.    			if(cacertificate instanceof X509Certificate)    				cadata.setExpireTime(((X509Certificate) cacertificate).getNotAfter().getTime());    			cadata.setStatus(SecConst.CA_ACTIVE);      			    			ca.setCertificateChain(cachain);    			cadata.setCA(ca);      			    			// Publish the new CA certificate                if(ca.getCRLPublishers() != null){                    int certtype = SecConst.CERTTYPE_SUBCA;	                    if(ca.getSignedBy() == CAInfo.SELFSIGNED)     			     certtype = SecConst.CERTTYPE_ROOTCA;                   ArrayList cacert = new ArrayList();                   cacert.add(ca.getCACertificate());     			   getSignSession().publishCACertificate(admin, cacert, ca.getCRLPublishers(), certtype);                }    		    			    		}catch(CATokenOfflineException e){    			ca.setStatus(SecConst.CA_OFFLINE);    			getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Couldn't Renew CA.",e);    			throw e;    		}    	}catch(Exception e){    		getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_ERROR_CAEDITED,"Couldn't Renew CA.",e);    		throw new EJBException(e);    	}      	    	getLogSession().log(admin, caid, LogEntry.MODULE_CA,  new java.util.Date(), null, null, LogEntry.EVENT_INFO_CAEDITED,"CA Renew Successfully.");            	    } // renewCA