问题场景:
最近在嵌入式终端做app tar包的认证和非对称解密过程,遇到了认证的问题,写段小记以便做记录。若有错误,还请各位大佬指正。
以下内容只描述问题,不讲解tar指令。
问题描述
客户端对app tar包进行了签名,并把签名和证书加在tar的最后。
嵌入式终端 认证tar包需要依次执行以下流程
tar xf app_1701856676374.tar sig.dat ydd.crt
tar --delete -f app_1701856676374.tar sig.dat ydd.crt
openssl x509 -in ydd.crt -pubkey -out ydd.crt.pub
openssl dgst -sha1 -verify ydd.crt.pub -signature sig.dat app_1701856676374.tar
第二步是为了将sig.dat 和 ydd.crt这两个文件从tar包删掉。(因为tar只做打包不做压缩,所以可以实现单独删除个别文件的操作)
在执行第二步的时候,因为嵌入式终端的tar的命令集比较老,不支持 ‘–delete’这个参数。所第二步执行失败。
tar --delete -f app_1701856676374.tar sig.dat ydd.crt
tar: unrecognized option '--delete'
BusyBox v1.22.1 (2023-07-05 15:18:37 CST) multi-call binary.
Usage: tar -[cxtzJjahmvO] [-X FILE] [-T FILE] [-f TARFILE] [-C DIR] [FILE]...
Create, extract, or list files from a tar file
Operation:
c Create
x Extract
t List
f Name of TARFILE ('-' for stdin/out)
C Change to DIR before operation
v Verbose
z (De)compress using gzip
J (De)compress using xz
j (De)compress using bzip2
a (De)compress using lzma
O Extract to stdout
h Follow symlinks
m Don't restore mtime
exclude File to exclude
X File with names to exclude
T File with names to include
于是就萌生一个想法—曲线救国
把app tar包整体解压 再重新打包,重新打包的时候不带上sig.dat 和 ydd.crt,这不是也能实现第二步嘛,
但是在执行最后一步验签的时候失败了
原因分析:
tar包的数据里面包含主机名/用户名
我用UEStudio软件打开app tar包,查看里面的数据,发现有这么一段
发现tar包里面的数据包含,打包者的主机名/用户名(在windows下,在linux下是用户名)。
重新打包的话tar包的这个名字就会变,tar包的数据发生变化了,也就直接导致了第四步验签不过。
解决方案:
open read write close
复杂的问题往往只需要朴素的解决方式。
因为tar包只是打包,并没有压缩,可以直接read tar包里面的数据,识别你想删除的文件名,这里我的是sig.dat
将文件名包括之后的数据直接裁剪掉,在write回去,也能见解达到效果。
以上就是今天的全部内容了,谢谢各位的观看!