pk7print.c
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 919 行 · 第 1/2 页
C
919 行
while ((entry = crl->entries[iv]) != NULL) { fprintf(out, "%sentry[%d].", m, iv); sv_PrintInteger(out, &(entry->serialNumber), "serialNumber="); fprintf(out, "%sentry[%d].", m, iv); sv_PrintUTCTime(out, &(entry->revocationDate), "revocationDate="); sprintf(om, "%sentry[%d].signedCRLEntriesExtensions.", m, iv++); sv_PrintExtensions(out, entry->extensions, om); } } sprintf(om, "%ssignedCRLEntriesExtensions.", m); sv_PrintExtensions(out, crl->extensions, om);}intsv_PrintCertificate(FILE *out, SECItem *der, char *m, int level){ PRArenaPool *arena = NULL; CERTCertificate *c; int rv; int iv; char mm[200]; /* Decode certificate */ c = (CERTCertificate*) PORT_ZAlloc(sizeof(CERTCertificate)); if (!c) return PORT_GetError(); arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); if (!arena) return SEC_ERROR_NO_MEMORY; rv = SEC_ASN1DecodeItem(arena, c, CERT_CertificateTemplate, der); if (rv) { PORT_FreeArena(arena, PR_FALSE); return rv; } /* Pretty print it out */ iv = DER_GetInteger(&c->version); fprintf(out, "%sversion=%d (0x%x)\n", m, iv + 1, iv); sprintf(mm, "%sserialNumber=", m); sv_PrintInteger(out, &c->serialNumber, mm); sprintf(mm, "%ssignatureAlgorithm=", m); sv_PrintAlgorithmID(out, &c->signature, mm); sprintf(mm, "%sissuerName=", m); sv_PrintName(out, &c->issuer, mm); sprintf(mm, "%svalidity.", m); sv_PrintValidity(out, &c->validity, mm); sprintf(mm, "%ssubject=", m); sv_PrintName(out, &c->subject, mm); sprintf(mm, "%ssubjectPublicKeyInfo", m); rv = sv_PrintSubjectPublicKeyInfo(out, arena, &c->subjectPublicKeyInfo, mm); if (rv) { PORT_FreeArena(arena, PR_FALSE); return rv; } sprintf(mm, "%ssignedExtensions.", m); sv_PrintExtensions(out, c->extensions, mm); PORT_FreeArena(arena, PR_FALSE); return 0;}intsv_PrintSignedData(FILE *out, SECItem *der, char *m, SECU_PPFunc inner){ PRArenaPool *arena = NULL; CERTSignedData *sd; int rv; /* Strip off the signature */ sd = (CERTSignedData*) PORT_ZAlloc(sizeof(CERTSignedData)); if (!sd) return PORT_GetError(); arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); if (!arena) return SEC_ERROR_NO_MEMORY; rv = SEC_ASN1DecodeItem(arena, sd, CERT_SignedDataTemplate, der); if (rv) { PORT_FreeArena(arena, PR_FALSE); return rv; }/* fprintf(out, "%s:\n", m); */ PORT_Strcat(m, "data."); rv = (*inner)(out, &sd->data, m, 0); if (rv) { PORT_FreeArena(arena, PR_FALSE); return rv; } m[PORT_Strlen(m) - 5] = 0; fprintf(out, m); sv_PrintAlgorithmID(out, &sd->signatureAlgorithm, "signatureAlgorithm="); DER_ConvertBitString(&sd->signature); fprintf(out, m); sv_PrintAsHex(out, &sd->signature, "signature="); PORT_FreeArena(arena, PR_FALSE); return 0;}/*** secu_PrintPKCS7Signed** Pretty print a PKCS7 signed data type (up to version 1).*/intsv_PrintPKCS7Signed(FILE *out, SEC_PKCS7SignedData *src){ SECAlgorithmID *digAlg; /* digest algorithms */ SECItem *aCert; /* certificate */ CERTSignedCrl *aCrl; /* certificate revocation list */ SEC_PKCS7SignerInfo *sigInfo; /* signer information */ int rv, iv; char om[120]; sv_PrintInteger(out, &(src->version), "pkcs7.version="); /* Parse and list digest algorithms (if any) */ if (src->digestAlgorithms != NULL) { iv = 0; while (src->digestAlgorithms[iv] != NULL) iv++; fprintf(out, "pkcs7.digestAlgorithmListLength=%d\n", iv); iv = 0; while ((digAlg = src->digestAlgorithms[iv]) != NULL) { sprintf(om, "pkcs7.digestAlgorithm[%d]=", iv++); sv_PrintAlgorithmID(out, digAlg, om); } } /* Now for the content */ rv = sv_PrintPKCS7ContentInfo(out, &(src->contentInfo), "pkcs7.contentInformation="); if (rv != 0) return rv; /* Parse and list certificates (if any) */ if (src->rawCerts != NULL) { iv = 0; while (src->rawCerts[iv] != NULL) iv++; fprintf(out, "pkcs7.certificateListLength=%d\n", iv); iv = 0; while ((aCert = src->rawCerts[iv]) != NULL) { sprintf(om, "certificate[%d].", iv++); rv = sv_PrintSignedData(out, aCert, om, sv_PrintCertificate); if (rv) return rv; } } /* Parse and list CRL's (if any) */ if (src->crls != NULL) { iv = 0; while (src->crls[iv] != NULL) iv++; fprintf(out, "pkcs7.signedRevocationLists=%d\n", iv); iv = 0; while ((aCrl = src->crls[iv]) != NULL) { sprintf(om, "signedRevocationList[%d].", iv); fprintf(out, om); sv_PrintAlgorithmID(out, &aCrl->signatureWrap.signatureAlgorithm, "signatureAlgorithm="); DER_ConvertBitString(&aCrl->signatureWrap.signature); fprintf(out, om); sv_PrintAsHex(out, &aCrl->signatureWrap.signature, "signature="); sprintf(om, "certificateRevocationList[%d].", iv); sv_PrintCRLInfo(out, &aCrl->crl, om); iv++; } } /* Parse and list signatures (if any) */ if (src->signerInfos != NULL) { iv = 0; while (src->signerInfos[iv] != NULL) iv++; fprintf(out, "pkcs7.signerInformationListLength=%d\n", iv); iv = 0; while ((sigInfo = src->signerInfos[iv]) != NULL) { sprintf(om, "signerInformation[%d].", iv++); sv_PrintSignerInfo(out, sigInfo, om); } } return 0;}#if 0/*** secu_PrintPKCS7Enveloped** Pretty print a PKCS7 enveloped data type (up to version 1).*/voidsecu_PrintPKCS7Enveloped(FILE *out, SEC_PKCS7EnvelopedData *src, char *m, int level){ SEC_PKCS7RecipientInfo *recInfo; /* pointer for signer information */ int iv; char om[100]; secu_Indent(out, level); fprintf(out, "%s:\n", m); sv_PrintInteger(out, &(src->version), "Version", level + 1); /* Parse and list recipients (this is not optional) */ if (src->recipientInfos != NULL) { secu_Indent(out, level + 1); fprintf(out, "Recipient Information List:\n"); iv = 0; while ((recInfo = src->recipientInfos[iv++]) != NULL) { sprintf(om, "Recipient Information (%x)", iv); secu_PrintRecipientInfo(out, recInfo, om, level + 2); } } secu_PrintPKCS7EncContent(out, &src->encContentInfo, "Encrypted Content Information", level + 1);}/*** secu_PrintPKCS7SignedEnveloped** Pretty print a PKCS7 singed and enveloped data type (up to version 1).*/intsecu_PrintPKCS7SignedAndEnveloped(FILE *out, SEC_PKCS7SignedAndEnvelopedData *src, char *m, int level){ SECAlgorithmID *digAlg; /* pointer for digest algorithms */ SECItem *aCert; /* pointer for certificate */ CERTSignedCrl *aCrl; /* pointer for certificate revocation list */ SEC_PKCS7SignerInfo *sigInfo; /* pointer for signer information */ SEC_PKCS7RecipientInfo *recInfo; /* pointer for recipient information */ int rv, iv; char om[100]; secu_Indent(out, level); fprintf(out, "%s:\n", m); sv_PrintInteger(out, &(src->version), "Version", level + 1); /* Parse and list recipients (this is not optional) */ if (src->recipientInfos != NULL) { secu_Indent(out, level + 1); fprintf(out, "Recipient Information List:\n"); iv = 0; while ((recInfo = src->recipientInfos[iv++]) != NULL) { sprintf(om, "Recipient Information (%x)", iv); secu_PrintRecipientInfo(out, recInfo, om, level + 2); } } /* Parse and list digest algorithms (if any) */ if (src->digestAlgorithms != NULL) { secu_Indent(out, level + 1); fprintf(out, "Digest Algorithm List:\n"); iv = 0; while ((digAlg = src->digestAlgorithms[iv++]) != NULL) { sprintf(om, "Digest Algorithm (%x)", iv); sv_PrintAlgorithmID(out, digAlg, om); } } secu_PrintPKCS7EncContent(out, &src->encContentInfo, "Encrypted Content Information", level + 1); /* Parse and list certificates (if any) */ if (src->rawCerts != NULL) { secu_Indent(out, level + 1); fprintf(out, "Certificate List:\n"); iv = 0; while ((aCert = src->rawCerts[iv++]) != NULL) { sprintf(om, "Certificate (%x)", iv); rv = SECU_PrintSignedData(out, aCert, om, level + 2, SECU_PrintCertificate); if (rv) return rv; } } /* Parse and list CRL's (if any) */ if (src->crls != NULL) { secu_Indent(out, level + 1); fprintf(out, "Signed Revocation Lists:\n"); iv = 0; while ((aCrl = src->crls[iv++]) != NULL) { sprintf(om, "Signed Revocation List (%x)", iv); secu_Indent(out, level + 2); fprintf(out, "%s:\n", om); sv_PrintAlgorithmID(out, &aCrl->signatureWrap.signatureAlgorithm, "Signature Algorithm"); DER_ConvertBitString(&aCrl->signatureWrap.signature); sv_PrintAsHex(out, &aCrl->signatureWrap.signature, "Signature", level+3); SECU_PrintCRLInfo(out, &aCrl->crl, "Certificate Revocation List", level + 3); } } /* Parse and list signatures (if any) */ if (src->signerInfos != NULL) { secu_Indent(out, level + 1); fprintf(out, "Signer Information List:\n"); iv = 0; while ((sigInfo = src->signerInfos[iv++]) != NULL) { sprintf(om, "Signer Information (%x)", iv); secu_PrintSignerInfo(out, sigInfo, om, level + 2); } } return 0;}PR_IMPLEMENT(int)SECU_PrintCrl (FILE *out, SECItem *der, char *m, int level){ PRArenaPool *arena = NULL; CERTCrl *c = NULL; int rv; do { /* Decode CRL */ c = (CERTCrl*) PORT_ZAlloc(sizeof(CERTCrl)); if (!c) { rv = PORT_GetError(); break; } arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); if (!arena) { rv = SEC_ERROR_NO_MEMORY; break; } rv = SEC_ASN1DecodeItem(arena, c, CERT_CrlTemplate, der); if (rv != SECSuccess) break; SECU_PrintCRLInfo (out, c, m, level); } while (0); PORT_FreeArena (arena, PR_FALSE); PORT_Free (c); return (rv);}/*** secu_PrintPKCS7Encrypted** Pretty print a PKCS7 encrypted data type (up to version 1).*/voidsecu_PrintPKCS7Encrypted(FILE *out, SEC_PKCS7EncryptedData *src, char *m, int level){ secu_Indent(out, level); fprintf(out, "%s:\n", m); sv_PrintInteger(out, &(src->version), "Version", level + 1); secu_PrintPKCS7EncContent(out, &src->encContentInfo, "Encrypted Content Information", level + 1);}/*** secu_PrintPKCS7Digested** Pretty print a PKCS7 digested data type (up to version 1).*/voidsv_PrintPKCS7Digested(FILE *out, SEC_PKCS7DigestedData *src){ secu_Indent(out, level); fprintf(out, "%s:\n", m); sv_PrintInteger(out, &(src->version), "Version", level + 1); sv_PrintAlgorithmID(out, &src->digestAlg, "Digest Algorithm"); sv_PrintPKCS7ContentInfo(out, &src->contentInfo, "Content Information", level + 1); sv_PrintAsHex(out, &src->digest, "Digest", level + 1); }#endif/*** secu_PrintPKCS7ContentInfo** Takes a SEC_PKCS7ContentInfo type and sends the contents to the ** appropriate function*/intsv_PrintPKCS7ContentInfo(FILE *out, SEC_PKCS7ContentInfo *src, char *m){ char *desc; SECOidTag kind; int rv; if (src->contentTypeTag == NULL) src->contentTypeTag = SECOID_FindOID(&(src->contentType)); if (src->contentTypeTag == NULL) { desc = "Unknown"; kind = SEC_OID_PKCS7_DATA; } else { desc = src->contentTypeTag->desc; kind = src->contentTypeTag->offset; } fprintf(out, "%s%s\n", m, desc); if (src->content.data == NULL) { fprintf(out, "pkcs7.data=<no content>\n"); return 0; } rv = 0; switch (kind) { case SEC_OID_PKCS7_SIGNED_DATA: /* Signed Data */ rv = sv_PrintPKCS7Signed(out, src->content.signedData); break; case SEC_OID_PKCS7_ENVELOPED_DATA: /* Enveloped Data */ fprintf(out, "pkcs7EnvelopedData=<unsupported>\n"); /*sv_PrintPKCS7Enveloped(out, src->content.envelopedData);*/ break; case SEC_OID_PKCS7_SIGNED_ENVELOPED_DATA: /* Signed and Enveloped */ fprintf(out, "pkcs7SignedEnvelopedData=<unsupported>\n"); /*rv = sv_PrintPKCS7SignedAndEnveloped(out, src->content.signedAndEnvelopedData);*/ break; case SEC_OID_PKCS7_DIGESTED_DATA: /* Digested Data */ fprintf(out, "pkcs7DigestedData=<unsupported>\n"); /*sv_PrintPKCS7Digested(out, src->content.digestedData);*/ break; case SEC_OID_PKCS7_ENCRYPTED_DATA: /* Encrypted Data */ fprintf(out, "pkcs7EncryptedData=<unsupported>\n"); /*sv_PrintPKCS7Encrypted(out, src->content.encryptedData);*/ break; default: fprintf(out, "pkcs7UnknownData=<unsupported>\n"); /*sv_PrintAsHex(out, src->content.data);*/ break; } return rv;}intSV_PrintPKCS7ContentInfo(FILE *out, SECItem *der){ SEC_PKCS7ContentInfo *cinfo; int rv = -1; cinfo = SEC_PKCS7DecodeItem(der, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (cinfo != NULL) { rv = sv_PrintPKCS7ContentInfo(out, cinfo, "pkcs7.contentInfo="); SEC_PKCS7DestroyContentInfo(cinfo); } return rv;}/*** End of PKCS7 functions*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?