4217 lines · 166.7 KB · Java
RawDownload
            }            if (s.charAt(0) == '-' || s.charAt(0) == '+') {                // Form 1: ([+-]nnn[ymdHMS])+                int start = 0;                while (start < len) {                    int sign = 0;                    switch (s.charAt(start)) {                        case '+': sign = 1; break;                        case '-': sign = -1; break;                        default: throw ioe;                    }                    int i = start+1;                    for (; i<len; i++) {                        char ch = s.charAt(i);                        if (ch < '0' || ch > '9') break;                    }                    if (i == start+1) throw ioe;                    int number = Integer.parseInt(s.substring(start+1, i));                    if (i >= len) throw ioe;                    int unit = 0;                    switch (s.charAt(i)) {                        case 'y': unit = Calendar.YEAR; break;                        case 'm': unit = Calendar.MONTH; break;                        case 'd': unit = Calendar.DATE; break;                        case 'H': unit = Calendar.HOUR; break;                        case 'M': unit = Calendar.MINUTE; break;                        case 'S': unit = Calendar.SECOND; break;                        default: throw ioe;                    }                    c.add(unit, sign * number);                    start = i + 1;                }            } else  {                // Form 2: [yyyy/mm/dd] [HH:MM:SS]                String date = null, time = null;                if (len == 19) {                    date = s.substring(0, 10);                    time = s.substring(11);                    if (s.charAt(10) != ' ')                        throw ioe;                } else if (len == 10) {                    date = s;                } else if (len == 8) {                    time = s;                } else {                    throw ioe;                }                if (date != null) {                    if (date.matches("\\d\\d\\d\\d\\/\\d\\d\\/\\d\\d")) {                        c.set(Integer.valueOf(date.substring(0, 4)),                                Integer.valueOf(date.substring(5, 7))-1,                                Integer.valueOf(date.substring(8, 10)));                    } else {                        throw ioe;                    }                }                if (time != null) {                    if (time.matches("\\d\\d:\\d\\d:\\d\\d")) {                        c.set(Calendar.HOUR_OF_DAY, Integer.valueOf(time.substring(0, 2)));                        c.set(Calendar.MINUTE, Integer.valueOf(time.substring(0, 2)));                        c.set(Calendar.SECOND, Integer.valueOf(time.substring(0, 2)));                        c.set(Calendar.MILLISECOND, 0);                    } else {                        throw ioe;                    }                }            }        }        return c.getTime();    }     /**     * Match a command (may be abbreviated) with a command set.     * @param s the command provided     * @param list the legal command set. If there is a null, commands after it     * are regarded experimental, which means they are supported but their     * existence should not be revealed to user.     * @return the position of a single match, or -1 if none matched     * @throws Exception if s is ambiguous     */    private static int oneOf(String s, String... list) throws Exception {        int[] match = new int[list.length];        int nmatch = 0;        int experiment = Integer.MAX_VALUE;        for (int i = 0; i<list.length; i++) {            String one = list[i];            if (one == null) {                experiment = i;                continue;            }            if (one.toLowerCase(Locale.ENGLISH)                    .startsWith(s.toLowerCase(Locale.ENGLISH))) {                match[nmatch++] = i;            } else {                StringBuffer sb = new StringBuffer();                boolean first = true;                for (char c: one.toCharArray()) {                    if (first) {                        sb.append(c);                        first = false;                    } else {                        if (!Character.isLowerCase(c)) {                            sb.append(c);                        }                    }                }                if (sb.toString().equalsIgnoreCase(s)) {                    match[nmatch++] = i;                }            }        }        if (nmatch == 0) {            return -1;        } else if (nmatch == 1) {            return match[0];        } else {            // If multiple matches is in experimental commands, ignore them            if (match[1] > experiment) {                return match[0];            }            StringBuffer sb = new StringBuffer();            MessageFormat form = new MessageFormat(rb.getString                ("command.{0}.is.ambiguous."));            Object[] source = {s};            sb.append(form.format(source));            sb.append("\n    ");            for (int i=0; i<nmatch && match[i]<experiment; i++) {                sb.append(' ');                sb.append(list[match[i]]);            }            throw new Exception(sb.toString());        }    }     /**     * Create a GeneralName object from known types     * @param t one of 5 known types     * @param v value     * @return which one     */    private GeneralName createGeneralName(String t, String v)            throws Exception {        GeneralNameInterface gn;        int p = oneOf(t, "EMAIL", "URI", "DNS", "IP", "OID");        if (p < 0) {            throw new Exception(rb.getString(                    "Unrecognized.GeneralName.type.") + t);        }        switch (p) {            case 0: gn = new RFC822Name(v); break;            case 1: gn = new URIName(v); break;            case 2: gn = new DNSName(v); break;            case 3: gn = new IPAddressName(v); break;            default: gn = new OIDName(v); break; //4        }        return new GeneralName(gn);    }     private static final String[] extSupported = {                        "BasicConstraints",                        "KeyUsage",                        "ExtendedKeyUsage",                        "SubjectAlternativeName",                        "IssuerAlternativeName",                        "SubjectInfoAccess",                        "AuthorityInfoAccess",                        null,                        "CRLDistributionPoints",    };     private ObjectIdentifier findOidForExtName(String type)            throws Exception {        switch (oneOf(type, extSupported)) {            case 0: return PKIXExtensions.BasicConstraints_Id;            case 1: return PKIXExtensions.KeyUsage_Id;            case 2: return PKIXExtensions.ExtendedKeyUsage_Id;            case 3: return PKIXExtensions.SubjectAlternativeName_Id;            case 4: return PKIXExtensions.IssuerAlternativeName_Id;            case 5: return PKIXExtensions.SubjectInfoAccess_Id;            case 6: return PKIXExtensions.AuthInfoAccess_Id;            case 8: return PKIXExtensions.CRLDistributionPoints_Id;            default: return new ObjectIdentifier(type);        }    }     /**     * Create X509v3 extensions from a string representation. Note that the     * SubjectKeyIdentifierExtension will always be created non-critical besides     * the extension requested in the <code>extstr</code> argument.     *     * @param reqex the requested extensions, can be null, used for -gencert     * @param ext the original extensions, can be null, used for -selfcert     * @param extstrs -ext values, Read keytool doc     * @param pkey the public key for the certificate     * @param akey the public key for the authority (issuer)     * @return the created CertificateExtensions     */    private CertificateExtensions createV3Extensions(            CertificateExtensions reqex,            CertificateExtensions ext,            List <String> extstrs,            PublicKey pkey,            PublicKey akey) throws Exception {         if (ext != null && reqex != null) {            // This should not happen            throw new Exception("One of request and original should be null.");        }        if (ext == null) ext = new CertificateExtensions();        try {            // name{:critical}{=value}            // Honoring requested extensions            if (reqex != null) {                for(String extstr: extstrs) {                    if (extstr.toLowerCase(Locale.ENGLISH).startsWith("honored=")) {                        List<String> list = Arrays.asList(                                extstr.toLowerCase(Locale.ENGLISH).substring(8).split(","));                        // First check existence of "all"                        if (list.contains("all")) {                            ext = reqex;    // we know ext was null                        }                        // one by one for others                        for (String item: list) {                            if (item.equals("all")) continue;                             // add or remove                            boolean add = true;                            // -1, unchanged, 0 crtical, 1 non-critical                            int action = -1;                            String type = null;                            if (item.startsWith("-")) {                                add = false;                                type = item.substring(1);                            } else {                                int colonpos = item.indexOf(':');                                if (colonpos >= 0) {                                    type = item.substring(0, colonpos);                                    action = oneOf(item.substring(colonpos+1),                                            "critical", "non-critical");                                    if (action == -1) {                                        throw new Exception(rb.getString                                            ("Illegal.value.") + item);                                    }                                }                            }                            String n = reqex.getNameByOid(findOidForExtName(type));                            if (add) {                                Extension e = (Extension)reqex.get(n);                                if (!e.isCritical() && action == 0                                        || e.isCritical() && action == 1) {                                    e = Extension.newExtension(                                            e.getExtensionId(),                                            !e.isCritical(),                                            e.getExtensionValue());                                    ext.set(n, e);                                }                            } else {                                ext.delete(n);                            }                        }                        break;                    }                }            }            for(String extstr: extstrs) {                String name, value;                boolean isCritical = false;                 int eqpos = extstr.indexOf('=');                if (eqpos >= 0) {                    name = extstr.substring(0, eqpos);                    value = extstr.substring(eqpos+1);                } else {                    name = extstr;                    value = null;                }                 int colonpos = name.indexOf(':');                if (colonpos >= 0) {                    if (oneOf(name.substring(colonpos+1), "critical") == 0) {                        isCritical = true;                    }                    name = name.substring(0, colonpos);                }                 if (name.equalsIgnoreCase("honored")) {                    continue;                }                int exttype = oneOf(name, extSupported);                switch (exttype) {                    case 0:     // BC                        int pathLen = -1;                        boolean isCA = false;                        if (value == null) {                            isCA = true;                        } else {                            try {   // the abbr format                                pathLen = Integer.parseInt(value);                                isCA = true;                            } catch (NumberFormatException ufe) {                                // ca:true,pathlen:1                                for (String part: value.split(",")) {                                    String[] nv = part.split(":");                                    if (nv.length != 2) {                                        throw new Exception(rb.getString                                                ("Illegal.value.") + extstr);                                    } else {                                        if (nv[0].equalsIgnoreCase("ca")) {                                            isCA = Boolean.parseBoolean(nv[1]);                                        } else if (nv[0].equalsIgnoreCase("pathlen")) {                                            pathLen = Integer.parseInt(nv[1]);                                        } else {                                            throw new Exception(rb.getString                                                ("Illegal.value.") + extstr);                                        }                                    }                                }                            }                        }                        ext.set(BasicConstraintsExtension.NAME,                                new BasicConstraintsExtension(isCritical, isCA,                                pathLen));                        break;                    case 1:     // KU                        if(value != null) {                            boolean[] ok = new boolean[9];                            for (String s: value.split(",")) {                                int p = oneOf(s,                                       "digitalSignature",  // (0),                                       "nonRepudiation",    // (1)                                       "keyEncipherment",   // (2),                                       "dataEncipherment",  // (3),                                       "keyAgreement",      // (4),                                       "keyCertSign",       // (5),                                       "cRLSign",           // (6),                                       "encipherOnly",      // (7),                                       "decipherOnly",      // (8)                                       "contentCommitment"  // also (1)                                       );                                if (p < 0) {                                    throw new Exception(rb.getString("Unknown.keyUsage.type.") + s);                                }                                if (p == 9) p = 1;                                ok[p] = true;                            }                            KeyUsageExtension kue = new KeyUsageExtension(ok);                            // The above KeyUsageExtension constructor does not                            // allow isCritical value, so...                            ext.set(KeyUsageExtension.NAME, Extension.newExtension(                                    kue.getExtensionId(),                                    isCritical,                                    kue.getExtensionValue()));                        } else {                            throw new Exception(rb.getString                                    ("Illegal.value.") + extstr);                        }                        break;                    case 2:     // EKU                        if(value != null) {                            Vector<ObjectIdentifier> v = new Vector<>();                            for (String s: value.split(",")) {                                int p = oneOf(s,                                        "anyExtendedKeyUsage",                                        "serverAuth",       //1                                        "clientAuth",       //2                                        "codeSigning",      //3                                        "emailProtection",  //4                                        "",                 //5                                        "",                 //6                                        "",                 //7                                        "timeStamping",     //8                                        "OCSPSigning"       //9                                       );                                if (p < 0) {                                    try {                                        v.add(new ObjectIdentifier(s));                                    } catch (Exception e) {                                        throw new Exception(rb.getString(                                                "Unknown.extendedkeyUsage.type.") + s);                                    }                                } else if (p == 0) {                                    v.add(new ObjectIdentifier("2.5.29.37.0"));                                } else {                                    v.add(new ObjectIdentifier("1.3.6.1.5.5.7.3." + p));                                }                            }                            ext.set(ExtendedKeyUsageExtension.NAME,                                    new ExtendedKeyUsageExtension(isCritical, v));                        } else {                            throw new Exception(rb.getString                                    ("Illegal.value.") + extstr);                        }                        break;                    case 3:     // SAN                    case 4:     // IAN                        if(value != null) {                            String[] ps = value.split(",");                            GeneralNames gnames = new GeneralNames();                            for(String item: ps) {                                colonpos = item.indexOf(':');                                if (colonpos < 0) {                                    throw new Exception("Illegal item " + item + " in " + extstr);                                }                                String t = item.substring(0, colonpos);                                String v = item.substring(colonpos+1);                                gnames.add(createGeneralName(t, v));                            }                            if (exttype == 3) {                                ext.set(SubjectAlternativeNameExtension.NAME,                                        new SubjectAlternativeNameExtension(                                            isCritical, gnames));                            } else {                                ext.set(IssuerAlternativeNameExtension.NAME,                                        new IssuerAlternativeNameExtension(                                            isCritical, gnames));                            }                        } else {                            throw new Exception(rb.getString                                    ("Illegal.value.") + extstr);                        }                        break;                    case 5:     // SIA, always non-critical                    case 6:     // AIA, always non-critical                        if (isCritical) {                            throw new Exception(rb.getString(                                    "This.extension.cannot.be.marked.as.critical.") + extstr);                        }                        if(value != null) {                            List<AccessDescription> accessDescriptions =                                    new ArrayList<>();                            String[] ps = value.split(",");                            for(String item: ps) {                                colonpos = item.indexOf(':');                                int colonpos2 = item.indexOf(':', colonpos+1);                                if (colonpos < 0 || colonpos2 < 0) {                                    throw new Exception(rb.getString                                            ("Illegal.value.") + extstr);                                }                                String m = item.substring(0, colonpos);                                String t = item.substring(colonpos+1, colonpos2);                                String v = item.substring(colonpos2+1);                                int p = oneOf(m,                                        "",                                        "ocsp",         //1                                        "caIssuers",    //2                                        "timeStamping", //3                                        "",                                        "caRepository"  //5                                        );                                ObjectIdentifier oid;                                if (p < 0) {                                    try {                                        oid = new ObjectIdentifier(m);                                    } catch (Exception e) {                                        throw new Exception(rb.getString(                                                "Unknown.AccessDescription.type.") + m);                                    }                                } else {                                    oid = new ObjectIdentifier("1.3.6.1.5.5.7.48." + p);                                }                                accessDescriptions.add(new AccessDescription(                                        oid, createGeneralName(t, v)));                            }                            if (exttype == 5) {                                ext.set(SubjectInfoAccessExtension.NAME,                                        new SubjectInfoAccessExtension(accessDescriptions));                            } else {                                ext.set(AuthorityInfoAccessExtension.NAME,                                        new AuthorityInfoAccessExtension(accessDescriptions));                            }                        } else {                            throw new Exception(rb.getString                                    ("Illegal.value.") + extstr);                        }                        break;                    case 8: // CRL, experimental, only support 1 distributionpoint                        if(value != null) {                            String[] ps = value.split(",");                            GeneralNames gnames = new GeneralNames();                            for(String item: ps) {                                colonpos = item.indexOf(':');                                if (colonpos < 0) {                                    throw new Exception("Illegal item " + item + " in " + extstr);                                }                                String t = item.substring(0, colonpos);                                String v = item.substring(colonpos+1);                                gnames.add(createGeneralName(t, v));                            }                            ext.set(CRLDistributionPointsExtension.NAME,                                    new CRLDistributionPointsExtension(                                        isCritical, Collections.singletonList(                                        new DistributionPoint(gnames, null, null))));                        } else {                            throw new Exception(rb.getString                                    ("Illegal.value.") + extstr);                        }                        break;                    case -1:                        ObjectIdentifier oid = new ObjectIdentifier(name);                        byte[] data = null;

Lines 3501–4000 of 4217. Previous lines