jdk/src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMPGPData.java
author mullan
Fri, 21 Nov 2014 15:23:36 -0500
changeset 27747 3a271dc8b758
parent 25859 3317bb8137f4
child 32003 acb12269398a
permissions -rw-r--r--
8046949: Generify the javax.xml.crypto API Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    11
 * "License"); you may not use this file except in compliance
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    12
 * with the License. You may obtain a copy of the License at
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    17
 * software distributed under the License is distributed on an
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    19
 * KIND, either express or implied. See the License for the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    21
 * under the License.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    22
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
/*
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
    24
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    26
/*
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    27
 * $Id: DOMPGPData.java 1203846 2011-11-18 21:18:17Z mullan $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.xml.crypto.dom.DOMCryptoContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.xml.crypto.dsig.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.xml.crypto.dsig.keyinfo.PGPData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import org.w3c.dom.NodeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.xml.internal.security.utils.Base64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * DOM-based implementation of PGPData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public final class DOMPGPData extends DOMStructure implements PGPData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final byte[] keyId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final byte[] keyPacket;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    53
    private final List<XMLStructure> externalElements;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Creates a <code>DOMPGPData</code> containing the specified key packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * and optional list of external elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param keyPacket a PGP Key Material Packet as defined in section 5.5 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *    <a href="http://www.ietf.org/rfc/rfc2440.txt"/>RFC 2440</a>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *    array is cloned to prevent subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param other a list of {@link XMLStructure}s representing elements from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *    an external namespace. The list is defensively copied to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *    subsequent modification. May be <code>null</code> or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @throws NullPointerException if <code>keyPacket</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *    <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @throws IllegalArgumentException if the key packet is not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *    correct format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws ClassCastException if <code>other</code> contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *    entries that are not of type {@link XMLStructure}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
    72
    public DOMPGPData(byte[] keyPacket, List<? extends XMLStructure> other) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (keyPacket == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new NullPointerException("keyPacket cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    76
        List<XMLStructure> tempList =
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    77
            Collections.checkedList(new ArrayList<XMLStructure>(),
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    78
                                    XMLStructure.class);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    79
        if (other != null) {
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    80
            tempList.addAll(other);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
    82
        this.externalElements = Collections.unmodifiableList(tempList);
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18780
diff changeset
    83
        this.keyPacket = keyPacket.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        checkKeyPacket(keyPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.keyId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Creates a <code>DOMPGPData</code> containing the specified key id and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * optional key packet and list of external elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param keyId a PGP public key id as defined in section 11.2 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *    <a href="http://www.ietf.org/rfc/rfc2440.txt"/>RFC 2440</a>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *    array is cloned to prevent subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param keyPacket a PGP Key Material Packet as defined in section 5.5 of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *    <a href="http://www.ietf.org/rfc/rfc2440.txt"/>RFC 2440</a> (may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *    be <code>null</code>). The array is cloned to prevent subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *    modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param other a list of {@link XMLStructure}s representing elements from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *    an external namespace. The list is defensively copied to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *    subsequent modification. May be <code>null</code> or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws NullPointerException if <code>keyId</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws IllegalArgumentException if the key id or packet is not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *    correct format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throws ClassCastException if <code>other</code> contains any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *    entries that are not of type {@link XMLStructure}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   108
    public DOMPGPData(byte[] keyId, byte[] keyPacket,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   109
                      List<? extends XMLStructure> other)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   110
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (keyId == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new NullPointerException("keyId cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // key ids must be 8 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (keyId.length != 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new IllegalArgumentException("keyId must be 8 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   118
        List<XMLStructure> tempList =
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   119
            Collections.checkedList(new ArrayList<XMLStructure>(),
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   120
                                    XMLStructure.class);
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   121
        if (other != null) {
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   122
            tempList.addAll(other);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
27747
3a271dc8b758 8046949: Generify the javax.xml.crypto API
mullan
parents: 25859
diff changeset
   124
        this.externalElements = Collections.unmodifiableList(tempList);
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18780
diff changeset
   125
        this.keyId = keyId.clone();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   126
        this.keyPacket = keyPacket == null ? null
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18780
diff changeset
   127
                                           : keyPacket.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (keyPacket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            checkKeyPacket(keyPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Creates a <code>DOMPGPData</code> from an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param pdElem a PGPData element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public DOMPGPData(Element pdElem) throws MarshalException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // get all children nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        byte[] keyId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        byte[] keyPacket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        NodeList nl = pdElem.getChildNodes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int length = nl.getLength();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   144
        List<XMLStructure> other = new ArrayList<XMLStructure>(length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        for (int x = 0; x < length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Node n = nl.item(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (n.getNodeType() == Node.ELEMENT_NODE) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   148
                Element childElem = (Element)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                String localName = childElem.getLocalName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    if (localName.equals("PGPKeyID")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        keyId = Base64.decode(childElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    } else if (localName.equals("PGPKeyPacket")){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        keyPacket = Base64.decode(childElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        other.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            (new javax.xml.crypto.dom.DOMStructure(childElem));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } catch (Base64DecodingException bde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    throw new MarshalException(bde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.keyId = keyId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.keyPacket = keyPacket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.externalElements = Collections.unmodifiableList(other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public byte[] getKeyId() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18780
diff changeset
   170
        return (keyId == null ? null : keyId.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public byte[] getKeyPacket() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 18780
diff changeset
   174
        return (keyPacket == null ? null : keyPacket.clone());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
24970
094bfaa699c3 8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents: 23010
diff changeset
   177
    public List<XMLStructure> getExternalElements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return externalElements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   182
        throws MarshalException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   183
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Document ownerDoc = DOMUtils.getOwnerDocument(parent);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   185
        Element pdElem = DOMUtils.createElement(ownerDoc, "PGPData",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   186
                                                XMLSignature.XMLNS, dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // create and append PGPKeyID element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (keyId != null) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   190
            Element keyIdElem = DOMUtils.createElement(ownerDoc, "PGPKeyID",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   191
                                                       XMLSignature.XMLNS,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   192
                                                       dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            keyIdElem.appendChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                (ownerDoc.createTextNode(Base64.encode(keyId)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            pdElem.appendChild(keyIdElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // create and append PGPKeyPacket element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (keyPacket != null) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   200
            Element keyPktElem = DOMUtils.createElement(ownerDoc,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   201
                                                        "PGPKeyPacket",
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   202
                                                        XMLSignature.XMLNS,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   203
                                                        dsPrefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            keyPktElem.appendChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                (ownerDoc.createTextNode(Base64.encode(keyPacket)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            pdElem.appendChild(keyPktElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // create and append any elements
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   210
        for (XMLStructure extElem : externalElements) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            DOMUtils.appendChild(pdElem, ((javax.xml.crypto.dom.DOMStructure)
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   212
                extElem).getNode());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        parent.appendChild(pdElem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * We assume packets use the new format packet syntax, as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * section 4 of RFC 2440.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * This method only checks if the packet contains a valid tag. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * contents of the packet should be checked by the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private void checkKeyPacket(byte[] keyPacket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // length must be at least 3 (one byte for tag, one byte for length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // and minimally one byte of content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (keyPacket.length < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new IllegalArgumentException("keypacket must be at least " +
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   230
                                               "3 bytes long");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int tag = keyPacket[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // first bit must be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if ((tag & 128) != 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new IllegalArgumentException("keypacket tag is invalid: " +
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   237
                                               "bit 7 is not set");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        // make sure using new format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if ((tag & 64) != 64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new IllegalArgumentException("old keypacket tag format is " +
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   242
                                               "unsupported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // tag value must be 6, 14, 5 or 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (((tag & 6) != 6) && ((tag & 14) != 14) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            ((tag & 5) != 5) && ((tag & 7) != 7)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new IllegalArgumentException("keypacket tag is invalid: " +
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 5506
diff changeset
   249
                                               "must be 6, 14, 5, or 7");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
}