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