View Javadoc
1   /*
2    * Copyright 2001-2013 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.apache.juddi.v3.client.cli;
18  
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.util.concurrent.atomic.AtomicReference;
22  import javax.xml.bind.JAXB;
23  
24  import org.apache.juddi.v3.client.cryptor.DigSigUtil;
25  import org.apache.juddi.v3.client.cryptor.XmlUtils;
26  import org.uddi.api_v3.*;
27  
28  /**
29   * This class shows you how to digital sign a business and save to file
30   *
31   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
32   */
33  public class UddiDigitalSignatureFile {
34  
35          
36  
37          /**
38           * This sets up the ws proxies using uddi.xml in META-INF
39           */
40          public UddiDigitalSignatureFile() {
41                  
42          }
43  
44          public enum UddiType {
45  
46                  Business, Service, Binding, TModel, PublisherAssertion
47          }
48  
49          public void fire(String fileIn, String fileOut, UddiType type) {
50                  try {
51                          System.out.println("WARN - All previous signatures will be removed!");
52                          if (fileIn == null || fileOut == null || type == null) {
53                                  System.out.print("Input file: ");
54                                  fileIn = System.console().readLine();
55                                  System.out.print("Out file: ");
56                                  fileOut = System.console().readLine();
57                                  System.out.println();
58                                  for (int i = 0; i < UddiType.values().length; i++) {
59                                          System.out.println("[" + i + "] " + UddiType.values()[i].toString());
60                                  }
61                                  System.out.print("UDDI Type: ");
62                                  String t = System.console().readLine();
63                                  type = UddiType.values()[Integer.parseInt(t)];
64                          }
65  
66                          DigSigUtil ds = null;
67  
68                          //option 1), set everything manually
69                          ds = new DigSigUtil();
70                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE, "keystore.jks");
71                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILETYPE, "JKS");
72                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE_PASSWORD, "Test");
73                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_KEY_ALIAS, "Test");
74                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_BASE64, "true");
75  
76                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SERIAL, "true");
77                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SUBJECTDN, "true");
78                          ds.put(DigSigUtil.TRUSTSTORE_FILE, "truststore.jks");
79                          ds.put(DigSigUtil.TRUSTSTORE_FILETYPE, "JKS");
80                          ds.put(DigSigUtil.TRUSTSTORE_FILE_PASSWORD, "Test");
81  
82                          FileInputStream fis = new FileInputStream(fileIn);
83                          Class expectedType = null;
84                          switch (type) {
85                                  case Binding:
86                                          expectedType = BindingTemplate.class;
87                                          break;
88                                  case Business:
89                                          expectedType = BusinessEntity.class;
90                                          break;
91                                  case PublisherAssertion:
92                                          expectedType = PublisherAssertion.class;
93                                          break;
94                                  case Service:
95                                          expectedType = BusinessService.class;
96                                          break;
97                                  case TModel:
98                                          expectedType = TModel.class;
99                                          break;
100                         }
101                         Object be = XmlUtils.unmarshal(fis, expectedType);
102                         fis.close();
103                         fis = null;
104                         
105                         switch (type) {
106                                 case Binding:
107                                         ((BindingTemplate)be).getSignature().clear();
108                                         break;
109                                 case Business:
110                                         ((BusinessEntity)be).getSignature().clear();
111                                         break;
112                                 case PublisherAssertion:
113                                         ((PublisherAssertion)be).getSignature().clear();
114                                         break;
115                                 case Service:
116                                         ((BusinessService)be).getSignature().clear();
117                                         break;
118                                 case TModel:
119                                         ((TModel)be).getSignature().clear();
120                                         break;
121                         }
122 
123                         System.out.println("signing");
124                         Object signUDDI_JAXBObject = ds.signUddiEntity(be);
125                         System.out.println("signed");
126                         DigSigUtil.JAXB_ToStdOut(signUDDI_JAXBObject);
127                         
128 
129                         System.out.println("verifing");
130                         AtomicReference<String> msg = new AtomicReference<String>();
131                         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
132                         if (verifySigned_UDDI_JAXB_Object) {
133                                 System.out.println("signature validation passed (expected)");
134                                 FileOutputStream fos = new FileOutputStream(fileOut);
135                                 JAXB.marshal(signUDDI_JAXBObject, fos);
136                                 fos.close();
137                         } else {
138                                 System.out.println("signature validation failed (not expected)");
139                         }
140                         System.out.println(msg.get());
141 
142                 } catch (Exception e) {
143                         e.printStackTrace();
144                 }
145         }
146 
147        
148 }