1 /*
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @author Weijun Wang
  27  * @bug 6418422
  28  * @bug 6418425
  29  * @bug 6418433
  30  * @summary ObjectIdentifier should reject 1.2.3.-4 and throw IOException on all format errors
  31  * @modules java.base/sun.security.util
  32  *          java.security.jgss
  33  */
  34 
  35 import java.io.IOException;
  36 import java.security.NoSuchAlgorithmException;
  37 import org.ietf.jgss.GSSException;
  38 import org.ietf.jgss.Oid;
  39 import javax.crypto.EncryptedPrivateKeyInfo;
  40 import sun.security.util.*;
  41 import java.util.Arrays;
  42 
  43 public class OidFormat {
  44     public static void main(String[] args) throws Exception {
  45         String[] badOids = {
  46             // number problems
  47             "0", "1", "2",
  48             "3.1.1", "3", "4",
  49             "1.40", "1.111.1",
  50             "-1.2", "0,-2", "1.-2", "2.-2",
  51             "1.2.-3.4", "1.2.3.-4",
  52             // format problems
  53             "aa", "aa.aa",
  54             "", "....", "1.2..4", "1.2.3.", "1.", "1.2.", "0.1.",
  55             "1,2",
  56         };
  57 
  58         for (String s: badOids) {
  59             testBad(s);
  60         }
  61 
  62         String[] goodOids = {
  63             "0.0", "0.1", "1.0", "1.2",
  64             "0.39", "1.39", "2.47", "2.40.3.6", "2.100.3", "2.123456.3",
  65             "1.2.3", "1.2.3445",
  66             "1.3.6.1.4.1.42.2.17",
  67             // 4811968: ASN.1 cannot handle huge OID components
  68             "2.16.764.1.3101555394.1.0.100.2.1",
  69             "2.2726957624935694386592435",  // as huge as possible
  70             "1.2.777777777777777777",
  71             "1.2.888888888888888888.111111111111111.2222222222222.33333333333333333.44444444444444",
  72             "1.2." +
  73                 "1111111111111111111111111111111111111111111111111111111111111." +
  74                 "2222222222222222222222222222222222222222222222222222222222222222." +
  75                 "333333333333333333333333333333333333333333333333333333333333333." +
  76                 "4444444444444444444444444444444444444444444444444444444." +
  77                 "55555555555555555555555555555555555555555555555555555555555555555555555." +
  78                 "666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666." +
  79                 "77777777777777777777777777777777777777777777777777777777777777777777777777." +
  80                 "8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888." +
  81                 "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
  82             "1.2.2147483647.4",
  83             "1.2.268435456.4",
  84         };
  85 
  86         for (String s: goodOids) {
  87             testGood(s);
  88         }
  89 
  90         int[][] goodInts = {
  91             {0,0}, {0,1}, {1,0}, {1,2},
  92             {0,39}, {1,39}, {2,47}, {2,40,3,6}, {2,100,3}, {2,123456,3},
  93             {1,2,3}, {1,2,3445},
  94             {1,3,6,1,4,1,42,2,17},
  95         };
  96 
  97         for (int[] is: goodInts) {
  98             testGood(is);
  99         }
 100 
 101         int[][] badInts = new int[][] {
 102             {0}, {1}, {2},
 103             {3,1,1}, {3}, {4},
 104             {1,40}, {1,111,1},
 105             {-1,2}, {0,-2}, {1,-2}, {2,-2},
 106             {1,2,-3,4}, {1,2,3,-4},
 107         };
 108 
 109         for (int[] is: badInts) {
 110             testBad(is);
 111         }
 112 
 113     }
 114 
 115     static void testBad(int[] ints) throws Exception {
 116         System.err.println("Trying " + Arrays.toString(ints));
 117         try {
 118             new ObjectIdentifier(ints);
 119             throw new Exception("should be invalid ObjectIdentifier");
 120         } catch (IOException ioe) {
 121             System.err.println(ioe);
 122         }
 123     }
 124 
 125     static void testGood(int[] ints) throws Exception {
 126         System.err.println("Trying " + Arrays.toString(ints));
 127         ObjectIdentifier oid = new ObjectIdentifier(ints);
 128         DerOutputStream os = new DerOutputStream();
 129         os.putOID(oid);
 130         DerInputStream is = new DerInputStream(os.toByteArray());
 131         ObjectIdentifier oid2 = is.getOID();
 132         if (!oid.equals((Object)oid2)) {
 133             throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);
 134         }
 135     }
 136 
 137     static void testGood(String s) throws Exception {
 138         System.err.println("Trying " + s);
 139         ObjectIdentifier oid = new ObjectIdentifier(s);
 140         if (!oid.toString().equals(s)) {
 141             throw new Exception("equal test fail");
 142         }
 143         DerOutputStream os = new DerOutputStream();
 144         os.putOID(oid);
 145         DerInputStream is = new DerInputStream(os.toByteArray());
 146         ObjectIdentifier oid2 = is.getOID();
 147         if (!oid.equals((Object)oid2)) {
 148             throw new Exception("Test DER I/O fails: " + oid + " and " + oid2);
 149         }
 150     }
 151 
 152     static void testBad(String s) throws Exception {
 153         System.err.println("Trying " + s);
 154         try {
 155             new ObjectIdentifier(s);
 156             throw new Exception("should be invalid ObjectIdentifier");
 157         } catch (IOException ioe) {
 158             System.err.println(ioe);
 159         }
 160 
 161         try {
 162             new Oid(s);
 163             throw new Exception("should be invalid Oid");
 164         } catch (GSSException gsse) {
 165             ;
 166         }
 167 
 168         try {
 169             new EncryptedPrivateKeyInfo(s, new byte[8]);
 170             throw new Exception("should be invalid algorithm");
 171         } catch (NoSuchAlgorithmException e) {
 172             ;
 173         }
 174     }
 175 }