Sign the mobileconfig file with Java code

专题:基于IOS上MDM技术相关资料整理及汇总 Sign the mobileconfig file with Java code

 

Sign the mobileconfig file with Java code:通过java实现mobileconfig描述配置文件的签名和认证

 

导读:我们以前已经提供了MDM全套的过程解析的资料,可以参考:基于IOS上MDM技术相关资料整理及汇总。 我们在安装了mobileconfig描述配置文件后,我们发现我们的文件显示的是“Not Verified”,我们希望变成绿色的“Verified”,我们这里参考了:http://www.rootmanager.com/iphone- ota-configuration/iphone-ota-setup-with-signed-mobileconfig.html 这篇文章来实现mobileconfig文件的签名和认证,代码使用java实现。下面详细介绍:

 

一、需要的如下文件:
1、mbaike.mobileconfig(尚未签名的描述文件)
2、mbaike.crt(https服务器的证书)
3、mbaike.key(https服务器的证书对应的密钥)
4、mbaikenopass.key(https服务器的证书对应的密钥转化成无需密码的密钥,文件生成详见:本文第三点)
5、ca-bundle.pem(startssl提供的认证机构的证书文件)
6、signed.mobileconfig(输出的签名后的描述文件)

 

二、openssl签名:
String content = MdmUtils.readConfig().replace("#deviceId#", deviceId);
boolean createSuccess = createMobileConfigFile(oldPath,content);
System.out.println("———————-生成未签名的mobileconfig文件 end———————");
//签名和认证过程
if(createSuccess){
     System.out.println("———————-签名mobileconfig文件 start———————");
     String oldCmd = "openssl smime -sign -in {0} -out {1} -signer {2} -inkey {3} -certfile {4} -outform der -nodetach";
     String newCmd = MessageFormat.format(oldCmd,oldPath,newPath,crtPath,keyPath,pemPath);
     Runtime.getRuntime().exec(newCmd);
     System.out.println("———————-签名mobileconfig文件 end———————");
 }
备注:我们在替换了mobileconfig模板文件中的“Check In URL”和“Server URL”的动态参数后,生成了未签名的mbaike.mobileconfig文件,然后通过openssl签名,其实就是通过java代码调用 linux的shell命令,可以参考这样的命令:
openssl smime -sign -in unsigned.mobileconfig -out signed.mobileconfig -signer mbaike.crt -inkey mbaike.key -certfile ca-bundle.pem -outform der -nodetach

 

三、mbaikenopass.key文件的生成:
openssl rsa -in mbaike.key -out mbaikenopass.key
备注:我们使用的书linux上的openssl命令完成的。

 

四、签名后的mobileconfig文件的下载:
可以参考下面的代码:
System.out.println("———————-下载签名后的mobileconfig文件 start———————");
response.setHeader("content-type", "application/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
String configTitle = "MbaikeApp_"+deviceId;
response.setHeader("Content-Disposition", "attachment; filename=" + configTitle + ".mobileconfig");
/**获取配置文件动态组装参数**/
System.out.println("———————-下载签名后的mobileconfig文件 end———————");
java.io.File f = new java.io.File(newPath);
while(true){
     if(f.exists() && f.length()>0){
          java.io.FileInputStream fis = new java.io.FileInputStream(newPath);
          java.io.OutputStream os = response.getOutputStream();
          byte[] b = new byte[1024];
          int i = 0;
          while ((i = fis.read(b)) > 0) {
               os.write(b, 0, i);
          }
          fis.close();
          os.flush();
          os.close();
          break;
     }else{
          continue;
      }
}
备注:上面的代码我用了while(true)来处理,因为java调用openssl签名会存在一定的延时,不能及时下载,所以这里暂时这样处理。


原创文章如转载,请注明:转载自:移动互联百科
本文固定链接:http://www.mbaike.net/ios/1747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值