1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2005 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 /*
  22  * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 /*
  25  * $Id: DOMCryptoBinary.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
  26  */
  27 package org.jcp.xml.dsig.internal.dom;
  28 
  29 import java.math.BigInteger;
  30 import javax.xml.crypto.*;
  31 import javax.xml.crypto.dom.DOMCryptoContext;
  32 import javax.xml.crypto.dsig.*;
  33 import org.w3c.dom.Node;
  34 import org.w3c.dom.Text;
  35 
  36 import com.sun.org.apache.xml.internal.security.utils.Base64;
  37 
  38 /**
  39  * A DOM-based representation of the XML <code>CryptoBinary</code> simple type
  40  * as defined in the W3C specification for XML-Signature Syntax and Processing.
  41  * The XML Schema Definition is defined as:
  42  *
  43  * <xmp>
  44  * <simpleType name="CryptoBinary">
  45  *   <restriction base = "base64Binary">
  46  *   </restriction>
  47  * </simpleType>
  48  * </xmp>
  49  *
  50  * @author Sean Mullan
  51  */
  52 public final class DOMCryptoBinary extends DOMStructure {
  53 
  54     private final BigInteger bigNum;
  55     private final String value;
  56 
  57     /**
  58      * Create a <code>DOMCryptoBinary</code> instance from the specified
  59      * <code>BigInteger</code>
  60      *
  61      * @param bigNum the arbitrary-length integer
  62      * @throws NullPointerException if <code>bigNum</code> is <code>null</code>
  63      */
  64     public DOMCryptoBinary(BigInteger bigNum) {
  65         if (bigNum == null) {
  66             throw new NullPointerException("bigNum is null");
  67         }
  68         this.bigNum = bigNum;
  69         // convert to bitstring
  70         value = Base64.encode(bigNum);
  71     }
  72 
  73     /**
  74      * Creates a <code>DOMCryptoBinary</code> from a node.
  75      *
  76      * @param cbNode a CryptoBinary text node
  77      * @throws MarshalException if value cannot be decoded (invalid format)
  78      */
  79     public DOMCryptoBinary(Node cbNode) throws MarshalException {
  80         value = cbNode.getNodeValue();
  81         try {
  82             bigNum = Base64.decodeBigIntegerFromText((Text) cbNode);
  83         } catch (Exception ex) {
  84             throw new MarshalException(ex);
  85         }
  86     }
  87 
  88     /**
  89      * Returns the <code>BigInteger</code> that this object contains.
  90      *
  91      * @return the <code>BigInteger</code> that this object contains
  92      */
  93     public BigInteger getBigNum() {
  94         return bigNum;
  95     }
  96 
  97     public void marshal(Node parent, String prefix, DOMCryptoContext context)
  98         throws MarshalException {
  99         parent.appendChild
 100             (DOMUtils.getOwnerDocument(parent).createTextNode(value));
 101     }
 102 }