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.DSAParams; 31 import java.security.interfaces.DSAPublicKey; 32 import java.security.spec.DSAPublicKeySpec; 33 import java.security.spec.InvalidKeySpecException; 34 35 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 36 import com.sun.org.apache.xml.internal.security.utils.Constants; 37 import com.sun.org.apache.xml.internal.security.utils.I18n; 38 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy; 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 */ DSAKeyValue(Element element, String baseURI)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 */ DSAKeyValue(Document doc, BigInteger P, BigInteger Q, BigInteger G, BigInteger Y)64 public DSAKeyValue(Document doc, BigInteger P, BigInteger Q, BigInteger G, BigInteger Y) { 65 super(doc); 66 67 addReturnToSelf(); 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 */ DSAKeyValue(Document doc, Key key)81 public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException { 82 super(doc); 83 84 addReturnToSelf(); 85 86 if (key instanceof DSAPublicKey) { 87 DSAParams params = ((DSAPublicKey) key).getParams(); 88 this.addBigIntegerElement(params.getP(), Constants._TAG_P); 89 this.addBigIntegerElement(params.getQ(), Constants._TAG_Q); 90 this.addBigIntegerElement(params.getG(), Constants._TAG_G); 91 this.addBigIntegerElement(((DSAPublicKey) key).getY(), Constants._TAG_Y); 92 } else { 93 Object[] exArgs = { Constants._TAG_DSAKEYVALUE, key.getClass().getName() }; 94 95 throw new IllegalArgumentException(I18n.translate("KeyValue.IllegalArgument", exArgs)); 96 } 97 } 98 99 /** {@inheritDoc} */ getPublicKey()100 public PublicKey getPublicKey() throws XMLSecurityException { 101 try { 102 DSAPublicKeySpec pkspec = 103 new DSAPublicKeySpec( 104 this.getBigIntegerFromChildElement( 105 Constants._TAG_Y, Constants.SignatureSpecNS 106 ), 107 this.getBigIntegerFromChildElement( 108 Constants._TAG_P, Constants.SignatureSpecNS 109 ), 110 this.getBigIntegerFromChildElement( 111 Constants._TAG_Q, Constants.SignatureSpecNS 112 ), 113 this.getBigIntegerFromChildElement( 114 Constants._TAG_G, Constants.SignatureSpecNS 115 ) 116 ); 117 KeyFactory dsaFactory = KeyFactory.getInstance("DSA"); 118 119 return dsaFactory.generatePublic(pkspec); 120 } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { 121 throw new XMLSecurityException(ex); 122 } 123 } 124 125 /** {@inheritDoc} */ getBaseLocalName()126 public String getBaseLocalName() { 127 return Constants._TAG_DSAKEYVALUE; 128 } 129 } 130