我从X509证书中提取2.5.29.32证书策略OID字节。 (不使用BouncyCastle)
bytes = _cert.getExtensionValue(“2.5.29.32”);
我得到的原始字节是4,15,48,13,48,11,6,9,96,-122,72,1,101,2,1,11,42
在DER解码(类型= 4,大小= 15)之后,我有:48,13,48,11,6,9,96,-122,72,1,101,2,1,11,42
我所追求的是:96,-122,72,1,101,2,1,11,42(2.16.840.1.101.2.1.11.42)
字节数是多少:48,13,48,11,6,9?
如果有人对我写的代码感兴趣,那么......
private static byte CONSTRUCTED_SEQUENCE_TAG = 48;
private static byte OID_CODE = 6;
private static String extractPolicyOid(X509Certificate pCert) throws IOException {
byte[] _bytes = pCert.getExtensionValue("2.5.29.32");
if (_bytes == null) {
return null;
}
DerValue _der = new DerValue(new ByteArrayInputStream(_bytes));
_bytes = _der.getOctetString();
/*
* Skip header goo
*/
int _idx = 0;
while (_idx < _bytes.length) {
if (_bytes[_idx] == CONSTRUCTED_SEQUENCE_TAG) {
_idx += 2;
} else if (_bytes[_idx] == OID_CODE) {
_idx += 2;
} else {
break;
}
}
/*
* Create string version of OID
*/
StringBuffer _sb = new StringBuffer();
byte _byte = _bytes[_idx++];
_sb.append(_byte / 40);
_sb.append(".");
_sb.append(_byte % 40);
int _accum = 0;
for (; _idx < _bytes.length; _idx++) {
_byte = _bytes[_idx];
if (_byte << ~7 < 0) {
_accum = (_byte & 0x7f) << 7;
} else {
_accum += _byte;
_sb.append(".");
_sb.append(_accum);
_accum = 0;
}
}
return _sb.toString();
}