@DSComment("Private Method")
  @DSBan(DSCat.PRIVATE_METHOD)
  @DSGenerator(
      tool_name = "Doppelganger",
      tool_version = "2.0",
      generated_on = "2013-12-30 13:00:13.241 -0500",
      hash_original_method = "03E7802E2319FF252715B6886182CABF",
      hash_generated_method = "792D08AC6A43E9804EEE0BCE0AF8FF5D")
  private Vector generateOcts() {
    Vector vec = new Vector();
    for (int i = 0; i < string.length; i += MAX_LENGTH) {
      int end;

      if (i + MAX_LENGTH > string.length) {
        end = string.length;
      } else {
        end = i + MAX_LENGTH;
      }

      byte[] nStr = new byte[end - i];

      System.arraycopy(string, i, nStr, 0, nStr.length);

      vec.addElement(new DEROctetString(nStr));
    }

    return vec;
  }
Esempio n. 2
0
  /**
   * Build adn hex byte array based on record size The format of byte array is defined in 51.011
   * 10.5.1
   *
   * @param recordSize is the size X of EF record
   * @return hex byte[recordSize] to be written to EF record return null for wrong format of dialing
   *     number or tag
   */
  @DSSource({DSSourceKind.NETWORK})
  @DSGenerator(
      tool_name = "Doppelganger",
      tool_version = "2.0",
      generated_on = "2013-12-30 12:59:17.039 -0500",
      hash_original_method = "43E5A3A3017D389C996FCD504E7A9103",
      hash_generated_method = "E3F599972D04B8E5D7F86AB1411559E9")
  public byte[] buildAdnString(int recordSize) {
    byte[] bcdNumber;
    byte[] byteTag;
    byte[] adnString;
    int footerOffset = recordSize - FOOTER_SIZE_BYTES;

    // create an empty record
    adnString = new byte[recordSize];
    for (int i = 0; i < recordSize; i++) {
      adnString[i] = (byte) 0xFF;
    }

    if (TextUtils.isEmpty(number)) {
      Log.w(LOG_TAG, "[buildAdnString] Empty dialing number");
      return adnString; // return the empty record (for delete)
    } else if (number.length() > (ADN_DIALING_NUMBER_END - ADN_DIALING_NUMBER_START + 1) * 2) {
      Log.w(LOG_TAG, "[buildAdnString] Max length of dialing number is 20");
      return null;
    } else if (alphaTag != null && alphaTag.length() > footerOffset) {
      Log.w(LOG_TAG, "[buildAdnString] Max length of tag is " + footerOffset);
      return null;
    } else {
      bcdNumber = PhoneNumberUtils.numberToCalledPartyBCD(number);

      System.arraycopy(bcdNumber, 0, adnString, footerOffset + ADN_TON_AND_NPI, bcdNumber.length);

      adnString[footerOffset + ADN_BCD_NUMBER_LENGTH] = (byte) (bcdNumber.length);
      adnString[footerOffset + ADN_CAPABILITY_ID] = (byte) 0xFF; // Capability Id
      adnString[footerOffset + ADN_EXTENSION_ID] = (byte) 0xFF; // Extension Record Id

      if (!TextUtils.isEmpty(alphaTag)) {
        byteTag = GsmAlphabet.stringToGsm8BitPacked(alphaTag);
        System.arraycopy(byteTag, 0, adnString, 0, byteTag.length);
      }

      return adnString;
    }
  }
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2013-12-30 12:34:11.148 -0500",
     hash_original_method = "CF45914111E05D752375948053626E9D",
     hash_generated_method = "DB8631F608D962E1CC5946A6A27F962A")
 @Override
 public void growArray(int oldSize, int newSize) {
   super.growArray(oldSize, newSize);
   final int[][] newStateSets = new int[newSize][];
   System.arraycopy(mStateSets, 0, newStateSets, 0, oldSize);
   mStateSets = newStateSets;
 }
  /**
   * Processes ServerHelloDone: makes verification of the server messages; sends client messages,
   * computers masterSecret, sends ChangeCipherSpec
   */
  @DSComment("Package priviledge")
  @DSBan(DSCat.DEFAULT_MODIFIER)
  @DSGenerator(
      tool_name = "Doppelganger",
      tool_version = "2.0",
      generated_on = "2013-12-30 13:01:10.434 -0500",
      hash_original_method = "2DDF37E22088D1FE8BC73EB3CA83F3A0",
      hash_generated_method = "D2F899ECF0C8F0AF7307958FBF03F0A2")
  void processServerHelloDone() {
    PrivateKey clientKey = null;

    if (serverCert != null) {
      if (session.cipherSuite.isAnonymous()) {
        unexpectedMessage();
        return;
      }
      verifyServerCert();
    } else {
      if (!session.cipherSuite.isAnonymous()) {
        unexpectedMessage();
        return;
      }
    }

    // Client certificate
    if (certificateRequest != null) {
      X509Certificate[] certs = null;
      // obtain certificates from key manager
      String alias = null;
      String[] certTypes = certificateRequest.getTypesAsString();
      X500Principal[] issuers = certificateRequest.certificate_authorities;
      X509KeyManager km = parameters.getKeyManager();
      if (km instanceof X509ExtendedKeyManager) {
        X509ExtendedKeyManager ekm = (X509ExtendedKeyManager) km;
        if (this.socketOwner != null) {
          alias = ekm.chooseClientAlias(certTypes, issuers, this.socketOwner);
        } else {
          alias = ekm.chooseEngineClientAlias(certTypes, issuers, this.engineOwner);
        }
        if (alias != null) {
          certs = ekm.getCertificateChain(alias);
        }
      } else {
        alias = km.chooseClientAlias(certTypes, issuers, this.socketOwner);
        if (alias != null) {
          certs = km.getCertificateChain(alias);
        }
      }

      session.localCertificates = certs;
      clientCert = new CertificateMessage(certs);
      clientKey = km.getPrivateKey(alias);
      send(clientCert);
    }
    // Client key exchange
    if (session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_RSA
        || session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_RSA_EXPORT) {
      // RSA encrypted premaster secret message
      Cipher c;
      try {
        c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        if (serverKeyExchange != null) {
          c.init(Cipher.ENCRYPT_MODE, serverKeyExchange.getRSAPublicKey());
        } else {
          c.init(Cipher.ENCRYPT_MODE, serverCert.certs[0]);
        }
      } catch (Exception e) {
        fatalAlert(AlertProtocol.INTERNAL_ERROR, "Unexpected exception", e);
        return;
      }
      preMasterSecret = new byte[48];
      parameters.getSecureRandom().nextBytes(preMasterSecret);
      System.arraycopy(clientHello.client_version, 0, preMasterSecret, 0, 2);
      try {
        clientKeyExchange =
            new ClientKeyExchange(c.doFinal(preMasterSecret), serverHello.server_version[1] == 1);
      } catch (Exception e) {
        fatalAlert(AlertProtocol.INTERNAL_ERROR, "Unexpected exception", e);
        return;
      }
    } else {
      try {
        KeyFactory kf = KeyFactory.getInstance("DH");
        KeyAgreement agreement = KeyAgreement.getInstance("DH");
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH");
        PublicKey serverPublic;
        DHParameterSpec spec;
        if (serverKeyExchange != null) {
          serverPublic =
              kf.generatePublic(
                  new DHPublicKeySpec(
                      serverKeyExchange.par3, serverKeyExchange.par1, serverKeyExchange.par2));
          spec = new DHParameterSpec(serverKeyExchange.par1, serverKeyExchange.par2);
        } else {
          serverPublic = serverCert.certs[0].getPublicKey();
          spec = ((DHPublicKey) serverPublic).getParams();
        }
        kpg.initialize(spec);

        KeyPair kp = kpg.generateKeyPair();
        Key key = kp.getPublic();
        if (clientCert != null
            && serverCert != null
            && (session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_DHE_RSA
                || session.cipherSuite.keyExchange == CipherSuite.KEY_EXCHANGE_DHE_DSS)) {
          PublicKey client_pk = clientCert.certs[0].getPublicKey();
          PublicKey server_pk = serverCert.certs[0].getPublicKey();
          if (client_pk instanceof DHKey && server_pk instanceof DHKey) {
            if (((DHKey) client_pk)
                    .getParams()
                    .getG()
                    .equals(((DHKey) server_pk).getParams().getG())
                && ((DHKey) client_pk)
                    .getParams()
                    .getP()
                    .equals(((DHKey) server_pk).getParams().getG())) {
              // client cert message DH public key parameters
              // matched those specified by the
              //   server in its certificate,
              clientKeyExchange = new ClientKeyExchange(); // empty
            }
          }
        } else {
          clientKeyExchange = new ClientKeyExchange(((DHPublicKey) key).getY());
        }
        key = kp.getPrivate();
        agreement.init(key);
        agreement.doPhase(serverPublic, true);
        preMasterSecret = agreement.generateSecret();
      } catch (Exception e) {
        fatalAlert(AlertProtocol.INTERNAL_ERROR, "Unexpected exception", e);
        return;
      }
    }
    if (clientKeyExchange != null) {
      send(clientKeyExchange);
    }

    computerMasterSecret();

    // send certificate verify for all certificates except those containing
    // fixed DH parameters
    if (clientCert != null && !clientKeyExchange.isEmpty()) {
      // Certificate verify
      String authType = clientKey.getAlgorithm();
      DigitalSignature ds = new DigitalSignature(authType);
      ds.init(clientKey);

      if ("RSA".equals(authType)) {
        ds.setMD5(io_stream.getDigestMD5());
        ds.setSHA(io_stream.getDigestSHA());
      } else if ("DSA".equals(authType)) {
        ds.setSHA(io_stream.getDigestSHA());
        // The Signature should be empty in case of anonymous signature algorithm:
        // } else if ("DH".equals(authType)) {
      }
      certificateVerify = new CertificateVerify(ds.sign());
      send(certificateVerify);
    }

    sendChangeCipherSpec();
  }