001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: /*
024: * JAXBSignatureFactory.java
025: *
026: * Created on February 2, 2006, 12:48 PM
027: *
028: * To change this template, choose Tools | Template Manager
029: * and open the template in the editor.
030: */
031:
032: package com.sun.xml.ws.security.opt.crypto.jaxb;
033:
034: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
035: import com.sun.xml.ws.security.opt.crypto.dsig.Signature;
036: import com.sun.xml.ws.security.opt.crypto.dsig.Transforms;
037: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.DSAKeyValue;
038: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyName;
039: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyValue;
040: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.PGPData;
041: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.RSAKeyValue;
042: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.RetrievalMethod;
043: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.SPKIData;
044: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.X509Data;
045: import com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.X509IssuerSerial;
046: import java.math.BigInteger;
047: import java.security.InvalidAlgorithmParameterException;
048: import java.security.NoSuchAlgorithmException;
049: import java.util.ArrayList;
050: import java.util.List;
051: import javax.xml.bind.JAXBContext;
052: import javax.xml.bind.JAXBException;
053: import javax.xml.crypto.Data;
054: import javax.xml.crypto.MarshalException;
055: import javax.xml.crypto.URIDereferencer;
056: import javax.xml.crypto.XMLStructure;
057: import javax.xml.crypto.dsig.CanonicalizationMethod;
058: import javax.xml.crypto.dsig.DigestMethod;
059: import javax.xml.crypto.dsig.Manifest;
060: import javax.xml.crypto.dsig.Reference;
061: import javax.xml.crypto.dsig.SignatureMethod;
062: import javax.xml.crypto.dsig.SignatureProperties;
063: import javax.xml.crypto.dsig.SignatureProperty;
064: import javax.xml.crypto.dsig.SignedInfo;
065: import javax.xml.crypto.dsig.Transform;
066: import javax.xml.crypto.dsig.XMLObject;
067: import javax.xml.crypto.dsig.XMLSignature;
068: import javax.xml.crypto.dsig.XMLValidateContext;
069: import javax.xml.crypto.dsig.keyinfo.KeyInfo;
070: import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec;
071: import javax.xml.crypto.dsig.spec.DigestMethodParameterSpec;
072: import javax.xml.crypto.dsig.spec.SignatureMethodParameterSpec;
073: import javax.xml.crypto.dsig.spec.TransformParameterSpec;
074:
075: /**
076: *
077: * @author Abhijit Das
078: */
079: public class JAXBSignatureFactory extends
080: javax.xml.crypto.dsig.XMLSignatureFactory {
081:
082: private static JAXBSignatureFactory instance = null;
083:
084: /** Creates a new instance of JAXBSignatureFactory */
085: private JAXBSignatureFactory() {
086: }
087:
088: public static JAXBSignatureFactory newInstance() {
089: if (instance == null)
090: instance = new JAXBSignatureFactory();
091:
092: return instance;
093: }
094:
095: public JAXBContext getJAXBContext() throws JAXBException {
096: return JAXBUtil.getJAXBContext();
097: }
098:
099: /**
100: * Creates an XMLSignature and initializes it with the contents of
101: * the specified SignedInfo and KeyInfo objects.
102: *
103: * @param signedInfo - signed info
104: * @param keyInfo - key info (may be null)
105: *
106: * @return XMLSignature
107: */
108: public XMLSignature newXMLSignature(SignedInfo signedInfo,
109: KeyInfo keyInfo) {
110: if (signedInfo == null) {
111: throw new NullPointerException("SignedInfo can not be null");
112: }
113: Signature signature = new Signature();
114: signature
115: .setKeyInfo((com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo) keyInfo);
116: signature
117: .setSignedInfo((com.sun.xml.ws.security.opt.crypto.dsig.SignedInfo) signedInfo);
118: return signature;
119: }
120:
121: /**
122: * Creates an XMLSignature and initializes it with the specified parameters.
123: *
124: * @param signedInfo - the Signed Info
125: * @param keyInfo - ths key info (may be null)
126: * @param objects - a list of XMLObjects (may be null)
127: * @param id - the id (may be null)
128: * @param type - the type (may be null)
129: *
130: * @return XMLSignature
131: *
132: */
133: public XMLSignature newXMLSignature(SignedInfo signedInfo,
134: KeyInfo keyInfo, List objects, String id, String type) {
135: Signature signature = (Signature) newXMLSignature(signedInfo,
136: keyInfo);
137: signature.setId(id);
138: signature.setType(type);
139: signature.setObjects(objects);
140: return signature;
141: }
142:
143: /**
144: *
145: * Creates a Reference with the specified URI and digest method.
146: *
147: * @param uri
148: * @param digestMethod
149: *
150: * @return Reference
151: *
152: */
153: public Reference newReference(String uri, DigestMethod digestMethod) {
154: if (digestMethod == null) {
155: throw new NullPointerException(
156: "Digest method can not be null");
157: }
158: com.sun.xml.ws.security.opt.crypto.dsig.Reference ref = new com.sun.xml.ws.security.opt.crypto.dsig.Reference();
159: ref.setURI(uri);
160: ref
161: .setDigestMethod((com.sun.xml.ws.security.opt.crypto.dsig.DigestMethod) digestMethod);
162: return ref;
163: }
164:
165: /**
166: * Creates a Reference with the specified parameters
167: *
168: * @param uri
169: * @param digestMethod
170: * @param transforms
171: * @param type
172: * @param id
173: * @return Reference
174: */
175: public Reference newReference(String uri,
176: DigestMethod digestMethod, List transforms, String type,
177: String id) {
178: com.sun.xml.ws.security.opt.crypto.dsig.Reference ref = (com.sun.xml.ws.security.opt.crypto.dsig.Reference) newReference(
179: uri, digestMethod);
180: ref.setType(type);
181: ref.setId(id);
182:
183: Transforms transfrormList = new Transforms();
184: transfrormList.setTransform(transforms);
185:
186: ref.setTransforms(transfrormList);
187: return ref;
188: }
189:
190: /**
191: *
192: * Creates a Reference with the specified parameters
193: *
194: * @param uri
195: * @param digestMethod
196: * @param transforms
197: * @param type
198: * @param id
199: * @param digestValue
200: * @return Reference
201: */
202: public Reference newReference(String uri,
203: DigestMethod digestMethod, List transforms, String type,
204: String id, byte[] digestValue) {
205: if (digestMethod == null) {
206: throw new NullPointerException(
207: "DigestMethod can not be null");
208: } else if (digestValue == null) {
209: throw new NullPointerException(
210: "Digest value can not be null");
211: }
212:
213: com.sun.xml.ws.security.opt.crypto.dsig.Reference ref = (com.sun.xml.ws.security.opt.crypto.dsig.Reference) newReference(
214: uri, digestMethod, transforms, type, id);
215: ref.setDigestValue(digestValue);
216: return ref;
217: }
218:
219: /**
220: *
221: * Creates a Reference with the specified parameters
222: *
223: * @param string
224: * @param digestMethod
225: * @param list
226: * @param data
227: * @param list0
228: * @param string0
229: * @param string1
230: * @return Reference
231: */
232: public Reference newReference(String string,
233: DigestMethod digestMethod, List list, Data data,
234: List list0, String string0, String string1) {
235: throw new UnsupportedOperationException("Not yet suported");
236: }
237:
238: /**
239: *
240: * Creates a SignedInfo with the specified parameters
241: *
242: * @param canonicalizationMethod
243: * @param signatureMethod
244: * @param references
245: * @return SignedInfo
246: */
247: public SignedInfo newSignedInfo(
248: CanonicalizationMethod canonicalizationMethod,
249: SignatureMethod signatureMethod, List references) {
250:
251: if (canonicalizationMethod == null) {
252: throw new NullPointerException(
253: "Canonicalization Method can not be null");
254: } else if (signatureMethod == null) {
255: throw new NullPointerException(
256: "Signature Method can not be null");
257: } else if (references == null || references.size() == 0) {
258: throw new NullPointerException("References can not be null");
259: }
260:
261: com.sun.xml.ws.security.opt.crypto.dsig.SignedInfo signedInfo = new com.sun.xml.ws.security.opt.crypto.dsig.SignedInfo();
262: signedInfo
263: .setCanonicalizationMethod((com.sun.xml.ws.security.opt.crypto.dsig.CanonicalizationMethod) canonicalizationMethod);
264: signedInfo
265: .setSignatureMethod((com.sun.xml.ws.security.opt.crypto.dsig.SignatureMethod) signatureMethod);
266: signedInfo.setReference(references);
267:
268: return signedInfo;
269: }
270:
271: /**
272: *
273: * Creates a SignedInfo with the specified parameters
274: *
275: * @param canonicalizationMethod
276: * @param signatureMethod
277: * @param references
278: * @param id
279: * @return SignedInfo
280: */
281: public SignedInfo newSignedInfo(
282: CanonicalizationMethod canonicalizationMethod,
283: SignatureMethod signatureMethod, List references, String id) {
284: com.sun.xml.ws.security.opt.crypto.dsig.SignedInfo signedInfo = (com.sun.xml.ws.security.opt.crypto.dsig.SignedInfo) newSignedInfo(
285: canonicalizationMethod, signatureMethod, references);
286: signedInfo.setId(id);
287: return signedInfo;
288: }
289:
290: /**
291: *
292: * Creates a XMLObject with the specified parameters
293: *
294: * @param content
295: * @param id
296: * @param mime
297: * @param encoding
298: * @return XMLObject
299: */
300: public XMLObject newXMLObject(List content, String id, String mime,
301: String encoding) {
302: com.sun.xml.ws.security.opt.crypto.dsig.XMLObject xmlObject = new com.sun.xml.ws.security.opt.crypto.dsig.XMLObject();
303: xmlObject.setEncoding(encoding);
304: xmlObject.setMimeType(mime);
305: xmlObject.setId(id);
306: xmlObject.setContent(content);
307: return xmlObject;
308: }
309:
310: /**
311: *
312: * Creates a Manifest with the specified parameters
313: *
314: * @param list
315: * @return Manifest
316: */
317: public Manifest newManifest(List list) {
318: return null;
319: }
320:
321: /**
322: * Creates a Manifest with the specified parameters
323: * @param list
324: * @param string
325: * @return Manifest
326: */
327: public Manifest newManifest(List list, String string) {
328: return null;
329: }
330:
331: /**
332: * Creates a SignatureProperty with the specified parameters
333: * @param list
334: * @param string
335: * @param string0
336: * @return SignatureProperty
337: */
338: public SignatureProperty newSignatureProperty(List list,
339: String string, String string0) {
340: return null;
341: }
342:
343: /**
344: * Creates a SignatureProperties with the specified parameters
345: * @param list
346: * @param string
347: * @return SignatureProperties
348: */
349: public SignatureProperties newSignatureProperties(List list,
350: String string) {
351: return null;
352: }
353:
354: /**
355: * Creates a DigestMethod with the specified parameters
356: * @param algorithm
357: * @param digestMethodParameterSpec
358: * @throws java.security.NoSuchAlgorithmException
359: * @throws java.security.InvalidAlgorithmParameterException
360: * @return DigestMethod
361: */
362: public DigestMethod newDigestMethod(String algorithm,
363: DigestMethodParameterSpec digestMethodParameterSpec)
364: throws NoSuchAlgorithmException,
365: InvalidAlgorithmParameterException {
366: if (algorithm == null) {
367: throw new NullPointerException(
368: "Digest algorithm can not be null");
369: }
370: com.sun.xml.ws.security.opt.crypto.dsig.DigestMethod digestMethod = new com.sun.xml.ws.security.opt.crypto.dsig.DigestMethod();
371: digestMethod.setParameterSpec(digestMethodParameterSpec);
372: digestMethod.setAlgorithm(algorithm);
373: return digestMethod;
374: }
375:
376: /**
377: * Creates a SignatureMethod with the specified parameters
378: * @param algorithm
379: * @param signatureMethodParameterSpec
380: * @throws java.security.NoSuchAlgorithmException
381: * @throws java.security.InvalidAlgorithmParameterException
382: * @return SignatureMethod
383: */
384: public SignatureMethod newSignatureMethod(String algorithm,
385: SignatureMethodParameterSpec signatureMethodParameterSpec)
386: throws NoSuchAlgorithmException,
387: InvalidAlgorithmParameterException {
388: if (algorithm == null) {
389: throw new NullPointerException(
390: "Signature Method algorithm can not be null");
391: }
392:
393: com.sun.xml.ws.security.opt.crypto.dsig.SignatureMethod signatureMethod = new com.sun.xml.ws.security.opt.crypto.dsig.SignatureMethod();
394: signatureMethod.setAlgorithm(algorithm);
395: if (signatureMethodParameterSpec != null)
396: signatureMethod.setParameter(signatureMethodParameterSpec);
397: return signatureMethod;
398: }
399:
400: /**
401: * Creates a Transform with the specified parameters
402: * @param algorithm
403: * @param transformParameterSpec
404: * @throws java.security.NoSuchAlgorithmException
405: * @throws java.security.InvalidAlgorithmParameterException
406: * @return Transforms
407: */
408: public Transform newTransform(String algorithm,
409: TransformParameterSpec transformParameterSpec)
410: throws NoSuchAlgorithmException,
411: InvalidAlgorithmParameterException {
412: com.sun.xml.ws.security.opt.crypto.dsig.Transform transform = new com.sun.xml.ws.security.opt.crypto.dsig.Transform();
413: transform.setAlgorithm(algorithm);
414: transform.setParameterSpec(transformParameterSpec);
415: return transform;
416: }
417:
418: /**
419: * Creates a Transform with the specified parameters
420: * @param algorithm
421: * @param xMLStructure
422: * @throws java.security.NoSuchAlgorithmException
423: * @throws java.security.InvalidAlgorithmParameterException
424: * @return Transform
425: */
426: public Transform newTransform(String algorithm,
427: XMLStructure xMLStructure) throws NoSuchAlgorithmException,
428: InvalidAlgorithmParameterException {
429: if (algorithm == null) {
430: throw new NullPointerException("Algorithm can not be null");
431: }
432: com.sun.xml.ws.security.opt.crypto.dsig.Transform transform = new com.sun.xml.ws.security.opt.crypto.dsig.Transform();
433: transform.setAlgorithm(algorithm);
434:
435: List content = new ArrayList();
436: content.add(((JAXBStructure) xMLStructure).getJAXBElement());
437:
438: transform.setContent(content);
439: return transform;
440: }
441:
442: /**
443: * Creates a CanonicalizationMethod with the specified parameters
444: * @param algorithm
445: * @param c14NMethodParameterSpec
446: * @throws java.security.NoSuchAlgorithmException
447: * @throws java.security.InvalidAlgorithmParameterException
448: * @return CanonicalizationMethod
449: */
450: public CanonicalizationMethod newCanonicalizationMethod(
451: String algorithm,
452: C14NMethodParameterSpec c14NMethodParameterSpec)
453: throws NoSuchAlgorithmException,
454: InvalidAlgorithmParameterException {
455: com.sun.xml.ws.security.opt.crypto.dsig.CanonicalizationMethod canonicalizationMethod = new com.sun.xml.ws.security.opt.crypto.dsig.CanonicalizationMethod();
456: canonicalizationMethod.setAlgorithm(algorithm);
457: canonicalizationMethod
458: .setParameterSpec(c14NMethodParameterSpec);
459: return canonicalizationMethod;
460: }
461:
462: /**
463: * Creates a CanonicalizationMethod with the specified parameters
464: * @param algorithm
465: * @param xMLStructure
466: * @throws java.security.NoSuchAlgorithmException
467: * @throws java.security.InvalidAlgorithmParameterException
468: * @return CanonicalizationMethod
469: */
470: public CanonicalizationMethod newCanonicalizationMethod(
471: String algorithm, XMLStructure xMLStructure)
472: throws NoSuchAlgorithmException,
473: InvalidAlgorithmParameterException {
474: com.sun.xml.ws.security.opt.crypto.dsig.CanonicalizationMethod canonicalizationMethod = new com.sun.xml.ws.security.opt.crypto.dsig.CanonicalizationMethod();
475: canonicalizationMethod.setAlgorithm(algorithm);
476: if (xMLStructure != null) {
477: List content = new ArrayList();
478: content.add(xMLStructure);
479: canonicalizationMethod.setContent(content);
480: }
481: return canonicalizationMethod;
482: }
483:
484: public KeyInfo newKeyInfo(List content) {
485: com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo ki = new com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo();
486: ki.setContent(content);
487: return ki;
488: }
489:
490: /**
491: * Creates a DSAKeyValue with the specified parameters
492: * @param p
493: * @param q
494: * @param g
495: * @param y
496: * @param j
497: * @param seed
498: * @param pgenCounter
499: * @return DSAKeyValue
500: */
501: public DSAKeyValue newDSAKeyValue(byte[] p, byte[] q, byte[] g,
502: byte[] y, byte[] j, byte[] seed, byte[] pgenCounter) {
503:
504: DSAKeyValue dsaKeyValue = new DSAKeyValue();
505: dsaKeyValue.setP(p);
506: dsaKeyValue.setQ(q);
507: dsaKeyValue.setG(g);
508: dsaKeyValue.setY(y);
509: dsaKeyValue.setJ(j);
510: dsaKeyValue.setSeed(seed);
511: dsaKeyValue.setPgenCounter(pgenCounter);
512:
513: return dsaKeyValue;
514: }
515:
516: /**
517: * Creates a KeyInfo with the specified parameters
518: * @param id
519: * @param content
520: * @return KeyInfo
521: */
522: public com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo newKeyInfo(
523: String id, List content) {
524: com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo keyInfo = new com.sun.xml.ws.security.opt.crypto.dsig.keyinfo.KeyInfo();
525: keyInfo.setId(id);
526: keyInfo.setContent(content);
527: return keyInfo;
528: }
529:
530: /**
531: * Creates a KeyName with the specified parameters
532: * @param name
533: * @return KeyName
534: */
535: public KeyName newKeyName(String name) {
536: KeyName keyName = new KeyName();
537: keyName.setKeyName(name);
538: return keyName;
539: }
540:
541: /**
542: * Creates a KeyValue with the specified parameters
543: * @param content
544: * @return KeyValue
545: */
546: public KeyValue newKeyValue(List content) {
547: KeyValue keyValue = new KeyValue();
548: keyValue.setContent(content);
549: return keyValue;
550: }
551:
552: /**
553: * Creates a PGPData with the specified parameters
554: * @param content
555: * @return PGPData
556: */
557: public PGPData newPGPData(List content) {
558: PGPData pgpData = new PGPData();
559: pgpData.setContent(content);
560: return pgpData;
561: }
562:
563: /**
564: * Creates a RSAKeyValue with the specified parameters
565: * @param modulas
566: * @param exponent
567: * @return RSAKeyValue
568: */
569: public RSAKeyValue newRSAKeyValue(byte[] modulas, byte[] exponent) {
570: RSAKeyValue rsaKeyValue = new RSAKeyValue();
571: rsaKeyValue.setExponent(exponent);
572: rsaKeyValue.setModulus(modulas);
573: return rsaKeyValue;
574: }
575:
576: /**
577: * Creates a RetrievalMethod with the specified parameters
578: * @param transforms
579: * @param type
580: * @param uri
581: * @return RetrievalMethod
582: */
583: public RetrievalMethod newRetrievalMethod(Transforms transforms,
584: String type, String uri) {
585: RetrievalMethod rm = new RetrievalMethod();
586: rm.setTransforms(transforms);
587: rm.setType(type);
588: rm.setURI(uri);
589: return rm;
590: }
591:
592: /**
593: * Creates a SPKIData with the specified parameters
594: * @param spkiSexpAndAny
595: * @return SPKIData
596: */
597: public SPKIData newSPKIData(List spkiSexpAndAny) {
598: SPKIData spkiData = new SPKIData();
599: spkiData.setSpkiSexpAndAny(spkiSexpAndAny);
600: return spkiData;
601: }
602:
603: /**
604: * Creates a X509Data with the specified parameters
605: * @param content
606: * @return X509Data
607: */
608: public X509Data newX509Data(List content) {
609: X509Data x509Data = new X509Data();
610: x509Data.setX509IssuerSerialOrX509SKIOrX509SubjectName(content);
611: return x509Data;
612: }
613:
614: /**
615: * Creates a X509IssuerSerial with the specified parameters
616: * @param issuer
617: * @param serialno
618: * @return X509IssuerSerial
619: */
620: public X509IssuerSerial newX509IssuerSerial(String issuer,
621: BigInteger serialno) {
622: X509IssuerSerial x509IssuerSerial = new X509IssuerSerial();
623: x509IssuerSerial.setX509IssuerName(issuer);
624: x509IssuerSerial.setX509SerialNumber(serialno);
625: return x509IssuerSerial;
626: }
627:
628: /**
629: *
630: * @param xMLValidateContext
631: * @throws javax.xml.crypto.MarshalException
632: * @return
633: */
634: public XMLSignature unmarshalXMLSignature(
635: XMLValidateContext xMLValidateContext)
636: throws MarshalException {
637: return null;
638: }
639:
640: /**
641: *
642: * @param xMLStructure
643: * @throws javax.xml.crypto.MarshalException
644: * @return
645: */
646: public XMLSignature unmarshalXMLSignature(XMLStructure xMLStructure)
647: throws MarshalException {
648: return null;
649: }
650:
651: /**
652: *
653: * @param string
654: * @return
655: */
656: public boolean isFeatureSupported(String string) {
657: return false;
658: }
659:
660: /**
661: *
662: * @return
663: */
664: public URIDereferencer getURIDereferencer() {
665: return null;
666: }
667:
668: }
|