Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.port25.webui.security;
- import com.port25.webui.util.StringParser;
- import com.port25.webui.util.Utility;
- import java.io.*;
- import java.security.*;
- import java.security.spec.X509EncodedKeySpec;
- import java.util.Calendar;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- import org.bouncycastle.util.encoders.Base64;
- import org.bouncycastle.util.io.pem.PemObject;
- import org.bouncycastle.util.io.pem.PemReader;
- // Referenced classes of package com.port25.webui.security:
- // Timer
- public class Lak
- implements Comparable
- {
- private static final class ParseState extends Enum
- {
- public static ParseState[] values()
- {
- return (ParseState[])$VALUES.clone();
- }
- public static ParseState valueOf(String name)
- {
- return (ParseState)Enum.valueOf(com/port25/webui/security/Lak$ParseState, name);
- }
- public static final ParseState OK;
- public static final ParseState NEW_LAK;
- public static final ParseState INVALID;
- private static final ParseState $VALUES[];
- static
- {
- OK = new ParseState("OK", 0);
- NEW_LAK = new ParseState("NEW_LAK", 1);
- INVALID = new ParseState("INVALID", 2);
- $VALUES = (new ParseState[] {
- OK, NEW_LAK, INVALID
- });
- }
- private ParseState(String s, int i)
- {
- super(s, i);
- }
- }
- private static String dec(byte pubKey[])
- {
- String xorKey = "Ljava/lang/String;";
- StringBuilder result = new StringBuilder();
- for(int p = 0; p < pubKey.length; p++)
- result.append((char)(pubKey[p] ^ (byte)xorKey.charAt(p % xorKey.length())));
- return result.toString();
- }
- Lak(Timer timer)
- {
- _timer = timer;
- }
- static Lak parse(String pubKey, StringParser parser, Timer timer, String needVersion)
- {
- Lak lak = new Lak(timer);
- boolean done = false;
- label0:
- do
- {
- int lakStart;
- StringBuilder lineBuffer;
- do
- {
- if(done || !parser.hasMore())
- break label0;
- lakStart = parser.getPosition();
- lineBuffer = new StringBuilder();
- lineBuffer.append(parser.scanUntil("\r\n#"));
- if(parser.nextCharIs('#'))
- parser.skipUntil("\r\n");
- parser.skipLineEndings();
- } while("".equals(lineBuffer.toString().trim()));
- for(; parser.nextCharIsIn(" \t"); parser.skipLineEndings())
- {
- parser.skipSpacesAndTabs();
- lineBuffer.append(parser.scanUntil("\r\n"));
- }
- String line = lineBuffer.toString();
- int colonPos = line.indexOf(':');
- if(colonPos == -1)
- return null;
- String key = line.substring(0, colonPos).trim();
- String value = line.substring(colonPos + 1).trim();
- static class _cls1
- {
- static final int $SwitchMap$com$port25$webui$security$Lak$ParseState[];
- static
- {
- $SwitchMap$com$port25$webui$security$Lak$ParseState = new int[ParseState.values().length];
- try
- {
- $SwitchMap$com$port25$webui$security$Lak$ParseState[ParseState.OK.ordinal()] = 1;
- }
- catch(NoSuchFieldError ex) { }
- try
- {
- $SwitchMap$com$port25$webui$security$Lak$ParseState[ParseState.NEW_LAK.ordinal()] = 2;
- }
- catch(NoSuchFieldError ex) { }
- try
- {
- $SwitchMap$com$port25$webui$security$Lak$ParseState[ParseState.INVALID.ordinal()] = 3;
- }
- catch(NoSuchFieldError ex) { }
- }
- }
- switch(_cls1..SwitchMap.com.port25.webui.security.Lak.ParseState[lak.set(key, value).ordinal()])
- {
- case 2: // '\002'
- parser.setPosition(lakStart);
- done = true;
- break;
- case 3: // '\003'
- log.debug((new StringBuilder()).append("Invalid LAK: Unknown field '").append(key).append("': '").append(value).append("'").toString());
- return null;
- }
- } while(true);
- if(lak.isValid(pubKey, needVersion))
- return lak;
- else
- return null;
- }
- private ParseState set(String key, String value)
- {
- if("product".equalsIgnoreCase(key))
- {
- if(_product != null)
- return ParseState.NEW_LAK;
- _product = value;
- } else
- {
- if(_product == null)
- return ParseState.INVALID;
- if("version".equalsIgnoreCase(key))
- _version = value;
- else
- if("platform".equalsIgnoreCase(key))
- _platform = value;
- else
- if("units".equalsIgnoreCase(key))
- _units = value;
- else
- if("instances".equalsIgnoreCase(key))
- _instances = value;
- else
- if("licensee".equalsIgnoreCase(key))
- _licensee = value;
- else
- if("comment".equalsIgnoreCase(key))
- _comment = value;
- else
- if("issued".equalsIgnoreCase(key))
- _issued = value;
- else
- if("expires".equalsIgnoreCase(key))
- _expiresMs = parseDate(value);
- else
- if("serial".equalsIgnoreCase(key))
- _serial = value;
- else
- if("options".equalsIgnoreCase(key))
- _options = value;
- else
- if("copyright".equalsIgnoreCase(key))
- _copyright = value;
- else
- if("check".equalsIgnoreCase(key))
- _check = value;
- else
- return ParseState.INVALID;
- }
- return ParseState.OK;
- }
- private long parseDate(String value)
- {
- if("never".equalsIgnoreCase(value))
- {
- return 0x7fffffffffffffffL;
- } else
- {
- int yearStart = 0;
- int yearStop = 4;
- String year = value.substring(0, 4);
- int monthStart = 5;
- int monthStop = 7;
- String month = value.substring(5, 7);
- int dayStart = 8;
- int dayStop = 10;
- String day = value.substring(8, 10);
- Calendar cal = Calendar.getInstance();
- cal.setTimeZone(_timer.getTimeZone());
- cal.set(1, Integer.parseInt(year));
- cal.set(2, Integer.parseInt(month) - 1);
- cal.set(5, Integer.parseInt(day));
- cal.set(11, 0);
- cal.set(12, 0);
- cal.set(13, 0);
- cal.set(14, 0);
- return cal.getTimeInMillis();
- }
- }
- private boolean isValid(String pubKey, String needVersion)
- {
- if(_check == null)
- {
- log.debug("Invalid LAK: no checksum");
- return false;
- }
- if(_check.startsWith("1-"))
- {
- if(!checkLak1(normalize(), _check.substring(2), pubKey))
- {
- log.debug("Invalid LAK: bad checksum");
- return false;
- }
- } else
- {
- log.debug("Invalid LAK: bad algorithm");
- return false;
- }
- if(!"PowerMTA Management Console".equals(_product))
- {
- log.debug((new StringBuilder()).append("Invalid LAK: product mismatch (need PowerMTA Management Console, but found ").append(_product).append(")").toString());
- return false;
- }
- if(!Utility.isEmpty(_platform) && !"linux-intel".equals(_platform))
- {
- log.debug((new StringBuilder()).append("Invalid LAK: platform mismatch (need linux-intel but found ").append(_platform).append(")").toString());
- return false;
- }
- String guiVersion = getLakCompatibleVersion(needVersion);
- if(guiVersion.compareToIgnoreCase(_version) > 0)
- {
- log.debug("Invalid LAK: version mismatch");
- return false;
- } else
- {
- return true;
- }
- }
- private byte[] normalize()
- {
- return normalize(false).getBytes();
- }
- private String normalize(boolean pretty)
- {
- StringBuilder normalized = new StringBuilder();
- normAppend(normalized, " product", _product, pretty);
- normAppend(normalized, " version", _version, pretty);
- normAppend(normalized, " platform", _platform, pretty);
- normAppend(normalized, " units", _units, pretty);
- if(_instances != null && !"1".equals(_instances))
- {
- String instances = "0".equals(_instances) ? "unlimited" : _instances;
- normAppend(normalized, "instances", instances, pretty);
- }
- normAppend(normalized, " licensee", _licensee, pretty);
- normAppend(normalized, " issued", _issued, pretty);
- normAppend(normalized, " expires", getExpiration(), pretty);
- normAppend(normalized, " serial", _serial, pretty);
- normAppend(normalized, " options", _options, pretty);
- normAppend(normalized, " comment", _comment, pretty);
- normAppend(normalized, "copyright", _copyright, pretty);
- return normalized.toString();
- }
- private void normAppend(StringBuilder normalized, String name, String value, boolean pretty)
- {
- if(pretty)
- normAppend(normalized, NL, name, ": ", value);
- else
- normAppend(normalized, ";", name.trim(), "", value);
- }
- private void normAppend(StringBuilder normalized, String recordSeparator, String name, String keyValueSeparator, String value)
- {
- if(normalized.length() > 0)
- normalized.append(recordSeparator);
- normalized.append(name);
- normalized.append(keyValueSeparator);
- if(value != null)
- normalized.append(value);
- }
- public static String getLakCompatibleVersion(String version)
- {
- int pos;
- for(pos = 0; pos < version.length() && "0123456789.".contains((new StringBuilder()).append("").append(version.charAt(pos)).toString()); pos++);
- return version.substring(0, pos);
- }
- public String getText()
- {
- return (new StringBuilder()).append(normalize(true)).append(NL).toString();
- }
- public String getSerial()
- {
- return _serial;
- }
- private boolean checkLak1(byte normalizedLak[], String sigBase64, String pubKey)
- {
- Signature signer;
- PemReader reader;
- Throwable throwable;
- Security.addProvider(new BouncyCastleProvider());
- signer = Signature.getInstance("SHA1withRSA", "BC");
- reader = new PemReader(new StringReader(pubKey));
- throwable = null;
- boolean flag;
- try
- {
- PemObject obj = reader.readPemObject();
- KeyFactory fact = KeyFactory.getInstance("RSA");
- PublicKey key = fact.generatePublic(new X509EncodedKeySpec(obj.getContent()));
- signer.initVerify(key);
- signer.update(normalizedLak);
- flag = signer.verify(Base64.decode(sigBase64));
- }
- catch(Throwable throwable1)
- {
- throwable = throwable1;
- throw throwable1;
- }
- if(reader != null)
- if(throwable != null)
- try
- {
- reader.close();
- }
- catch(Throwable x2)
- {
- throwable.addSuppressed(x2);
- }
- else
- reader.close();
- return flag;
- Exception exception;
- exception;
- if(reader != null)
- if(throwable != null)
- try
- {
- reader.close();
- }
- catch(Throwable x2)
- {
- throwable.addSuppressed(x2);
- }
- else
- reader.close();
- throw exception;
- Exception e;
- e;
- throw new RuntimeException(e);
- }
- public String getExpiration()
- {
- return _timer.formattedDate(_expiresMs);
- }
- long getExpirationMs()
- {
- return _expiresMs + 0x5265c00L;
- }
- public boolean isExpired()
- {
- return _expiresMs != 0x7fffffffffffffffL && msTillExpiration() <= 0L;
- }
- private long msTillExpiration()
- {
- if(_expiresMs == 0x7fffffffffffffffL)
- return 0x7fffffffffffffffL;
- else
- return getExpirationMs() - _timer.getTime();
- }
- public String getExpirationWarning()
- {
- if(isExpired())
- return "The license key for this PowerMTA Management Console has expired. Please contact sales@ to renew.";
- long daysLeft = msTillExpiration() / 0x5265c00L - 1L;
- int daysWarn = isEvaluation() ? 10 : 30;
- if(daysLeft <= (long)daysWarn && daysLeft >= 0L)
- return (new StringBuilder()).append("The license key for this PowerMTA Management Console expires in ").append(daysPl(daysLeft)).append(". ").append("Please contact sales@ to renew.").toString();
- else
- return null;
- }
- private String daysPl(long days)
- {
- return (new StringBuilder()).append(days).append(days != 1L ? " days" : " day").toString();
- }
- public boolean isEvaluation()
- {
- return hasOption("evaluation");
- }
- public boolean hasOption(String option)
- {
- return _options != null && _options.contains(option);
- }
- public int compareTo(Lak lak)
- {
- long expirationDiff = msTillExpiration() - lak.msTillExpiration();
- if(expirationDiff < 0L)
- return -1;
- return expirationDiff <= 0L ? 0 : 1;
- }
- public boolean equals(Object other)
- {
- return other != null && other.getClass().equals(getClass()) && other.hashCode() == hashCode();
- }
- public int hashCode()
- {
- int prime = 17;
- return _product.hashCode() + _serial.hashCode() * 17 + _options.hashCode() * 17 + _units.hashCode() * 17 + _instances.hashCode() * 17 + _version.hashCode() * 17 + _platform.hashCode() * 17 + _licensee.hashCode() * 17 + _comment.hashCode() * 17 + _issued.hashCode() * 17 + Long.valueOf(_expiresMs).hashCode() * 17 + _copyright.hashCode() * 17 + _check.hashCode() * 17;
- }
- public volatile int compareTo(Object x0)
- {
- return compareTo((Lak)x0);
- }
- static final String NL;
- static final String PUB_KEY = dec(new byte[] {
- 97, 71, 76, 91, 76, 109, 41, 38, 39, 41,
- 15, 3, 33, 48, 37, 39, 36, 27, 7, 47,
- 56, 91, 76, 2, 65, 76, 100, 42, 105, 36,
- 3, 54, 56, 55, 45, 112, 35, 48, 40, 30,
- 23, 76, 34, 32, 63, 34, 109, 17, 37, 51,
- 45, 61, 16, 122, 59, 57, 32, 60, 35, 110,
- 33, 16, 33, 63, 28, 24, 19, 32, 5, 39,
- 45, 116, 11, 32, 21, 27, 57, 118, 29, 37,
- 15, 31, 24, 63, 91, 66, 63, 63, 51, 126,
- 36, 96, 87, 62, 5, 94, 32, 0, 44, 49,
- 100, 55, 33, 63, 7, 94, 0, 74, 35, 19,
- 37, 3, 22, 99, 20, 8, 7, 62, 126, 53,
- 60, 57, 58, 55, 40, 114, 3, 59, 41, 78,
- 16, 66, 24, 14, 42, 9, 78, 29, 46, 74,
- 19, 60, 21, 98, 46, 57, 52, 53, 32, 88,
- 41, 32, 47, 54, 18, 110, 126, 95, 68, 67,
- 74, 22, 9, 36, 37, 86, 49, 122, 46, 45,
- 39, 36, 15, 24, 49, 43, 68, 67, 74, 22,
- 97, 96
- });
- private static final int EVAL_LAK_GRACE = 10;
- private static final int NORMAL_LAK_GRACE = 30;
- private static Log log = LogFactory.getLog(com/port25/webui/security/Lak);
- private Timer _timer;
- private String _product;
- private String _serial;
- private String _options;
- private String _units;
- private String _instances;
- private String _version;
- private String _platform;
- private String _licensee;
- private String _comment;
- private String _issued;
- private long _expiresMs;
- private String _copyright;
- private String _check;
- static
- {
- NL = File.separatorChar != '/' ? "\r\n" : "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement