1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
#include <string.h>
#include <openssl/x509v3.h>
int X509V3_EXT_info_access(const X509V3_EXT_METHOD *method, void *ext_str) { printf("精细的数据: NID_info_access\n"); AUTHORITY_INFO_ACCESS *ainfo = ext_str; STACK_OF(CONF_VALUE) *result = NULL;
for (int i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) { ACCESS_DESCRIPTION *desc = sk_ACCESS_DESCRIPTION_value(ainfo, i); result = i2v_GENERAL_NAME((X509V3_EXT_METHOD *)method, desc->location, result); } if (result == NULL) { printf("ERROR: [%s][%s][%d]\n", __FILE__, __func__, __LINE__); return -1; }
for (int i = 0; i < sk_CONF_VALUE_num(result); i++) { CONF_VALUE *nval = sk_CONF_VALUE_value(result, i); if (NULL != nval && NULL != nval->name && NULL != nval->value) { printf("Name=[%s], Value=[%s]\n", nval->name, nval->value); } } sk_CONF_VALUE_pop_free(result, X509V3_conf_free); return 0; }
int X509V3_EXT_subject_alt_name(const X509V3_EXT_METHOD *method, void *ext_str) { int i; GENERAL_NAMES *gens = ext_str; STACK_OF(CONF_VALUE) *result = NULL;
for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { GENERAL_NAME *gen = sk_GENERAL_NAME_value(gens, i); result = i2v_GENERAL_NAME((X509V3_EXT_METHOD *)method, gen, result); }
if(NULL == result) { printf("ERROR: [%s][%s][%d]\n", __FILE__, __func__, __LINE__); return -1; }
printf("精细的数据: NID_subject_alt_name\n"); for (i = 0; i < sk_CONF_VALUE_num(result); i++) { CONF_VALUE *nval = sk_CONF_VALUE_value(result, i); if (NULL != nval && NULL != nval->name && NULL != nval->value) { printf("Name=[%s], Value=[%s]\n", nval->name, nval->value); } }
sk_CONF_VALUE_pop_free(result, X509V3_conf_free); return 0; }
int X509V3_EXT_detial_print(int nid, X509_EXTENSION *x509_ext) { ASN1_OCTET_STRING *extoct = X509_EXTENSION_get_data(x509_ext); const unsigned char *p = ASN1_STRING_get0_data(extoct); int extlen = ASN1_STRING_length(extoct); const X509V3_EXT_METHOD *method = method = X509V3_EXT_get(x509_ext); void *ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it)); if(NULL == method || NULL == ext_str) { printf("ERROR: [%s][%s][%d]\n", __FILE__, __func__, __LINE__); return -1; }
switch(nid) { case NID_subject_alt_name: X509V3_EXT_subject_alt_name(method, ext_str); break;
case NID_info_access: X509V3_EXT_info_access(method, ext_str); break; } ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it)); return 0; }
void GET_X509_EXTENSION(X509 *x509) { int loop = X509_get_ext_count(x509); printf("X509 证书扩展项个数 [%d]\n", loop); for(int i = 0; i < loop; i++) { X509_EXTENSION *x509_ext = X509_get_ext(x509, i); if(NULL == x509_ext) { printf("FAIL: X509_get_ext \n"); exit(-1); }
ASN1_OBJECT *obj = X509_EXTENSION_get_object(x509_ext);
BIO *ext_bio = BIO_new(BIO_s_mem()); X509V3_EXT_print(ext_bio, x509_ext, 0, 0);
BUF_MEM *bptr = NULL; BIO_get_mem_ptr(ext_bio, &bptr);
unsigned nid = OBJ_obj2nid(obj); if (nid == NID_undef) { printf("UnKnown nid[%d]\n", nid); return; } ASN1_OBJECT_free(obj); printf("[%02d] nid=[%04d] TYPE=[%s]\n", i, nid, OBJ_nid2ln(nid));
{ printf(" "); char buf[4096]; memset(buf, 0, 4096); memcpy(buf, bptr->data, bptr->length); printf("len=[%zu], value=[%s]\n", bptr->length, buf); } BIO_free(ext_bio); X509V3_EXT_detial_print(nid, x509_ext); printf("\n");
} }
int X509_Public_Print(X509 *x509) { EVP_PKEY *pkey = X509_get0_pubkey(x509); if(NULL == pkey) { printf("FAIL X509_get0_pubkey\n"); exit(1); }
char Algorithm[80] = {0}; int Len_Algorithm = sizeof(Algorithm); ASN1_OBJECT *xpoid; X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x509); X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey); Len_Algorithm = i2t_ASN1_OBJECT(Algorithm, Len_Algorithm, xpoid); printf("公钥算法:[%s]\n", Algorithm);
int keyid = EVP_PKEY_id(pkey); printf("证书类型:%d\n", keyid); if (keyid == EVP_PKEY_EC) { struct ec_key_st *eckey = EVP_PKEY_get0_EC_KEY(pkey); if(NULL == eckey) { printf("ERROR EVP_PKEY_get0_EC_KEY\n"); exit(1); }
const EC_GROUP *group = EC_KEY_get0_group(eckey); printf("加密级别:%d bit\n", EC_GROUP_order_bits(group));
size_t publen = 0; unsigned char *pub = NULL; publen = EC_KEY_key2buf(eckey, EC_KEY_get_conv_form(eckey), &pub, NULL); if (publen == 0) { printf("ERROR: EC_KEY_key2buf\n"); } printf("公钥长度=[%zd]\n", publen); for(int i=0; i<publen; i++) { printf("%02X ", pub[i]); if(i%8 == 7) printf(" "); if(i%32 == 31) printf("\n"); } printf("\n"); OPENSSL_free(pub); } else if(keyid == NID_rsaEncryption) { const RSA *rsa = EVP_PKEY_get0_RSA(pkey); if (rsa != NULL) printf("加密级别:%d bit \n", BN_num_bits(RSA_get0_n(rsa)));
int buflen = BN_num_bytes(RSA_get0_n(rsa)) + 1; printf("RSA公钥长度:%d Byte\n", buflen); unsigned char *buf = OPENSSL_malloc(buflen); if (buf == NULL) { printf("ERROR: OPENSSL_malloc\n"); return -1; }
buf[0]=0; buflen = BN_bn2bin(RSA_get0_n(rsa), buf+1); if (buf[1] & 0x80) buflen++;
printf("len=%d\n", buflen);
for(int i=0; i<buflen; i++) { printf("%02X ", (unsigned char)buf[i]); if(i%8==7) printf(" "); if(i%32==31) printf("\n"); } printf("\n"); OPENSSL_free(buf); } EVP_PKEY_free(pkey);
return 0; }
int main (int num, char**list) { if(num <2) { printf("need a DER file\n"); exit(-1); } printf("File name %s\n", list[1]);
BIO *bio_file = BIO_new_file(list[1], "rb"); if(NULL == bio_file) { printf("FAIL: BIO_new_file \n"); exit(-1); }
X509 *x509 = d2i_X509_bio(bio_file, NULL); if(NULL == x509) { printf("FAIL: d2i_X509_bio \n"); exit(-1); }
printf("1. Certificate Version=[%ld]\n", X509_get_version(x509));
ASN1_INTEGER* serial = X509_get_serialNumber(x509); char buf[2048]; memset(buf, 0, 2048); memcpy(buf, ASN1_STRING_get0_data(serial), ASN1_STRING_length(serial) ); printf("2. Certificate Serial len=[%d] Serial:", ASN1_STRING_length(serial)); for(int i=0; i < ASN1_STRING_length(serial); i++) { printf("%02X", (unsigned char)buf[i]); } printf("\n");
const X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x509); const int sig_nid = OBJ_obj2nid(tsig_alg->algorithm); if (sig_nid != NID_undef) { printf("3. signature=[%s][%s]\n", OBJ_nid2sn(sig_nid), OBJ_nid2ln(sig_nid)); }
const X509_NAME *X509_issuer_name = X509_get_issuer_name(x509); char *issuer_name = X509_NAME_oneline(X509_issuer_name, NULL, 0); printf("4. issuer_name=[%s]\n", issuer_name); OPENSSL_free(issuer_name);
const ASN1_TIME *ASN1_time; struct tm Certificate_time; printf("5. Certificate time\n"); ASN1_time = X509_get0_notBefore(x509); if(ASN1_TIME_to_tm(ASN1_time, &Certificate_time) > 0) { printf("%4sFrom: %s"," ", asctime(&Certificate_time)); }
ASN1_time = X509_get0_notAfter(x509); if(ASN1_TIME_to_tm(ASN1_time, &Certificate_time) > 0) { printf("%4sTo : %s", " ",asctime(&Certificate_time)); }
const X509_NAME *X509_subject_name = X509_get_subject_name(x509); char *subject_name = X509_NAME_oneline(X509_subject_name, NULL, 0); int subject_number = X509_NAME_entry_count(X509_subject_name); printf("6. subject num=[%d][%s]\n", subject_number, subject_name); OPENSSL_free(subject_name);
printf("7. Subject PublicKey\n"); X509_Public_Print(x509);
printf("扩展学习1:如何精细的获取 证书接受者的数据?\n"); { X509_NAME *subj = X509_get_subject_name(x509); printf("subject_name_num=[%d]\n", X509_NAME_entry_count(subj)); for (int i = 0; i < X509_NAME_entry_count(subj); i++) { X509_NAME_ENTRY *e = X509_NAME_get_entry(subj, i); ASN1_STRING *type = (ASN1_STRING *)X509_NAME_ENTRY_get_object(e); ASN1_STRING *value = X509_NAME_ENTRY_get_data(e); const unsigned char *str1 = ASN1_STRING_get0_data(type); const unsigned char *str2 = ASN1_STRING_get0_data(value); printf("[%02d][%s]=[%s]\n", i, str1, str2); } }
printf("扩展学习2:如何精细的获取 证书签发着的数据?\n"); { X509_NAME *subj = X509_get_issuer_name(x509); printf("subject_name_num=[%d]\n", X509_NAME_entry_count(subj)); for (int i = 0; i < X509_NAME_entry_count(subj); i++) { X509_NAME_ENTRY *e = X509_NAME_get_entry(subj, i); ASN1_STRING *type = (ASN1_STRING *)X509_NAME_ENTRY_get_object(e); ASN1_STRING *value = X509_NAME_ENTRY_get_data(e); const unsigned char *str1 = ASN1_STRING_get0_data(type); const unsigned char *str2 = ASN1_STRING_get0_data(value); printf("[%02d][%s]=[%s]\n", i, str1, str2); } }
printf("X509 扩展处理 \n"); GET_X509_EXTENSION(x509);
BIO_free_all(bio_file); X509_free(x509); printf("********************************************************\n");
}
|