如何实现我自己的功能来验证 SM2 签名?
How to implement my own function to verify a SM2 signature?
在我的工作中,我必须使用中文SM2加密算法来签署一个pdf并验证签名,我选择了itext来帮助我work.But因为我是第一次使用itext lib,我'好陌生啊
签名部分我用的是外部签名,把一个SM2签名放到一个pdf的签名里dictionary.I在网上可以找到很多关于this.But验证签名的代码,我找不到什么帮助(itext 不支持 SM2 算法,这就是为什么我不能使用标准验证方法并且必须实现我自己的函数来验证 SM2 签名)。我的意思是我不知道如何获得签名的来源数据,就像我在签名部分使用 "GetRangeStream" 所做的那样,我不知道如何从 pdf 的表单字段中读取 SM2 签名。
谁能帮帮我?非常感谢。
您需要的代码显然类似于 iText 方法 AcroFields.VerifySignature
在创建表示相关签名的 PdfPKCS7
对象时执行的代码。由于 iText 是开源的,您可以根据需要简单地复制该代码!
例如
I don't know how to read the SM2 signature from the pdf's form field.
AcroFields.VerifySignature
开始是这样的:
virtual public PdfPKCS7 VerifySignature(String name) {
PdfDictionary v = GetSignatureDictionary(name);
if (v == null)
return null;
PdfName sub = v.GetAsName(PdfName.SUBFILTER);
PdfString contents = v.GetAsString(PdfName.CONTENTS);
PdfPKCS7 pk = null;
if (sub.Equals(PdfName.ADBE_X509_RSA_SHA1)) {
PdfString cert = v.GetAsString(PdfName.CERT);
if (cert == null)
cert = v.GetAsArray(PdfName.CERT).GetAsString(0);
pk = new PdfPKCS7(contents.GetOriginalBytes(), cert.GetBytes());
}
else
pk = new PdfPKCS7(contents.GetOriginalBytes(), sub);
因此,提取嵌入签名对象的基本代码如下
AcroFields acroFields = reader.AcroFields;
PdfDictionary v = acroFields.GetSignatureDictionary(name);
if (v != null) {
PdfString contents = v.GetAsString(PdfName.CONTENTS);
byte[] embeddedSignatureObjectBytes = contents.GetOriginalBytes();
[... process embeddedSignatureObjectBytes ...]
}
注意:由于 Contents 字符串用零填充,实际签名对象后的 embeddedSignatureObjectBytes
字节将包含 0x00 字节的尾部。
I don't know how to get the signed origin data
AcroFields.VerifySignature
继续这样:
UpdateByteRange(pk, v);
AcroFields.UpdateByteRange
是这样实现的:
private void UpdateByteRange(PdfPKCS7 pkcs7, PdfDictionary v) {
PdfArray b = v.GetAsArray(PdfName.BYTERANGE);
RandomAccessFileOrArray rf = reader.SafeFile;
Stream rg = null;
try {
rg = new RASInputStream(new RandomAccessSourceFactory().CreateRanged(rf.CreateSourceView(), b.AsLongArray()));
byte[] buf = new byte[8192];
int rd;
while ((rd = rg.Read(buf, 0, buf.Length)) > 0) {
pkcs7.Update(buf, 0, rd);
}
} finally {
if (rg != null) rg.Close();
}
}
因此,读取签名文档字节的基本代码如下
AcroFields acroFields = reader.AcroFields;
PdfDictionary v = acroFields.GetSignatureDictionary(name);
if (v != null) {
PdfArray b = v.GetAsArray(PdfName.BYTERANGE);
RandomAccessFileOrArray rf = reader.SafeFile;
Stream rg = null;
try {
rg = new RASInputStream(new RandomAccessSourceFactory().CreateRanged(rf.CreateSourceView(), b.AsLongArray()));
[... process the signed data in the Stream rg ...]
} finally {
if (rg != null) rg.Close();
}
}
在我的工作中,我必须使用中文SM2加密算法来签署一个pdf并验证签名,我选择了itext来帮助我work.But因为我是第一次使用itext lib,我'好陌生啊
签名部分我用的是外部签名,把一个SM2签名放到一个pdf的签名里dictionary.I在网上可以找到很多关于this.But验证签名的代码,我找不到什么帮助(itext 不支持 SM2 算法,这就是为什么我不能使用标准验证方法并且必须实现我自己的函数来验证 SM2 签名)。我的意思是我不知道如何获得签名的来源数据,就像我在签名部分使用 "GetRangeStream" 所做的那样,我不知道如何从 pdf 的表单字段中读取 SM2 签名。
谁能帮帮我?非常感谢。
您需要的代码显然类似于 iText 方法 AcroFields.VerifySignature
在创建表示相关签名的 PdfPKCS7
对象时执行的代码。由于 iText 是开源的,您可以根据需要简单地复制该代码!
例如
I don't know how to read the SM2 signature from the pdf's form field.
AcroFields.VerifySignature
开始是这样的:
virtual public PdfPKCS7 VerifySignature(String name) {
PdfDictionary v = GetSignatureDictionary(name);
if (v == null)
return null;
PdfName sub = v.GetAsName(PdfName.SUBFILTER);
PdfString contents = v.GetAsString(PdfName.CONTENTS);
PdfPKCS7 pk = null;
if (sub.Equals(PdfName.ADBE_X509_RSA_SHA1)) {
PdfString cert = v.GetAsString(PdfName.CERT);
if (cert == null)
cert = v.GetAsArray(PdfName.CERT).GetAsString(0);
pk = new PdfPKCS7(contents.GetOriginalBytes(), cert.GetBytes());
}
else
pk = new PdfPKCS7(contents.GetOriginalBytes(), sub);
因此,提取嵌入签名对象的基本代码如下
AcroFields acroFields = reader.AcroFields;
PdfDictionary v = acroFields.GetSignatureDictionary(name);
if (v != null) {
PdfString contents = v.GetAsString(PdfName.CONTENTS);
byte[] embeddedSignatureObjectBytes = contents.GetOriginalBytes();
[... process embeddedSignatureObjectBytes ...]
}
注意:由于 Contents 字符串用零填充,实际签名对象后的 embeddedSignatureObjectBytes
字节将包含 0x00 字节的尾部。
I don't know how to get the signed origin data
AcroFields.VerifySignature
继续这样:
UpdateByteRange(pk, v);
AcroFields.UpdateByteRange
是这样实现的:
private void UpdateByteRange(PdfPKCS7 pkcs7, PdfDictionary v) {
PdfArray b = v.GetAsArray(PdfName.BYTERANGE);
RandomAccessFileOrArray rf = reader.SafeFile;
Stream rg = null;
try {
rg = new RASInputStream(new RandomAccessSourceFactory().CreateRanged(rf.CreateSourceView(), b.AsLongArray()));
byte[] buf = new byte[8192];
int rd;
while ((rd = rg.Read(buf, 0, buf.Length)) > 0) {
pkcs7.Update(buf, 0, rd);
}
} finally {
if (rg != null) rg.Close();
}
}
因此,读取签名文档字节的基本代码如下
AcroFields acroFields = reader.AcroFields;
PdfDictionary v = acroFields.GetSignatureDictionary(name);
if (v != null) {
PdfArray b = v.GetAsArray(PdfName.BYTERANGE);
RandomAccessFileOrArray rf = reader.SafeFile;
Stream rg = null;
try {
rg = new RASInputStream(new RandomAccessSourceFactory().CreateRanged(rf.CreateSourceView(), b.AsLongArray()));
[... process the signed data in the Stream rg ...]
} finally {
if (rg != null) rg.Close();
}
}