jar.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 834 行 · 第 1/2 页

C
834
字号
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//* *  JAR.C * *  Jarnature. *  Routines common to signing and validating. * */#include "jar.h"#include "jarint.h"static void jar_destroy_list (ZZList *list);static int jar_find_first_cert     (JAR_Signer *signer, int type, JAR_Item **it);/* *  J A R _ n e w  * *  Create a new instantiation of a manifest representation. *  Use this as a token to any calls to this API. * */JAR *JAR_new (void)  {  JAR *jar;  if ((jar = (JAR*)PORT_ZAlloc (sizeof (JAR))) == NULL)    goto loser;  if ((jar->manifest = ZZ_NewList()) == NULL)    goto loser;  if ((jar->hashes = ZZ_NewList()) == NULL)    goto loser;  if ((jar->phy = ZZ_NewList()) == NULL)    goto loser;  if ((jar->metainfo = ZZ_NewList()) == NULL)    goto loser;  if ((jar->signers = ZZ_NewList()) == NULL)    goto loser;  return jar;loser:  if (jar)    {    if (jar->manifest)      ZZ_DestroyList (jar->manifest);    if (jar->hashes)      ZZ_DestroyList (jar->hashes);    if (jar->phy)      ZZ_DestroyList (jar->phy);    if (jar->metainfo)      ZZ_DestroyList (jar->metainfo);    if (jar->signers)      ZZ_DestroyList (jar->signers);    PORT_Free (jar);    }  return NULL;  }/*  *  J A R _ d e s t r o y * *  Godzilla. * */void PR_CALLBACK JAR_destroy (JAR *jar)  {  JAR *z;  PORT_Assert( jar != NULL );  if (jar == NULL)    return;  if (jar->fp) JAR_FCLOSE ((PRFileDesc*)jar->fp);  if (jar->url)      PORT_Free (jar->url);  if (jar->filename) PORT_Free (jar->filename);  /* Free the linked list elements */  jar_destroy_list (jar->manifest);  ZZ_DestroyList (jar->manifest);  jar_destroy_list (jar->hashes);  ZZ_DestroyList (jar->hashes);  jar_destroy_list (jar->phy);  ZZ_DestroyList (jar->phy);  jar_destroy_list (jar->metainfo);  ZZ_DestroyList (jar->metainfo);  jar_destroy_list (jar->signers);  ZZ_DestroyList (jar->signers);  PORT_Free (jar);  }static void jar_destroy_list (ZZList *list)  {  ZZLink *link, *oldlink;  JAR_Item *it;  JAR_Physical *phy;  JAR_Digest *dig;  JAR_Cert *fing;  JAR_Metainfo *met;  JAR_Signer *signer;  if (list && !ZZ_ListEmpty (list))    {    link = ZZ_ListHead (list);    while (!ZZ_ListIterDone (list, link))      {      it = link->thing;      if (!it) goto next;      if (it->pathname) PORT_Free (it->pathname);       switch (it->type)        {        case jarTypeMeta:          met = (JAR_Metainfo *) it->data;          if (met)            {            if (met->header) PORT_Free (met->header);            if (met->info) PORT_Free (met->info);            PORT_Free (met);            }          break;	case jarTypePhy:          phy = (JAR_Physical *) it->data;          if (phy)            PORT_Free (phy);          break;        case jarTypeSign:          fing = (JAR_Cert *) it->data;          if (fing)            {            if (fing->cert)              CERT_DestroyCertificate (fing->cert);            if (fing->key)              PORT_Free (fing->key);            PORT_Free (fing);            }	  break;        case jarTypeSect:        case jarTypeMF:        case jarTypeSF:          dig = (JAR_Digest *) it->data;          if (dig)            {            PORT_Free (dig);            }          break;        case jarTypeOwner:          signer = (JAR_Signer *) it->data;          if (signer)            JAR_destroy_signer (signer);          break;        default:          /* PORT_Assert( 1 != 2 ); */          break;        }      PORT_Free (it);    next:      oldlink = link;      link = link->next;      ZZ_DestroyLink (oldlink);      }    }  }/* *  J A R _ g e t _ m e t a i n f o * *  Retrieve meta information from the manifest file. *  It doesn't matter whether it's from .MF or .SF, does it? * */int JAR_get_metainfo    (JAR *jar, char *name, char *header, void **info, unsigned long *length)  {  JAR_Item *it;  ZZLink *link;  ZZList *list;  JAR_Metainfo *met;  PORT_Assert( jar != NULL && header != NULL );  if (jar == NULL || header == NULL)    return JAR_ERR_PNF;  list = jar->metainfo;  if (ZZ_ListEmpty (list))    return JAR_ERR_PNF;  for (link = ZZ_ListHead (list);        !ZZ_ListIterDone (list, link);        link = link->next)    {    it = link->thing;    if (it->type == jarTypeMeta)      {      if ((name && !it->pathname) || (!name && it->pathname))        continue;      if (name && it->pathname && strcmp (it->pathname, name))        continue;      met = (JAR_Metainfo *) it->data;      if (!PORT_Strcasecmp (met->header, header))        {        *info = PORT_Strdup (met->info);        *length = PORT_Strlen (met->info);        return 0;        }      }    }  return JAR_ERR_PNF;  }/* *  J A R _ f i n d * *  Establish the search pattern for use *  by JAR_find_next, to traverse the filenames *  or certificates in the JAR structure. * *  See jar.h for a description on how to use. * */JAR_Context *JAR_find (JAR *jar, char *pattern, jarType type)  {  JAR_Context *ctx;  PORT_Assert( jar != NULL );  if (!jar)    return NULL;  ctx = (JAR_Context *) PORT_ZAlloc (sizeof (JAR_Context));  if (ctx == NULL)    return NULL;  ctx->jar = jar;  if (pattern)    {    if ((ctx->pattern = PORT_Strdup (pattern)) == NULL)      {      PORT_Free (ctx);      return NULL;      }    }  ctx->finding = type;  switch (type)    {    case jarTypeMF:     ctx->next = ZZ_ListHead (jar->hashes);                        break;    case jarTypeSF:    case jarTypeSign:   ctx->next = NULL;                        ctx->nextsign = ZZ_ListHead (jar->signers);                        break;    case jarTypeSect:   ctx->next = ZZ_ListHead (jar->manifest);                        break;    case jarTypePhy:    ctx->next = ZZ_ListHead (jar->phy);                        break;    case jarTypeOwner:  if (jar->signers)                          ctx->next = ZZ_ListHead (jar->signers);                        else                          ctx->next = NULL;                        break;    case jarTypeMeta:   ctx->next = ZZ_ListHead (jar->metainfo);                        break;    default:            PORT_Assert( 1 != 2);                        break;    }  return ctx;  }/* *  J A R _ f i n d _ e n d * *  Destroy the find iterator context. * */void JAR_find_end (JAR_Context *ctx)  {  PORT_Assert( ctx != NULL );  if (ctx)    {    if (ctx->pattern)       PORT_Free (ctx->pattern);    PORT_Free (ctx);    }  }/* *  J A R _ f i n d _ n e x t * *  Return the next item of the given type *  from one of the JAR linked lists. * */int JAR_find_next (JAR_Context *ctx, JAR_Item **it)  {  JAR *jar;  ZZList *list;  int finding;  JAR_Signer *signer = NULL;  PORT_Assert( ctx != NULL );  PORT_Assert( ctx->jar != NULL );  jar = ctx->jar;  /* Internally, convert jarTypeSign to jarTypeSF, and return     the actual attached certificate later */  finding = (ctx->finding == jarTypeSign) ? jarTypeSF : ctx->finding;  if (ctx->nextsign)    {      if (ZZ_ListIterDone (jar->signers, ctx->nextsign))       {       *it = NULL;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?