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.algorithms.implementations;
24 
25 import java.io.IOException;
26 import java.security.InvalidAlgorithmParameterException;
27 import java.security.Key;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.NoSuchProviderException;
30 import java.security.Provider;
31 import java.security.SecureRandom;
32 import java.security.Signature;
33 import java.security.SignatureException;
34 import java.security.interfaces.DSAKey;
35 import java.security.spec.AlgorithmParameterSpec;
36 
37 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
38 import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithmSpi;
39 import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
40 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
41 import com.sun.org.apache.xml.internal.security.utils.Constants;
42 import com.sun.org.apache.xml.internal.security.utils.JavaUtils;
43 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
44 
45 public class SignatureDSA extends SignatureAlgorithmSpi {
46 
47     public static final String URI = Constants.SignatureSpecNS + "dsa-sha1";
48 
49     private static final com.sun.org.slf4j.internal.Logger LOG =
50         com.sun.org.slf4j.internal.LoggerFactory.getLogger(SignatureDSA.class);
51 
52     /** Field algorithm */
53     private final Signature signatureAlgorithm;
54 
55     /** size of Q */
56     private int size;
57 
58     /**
59      * Method engineGetURI
60      *
61      * {@inheritDoc}
62      */
engineGetURI()63     protected String engineGetURI() {
64         return XMLSignature.ALGO_ID_SIGNATURE_DSA;
65     }
66 
67     /**
68      * Constructor SignatureDSA
69      *
70      * @throws XMLSignatureException
71      */
SignatureDSA()72     public SignatureDSA() throws XMLSignatureException {
73         this(null);
74     }
75 
SignatureDSA(Provider provider)76     public SignatureDSA(Provider provider) throws XMLSignatureException {
77         String algorithmID = JCEMapper.translateURItoJCEID(engineGetURI());
78         LOG.debug("Created SignatureDSA using {}", algorithmID);
79 
80         try {
81             if (provider == null) {
82                 String providerId = JCEMapper.getProviderId();
83                 if (providerId == null) {
84                     this.signatureAlgorithm = Signature.getInstance(algorithmID);
85 
86                 } else {
87                     this.signatureAlgorithm = Signature.getInstance(algorithmID, providerId);
88                 }
89 
90             } else {
91                 this.signatureAlgorithm = Signature.getInstance(algorithmID, provider);
92             }
93 
94         } catch (NoSuchAlgorithmException | NoSuchProviderException ex) {
95             Object[] exArgs = {algorithmID, ex.getLocalizedMessage()};
96             throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
97         }
98     }
99 
100     /**
101      * {@inheritDoc}
102      */
engineSetParameter(AlgorithmParameterSpec params)103     protected void engineSetParameter(AlgorithmParameterSpec params)
104         throws XMLSignatureException {
105         try {
106             this.signatureAlgorithm.setParameter(params);
107         } catch (InvalidAlgorithmParameterException ex) {
108             throw new XMLSignatureException(ex);
109         }
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
engineVerify(byte[] signature)115     protected boolean engineVerify(byte[] signature)
116         throws XMLSignatureException {
117         try {
118             if (LOG.isDebugEnabled()) {
119                 LOG.debug("Called DSA.verify() on " + XMLUtils.encodeToString(signature));
120             }
121 
122             byte[] jcebytes = JavaUtils.convertDsaXMLDSIGtoASN1(signature, size / 8);
123 
124             return this.signatureAlgorithm.verify(jcebytes);
125         } catch (SignatureException | IOException ex) {
126             throw new XMLSignatureException(ex);
127         }
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
engineInitVerify(Key publicKey)133     protected void engineInitVerify(Key publicKey) throws XMLSignatureException {
134         engineInitVerify(publicKey, this.signatureAlgorithm);
135         size = ((DSAKey)publicKey).getParams().getQ().bitLength();
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
engineSign()141     protected byte[] engineSign() throws XMLSignatureException {
142         try {
143             byte[] jcebytes = this.signatureAlgorithm.sign();
144 
145             return JavaUtils.convertDsaASN1toXMLDSIG(jcebytes, size / 8);
146         } catch (IOException | SignatureException ex) {
147             throw new XMLSignatureException(ex);
148         }
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
engineInitSign(Key privateKey, SecureRandom secureRandom)154     protected void engineInitSign(Key privateKey, SecureRandom secureRandom)
155         throws XMLSignatureException {
156         engineInitSign(privateKey, secureRandom, this.signatureAlgorithm);
157         size = ((DSAKey)privateKey).getParams().getQ().bitLength();
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
engineInitSign(Key privateKey)163     protected void engineInitSign(Key privateKey) throws XMLSignatureException {
164         engineInitSign(privateKey, (SecureRandom)null);
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
engineUpdate(byte[] input)170     protected void engineUpdate(byte[] input) throws XMLSignatureException {
171         try {
172             this.signatureAlgorithm.update(input);
173         } catch (SignatureException ex) {
174             throw new XMLSignatureException(ex);
175         }
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
engineUpdate(byte input)181     protected void engineUpdate(byte input) throws XMLSignatureException {
182         try {
183             this.signatureAlgorithm.update(input);
184         } catch (SignatureException ex) {
185             throw new XMLSignatureException(ex);
186         }
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
engineUpdate(byte[] buf, int offset, int len)192     protected void engineUpdate(byte[] buf, int offset, int len) throws XMLSignatureException {
193         try {
194             this.signatureAlgorithm.update(buf, offset, len);
195         } catch (SignatureException ex) {
196             throw new XMLSignatureException(ex);
197         }
198     }
199 
200     /**
201      * Method engineGetJCEAlgorithmString
202      *
203      * {@inheritDoc}
204      */
engineGetJCEAlgorithmString()205     protected String engineGetJCEAlgorithmString() {
206         return this.signatureAlgorithm.getAlgorithm();
207     }
208 
209     /**
210      * Method engineGetJCEProviderName
211      *
212      * {@inheritDoc}
213      */
engineGetJCEProviderName()214     protected String engineGetJCEProviderName() {
215         return this.signatureAlgorithm.getProvider().getName();
216     }
217 
218     /**
219      * Method engineSetHMACOutputLength
220      *
221      * @param HMACOutputLength
222      * @throws XMLSignatureException
223      */
engineSetHMACOutputLength(int HMACOutputLength)224     protected void engineSetHMACOutputLength(int HMACOutputLength) throws XMLSignatureException {
225         throw new XMLSignatureException("algorithms.HMACOutputLengthOnlyForHMAC");
226     }
227 
228     /**
229      * Method engineInitSign
230      *
231      * @param signingKey
232      * @param algorithmParameterSpec
233      * @throws XMLSignatureException
234      */
engineInitSign( Key signingKey, AlgorithmParameterSpec algorithmParameterSpec )235     protected void engineInitSign(
236         Key signingKey, AlgorithmParameterSpec algorithmParameterSpec
237     ) throws XMLSignatureException {
238         throw new XMLSignatureException("algorithms.CannotUseAlgorithmParameterSpecOnDSA");
239     }
240 
241     public static class SHA256 extends SignatureDSA {
242 
SHA256()243         public SHA256() throws XMLSignatureException {
244             super();
245         }
246 
SHA256(Provider provider)247         public SHA256(Provider provider) throws XMLSignatureException {
248             super(provider);
249         }
250 
251         @Override
engineGetURI()252         public String engineGetURI() {
253             return XMLSignature.ALGO_ID_SIGNATURE_DSA_SHA256;
254         }
255     }
256 }
257