src/java.base/share/classes/sun/security/util/Pem.java
author redestad
Mon, 08 Oct 2018 18:16:03 +0200
changeset 52040 d8aebcc2d3ac
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211860: Avoid reading security properties eagerly on Manifest class initialization Reviewed-by: mullan, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     1
/*
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     4
 *
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    10
 *
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    15
 * accompanied this code).
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    16
 *
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    20
 *
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    23
 * questions.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    24
 */
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    25
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    26
package sun.security.util;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    27
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    28
import java.io.IOException;
39054
e8da0515445a 8158633: BASE64 encoded cert not correctly parsed with UTF-16
weijun
parents: 29596
diff changeset
    29
import java.nio.charset.StandardCharsets;
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    30
import java.util.Base64;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    31
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    32
/**
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    33
 * The Length interface defines the length of an object
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    34
 */
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    35
public class Pem {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    36
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    37
    /**
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    38
     * Decodes a PEM-encoded block.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    39
     *
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    40
     * @param input the input string, according to RFC 1421, can only contain
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    41
     *              characters in the base-64 alphabet and whitespaces.
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    42
     * @return the decoded bytes
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    43
     * @throws java.io.IOException if input is invalid
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    44
     */
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    45
    public static byte[] decode(String input) throws IOException {
39054
e8da0515445a 8158633: BASE64 encoded cert not correctly parsed with UTF-16
weijun
parents: 29596
diff changeset
    46
        byte[] src = input.replaceAll("\\s+", "")
e8da0515445a 8158633: BASE64 encoded cert not correctly parsed with UTF-16
weijun
parents: 29596
diff changeset
    47
                .getBytes(StandardCharsets.ISO_8859_1);
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    48
        try {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    49
            return Base64.getDecoder().decode(src);
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    50
        } catch (IllegalArgumentException e) {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    51
            throw new IOException(e);
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    52
        }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    53
    }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents:
diff changeset
    54
}