Java Code Examples for android.content.pm.PackageManager.GET_SIGNATURES

The following are Jave code examples for showing how to use GET_SIGNATURES of the android.content.pm.PackageManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: SecuritySample   File: Utils.java   View Source Code Vote up 6 votes
/**
 * Gets the encoded representation of the first signing certificated used to sign current APK
 *
 * @param ctx
 * @return
 */
private static byte[] getSigningKeyCertificate(Context ctx) {
    try {
        PackageManager pm = ctx.getPackageManager();
        String packageName = ctx.getPackageName();
        int flags = PackageManager.GET_SIGNATURES;
        PackageInfo packageInfo = pm.getPackageInfo(packageName, flags);
        Signature[] signatures = packageInfo.signatures;

        if (signatures != null && signatures.length >= 1) {
            //takes just the first signature, TODO: handle multi signed apks
            byte[] cert = signatures[0].toByteArray();
            InputStream input = new ByteArrayInputStream(cert);
            CertificateFactory cf = CertificateFactory.getInstance("X509");
            X509Certificate c = (X509Certificate) cf.generateCertificate(input);
            return c.getEncoded();
        }
    } catch (Exception e) {
        Log.w(TAG, e);
    }
    return null;
}
 
Example 2
Project: DroidPlugin   File: IPluginManagerImpl.java   View Source Code Vote up 6 votes
@Override
public PackageInfo getPackageInfo(String packageName, int flags) throws RemoteException {
    waitForReadyInner();
    try {
        String pkg = getAndCheckCallingPkg(packageName);
        if (pkg != null && !TextUtils.equals(packageName, mContext.getPackageName())) {
            enforcePluginFileExists();
            PluginPackageParser parser = mPluginCache.get(pkg);
            if (parser != null) {
                PackageInfo packageInfo = parser.getPackageInfo(flags);
                if (packageInfo != null && (flags & PackageManager.GET_SIGNATURES) != 0 && packageInfo.signatures == null) {
                    packageInfo.signatures = mSignatureCache.get(packageName);
                }
                return packageInfo;
            }
        }
    } catch (Exception e) {
        handleException(e);
    }
    return null;
}
 
Example 3
Project: container   File: VPackageManagerService.java   View Source Code Vote up 6 votes
@Override
public PackageInfo getPackageInfo(String packageName, int flags, int userId) {
	synchronized (mPackages) {
		PackageParser.Package pkg = mPackages.get(packageName);
		if (pkg != null) {
			AppSetting setting = (AppSetting) pkg.mExtras;
			if ((flags & PackageManager.GET_SIGNATURES) != 0 && pkg.mSignatures == null) {
				if (pkg.mAppMetaData != null && pkg.mAppMetaData.containsKey(Constants.FEATURE_FAKE_SIGNATURE)) {
					String sig = pkg.mAppMetaData.getString("fake-signature");
					if (sig != null) {
						pkg.mSignatures = new Signature[] {new Signature(sig)};
					}
				} else {
					PackageParserCompat.collectCertificates(setting.parser, pkg, PackageParser.PARSE_IS_SYSTEM);
				}
			}
			PackageInfo packageInfo = PackageParserCompat.generatePackageInfo(pkg, flags,
					getFirstInstallTime(pkg), getLastInstallTime(pkg));
			if (packageInfo != null) {
				ComponentFixer.fixApplicationInfo(setting, packageInfo.applicationInfo, userId);
				return packageInfo;
			}
		}
	}
	return null;
}
 
Example 4
Project: springreplugin   File: PackageUtils.java   View Source Code Vote up 5 votes
/**
 * 获取PackageInfo对象
 * 该方法解决了Android 2.x环境不能获取签名的系统问题,故可以“全面使用”
 */
