📄 rsasignsessionbean.java
字号:
* @throws AuthLoginException If the password is incorrect. * @throws IllegalKeyException if the public key is of wrong type. * @ejb.permission unchecked="true" * @ejb.interface-method view-type="both" */ public Certificate createCertificate(Admin admin, String username, String password, int certType, PublicKey pk) throws ObjectNotFoundException, AuthStatusException, AuthLoginException, IllegalKeyException, CADoesntExistsException { debug(">createCertificate(pk, certType)"); // Create an array for KeyUsage acoording to X509Certificate.getKeyUsage() boolean[] keyusage = new boolean[9]; Arrays.fill(keyusage, false); switch (certType) { case CertificateDataBean.CERT_TYPE_ENCRYPTION: // keyEncipherment keyusage[2] = true; // dataEncipherment keyusage[3] = true; break; case CertificateDataBean.CERT_TYPE_SIGNATURE: // digitalSignature keyusage[0] = true; // non-repudiation keyusage[1] = true; break; default: // digitalSignature keyusage[0] = true; // keyEncipherment keyusage[2] = true; break; } Certificate ret = createCertificate(admin, username, password, pk, keyusage); debug("<createCertificate(pk, certType)"); return ret; } // createCertificate /** * Requests for a certificate to be created for the passed public key wrapped in a self-signed * certificate. Verification of the signature (proof-of-possesion) on the request is * performed, and an exception thrown if verification fails. The method queries the user * database for authorization of the user. * * @param admin Information about the administrator or admin preforming the event. * @param username unique username within the instance. * @param password password for the user. * @param incert a certificate containing the public key to be put in the created certificate. * Other (requested) parameters in the passed certificate can be used, such as DN, * Validity, KeyUsage etc. Currently only KeyUsage is considered! * @return The newly created certificate or null. * @throws ObjectNotFoundException if the user does not exist. * @throws AuthStatusException If the users status is incorrect. * @throws AuthLoginException If the password is incorrect. * @throws IllegalKeyException if the public key is of wrong type. * @throws SignRequestSignatureException if the provided client certificate was not signed by * the CA. * @ejb.permission unchecked="true" * @ejb.interface-method view-type="both" */ public Certificate createCertificate(Admin admin, String username, String password, Certificate incert) throws ObjectNotFoundException, AuthStatusException, AuthLoginException, IllegalKeyException, SignRequestSignatureException, CADoesntExistsException { debug(">createCertificate(cert)"); X509Certificate cert = (X509Certificate) incert; try { // Convert the certificate to a BC certificate. SUN does not handle verifying RSASha256WithMGF1 for example X509Certificate bccert = CertTools.getCertfromByteArray(incert.getEncoded()); bccert.verify(cert.getPublicKey()); } catch (Exception e) { log.debug("Exception verify POPO: ", e); String msg = intres.getLocalizedMessage("signsession.popverificationfailed"); throw new SignRequestSignatureException(msg); } Certificate ret = createCertificate(admin, username, password, cert.getPublicKey(), cert.getKeyUsage()); debug("<createCertificate(cert)"); return ret; } // createCertificate /** * Requests for a certificate to be created for the passed public key wrapped in a * certification request message (ex PKCS10). Verification of the signature * (proof-of-possesion) on the request is performed, and an exception thrown if verification * fails. The method queries the user database for authorization of the user. * * @param admin Information about the administrator or admin preforming the event. * @param req a Certification Request message, containing the public key to be put in the * created certificate. Currently no additional parameters in requests are considered! * Currently no additional parameters in the PKCS10 request is considered! * @param responseClass The implementation class that will be used as the response message. * @return The newly created response message or null. * @throws ObjectNotFoundException if the user does not exist. * @throws AuthStatusException If the users status is incorrect. * @throws AuthLoginException If the password is incorrect. * @throws IllegalKeyException if the public key is of wrong type. * @throws SignRequestException if the provided request is invalid. * @throws SignRequestSignatureException if the provided client certificate was not signed by * the CA. * @ejb.permission unchecked="true" * @ejb.interface-method view-type="both" */ public IResponseMessage createCertificate(Admin admin, IRequestMessage req, Class responseClass) throws NotFoundException, AuthStatusException, AuthLoginException, IllegalKeyException, CADoesntExistsException, SignRequestException, SignRequestSignatureException { return createCertificate(admin, req, -1, responseClass); } /** * Requests for a certificate to be created for the passed public key with the passed key * usage and using the given certificate profile. This method is primarily intended to be used when * issueing hardtokens having multiple certificates per user. * The method queries the user database for authorization of the user. CAs are only * allowed to have certificateSign and CRLSign set. * * @param admin Information about the administrator or admin preforming the event. * @param username unique username within the instance. * @param password password for the user. * @param pk the public key to be put in the created certificate. * @param keyusage integer with bit mask describing desired keys usage, overrides keyUsage from * CertificateProfiles if allowed. Bit mask is packed in in integer using constants * from CertificateData. -1 means use default keyUsage from CertificateProfile. ex. int * keyusage = CertificateData.digitalSignature | CertificateData.nonRepudiation; gives * digitalSignature and nonRepudiation. ex. int keyusage = CertificateData.keyCertSign * | CertificateData.cRLSign; gives keyCertSign and cRLSign * @param certificateprofileid used to override the one set in userdata. * Should be set to SecConst.PROFILE_NO_PROFILE if the usedata certificateprofileid should be used * @param caid used to override the one set in userdata.� * Should be set to SecConst.CAID_USEUSERDEFINED if the regular certificateprofileid should be used * * * @return The newly created certificate or null. * @throws ObjectNotFoundException if the user does not exist. * @throws AuthStatusException If the users status is incorrect. * @throws AuthLoginException If the password is incorrect. * @throws IllegalKeyException if the public key is of wrong type. * * @ejb.permission unchecked="true" * @ejb.interface-method view-type="both" */ public Certificate createCertificate(Admin admin, String username, String password, PublicKey pk, int keyusage, int certificateprofileid, int caid) throws ObjectNotFoundException, AuthStatusException, AuthLoginException, IllegalKeyException, CADoesntExistsException { return createCertificate(admin, username, password, pk, keyusage, null, null, certificateprofileid, caid); } /** * Requests for a certificate to be created for the passed public key wrapped in a * certification request message (ex PKCS10). The username and password used to authorize is * taken from the request message. Verification of the signature (proof-of-possesion) on the * request is performed, and an exception thrown if verification fails. The method queries the * user database for authorization of the user. * * @param admin Information about the administrator or admin preforming the event. * @param req a Certification Request message, containing the public key to be put in the * created certificate. Currently no additional parameters in requests are considered! * @param keyUsage integer with bit mask describing desired keys usage. Bit mask is packed in * in integer using contants from CertificateDataBean. ex. int keyusage = * CertificateDataBean.digitalSignature | CertificateDataBean.nonRepudiation; gives * digitalSignature and nonRepudiation. ex. int keyusage = CertificateDataBean.keyCertSign * | CertificateDataBean.cRLSign; gives keyCertSign and cRLSign. Keyusage < 0 means that default * keyUsage should be used. * @param responseClass The implementation class that will be used as the response message. * @return The newly created response or null. * @throws ObjectNotFoundException if the user does not exist. * @throws AuthStatusException If the users status is incorrect. * @throws AuthLoginException If the password is incorrect. * @throws IllegalKeyException if the public key is of wrong type. * @throws CADoesntExistsException if the targeted CA does not exist * @throws SignRequestException if the provided request is invalid. * @throws SignRequestSignatureException if the provided client certificate was not signed by * the CA. * @ejb.permission unchecked="true" * @ejb.interface-method view-type="both" * @see org.ejbca.core.ejb.ca.store.CertificateDataBean * @see org.ejbca.core.protocol.IRequestMessage * @see org.ejbca.core.protocol.IResponseMessage * @see org.ejbca.core.protocol.X509ResponseMessage */ public IResponseMessage createCertificate(Admin admin, IRequestMessage req, int keyUsage, Class responseClass) throws AuthStatusException, AuthLoginException, IllegalKeyException, CADoesntExistsException, SignRequestException, SignRequestSignatureException, NotFoundException { debug(">createCertificate(IRequestMessage)"); // Get CA that will receive request CADataLocal cadata = null; UserDataVO data = null; IResponseMessage ret = null; try { cadata = getCAFromRequest(admin, req); CA ca = cadata.getCA(); CAToken catoken = ca.getCAToken(); // See if we need some key material to decrypt request if (req.requireKeyInfo()) { // You go figure...scep encrypts message with the public CA-cert req.setKeyInfo((X509Certificate)ca.getCACertificate(), catoken.getPrivateKey(SecConst.CAKEYPURPOSE_CERTSIGN), catoken.getProvider()); } // Verify the request if (req.verify() == false) { String msg = intres.getLocalizedMessage("signsession.popverificationfailed"); getLogSession().log(admin, cadata.getCaId().intValue(), LogEntry.MODULE_CA, new java.util.Date(), req.getUsername(), null, LogEntry.EVENT_ERROR_CREATECERTIFICATE, msg); throw new SignRequestSignatureException(msg); } if (req.getUsername() == null) { String msg = intres.getLocalizedMessage("signsession.nouserinrequest", req.getRequestDN()); getLogSession().log(admin, cadata.getCaId().intValue(), LogEntry.MODULE_CA, new java.util.Date(), req.getUsername(), null, LogEntry.EVENT_ERROR_CREATECERTIFICATE, msg); throw new SignRequestException(msg); //ret.setFailInfo(FailInfo.BAD_REQUEST); //ret.setStatus(ResponseStatus.FAILURE); } else if (req.getPassword() == null) { String msg = intres.getLocalizedMessage("signsession.nopasswordinrequest"); getLogSession().log(admin, cadata.getCaId().intValue(), LogEntry.MODULE_CA, new java.util.Date(), req.getUsername(), null, LogEntry.EVENT_ERROR_CREATECERTIFICATE, msg); throw new SignRequestException(msg); } else { ResponseStatus status = ResponseStatus.SUCCESS; FailInfo failInfo = null; String failText = null; Certificate cert = null; try { // If we haven't done so yet, authenticate user data = authUser(admin, req.getUsername(), req.getPassword()); PublicKey reqpk = req.getRequestPublicKey(); if (reqpk == null) { getLogSession().log(admin, cadata.getCaId().intValue(), LogEntry.MODULE_CA, new java.util.Date(), req.getUsername(), null, LogEntry.EVENT_ERROR_CREATECERTIFICATE, intres.getLocalizedMessage("signsession.nokeyinrequest")); throw new InvalidKeyException("Key is null!"); } // We need to make sure we use the users registered CA here if (data.getCAId() != ca.getCAId()) { failText = intres.getLocalizedMessage("signsession.wrongauthority", new Integer(ca.getCAId()), new Integer(data.getCAId())); status = ResponseStatus.FAILURE; failInfo = FailInfo.WRONG_AUTHORITY; getLogSession().log(admin, cadata.getCaId().intValue(), LogEntry.MODULE_CA, new java.util.Date(), req.getUsername(), null, LogEntry.EVENT_ERROR_CREATECERTIFICATE, failText); } if (status.equals(ResponseStatus.SUCCESS)) { Date notBefore = req.getRequestValidityNotBefore(); // Optionally requested validity Date notAfter = req.getRequestValidityNotAfter(); // Optionally requested validity cert = createCertificate(admin, data, ca, reqpk, keyUsage, notBefore, notAfter); } } catch (ObjectNotFoundException oe) { // If we didn't find the entity return error message log.error("User not found: ", oe); failText = intres.getLocalizedMessage("signsession.nosuchuser", req.getUsername()); status = ResponseStatus.FAILURE; failInfo = FailInfo.INCORRECT_DATA; getLogSession().log(admin, cadata.getCaId().intValue(), LogEntry.MODULE_CA, new java.util.Date(), req.getUsername(), null, LogEntry.EVENT_ERROR_CREATECERTIFICATE, failText); } //Create the response message with all nonces and checks etc ret = req.createResponseMessage(responseClass, req, ca.getCACertificate(), catoken.getPrivateKey(SecConst.CAKEYPURPOSE_CERTSIGN), catoken.getPrivateKey(SecConst.CAKEYPURPOSE_KEYENCRYPT), catoken.getProvider());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -