From: Greg Beaver Date: Mon, 26 Mar 2007 00:00:56 +0000 (+0000) Subject: [DOC] X-Git-Tag: RELEASE_1_1_0~117 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a92eeb61c45223d1d236be5937d47634045a1e34;p=php [DOC] implement setSignatureAlgorithm() and add class constants Phar::MD5, Phar::SHA1, Phar::SHA256, Phar::SHA512, Phar::PGP --- diff --git a/ext/phar/TODO b/ext/phar/TODO index 5ab7a3c3f8..c5cd455204 100644 --- a/ext/phar/TODO +++ b/ext/phar/TODO @@ -40,6 +40,8 @@ Version 1.1.0 * [optional] Phar->rollback() to abort a write transaction * implement GPG signing X add SHA-2 (256, 512) support [Marcus] + X setSignatureAlgorithm() and Phar::MD5 Phar::SHA1 Phar::SHA256 Phar::SHA512 Phar::PGP to + choose the kind of signature to use (PGP falls back to SHA1) [Greg] * ability to match files containing a metadata key opendir('phar://a.phar/?mime-type=image/jpeg') or foreach ($p->match('mime-type', 'image/jpeg') as $file) * Phar::copy($from, $to); diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 76d753a6e0..df7bd25451 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -2522,9 +2522,7 @@ int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **err efree(archive->signature); } - switch(PHAR_SIG_USE) { - case PHAR_SIG_PGP: - /* TODO: currently fall back to sha1,later do both */ + switch(archive->sig_flags) { #if HAVE_HASH_EXT case PHAR_SIG_SHA512: { unsigned char digest[64]; @@ -2566,6 +2564,8 @@ int phar_flush(phar_archive_data *archive, char *user_stub, long len, char **err } return EOF; #endif + case PHAR_SIG_PGP: + /* TODO: currently fall back to sha1,later do both */ default: case PHAR_SIG_SHA1: { unsigned char digest[20]; diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h index 6bb532c92a..2a5c43e1f7 100755 --- a/ext/phar/phar_internal.h +++ b/ext/phar/phar_internal.h @@ -83,8 +83,6 @@ #define PHAR_SIG_SHA512 0x0004 #define PHAR_SIG_PGP 0x0010 -#define PHAR_SIG_USE PHAR_SIG_SHA1 - /* flags byte for each file adheres to these bitmasks. All unused values are reserved */ #define PHAR_ENT_COMPRESSION_MASK 0x0000F000 diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 746de8f24d..ae69c1ef8e 100755 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -350,6 +350,51 @@ PHP_METHOD(Phar, setStub) } /* }}} */ +/* {{{ proto array Phar::setSignatureAlgorithm(int sigtype) + * set the signature algorithm for a phar and apply it. The + * signature algorithm must be one of Phar::MD5, Phar::SHA1, + * Phar::SHA256 or Phar::SHA512 + */ +PHP_METHOD(Phar, setSignatureAlgorithm) +{ + long algo; + char *error; + PHAR_ARCHIVE_OBJECT(); + + if (PHAR_G(readonly)) { + zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, + "Cannot change stub, phar is read-only"); + } + + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "l", &algo) != SUCCESS) { + return; + } + + switch (algo) { + case PHAR_SIG_SHA256 : + case PHAR_SIG_SHA512 : +#if !HAVE_HASH_EXT + zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, + "SHA-256 and SHA-512 signatures are only supported if the hash extension is enabled"); +#endif + case PHAR_SIG_MD5 : + case PHAR_SIG_SHA1 : + case PHAR_SIG_PGP : + phar_obj->arc.archive->sig_flags = algo; + phar_obj->arc.archive->is_modified = 1; + + phar_flush(phar_obj->arc.archive, 0, 0, &error TSRMLS_CC); + if (error) { + zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, error); + efree(error); + } + default : + zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, + "Unknown signature algorithm specified"); + } +} +/* }}} */ + /* {{{ proto array Phar::getSupportedSignatures() * Return array of supported signature types */ @@ -1208,6 +1253,7 @@ zend_function_entry php_archive_methods[] = { PHP_ME(Phar, isBuffering, NULL, ZEND_ACC_PUBLIC) PHP_ME(Phar, setMetadata, arginfo_entry_setMetadata, ZEND_ACC_PUBLIC) PHP_ME(Phar, setStub, arginfo_phar_setStub, ZEND_ACC_PUBLIC) + PHP_ME(Phar, setSignatureAlgorithm, arginfo_entry_setMetadata, ZEND_ACC_PUBLIC) PHP_ME(Phar, offsetExists, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC) PHP_ME(Phar, offsetGet, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC) PHP_ME(Phar, offsetSet, arginfo_phar_offsetSet, ZEND_ACC_PUBLIC) @@ -1289,6 +1335,11 @@ void phar_object_init(TSRMLS_D) /* {{{ */ REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "COMPRESSED", PHAR_ENT_COMPRESSION_MASK) REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "GZ", PHAR_ENT_COMPRESSED_GZ) REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "BZ2", PHAR_ENT_COMPRESSED_BZ2) + REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5) + REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1) + REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256) + REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512) + REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PGP", PHAR_SIG_PGP) } /* }}} */