Source Code Cross Referenced for Signature.java in  » 6.0-JDK-Modules-com.sun » xws-security » com » sun » xml » ws » security » opt » impl » incoming » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules com.sun » xws security » com.sun.xml.ws.security.opt.impl.incoming 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         * 
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         * 
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common Development
008:         * and Distribution License("CDDL") (collectively, the "License").  You
009:         * may not use this file except in compliance with the License. You can obtain
010:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
012:         * language governing permissions and limitations under the License.
013:         * 
014:         * When distributing the software, include this License Header Notice in each
015:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016:         * Sun designates this particular file as subject to the "Classpath" exception
017:         * as provided by Sun in the GPL Version 2 section of the License file that
018:         * accompanied this code.  If applicable, add the following below the License
019:         * Header, with the fields enclosed by brackets [] replaced by your own
020:         * identifying information: "Portions Copyrighted [year]
021:         * [name of copyright owner]"
022:         * 
023:         * Contributor(s):
024:         * 
025:         * If you wish your version of this file to be governed by only the CDDL or
026:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
027:         * elects to include this software in this distribution under the [CDDL or GPL
028:         * Version 2] license."  If you don't indicate a single choice of license, a
029:         * recipient has the option to distribute your version of this file under
030:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
031:         * its licensees as provided above.  However, if you add GPL Version 2 code
032:         * and therefore, elected the GPL Version 2 license, then the option applies
033:         * only if the new code is made subject to such option by the copyright
034:         * holder.
035:         */
036:
037:        package com.sun.xml.ws.security.opt.impl.incoming;
038:
039:        import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
040:        import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
041:        import com.sun.xml.stream.buffer.stax.StreamReaderBufferCreator;
042:        import com.sun.xml.ws.encoding.TagInfoset;
043:        import com.sun.xml.ws.security.opt.api.NamespaceContextInfo;
044:        import com.sun.xml.ws.security.opt.api.PolicyBuilder;
045:        import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
046:        import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
047:        import com.sun.xml.ws.security.opt.crypto.dsig.SignatureValue;
048:        import com.sun.xml.ws.security.opt.crypto.dsig.SignedInfo;
049:        import com.sun.xml.ws.security.opt.crypto.jaxb.JAXBSignatureFactory;
050:        import com.sun.xml.ws.security.opt.crypto.jaxb.JAXBValidateContext;
051:        import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
052:        import com.sun.xml.ws.security.opt.impl.incoming.processor.KeyInfoProcessor;
053:        import com.sun.xml.ws.security.opt.impl.incoming.processor.SignedInfoProcessor;
054:        import com.sun.xml.ws.security.opt.impl.util.StreamUtil;
055:        import com.sun.xml.ws.security.opt.impl.util.XMLStreamReaderFactory;
056:        import com.sun.xml.wss.XWSSecurityException;
057:        import com.sun.xml.wss.impl.MessageConstants;
058:        import com.sun.xml.wss.impl.misc.Base64;
059:        import com.sun.xml.wss.impl.policy.MLSPolicy;
060:        import com.sun.xml.wss.impl.policy.mls.SignaturePolicy;
061:        import com.sun.xml.wss.impl.policy.mls.WSSPolicy;
062:        import java.io.ByteArrayOutputStream;
063:        import java.io.OutputStream;
064:        import java.security.Key;
065:        import java.util.ArrayList;
066:        import java.util.HashMap;
067:        import java.util.Iterator;
068:        import java.util.List;
069:        import java.util.Map;
070:        import java.util.logging.Level;
071:        import javax.xml.crypto.KeySelector.Purpose;
072:        import javax.xml.crypto.dsig.Reference;
073:        import javax.xml.crypto.dsig.XMLSignatureException;
074:
075:        import javax.xml.stream.XMLStreamException;
076:        import javax.xml.stream.XMLStreamReader;
077:        import javax.xml.stream.XMLStreamWriter;
078:        import org.jvnet.staxex.Base64Data;
079:        import org.jvnet.staxex.XMLStreamReaderEx;
080:        import com.sun.xml.wss.impl.c14n.StAXEXC14nCanonicalizerImpl;
081:
082:        import java.security.MessageDigest;
083:        import org.jcp.xml.dsig.internal.DigesterOutputStream;
084:        import com.sun.xml.ws.security.opt.impl.incoming.processor.StreamingPayLoadDigester;
085:        import java.security.NoSuchAlgorithmException;
086:        import javax.xml.crypto.dsig.Transform;
087:        import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
088:        import java.util.logging.Logger;
089:        import com.sun.xml.wss.logging.LogDomainConstants;
090:        import com.sun.xml.wss.logging.impl.opt.signature.LogStringsMessages;
091:
092:        /**
093:         * <element name="Signature" type="ds:SignatureType"/>
094:         * <complexType name="SignatureType">
095:         * <sequence>
096:         * <element ref="ds:SignedInfo"/>
097:         * <element ref="ds:SignatureValue"/>
098:         * <element ref="ds:KeyInfo" minOccurs="0"/>
099:         * <element ref="ds:Object" minOccurs="0" maxOccurs="unbounded"/>
100:         * </sequence>
101:         * <attribute name="Id" type="ID" use="optional"/>
102:         * </complexType>
103:         *
104:         */
105:
106:        /**
107:         *
108:         * @author K.Venugopal@sun.com
109:         */
110:        public class Signature implements  SecurityHeaderElement,
111:                NamespaceContextInfo, SecurityElementWriter, PolicyBuilder {
112:
113:            private static final Logger logger = Logger.getLogger(
114:                    LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN,
115:                    LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN_BUNDLE);
116:
117:            public static final int SIGNEDINFO_EVENT = 1;
118:            public static final int SIGNATUREVALUE_EVENT = 2;
119:            public static final int KEYINFO_EVENT = 3;
120:            public static final int OBJECT_EVENT = 4;
121:
122:            public static final String SIGNED_INFO = "SignedInfo";
123:            public static final String SIGNATURE_VALUE = "SignatureValue";
124:            public static final String KEYINFO = "KeyInfo";
125:            public static final String OBJECT = "Object";
126:
127:            private SignaturePolicy signPolicy = null;
128:
129:            private HashMap<String, String> currentParentNS = new HashMap<String, String>();
130:            private JAXBFilterProcessingContext context;
131:            private StreamReaderBufferCreator creator = null;
132:            private SecurityContext securityContext = null;
133:            private String id = "";
134:            private SignedInfoProcessor sip = null;
135:            private XMLStreamWriter canonWriter = null;
136:            private com.sun.xml.ws.security.opt.crypto.dsig.Signature sig = null;
137:            private JAXBValidateContext jvc = new JAXBValidateContext();
138:            private SignedInfo si = null;
139:            private boolean validationStatus = false;
140:            private MutableXMLStreamBuffer buffer = null;
141:            private boolean cacheSignature = false;
142:            private boolean storeSigConfirmValue = true;
143:
144:            //private JAXBSignatureFactory signatureFactory = null;
145:
146:            /** Creates a new instance of SignatureProcessor */
147:
148:            public Signature(JAXBFilterProcessingContext jpc,
149:                    Map<String, String> namespaceList,
150:                    StreamReaderBufferCreator sbc) {
151:                this .currentParentNS.putAll(namespaceList);
152:                this .creator = sbc;
153:                this .securityContext = jpc.getSecurityContext();
154:                this .context = jpc;
155:                cacheSignature = true;
156:                signPolicy = new SignaturePolicy();
157:                signPolicy
158:                        .setFeatureBinding(new SignaturePolicy.FeatureBinding());
159:                //signatureFactory = JAXBSignatureFactory.newInstance();
160:            }
161:
162:            public Signature(JAXBFilterProcessingContext jpc,
163:                    Map<String, String> namespaceList,
164:                    StreamReaderBufferCreator sbc, boolean cacheSig) {
165:                this .currentParentNS.putAll(namespaceList);
166:                this .creator = sbc;
167:                this .securityContext = jpc.getSecurityContext();
168:                this .context = jpc;
169:                cacheSignature = cacheSig;
170:                signPolicy = new SignaturePolicy();
171:                signPolicy
172:                        .setFeatureBinding(new SignaturePolicy.FeatureBinding());
173:            }
174:
175:            public void process(XMLStreamReader signature)
176:                    throws XWSSecurityException {
177:                try {
178:                    XMLStreamReader reader = null;
179:                    //by default buffer now , enable buffering using policy
180:                    buffer = new MutableXMLStreamBuffer();//resuse pick from pool
181:                    buffer.createFromXMLStreamReader(signature);
182:                    reader = buffer.readAsXMLStreamReader();
183:                    reader.next();
184:                    byte[] signatureValue = null;
185:                    TagInfoset signatureRoot = new TagInfoset(reader);
186:                    for (int i = 0; i < reader.getNamespaceCount(); i++) {
187:                        String prefix = reader.getNamespacePrefix(i);
188:                        String uri = reader.getNamespaceURI(i);
189:                        if (prefix == null) {
190:                            prefix = "";
191:                        }
192:                        currentParentNS.put(prefix, uri);
193:                    }
194:                    this .context.setCurrentBuffer(buffer);
195:                    Key key = null;
196:                    id = reader.getAttributeValue(null, "Id");
197:                    if (id == null || id.length() == 0) {
198:                        id = "pvid" + context.generateID();// need an Id for policy verification.
199:                        if (logger.isLoggable(Level.FINEST)) {
200:                            logger.log(Level.FINEST, LogStringsMessages
201:                                    .WSS_1755_MISSINGID_INCOMING_SIGNATURE(id));
202:                        }
203:                    }
204:                    // policy creation
205:                    signPolicy.setUUID(id);
206:
207:                    if (StreamUtil.moveToNextElement(reader)) {
208:                        int refElement = getEventType(reader);
209:                        while (reader.getEventType() != reader.END_DOCUMENT) {
210:                            switch (refElement) {
211:                            case SIGNEDINFO_EVENT: {
212:                                sip = new SignedInfoProcessor(signatureRoot,
213:                                        currentParentNS, reader, context,
214:                                        signPolicy, buffer);
215:                                si = (SignedInfo) sip.process();
216:                                canonWriter = sip.getCanonicalizer();
217:                                break;
218:                            }
219:                            case SIGNATUREVALUE_EVENT: {
220:                                if (canonWriter == null) {
221:                                    logger
222:                                            .log(
223:                                                    Level.SEVERE,
224:                                                    LogStringsMessages
225:                                                            .WSS_1707_ERROR_PROCESSING_SIGNEDINFO(id));
226:                                    throw new XWSSecurityException(
227:                                            "Elements under Signature are not as per defined schema"
228:                                                    + " or error must have occurred while processing SignedInfo for Signature with ID"
229:                                                    + id);
230:                                }
231:                                StreamUtil.writeCurrentEvent(reader,
232:                                        canonWriter);
233:
234:                                if (reader instanceof  XMLStreamReaderEx) {
235:                                    reader.next();
236:                                    StringBuffer sb = null;
237:                                    while (reader.getEventType() == reader.CHARACTERS
238:                                            && reader.getEventType() != reader.END_ELEMENT) {
239:                                        CharSequence charSeq = ((XMLStreamReaderEx) reader)
240:                                                .getPCDATA();
241:                                        if (charSeq instanceof  Base64Data) {
242:                                            Base64Data bd = (Base64Data) ((XMLStreamReaderEx) reader)
243:                                                    .getPCDATA();
244:                                            signatureValue = bd.getExact();
245:                                            canonWriter.writeCharacters(Base64
246:                                                    .encode(signatureValue));
247:                                        } else {
248:                                            if (sb == null) {
249:                                                sb = new StringBuffer();
250:                                            }
251:                                            for (int i = 0; i < charSeq
252:                                                    .length(); i++) {
253:                                                sb.append(charSeq.charAt(i));
254:                                            }
255:                                        }
256:                                        reader.next();
257:                                    }
258:                                    if (sb != null) {
259:                                        String tmp = sb.toString();
260:                                        canonWriter.writeCharacters(tmp);
261:                                        try {
262:                                            signatureValue = Base64.decode(tmp);
263:                                        } catch (Base64DecodingException dec) {
264:                                            logger
265:                                                    .log(
266:                                                            Level.SEVERE,
267:                                                            LogStringsMessages
268:                                                                    .WSS_1708_BASE_64_DECODING_ERROR(id));
269:                                            throw new XWSSecurityException(
270:                                                    "Error occurred while decoding signatureValue for Signature with ID"
271:                                                            + id);
272:                                        }
273:                                    } else {
274:                                        reader.next();
275:                                    }
276:                                } else {
277:                                    String tmp = StreamUtil.getCV(reader);
278:                                    //reader.getElementText();
279:                                    canonWriter.writeCharacters(tmp);
280:                                    try {
281:                                        signatureValue = Base64.decode(tmp);
282:                                    } catch (Base64DecodingException dec) {
283:                                        logger
284:                                                .log(
285:                                                        Level.SEVERE,
286:                                                        LogStringsMessages
287:                                                                .WSS_1708_BASE_64_DECODING_ERROR(id));
288:                                        throw new XWSSecurityException(
289:                                                "Error occurred while decoding signatureValue for Signature with ID"
290:                                                        + id);
291:                                    }
292:                                }
293:                                //For SignatureConfirmation
294:                                List scList = (ArrayList) context
295:                                        .getExtraneousProperty("receivedSignValues");
296:                                if (scList != null && storeSigConfirmValue) {
297:                                    scList.add(signatureValue);
298:                                }
299:                                //End SignatureConfirmation specific code
300:                                break;
301:                            }
302:                            case KEYINFO_EVENT: {
303:                                if (canonWriter == null) {
304:                                    logger
305:                                            .log(
306:                                                    Level.SEVERE,
307:                                                    LogStringsMessages
308:                                                            .WSS_1707_ERROR_PROCESSING_SIGNEDINFO(id));
309:                                    throw new XWSSecurityException(
310:                                            "Elements under Signature are not as per defined schema"
311:                                                    + " or error must have occurred while processing SignedInfo for Signature with ID"
312:                                                    + id);
313:                                }
314:                                // StreamUtil.writeCurrentEvent(reader,canonWriter);
315:                                securityContext.setInferredKB(null);
316:                                KeyInfoProcessor kip = new KeyInfoProcessor(
317:                                        context, Purpose.VERIFY);
318:                                key = kip.getKey(reader);
319:                                MLSPolicy inferredKB = securityContext
320:                                        .getInferredKB();
321:                                signPolicy.setKeyBinding(inferredKB);
322:                                securityContext.setInferredKB(null);
323:                                break;
324:                            }
325:                            default: {
326:                                logger
327:                                        .log(
328:                                                Level.SEVERE,
329:                                                LogStringsMessages
330:                                                        .WSS_1709_UNRECOGNIZED_SIGNATURE_ELEMENT(reader
331:                                                                .getName()));
332:                                throw new XWSSecurityException("Element name "
333:                                        + reader.getName()
334:                                        + " is not recognized under Signature");
335:                            }
336:                            }
337:
338:                            if (StreamUtil._break(reader, "Signature",
339:                                    MessageConstants.DSIG_NS)) {
340:                                reader.next();
341:                                break;
342:                            }
343:
344:                            if (!StreamUtil.isStartElement(reader)
345:                                    && StreamUtil
346:                                            .moveToNextStartOREndElement(reader)
347:                                    && StreamUtil._break(reader, "Signature",
348:                                            MessageConstants.DSIG_NS)) {
349:                                reader.next();
350:                                break;
351:                            } else {
352:                                if (reader.getEventType() != XMLStreamReader.START_ELEMENT) {
353:                                    StreamUtil
354:                                            .moveToNextStartOREndElement(reader);
355:                                    boolean isBreak = false;
356:                                    while (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
357:                                        if (StreamUtil._break(reader,
358:                                                "Signature",
359:                                                MessageConstants.XENC_NS)) {
360:                                            isBreak = true;
361:                                            break;
362:                                        }
363:                                        StreamUtil
364:                                                .moveToNextStartOREndElement(reader);
365:                                    }
366:                                    if (isBreak)
367:                                        break;
368:                                    if (reader.getEventType() == XMLStreamReader.END_DOCUMENT) {
369:                                        break;
370:                                    }
371:                                }
372:                            }
373:                            refElement = getEventType(reader);
374:                        }
375:                    }
376:
377:                    sig = new com.sun.xml.ws.security.opt.crypto.dsig.Signature();
378:                    SignatureValue sv = new SignatureValue();
379:                    sv.setValue(signatureValue);
380:                    sig.setSignatureValue(sv);
381:
382:                    jvc.setURIDereferencer(new URIResolver(context));
383:                    sig.setSignedInfo(si);
384:                    sig.setVerificationKey(key);
385:                    if (sip.getReferenceList().size() == 0) {
386:                        if (!sig.validate(jvc)) {
387:                            validationStatus = true;
388:                            logger.log(Level.SEVERE, LogStringsMessages
389:                                    .WSS_1710_SIGNATURE_VERFICATION_FAILED(id));
390:                            throw new XWSSecurityException(
391:                                    "Signature Verification for Signature with ID "
392:                                            + id + " failed");
393:                        } else {
394:                            validationStatus = true;
395:                        }
396:                    }
397:                } catch (XMLStreamException ex) {
398:                    logger.log(Level.SEVERE, LogStringsMessages
399:                            .WSS_1711_ERROR_VERIFYING_SIGNATURE());
400:                    throw new XWSSecurityException(ex);
401:                } catch (XMLSignatureException xse) {
402:                    logger.log(Level.SEVERE, LogStringsMessages
403:                            .WSS_1711_ERROR_VERIFYING_SIGNATURE());
404:                    throw new XWSSecurityException(xse);
405:                }
406:            }
407:
408:            public void process(XMLStreamReader signature,
409:                    boolean storeSigConfirmValue) throws XWSSecurityException {
410:                this .storeSigConfirmValue = storeSigConfirmValue;
411:                process(signature);
412:                this .storeSigConfirmValue = true;
413:            }
414:
415:            public boolean validate() throws XWSSecurityException {
416:                if (isReady()) {
417:                    try {
418:                        boolean status = sig.validate(jvc);
419:                        validationStatus = true;
420:                        return status;
421:                    } catch (XMLSignatureException ex) {
422:                        validationStatus = true;
423:                        logger.log(Level.SEVERE, LogStringsMessages
424:                                .WSS_1713_SIGNATURE_VERIFICATION_EXCEPTION(ex
425:                                        .getMessage()), ex);
426:                        throw new XWSSecurityException(ex);
427:                    }
428:                }
429:                return false;
430:            }
431:
432:            public com.sun.xml.ws.security.opt.crypto.dsig.Reference removeReferenceWithID(
433:                    String id) {
434:                ArrayList<com.sun.xml.ws.security.opt.crypto.dsig.Reference> refList = sip
435:                        .getReferenceList();
436:                com.sun.xml.ws.security.opt.crypto.dsig.Reference ref = null;
437:                for (int i = 0; i < refList.size(); i++) {
438:                    if (refList.get(i).getURI().equals(id)) {
439:                        ref = sip.getReferenceList().remove(i);
440:                        break;
441:                    }
442:                }
443:                return ref;
444:            }
445:
446:            public ArrayList<com.sun.xml.ws.security.opt.crypto.dsig.Reference> getReferences() {
447:                return sip.getReferenceList();
448:            }
449:
450:            public boolean isValidated() {
451:                return validationStatus;
452:            }
453:
454:            public boolean isReady() throws XWSSecurityException {
455:                if (sip.getReferenceList().size() == 0) {
456:                    return true;
457:                } else {
458:                    ArrayList<com.sun.xml.ws.security.opt.crypto.dsig.Reference> refList = (ArrayList) sip
459:                            .getReferenceList().clone();
460:                    for (int i = 0; i < refList.size(); i++) {
461:                        com.sun.xml.ws.security.opt.crypto.dsig.Reference ref = (com.sun.xml.ws.security.opt.crypto.dsig.Reference) refList
462:                                .get(i);
463:                        if (sip.processReference(ref)) {
464:                            sip.getReferenceList().remove(ref);
465:                        }
466:                    }
467:                }
468:                if (sip.getReferenceList().size() == 0) {
469:                    return true;
470:                }
471:                return false;
472:            }
473:
474:            public boolean verifyReferences() {
475:                throw new UnsupportedOperationException();
476:            }
477:
478:            public boolean verifySignatureValue() {
479:                throw new UnsupportedOperationException();
480:            }
481:
482:            private int getEventType(XMLStreamReader reader) {
483:                if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
484:                    if (reader.getLocalName() == SIGNED_INFO) {
485:                        return SIGNEDINFO_EVENT;
486:                    }
487:                    if (reader.getLocalName() == SIGNATURE_VALUE) {
488:                        return SIGNATUREVALUE_EVENT;
489:                    }
490:                    if (reader.getLocalName() == KEYINFO) {
491:                        return KEYINFO_EVENT;
492:                    }
493:                    if (reader.getLocalName() == OBJECT) {
494:                        return OBJECT_EVENT;
495:                    }
496:                }
497:                return -1;
498:            }
499:
500:            private boolean _break(XMLStreamReader reader)
501:                    throws XMLStreamException {
502:                if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
503:                    if (reader.getLocalName() == "Signature"
504:                            && reader.getNamespaceURI() == MessageConstants.DSIG_NS) {
505:                        reader.next();
506:                        return true;
507:                    }
508:                }
509:                return false;
510:            }
511:
512:            public boolean refersToSecHdrWithId(String id) {
513:                throw new UnsupportedOperationException();
514:            }
515:
516:            public String getId() {
517:                return id;
518:            }
519:
520:            public void setId(String id) {
521:                throw new UnsupportedOperationException();
522:            }
523:
524:            public String getNamespaceURI() {
525:                return MessageConstants.DSIG_NS;
526:            }
527:
528:            public String getLocalPart() {
529:                return MessageConstants.SIGNATURE_LNAME;
530:            }
531:
532:            public XMLStreamReader readHeader() throws XMLStreamException {
533:                throw new UnsupportedOperationException();
534:            }
535:
536:            public void writeTo(OutputStream os) {
537:                throw new UnsupportedOperationException();
538:            }
539:
540:            public void writeTo(XMLStreamWriter streamWriter)
541:                    throws XMLStreamException {
542:                if (buffer != null) {
543:                    buffer.writeToXMLStreamWriter(streamWriter);
544:                } else {
545:                    logger.log(Level.SEVERE, LogStringsMessages
546:                            .WSS_1712_UNBUFFERED_SIGNATURE_ERROR());
547:                    throw new XMLStreamException(
548:                            "Signature is not buffered , message not as per configured policy");
549:                }
550:            }
551:
552:            public XMLStreamReader wrapWithDigester(Reference ref,
553:                    XMLStreamReader message, TagInfoset bodyTag,
554:                    HashMap<String, String> parentNS, boolean payLoad)
555:                    throws XWSSecurityException {
556:
557:                MessageDigest md = null;
558:                try {
559:                    String algo = StreamUtil.convertDigestAlgorithm(ref
560:                            .getDigestMethod().getAlgorithm());
561:
562:                    if (logger.isLoggable(Level.FINE)) {
563:                        logger.log(Level.FINE, "Digest Algorithm is "
564:                                + ref.getDigestMethod().getAlgorithm());
565:                        logger.log(Level.FINE, "Mapped Digest Algorithm is "
566:                                + algo);
567:                    }
568:                    md = MessageDigest.getInstance(algo);
569:
570:                } catch (NoSuchAlgorithmException nsae) {
571:                    throw new XWSSecurityException(nsae);
572:                }
573:
574:                DigesterOutputStream dos;
575:                dos = new DigesterOutputStream(md);
576:                // OutputStream os = new UnsyncBufferedOutputStream(dos);
577:                StAXEXC14nCanonicalizerImpl canonicalizer = new StAXEXC14nCanonicalizerImpl();
578:                //TODO:share canonicalizers .
579:                canonicalizer.setStream(dos);
580:                if (logger.isLoggable(Level.FINEST)) {
581:                    canonicalizer.setStream(new ByteArrayOutputStream());
582:                }
583:                List trList = ref.getTransforms();
584:                if (trList.size() > 1) {
585:                    logger.log(Level.SEVERE, LogStringsMessages
586:                            .WSS_1714_UNSUPPORTED_TRANSFORM_ERROR());
587:                    throw new XWSSecurityException(
588:                            "Only EXC14n Transform is supported");
589:                }
590:                Transform tr = (Transform) trList.get(0);
591:
592:                ExcC14NParameterSpec spec = (ExcC14NParameterSpec) tr
593:                        .getParameterSpec();
594:                if (spec != null) {
595:                    canonicalizer.setInclusivePrefixList(spec.getPrefixList());
596:                }
597:                if (parentNS != null && parentNS.size() > 0) {
598:                    Iterator<Map.Entry<String, String>> itr = parentNS
599:                            .entrySet().iterator();
600:
601:                    while (itr.hasNext()) {
602:                        Map.Entry<String, String> entry = itr.next();
603:                        String prefix = entry.getKey();
604:                        try {
605:                            String uri = entry.getValue();
606:                            canonicalizer.writeNamespace(prefix, uri);
607:                        } catch (XMLStreamException ex) {
608:                            logger.log(Level.SEVERE, LogStringsMessages
609:                                    .WSS_1715_ERROR_CANONICALIZING_BODY(), ex);
610:                        }
611:                    }
612:                }
613:                try {
614:                    if (!payLoad) {
615:                        bodyTag.writeStart(canonicalizer);
616:                    }
617:                } catch (XMLStreamException ex) {
618:                    logger.log(Level.SEVERE, LogStringsMessages
619:                            .WSS_1715_ERROR_CANONICALIZING_BODY(), ex);
620:                    throw new XWSSecurityException(
621:                            "Error occurred while canonicalizing BodyTag" + ex);
622:                }
623:                StreamingPayLoadDigester digester = new StreamingPayLoadDigester(
624:                        ref, message, canonicalizer, payLoad);
625:                return XMLStreamReaderFactory.createFilteredXMLStreamReader(
626:                        message, digester);
627:            }
628:
629:            public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
630:                    HashMap props) throws javax.xml.stream.XMLStreamException {
631:                throw new UnsupportedOperationException();
632:            }
633:
634:            public HashMap<String, String> getInscopeNSContext() {
635:                return currentParentNS;
636:            }
637:
638:            public WSSPolicy getPolicy() {
639:                return signPolicy;
640:            }
641:        }
w___w__w__._j_a___v___a2s.c__o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.