Header And Logo

PostgreSQL
| The world's most advanced open source database.

pgp.c

Go to the documentation of this file.
00001 /*
00002  * pgp.c
00003  *    Various utility stuff.
00004  *
00005  * Copyright (c) 2005 Marko Kreen
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * contrib/pgcrypto/pgp.c
00030  */
00031 
00032 #include "postgres.h"
00033 
00034 #include "px.h"
00035 #include "mbuf.h"
00036 #include "pgp.h"
00037 
00038 /*
00039  * Defaults.
00040  */
00041 static int  def_cipher_algo = PGP_SYM_AES_128;
00042 static int  def_s2k_cipher_algo = -1;
00043 static int  def_s2k_mode = PGP_S2K_ISALTED;
00044 static int  def_s2k_digest_algo = PGP_DIGEST_SHA1;
00045 static int  def_compress_algo = PGP_COMPR_NONE;
00046 static int  def_compress_level = 6;
00047 static int  def_disable_mdc = 0;
00048 static int  def_use_sess_key = 0;
00049 static int  def_text_mode = 0;
00050 static int  def_unicode_mode = 0;
00051 static int  def_convert_crlf = 0;
00052 
00053 struct digest_info
00054 {
00055     const char *name;
00056     int         code;
00057     const char *int_name;
00058 };
00059 
00060 struct cipher_info
00061 {
00062     const char *name;
00063     int         code;
00064     const char *int_name;
00065     int         key_len;
00066     int         block_len;
00067 };
00068 
00069 static const struct digest_info digest_list[] = {
00070     {"md5", PGP_DIGEST_MD5},
00071     {"sha1", PGP_DIGEST_SHA1},
00072     {"sha-1", PGP_DIGEST_SHA1},
00073     {"ripemd160", PGP_DIGEST_RIPEMD160},
00074     {"sha256", PGP_DIGEST_SHA256},
00075     {"sha384", PGP_DIGEST_SHA384},
00076     {"sha512", PGP_DIGEST_SHA512},
00077     {NULL, 0}
00078 };
00079 
00080 static const struct cipher_info cipher_list[] = {
00081     {"3des", PGP_SYM_DES3, "3des-ecb", 192 / 8, 64 / 8},
00082     {"cast5", PGP_SYM_CAST5, "cast5-ecb", 128 / 8, 64 / 8},
00083     {"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
00084     {"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
00085     {"aes", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
00086     {"aes128", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
00087     {"aes192", PGP_SYM_AES_192, "aes-ecb", 192 / 8, 128 / 8},
00088     {"aes256", PGP_SYM_AES_256, "aes-ecb", 256 / 8, 128 / 8},
00089     {"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256 / 8, 128 / 8},
00090     {NULL, 0, NULL}
00091 };
00092 
00093 static const struct cipher_info *
00094 get_cipher_info(int code)
00095 {
00096     const struct cipher_info *i;
00097 
00098     for (i = cipher_list; i->name; i++)
00099         if (i->code == code)
00100             return i;
00101     return NULL;
00102 }
00103 
00104 int
00105 pgp_get_digest_code(const char *name)
00106 {
00107     const struct digest_info *i;
00108 
00109     for (i = digest_list; i->name; i++)
00110         if (pg_strcasecmp(i->name, name) == 0)
00111             return i->code;
00112     return PXE_PGP_UNSUPPORTED_HASH;
00113 }
00114 
00115 int
00116 pgp_get_cipher_code(const char *name)
00117 {
00118     const struct cipher_info *i;
00119 
00120     for (i = cipher_list; i->name; i++)
00121         if (pg_strcasecmp(i->name, name) == 0)
00122             return i->code;
00123     return PXE_PGP_UNSUPPORTED_CIPHER;
00124 }
00125 
00126 const char *
00127 pgp_get_digest_name(int code)
00128 {
00129     const struct digest_info *i;
00130 
00131     for (i = digest_list; i->name; i++)
00132         if (i->code == code)
00133             return i->name;
00134     return NULL;
00135 }
00136 
00137 const char *
00138 pgp_get_cipher_name(int code)
00139 {
00140     const struct cipher_info *i = get_cipher_info(code);
00141 
00142     if (i != NULL)
00143         return i->name;
00144     return NULL;
00145 }
00146 
00147 int
00148 pgp_get_cipher_key_size(int code)
00149 {
00150     const struct cipher_info *i = get_cipher_info(code);
00151 
00152     if (i != NULL)
00153         return i->key_len;
00154     return 0;
00155 }
00156 
00157 int
00158 pgp_get_cipher_block_size(int code)
00159 {
00160     const struct cipher_info *i = get_cipher_info(code);
00161 
00162     if (i != NULL)
00163         return i->block_len;
00164     return 0;
00165 }
00166 
00167 int
00168 pgp_load_cipher(int code, PX_Cipher **res)
00169 {
00170     int         err;
00171     const struct cipher_info *i = get_cipher_info(code);
00172 
00173     if (i == NULL)
00174         return PXE_PGP_CORRUPT_DATA;
00175 
00176     err = px_find_cipher(i->int_name, res);
00177     if (err == 0)
00178         return 0;
00179 
00180     return PXE_PGP_UNSUPPORTED_CIPHER;
00181 }
00182 
00183 int
00184 pgp_load_digest(int code, PX_MD **res)
00185 {
00186     int         err;
00187     const char *name = pgp_get_digest_name(code);
00188 
00189     if (name == NULL)
00190         return PXE_PGP_CORRUPT_DATA;
00191 
00192     err = px_find_digest(name, res);
00193     if (err == 0)
00194         return 0;
00195 
00196     return PXE_PGP_UNSUPPORTED_HASH;
00197 }
00198 
00199 int
00200 pgp_init(PGP_Context **ctx_p)
00201 {
00202     PGP_Context *ctx;
00203 
00204     ctx = px_alloc(sizeof *ctx);
00205     memset(ctx, 0, sizeof *ctx);
00206 
00207     ctx->cipher_algo = def_cipher_algo;
00208     ctx->s2k_cipher_algo = def_s2k_cipher_algo;
00209     ctx->s2k_mode = def_s2k_mode;
00210     ctx->s2k_digest_algo = def_s2k_digest_algo;
00211     ctx->compress_algo = def_compress_algo;
00212     ctx->compress_level = def_compress_level;
00213     ctx->disable_mdc = def_disable_mdc;
00214     ctx->use_sess_key = def_use_sess_key;
00215     ctx->unicode_mode = def_unicode_mode;
00216     ctx->convert_crlf = def_convert_crlf;
00217     ctx->text_mode = def_text_mode;
00218 
00219     *ctx_p = ctx;
00220     return 0;
00221 }
00222 
00223 int
00224 pgp_free(PGP_Context *ctx)
00225 {
00226     if (ctx->pub_key)
00227         pgp_key_free(ctx->pub_key);
00228     memset(ctx, 0, sizeof *ctx);
00229     px_free(ctx);
00230     return 0;
00231 }
00232 
00233 int
00234 pgp_disable_mdc(PGP_Context *ctx, int disable)
00235 {
00236     ctx->disable_mdc = disable ? 1 : 0;
00237     return 0;
00238 }
00239 
00240 int
00241 pgp_set_sess_key(PGP_Context *ctx, int use)
00242 {
00243     ctx->use_sess_key = use ? 1 : 0;
00244     return 0;
00245 }
00246 
00247 int
00248 pgp_set_convert_crlf(PGP_Context *ctx, int doit)
00249 {
00250     ctx->convert_crlf = doit ? 1 : 0;
00251     return 0;
00252 }
00253 
00254 int
00255 pgp_set_s2k_mode(PGP_Context *ctx, int mode)
00256 {
00257     int         err = PXE_OK;
00258 
00259     switch (mode)
00260     {
00261         case PGP_S2K_SIMPLE:
00262         case PGP_S2K_SALTED:
00263         case PGP_S2K_ISALTED:
00264             ctx->s2k_mode = mode;
00265             break;
00266         default:
00267             err = PXE_ARGUMENT_ERROR;
00268             break;
00269     }
00270     return err;
00271 }
00272 
00273 int
00274 pgp_set_compress_algo(PGP_Context *ctx, int algo)
00275 {
00276     switch (algo)
00277     {
00278         case PGP_COMPR_NONE:
00279         case PGP_COMPR_ZIP:
00280         case PGP_COMPR_ZLIB:
00281         case PGP_COMPR_BZIP2:
00282             ctx->compress_algo = algo;
00283             return 0;
00284     }
00285     return PXE_ARGUMENT_ERROR;
00286 }
00287 
00288 int
00289 pgp_set_compress_level(PGP_Context *ctx, int level)
00290 {
00291     if (level >= 0 && level <= 9)
00292     {
00293         ctx->compress_level = level;
00294         return 0;
00295     }
00296     return PXE_ARGUMENT_ERROR;
00297 }
00298 
00299 int
00300 pgp_set_text_mode(PGP_Context *ctx, int mode)
00301 {
00302     ctx->text_mode = mode;
00303     return 0;
00304 }
00305 
00306 int
00307 pgp_set_cipher_algo(PGP_Context *ctx, const char *name)
00308 {
00309     int         code = pgp_get_cipher_code(name);
00310 
00311     if (code < 0)
00312         return code;
00313     ctx->cipher_algo = code;
00314     return 0;
00315 }
00316 
00317 int
00318 pgp_set_s2k_cipher_algo(PGP_Context *ctx, const char *name)
00319 {
00320     int         code = pgp_get_cipher_code(name);
00321 
00322     if (code < 0)
00323         return code;
00324     ctx->s2k_cipher_algo = code;
00325     return 0;
00326 }
00327 
00328 int
00329 pgp_set_s2k_digest_algo(PGP_Context *ctx, const char *name)
00330 {
00331     int         code = pgp_get_digest_code(name);
00332 
00333     if (code < 0)
00334         return code;
00335     ctx->s2k_digest_algo = code;
00336     return 0;
00337 }
00338 
00339 int
00340 pgp_get_unicode_mode(PGP_Context *ctx)
00341 {
00342     return ctx->unicode_mode;
00343 }
00344 
00345 int
00346 pgp_set_unicode_mode(PGP_Context *ctx, int mode)
00347 {
00348     ctx->unicode_mode = mode ? 1 : 0;
00349     return 0;
00350 }
00351 
00352 int
00353 pgp_set_symkey(PGP_Context *ctx, const uint8 *key, int len)
00354 {
00355     if (key == NULL || len < 1)
00356         return PXE_ARGUMENT_ERROR;
00357     ctx->sym_key = key;
00358     ctx->sym_key_len = len;
00359     return 0;
00360 }