001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.core.protocol.xkms.generators;
013:
014: import java.security.PublicKey;
015: import java.security.cert.X509Certificate;
016:
017: import org.ejbca.core.model.ra.UserDataVO;
018: import org.ejbca.core.protocol.xkms.common.XKMSConstants;
019: import org.w3._2002._03.xkms_.KeyBindingAbstractType;
020: import org.w3._2002._03.xkms_.KeyBindingType;
021: import org.w3._2002._03.xkms_.RegisterRequestType;
022: import org.w3._2002._03.xkms_.RegisterResultType;
023: import org.w3c.dom.Document;
024:
025: /**
026: * Class generating a response for a register call
027: *
028: *
029: * @author Philip Vendil
030: *
031: * @version $Id: RegisterResponseGenerator.java,v 1.1 2007/01/05 05:32:51 herrvendil Exp $
032: */
033:
034: public class RegisterResponseGenerator extends KRSSResponseGenerator {
035: //private static Logger log = Logger.getLogger(RegisterResponseGenerator.class);
036:
037: public RegisterResponseGenerator(String remoteIP,
038: RegisterRequestType req, Document requestDoc) {
039: super (remoteIP, req, requestDoc);
040: }
041:
042: /**
043: * Returns a register response
044: */
045: public RegisterResultType getResponse(boolean requestVerifies) {
046: RegisterResultType result = xkmsFactory
047: .createRegisterResultType();
048: super .populateResponse(result, requestVerifies);
049: RegisterRequestType req = (RegisterRequestType) this .req;
050:
051: if (resultMajor == null) {
052: if (!checkValidRespondWithRequest(req.getRespondWith(),
053: false)) {
054: resultMajor = XKMSConstants.RESULTMAJOR_SENDER;
055: resultMinor = XKMSConstants.RESULTMINOR_MESSAGENOTSUPPORTED;
056: }
057:
058: if (resultMajor == null) {
059:
060: if (resultMajor == null) {
061: PublicKey publicKey = getPublicKey(req);
062: if (confirmPOP(publicKey)) {
063: String subjectDN = getSubjectDN(req);
064: UserDataVO userData = findUserData(subjectDN);
065: if (userData != null) {
066: String password = "";
067: boolean encryptedPassword = isPasswordEncrypted(req);
068: if (encryptedPassword) {
069: password = getEncryptedPassword(
070: requestDoc, userData
071: .getPassword());
072: } else {
073: password = getClearPassword(req,
074: userData.getPassword());
075: }
076: String revocationCode = getRevocationCode(req);
077: if (password != null) {
078: X509Certificate cert = registerReissueOrRecover(
079: false, false, result, userData,
080: password, publicKey,
081: revocationCode);
082: if (cert != null) {
083: KeyBindingAbstractType keyBinding = getResponseValues(
084: req
085: .getPrototypeKeyBinding(),
086: cert, false, true);
087: result
088: .getKeyBinding()
089: .add(
090: (KeyBindingType) keyBinding);
091: }
092: }
093: }
094: }
095: }
096: }
097: }
098:
099: if (resultMajor == null) {
100: resultMajor = XKMSConstants.RESULTMAJOR_SUCCESS;
101: }
102:
103: setResult(result);
104:
105: return result;
106: }
107:
108: /**
109: * Method extracting the public key from the message.
110: * @param req the request
111: * @return the public key or null if no public key could be found.
112: */
113: protected PublicKey getPublicKey(RegisterRequestType req) {
114: Object retval = getPublicKeyInfo(req, true);
115: if (retval instanceof X509Certificate) {
116: retval = ((X509Certificate) retval).getPublicKey();
117: }
118:
119: return (PublicKey) retval;
120: }
121:
122: }
|