src/share/classes/com/sun/org/apache/xml/internal/security/keys/content/keyvalues/DSAKeyValue.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  You may obtain a copy of the License at


  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  *  Unless required by applicable law or agreed to in writing, software
  15  *  distributed under the License is distributed on an "AS IS" BASIS,
  16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  *  See the License for the specific language governing permissions and
  18  *  limitations under the License.
  19  *
  20  */
  21 package com.sun.org.apache.xml.internal.security.keys.content.keyvalues;
  22 
  23 import java.math.BigInteger;
  24 import java.security.Key;
  25 import java.security.KeyFactory;
  26 import java.security.NoSuchAlgorithmException;
  27 import java.security.PublicKey;
  28 import java.security.interfaces.DSAPublicKey;
  29 import java.security.spec.DSAPublicKeySpec;
  30 import java.security.spec.InvalidKeySpecException;
  31 
  32 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  33 import com.sun.org.apache.xml.internal.security.utils.Constants;
  34 import com.sun.org.apache.xml.internal.security.utils.I18n;
  35 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
  36 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  37 import org.w3c.dom.Document;
  38 import org.w3c.dom.Element;
  39 
  40 /**
  41  *
  42  * @author $Author: mullan $
  43  */
  44 public class DSAKeyValue extends SignatureElementProxy
  45         implements KeyValueContent {
  46 
  47    /**
  48     * Constructor DSAKeyValue
  49     *
  50     * @param element
  51     * @param BaseURI
  52     * @throws XMLSecurityException
  53     */
  54    public DSAKeyValue(Element element, String BaseURI)
  55            throws XMLSecurityException {
  56       super(element, BaseURI);
  57    }
  58 
  59    /**
  60     * Constructor DSAKeyValue
  61     *
  62     * @param doc
  63     * @param P
  64     * @param Q
  65     * @param G
  66     * @param Y
  67     */
  68    public DSAKeyValue(Document doc, BigInteger P, BigInteger Q, BigInteger G,
  69                       BigInteger Y) {
  70 
  71       super(doc);
  72 
  73       XMLUtils.addReturnToElement(this._constructionElement);
  74       this.addBigIntegerElement(P, Constants._TAG_P);
  75       this.addBigIntegerElement(Q, Constants._TAG_Q);
  76       this.addBigIntegerElement(G, Constants._TAG_G);
  77       this.addBigIntegerElement(Y, Constants._TAG_Y);
  78    }
  79 
  80    /**
  81     * Constructor DSAKeyValue
  82     *
  83     * @param doc
  84     * @param key
  85     * @throws IllegalArgumentException
  86     */
  87    public DSAKeyValue(Document doc, Key key) throws IllegalArgumentException {
  88 
  89       super(doc);
  90 
  91       XMLUtils.addReturnToElement(this._constructionElement);
  92 
  93       if (key instanceof java.security.interfaces.DSAPublicKey) {
  94          this.addBigIntegerElement(((DSAPublicKey) key).getParams().getP(),
  95                                    Constants._TAG_P);
  96          this.addBigIntegerElement(((DSAPublicKey) key).getParams().getQ(),
  97                                    Constants._TAG_Q);
  98          this.addBigIntegerElement(((DSAPublicKey) key).getParams().getG(),
  99                                    Constants._TAG_G);
 100          this.addBigIntegerElement(((DSAPublicKey) key).getY(),
 101                                    Constants._TAG_Y);
 102       } else {
 103          Object exArgs[] = { Constants._TAG_DSAKEYVALUE,
 104                              key.getClass().getName() };
 105 
 106          throw new IllegalArgumentException(I18n
 107             .translate("KeyValue.IllegalArgument", exArgs));
 108       }
 109    }
 110 
 111    /** @inheritDoc */
 112    public PublicKey getPublicKey() throws XMLSecurityException {
 113 
 114       try {
 115          DSAPublicKeySpec pkspec =
 116             new DSAPublicKeySpec(this
 117                .getBigIntegerFromChildElement(Constants._TAG_Y, Constants
 118                .SignatureSpecNS), this
 119                   .getBigIntegerFromChildElement(Constants._TAG_P, Constants
 120                   .SignatureSpecNS), this
 121                      .getBigIntegerFromChildElement(Constants._TAG_Q, Constants
 122                      .SignatureSpecNS), this
 123                         .getBigIntegerFromChildElement(Constants
 124                            ._TAG_G, Constants.SignatureSpecNS));





 125          KeyFactory dsaFactory = KeyFactory.getInstance("DSA");
 126          PublicKey pk = dsaFactory.generatePublic(pkspec);
 127 
 128          return pk;
 129       } catch (NoSuchAlgorithmException ex) {
 130          throw new XMLSecurityException("empty", ex);
 131       } catch (InvalidKeySpecException ex) {
 132          throw new XMLSecurityException("empty", ex);
 133       }
 134    }
 135 
 136    /** @inheritDoc */
 137    public String getBaseLocalName() {
 138       return Constants._TAG_DSAKEYVALUE;
 139    }
 140 }
   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 }