public static PackageInfo getPackageArchiveInfo(PackageManager pm, String pkgFilePath, int flags) {
    PackageInfo info = null;
    try {
        info = pm.getPackageArchiveInfo(pkgFilePath, flags);
    } catch (Throwable e) {
        if (LogDebug.LOG) {
            e.printStackTrace();
        }
    }

    if (info == null) {
        return null;
    }

    // Android 2.x的系统通过常规手段(pm.getPackageArchiveInfo)时会返回null,只能通过底层实现
    // 判断依据:1、想要签名;2、没拿到签名;3、Android 4.0以前
    if ((flags & PackageManager.GET_SIGNATURES) != 0) {
        if (info.signatures == null) {
            if (Build.VERSION.SDK_INT < 14) {
                info.signatures = getPackageArchiveSignaturesInfoAndroid2x(pkgFilePath);
            }
        }
    }

    return info;
}
 
Example 5
Project: springreplugin   File: PluginManagerServer.java   View Source Code Vote up 4 votes
private PluginInfo installLocked(String path) {
    final boolean verifySignEnable = RePlugin.getConfig().getVerifySign();
    final int flags = verifySignEnable ? PackageManager.GET_META_DATA | PackageManager.GET_SIGNATURES : PackageManager.GET_META_DATA;

    // 1. 读取APK内容
    PackageInfo pi = mContext.getPackageManager().getPackageArchiveInfo(path, flags);
    if (pi == null) {
        if (LogDebug.LOG) {
            LogDebug.e(TAG, "installLocked: Not a valid apk. path=" + path);
        }

        RePlugin.getConfig().getEventCallbacks().onInstallPluginFailed(path, RePluginEventCallbacks.InstallResult.READ_PKG_INFO_FAIL);
        return null;
    }

    // 2. 校验插件签名
    if (verifySignEnable) {
        if (!verifySignature(pi, path)) {
            return null;
        }
    }

    // 3. 解析出名字和三元组
    PluginInfo instPli = PluginInfo.parseFromPackageInfo(pi, path);
    if (LogDebug.LOG) {
        LogDebug.i(TAG, "installLocked: Info=" + instPli);
    }
    instPli.setType(PluginInfo.TYPE_NOT_INSTALL);

    // 若要安装的插件版本小于或等于当前版本,则安装失败
    // NOTE 绝大多数情况下,应该在调用RePlugin.install方法前,根据云端回传的信息来判断,以防止下载旧插件,浪费流量
    // NOTE 这里仅做双保险,或通过特殊渠道安装时会有用

    // 注意:这里必须用“非Clone过的”PluginInfo,因为要修改里面的内容
    PluginInfo curPli = RePluginOS.getPlugin(instPli.getName(), false);
    if (curPli != null) {
        if (LogDebug.LOG) {
            LogDebug.i(TAG, "installLocked: Has installed plugin. current=" + curPli);
        }

        // 版本较老?直接返回
        final int checkResult = checkVersion(instPli, curPli);
        if (checkResult < 0) {
            RePlugin.getConfig().getEventCallbacks().onInstallPluginFailed(path, RePluginEventCallbacks.InstallResult.VERIFY_VER_FAIL);
            return null;
        } else if (checkResult == 0){
            instPli.setIsPendingCover(true);
        }
    }

    // 4. 将合法的APK改名后,移动(或复制,见RePluginConfig.isMoveFileWhenInstalling)到新位置
    // 注意:不能和p-n的最终释放位置相同,因为管理方式不一样
    if (!copyOrMoveApk(path, instPli)) {
        RePlugin.getConfig().getEventCallbacks().onInstallPluginFailed(path, RePluginEventCallbacks.InstallResult.COPY_APK_FAIL);
        return null;
    }

    // 5. 从插件中释放 So 文件
    PluginNativeLibsHelper.install(instPli.getPath(), instPli.getNativeLibsDir());

    // 6. 若已经安装旧版本插件,则尝试更新插件信息,否则直接加入到列表中
    if (curPli != null) {
        updateOrLater(curPli, instPli);
    } else {
        mList.add(instPli);
    }

    // 7. 保存插件信息到文件中,下次可直接使用
    mList.save(mContext);

    return instPli;
}