1 /*
2 * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
621 /*
622 * Converts a Base64-encoded X.509 certificate or X.509 CRL or PKCS#7 data
623 * to binary encoding.
624 * In all cases, the data must be bounded at the beginning by
625 * "-----BEGIN", and must be bounded at the end by "-----END".
626 */
627 private byte[] base64_to_binary(InputStream is)
628 throws IOException
629 {
630 long len = 0; // total length of base64 encoding, including boundaries
631
632 is.mark(is.available());
633
634 BufferedInputStream bufin = new BufferedInputStream(is);
635 BufferedReader br =
636 new BufferedReader(new InputStreamReader(bufin, "ASCII"));
637
638 // First read all of the data that is found between
639 // the "-----BEGIN" and "-----END" boundaries into a buffer.
640 String temp;
641 if ((temp=readLine(br))==null || !temp.startsWith("-----BEGIN")) {
642 throw new IOException("Unsupported encoding");
643 } else {
644 len += temp.length();
645 }
646 StringBuffer strBuf = new StringBuffer();
647 while ((temp=readLine(br))!=null && !temp.startsWith("-----END")) {
648 strBuf.append(temp);
649 }
650 if (temp == null) {
651 throw new IOException("Unsupported encoding");
652 } else {
653 len += temp.length();
654 }
655
656 // consume only as much as was needed
657 len += strBuf.length();
658 is.reset();
659 is.skip(len);
660
661 // Now, that data is supposed to be a single X.509 certificate or
662 // X.509 CRL or PKCS#7 formatted data... Base64 encoded.
663 // Decode into binary and return the result.
664 BASE64Decoder decoder = new BASE64Decoder();
666 }
667
668 /*
669 * Reads the entire input stream into a byte array.
670 */
671 private byte[] getTotalBytes(InputStream is) throws IOException {
672 byte[] buffer = new byte[8192];
673 ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
674 int n;
675 baos.reset();
676 while ((n = is.read(buffer, 0, buffer.length)) != -1) {
677 baos.write(buffer, 0, n);
678 }
679 return baos.toByteArray();
680 }
681
682 /*
683 * Determines if input is binary or Base64 encoded.
684 */
685 private boolean isBase64(InputStream is) throws IOException {
686 if (is.available() >= 10) {
687 is.mark(10);
688 int c1 = is.read();
689 int c2 = is.read();
690 int c3 = is.read();
691 int c4 = is.read();
692 int c5 = is.read();
693 int c6 = is.read();
694 int c7 = is.read();
695 int c8 = is.read();
696 int c9 = is.read();
697 int c10 = is.read();
698 is.reset();
699 if (c1 == '-' && c2 == '-' && c3 == '-' && c4 == '-'
700 && c5 == '-' && c6 == 'B' && c7 == 'E' && c8 == 'G'
701 && c9 == 'I' && c10 == 'N') {
702 return true;
703 } else {
704 return false;
705 }
706 } else {
707 return false;
708 }
709 }
710
711 /*
712 * Read a line of text. A line is considered to be terminated by any one
713 * of a line feed ('\n'), a carriage return ('\r'), a carriage return
714 * followed immediately by a linefeed, or an end-of-certificate marker.
715 *
716 * @return A String containing the contents of the line, including
717 * any line-termination characters, or null if the end of the
718 * stream has been reached.
719 */
720 private String readLine(BufferedReader br) throws IOException {
721 int c;
|
1 /*
2 * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
621 /*
622 * Converts a Base64-encoded X.509 certificate or X.509 CRL or PKCS#7 data
623 * to binary encoding.
624 * In all cases, the data must be bounded at the beginning by
625 * "-----BEGIN", and must be bounded at the end by "-----END".
626 */
627 private byte[] base64_to_binary(InputStream is)
628 throws IOException
629 {
630 long len = 0; // total length of base64 encoding, including boundaries
631
632 is.mark(is.available());
633
634 BufferedInputStream bufin = new BufferedInputStream(is);
635 BufferedReader br =
636 new BufferedReader(new InputStreamReader(bufin, "ASCII"));
637
638 // First read all of the data that is found between
639 // the "-----BEGIN" and "-----END" boundaries into a buffer.
640 String temp;
641 while (true) {
642 temp=readLine(br);
643 if (temp == null) {
644 throw new IOException("Unsupported encoding");
645 }
646 len += temp.length();
647 if (temp.startsWith("-----BEGIN")) {
648 break;
649 }
650 }
651 StringBuffer strBuf = new StringBuffer();
652 while ((temp=readLine(br))!=null && !temp.startsWith("-----END")) {
653 strBuf.append(temp);
654 }
655 if (temp == null) {
656 throw new IOException("Unsupported encoding");
657 } else {
658 len += temp.length();
659 }
660
661 // consume only as much as was needed
662 len += strBuf.length();
663 is.reset();
664 is.skip(len);
665
666 // Now, that data is supposed to be a single X.509 certificate or
667 // X.509 CRL or PKCS#7 formatted data... Base64 encoded.
668 // Decode into binary and return the result.
669 BASE64Decoder decoder = new BASE64Decoder();
671 }
672
673 /*
674 * Reads the entire input stream into a byte array.
675 */
676 private byte[] getTotalBytes(InputStream is) throws IOException {
677 byte[] buffer = new byte[8192];
678 ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
679 int n;
680 baos.reset();
681 while ((n = is.read(buffer, 0, buffer.length)) != -1) {
682 baos.write(buffer, 0, n);
683 }
684 return baos.toByteArray();
685 }
686
687 /*
688 * Determines if input is binary or Base64 encoded.
689 */
690 private boolean isBase64(InputStream is) throws IOException {
691 if (is.available() >= 1) {
692 is.mark(1);
693 int c1 = is.read();
694 is.reset();
695 if (c1 != DerValue.tag_Sequence) {
696 return true;
697 } else {
698 return false;
699 }
700 } else {
701 return false;
702 }
703 }
704
705 /*
706 * Read a line of text. A line is considered to be terminated by any one
707 * of a line feed ('\n'), a carriage return ('\r'), a carriage return
708 * followed immediately by a linefeed, or an end-of-certificate marker.
709 *
710 * @return A String containing the contents of the line, including
711 * any line-termination characters, or null if the end of the
712 * stream has been reached.
713 */
714 private String readLine(BufferedReader br) throws IOException {
715 int c;
|