View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /**
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements. See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership. The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License. You may obtain a copy of the License at
13   *
14   * http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied. See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  package com.sun.org.apache.xml.internal.security.keys.content.keyvalues;
24  
25  import java.math.BigInteger;
26  import java.security.Key;
27  import java.security.KeyFactory;
28  import java.security.NoSuchAlgorithmException;
29  import java.security.PublicKey;
30  import java.security.interfaces.DSAPublicKey;
31  import java.security.spec.DSAPublicKeySpec;
32  import java.security.spec.InvalidKeySpecException;
33  
34  import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
35  import com.sun.org.apache.xml.internal.security.utils.Constants;
36  import com.sun.org.apache.xml.internal.security.utils.I18n;
37  import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
38  import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
39  import org.w3c.dom.Document;
40  import org.w3c.dom.Element;
41  
42  public class DSAKeyValue extends SignatureElementProxy implements KeyValueContent {
43  
44      /**
45       * Constructor DSAKeyValue
46       *
47       * @param element
48       * @param baseURI
49       * @throws XMLSecurityException
50       */
51      public DSAKeyValue(Element element, String baseURI) throws XMLSecurityException {
52          super(element, baseURI);
53      }
54  
55      /**
56       * Constructor DSAKeyValue
57       *
58       * @param doc
59       * @param P
60       * @param Q
61       * @param G
62       * @param Y
63       */
64      public DSAKeyValue(Document doc, BigInteger P, BigInteger Q, BigInteger G, BigInteger Y) {
65          super(doc);
66  
67          XMLUtils.addReturnToElement(this.constructionElement);
68          this.addBigIntegerElement(P, Constants._TAG_P);
69          this.addBigIntegerElement(Q, Constants._TAG_Q);
70          this.addBigIntegerElement(G, Constants._TAG_G);
71          this.addBigIntegerElement(Y, Constants._TAG_Y);
72      }
73  
74      /**
75       * Constructor DSAKeyValue
76       *
77       * @param doc
78       * @param key
79       * @throws IllegalArgumentException
80       */
81      public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
82          super(doc);
83  
84          XMLUtils.addReturnToElement(this.constructionElement);
85  
86          if (key instanceof java.security.interfaces.DSAPublicKey) {
87              this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(), Constants._TAG_P);
88              this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(), Constants._TAG_Q);
89              this.addBigIntegerElement(((DSAPublicKey) key).getParams().getG(), Constants._TAG_G);
90              this.addBigIntegerElement(((DSAPublicKey) key).getY(), Constants._TAG_Y);
91          } else {
92              Object exArgs[] = { Constants._TAG_DSAKEYVALUE, key.getClass().getName() };
93  
94              throw new IllegalArgumentException(I18n.translate("KeyValue.IllegalArgument", exArgs));
95          }
96      }
97  
98      /** @inheritDoc */
99      public PublicKey getPublicKey() throws XMLSecurityException {
100         try {
101             DSAPublicKeySpec pkspec =
102                 new DSAPublicKeySpec(
103                     this.getBigIntegerFromChildElement(
104                         Constants._TAG_Y, Constants.SignatureSpecNS
105                     ),
106                     this.getBigIntegerFromChildElement(
107                         Constants._TAG_P, Constants.SignatureSpecNS
108                     ),
109                     this.getBigIntegerFromChildElement(
110                         Constants._TAG_Q, Constants.SignatureSpecNS
111                     ),
112                     this.getBigIntegerFromChildElement(
113                         Constants._TAG_G, Constants.SignatureSpecNS
114                     )
115                 );
116             KeyFactory dsaFactory = KeyFactory.getInstance("DSA");
117             PublicKey pk = dsaFactory.generatePublic(pkspec);
118 
119             return pk;
120         } catch (NoSuchAlgorithmException ex) {
121             throw new XMLSecurityException("empty", ex);
122         } catch (InvalidKeySpecException ex) {
123             throw new XMLSecurityException("empty", ex);
124         }
125     }
126 
127     /** @inheritDoc */
128     public String getBaseLocalName() {
129         return Constants._TAG_DSAKEYVALUE;
130     }
131 }