diff options
author | Felix Fietkau <nbd@openwrt.org> | 2014-03-21 15:55:23 +0000 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2014-03-21 15:55:23 +0000 |
commit | d0448ca8acaf1674057034724561344e1d28c17e (patch) | |
tree | 90ed8df156dbfda6100ace3391cc2439acb8b83f | |
parent | 55a9bf9653428218ca65420d10e3f2a67cac74fd (diff) | |
download | master-187ad058-d0448ca8acaf1674057034724561344e1d28c17e.tar.gz master-187ad058-d0448ca8acaf1674057034724561344e1d28c17e.tar.bz2 master-187ad058-d0448ca8acaf1674057034724561344e1d28c17e.zip |
px5g: rename the old package to px5g-standalone, add a new one that links against polarssl
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@40000 3c298f89-4303-0410-b956-a3cf2f4a3e73
-rw-r--r-- | package/utils/px5g-standalone/Makefile | 40 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/Makefile (renamed from package/utils/px5g/src/Makefile) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/base64.c (renamed from package/utils/px5g/src/library/base64.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/bignum.c (renamed from package/utils/px5g/src/library/bignum.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/havege.c (renamed from package/utils/px5g/src/library/havege.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/rsa.c (renamed from package/utils/px5g/src/library/rsa.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/sha1.c (renamed from package/utils/px5g/src/library/sha1.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/timing.c (renamed from package/utils/px5g/src/library/timing.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/library/x509write.c (renamed from package/utils/px5g/src/library/x509write.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/base64.h (renamed from package/utils/px5g/src/polarssl/base64.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/bignum.h (renamed from package/utils/px5g/src/polarssl/bignum.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/bn_mul.h (renamed from package/utils/px5g/src/polarssl/bn_mul.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/config.h (renamed from package/utils/px5g/src/polarssl/config.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/havege.h (renamed from package/utils/px5g/src/polarssl/havege.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/rsa.h (renamed from package/utils/px5g/src/polarssl/rsa.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/sha1.h (renamed from package/utils/px5g/src/polarssl/sha1.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/timing.h (renamed from package/utils/px5g/src/polarssl/timing.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/polarssl/x509.h (renamed from package/utils/px5g/src/polarssl/x509.h) | 0 | ||||
-rw-r--r-- | package/utils/px5g-standalone/src/px5g.c (renamed from package/utils/px5g/src/px5g.c) | 0 | ||||
-rw-r--r-- | package/utils/px5g/Makefile | 11 | ||||
-rw-r--r-- | package/utils/px5g/px5g.c | 269 |
21 files changed, 317 insertions, 3 deletions
diff --git a/package/utils/px5g-standalone/Makefile b/package/utils/px5g-standalone/Makefile new file mode 100644 index 0000000000..25777967bc --- /dev/null +++ b/package/utils/px5g-standalone/Makefile @@ -0,0 +1,40 @@ +# +# Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org> +# +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=px5g +PKG_RELEASE:=1 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/px5g-standalone + SECTION:=utils + CATEGORY:=Utilities + TITLE:=Standalone X.509 certificate generator (standalone version) + MAINTAINER:=Jo-Philipp Wich <xm@subsignal.org> +endef + +define Package/px5g-standalone/description + Px5g is a tiny standalone X.509 certificate generator. + It suitable to create key files and certificates in DER + and PEM format for use with stunnel, uhttpd and others. +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Package/px5g-standalone/install + $(INSTALL_DIR) $(1)/usr/sbin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/px5g $(1)/usr/sbin/px5g +endef + +$(eval $(call BuildPackage,px5g-standalone)) diff --git a/package/utils/px5g/src/Makefile b/package/utils/px5g-standalone/src/Makefile index 2bd95739cb..2bd95739cb 100644 --- a/package/utils/px5g/src/Makefile +++ b/package/utils/px5g-standalone/src/Makefile diff --git a/package/utils/px5g/src/library/base64.c b/package/utils/px5g-standalone/src/library/base64.c index b7cc5b84ea..b7cc5b84ea 100644 --- a/package/utils/px5g/src/library/base64.c +++ b/package/utils/px5g-standalone/src/library/base64.c diff --git a/package/utils/px5g/src/library/bignum.c b/package/utils/px5g-standalone/src/library/bignum.c index 8b7c12ff00..8b7c12ff00 100644 --- a/package/utils/px5g/src/library/bignum.c +++ b/package/utils/px5g-standalone/src/library/bignum.c diff --git a/package/utils/px5g/src/library/havege.c b/package/utils/px5g-standalone/src/library/havege.c index 266299d3bb..266299d3bb 100644 --- a/package/utils/px5g/src/library/havege.c +++ b/package/utils/px5g-standalone/src/library/havege.c diff --git a/package/utils/px5g/src/library/rsa.c b/package/utils/px5g-standalone/src/library/rsa.c index 131b6c6c9c..131b6c6c9c 100644 --- a/package/utils/px5g/src/library/rsa.c +++ b/package/utils/px5g-standalone/src/library/rsa.c diff --git a/package/utils/px5g/src/library/sha1.c b/package/utils/px5g-standalone/src/library/sha1.c index 54a4416f31..54a4416f31 100644 --- a/package/utils/px5g/src/library/sha1.c +++ b/package/utils/px5g-standalone/src/library/sha1.c diff --git a/package/utils/px5g/src/library/timing.c b/package/utils/px5g-standalone/src/library/timing.c index 6b7ab740e1..6b7ab740e1 100644 --- a/package/utils/px5g/src/library/timing.c +++ b/package/utils/px5g-standalone/src/library/timing.c diff --git a/package/utils/px5g/src/library/x509write.c b/package/utils/px5g-standalone/src/library/x509write.c index fabee20ea6..fabee20ea6 100644 --- a/package/utils/px5g/src/library/x509write.c +++ b/package/utils/px5g-standalone/src/library/x509write.c diff --git a/package/utils/px5g/src/polarssl/base64.h b/package/utils/px5g-standalone/src/polarssl/base64.h index c48267b1b5..c48267b1b5 100644 --- a/package/utils/px5g/src/polarssl/base64.h +++ b/package/utils/px5g-standalone/src/polarssl/base64.h diff --git a/package/utils/px5g/src/polarssl/bignum.h b/package/utils/px5g-standalone/src/polarssl/bignum.h index c667303329..c667303329 100644 --- a/package/utils/px5g/src/polarssl/bignum.h +++ b/package/utils/px5g-standalone/src/polarssl/bignum.h diff --git a/package/utils/px5g/src/polarssl/bn_mul.h b/package/utils/px5g-standalone/src/polarssl/bn_mul.h index f6d34da58a..f6d34da58a 100644 --- a/package/utils/px5g/src/polarssl/bn_mul.h +++ b/package/utils/px5g-standalone/src/polarssl/bn_mul.h diff --git a/package/utils/px5g/src/polarssl/config.h b/package/utils/px5g-standalone/src/polarssl/config.h index ad3f9a7f39..ad3f9a7f39 100644 --- a/package/utils/px5g/src/polarssl/config.h +++ b/package/utils/px5g-standalone/src/polarssl/config.h diff --git a/package/utils/px5g/src/polarssl/havege.h b/package/utils/px5g-standalone/src/polarssl/havege.h index c27cecac0f..c27cecac0f 100644 --- a/package/utils/px5g/src/polarssl/havege.h +++ b/package/utils/px5g-standalone/src/polarssl/havege.h diff --git a/package/utils/px5g/src/polarssl/rsa.h b/package/utils/px5g-standalone/src/polarssl/rsa.h index b31dc2f144..b31dc2f144 100644 --- a/package/utils/px5g/src/polarssl/rsa.h +++ b/package/utils/px5g-standalone/src/polarssl/rsa.h diff --git a/package/utils/px5g/src/polarssl/sha1.h b/package/utils/px5g-standalone/src/polarssl/sha1.h index 3ca7dc3195..3ca7dc3195 100644 --- a/package/utils/px5g/src/polarssl/sha1.h +++ b/package/utils/px5g-standalone/src/polarssl/sha1.h diff --git a/package/utils/px5g/src/polarssl/timing.h b/package/utils/px5g-standalone/src/polarssl/timing.h index 62d627f61b..62d627f61b 100644 --- a/package/utils/px5g/src/polarssl/timing.h +++ b/package/utils/px5g-standalone/src/polarssl/timing.h diff --git a/package/utils/px5g/src/polarssl/x509.h b/package/utils/px5g-standalone/src/polarssl/x509.h index 908a1dbf51..908a1dbf51 100644 --- a/package/utils/px5g/src/polarssl/x509.h +++ b/package/utils/px5g-standalone/src/polarssl/x509.h diff --git a/package/utils/px5g/src/px5g.c b/package/utils/px5g-standalone/src/px5g.c index cf50ad28e2..cf50ad28e2 100644 --- a/package/utils/px5g/src/px5g.c +++ b/package/utils/px5g-standalone/src/px5g.c diff --git a/package/utils/px5g/Makefile b/package/utils/px5g/Makefile index ad4ec40580..9c0caa7b8d 100644 --- a/package/utils/px5g/Makefile +++ b/package/utils/px5g/Makefile @@ -10,15 +10,17 @@ include $(TOPDIR)/rules.mk PKG_NAME:=px5g PKG_RELEASE:=1 -PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT) +PKG_USE_MIPS16:=0 include $(INCLUDE_DIR)/package.mk define Package/px5g SECTION:=utils CATEGORY:=Utilities - TITLE:=Standalone X.509 certificate generator + TITLE:=X.509 certificate generator (using PolarSSL) MAINTAINER:=Jo-Philipp Wich <xm@subsignal.org> + DEPENDS:=+libpolarssl endef define Package/px5g/description @@ -29,7 +31,10 @@ endef define Build/Prepare mkdir -p $(PKG_BUILD_DIR) - $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Build/Compile + $(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/px5g px5g.c -lpolarssl endef define Package/px5g/install diff --git a/package/utils/px5g/px5g.c b/package/utils/px5g/px5g.c new file mode 100644 index 0000000000..6b977081e1 --- /dev/null +++ b/package/utils/px5g/px5g.c @@ -0,0 +1,269 @@ +/* + * px5g - Embedded x509 key and certificate generator based on PolarSSL + * + * Copyright (C) 2009 Steven Barth <steven@midlink.org> + * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License, version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include <sys/types.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <limits.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdbool.h> + +#include <polarssl/bignum.h> +#include <polarssl/x509_crt.h> +#include <polarssl/rsa.h> + +#define PX5G_VERSION "0.2" +#define PX5G_COPY "Copyright (c) 2009 Steven Barth <steven@midlink.org>" +#define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1" + +static int urandom_fd; +static char buf[16384]; + +static int _urandom(void *ctx, unsigned char *out, size_t len) +{ + read(urandom_fd, out, len); + return 0; +} + +static void write_file(const char *path, int len, bool pem) +{ + FILE *f = stdout; + const char *buf_start = buf; + + if (!pem) + buf_start += sizeof(buf) - len; + + if (!len) { + fprintf(stderr, "No data to write\n"); + exit(1); + } + + if (!f) { + fprintf(stderr, "error: I/O error\n"); + exit(1); + } + + if (path) + f = fopen(path, "w"); + + fwrite(buf_start, 1, len, f); + fclose(f); +} + +static void write_key(pk_context *key, const char *path, bool pem) +{ + int len = 0; + + if (pem) { + if (pk_write_key_pem(key, (void *) buf, sizeof(buf)) == 0) + len = strlen(buf); + } else { + len = pk_write_key_der(key, (void *) buf, sizeof(buf)); + if (len < 0) + len = 0; + } + + write_file(path, len, pem); +} + +static void gen_key(pk_context *key, int ksize, int exp, bool pem) +{ + pk_init(key); + pk_init_ctx(key, pk_info_from_type(POLARSSL_PK_RSA)); + fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize); + if (rsa_gen_key(pk_rsa(*key), _urandom, NULL, ksize, exp)) { + fprintf(stderr, "error: key generation failed\n"); + exit(1); + } +} + +int rsakey(char **arg) +{ + pk_context key; + unsigned int ksize = 512; + int exp = 65537; + char *path = NULL; + bool pem = true; + + while (*arg && **arg == '-') { + if (!strcmp(*arg, "-out") && arg[1]) { + path = arg[1]; + arg++; + } else if (!strcmp(*arg, "-3")) { + exp = 3; + } else if (!strcmp(*arg, "-der")) { + pem = false; + } + arg++; + } + + if (*arg) + ksize = (unsigned int)atoi(*arg); + + gen_key(&key, ksize, exp, pem); + write_key(&key, path, pem); + + pk_free(&key); + + return 0; +} + +int selfsigned(char **arg) +{ + pk_context key; + x509write_cert cert; + mpi serial; + + char *subject = ""; + unsigned int ksize = 512; + int exp = 65537; + unsigned int days = 30; + char *keypath = NULL, *certpath = NULL; + bool pem = true; + time_t from = time(NULL), to; + char fstr[20], tstr[20]; + int len; + + while (*arg && **arg == '-') { + if (!strcmp(*arg, "-der")) { + pem = false; + } else if (!strcmp(*arg, "-newkey") && arg[1]) { + if (strncmp(arg[1], "rsa:", 4)) { + fprintf(stderr, "error: invalid algorithm"); + return 1; + } + ksize = (unsigned int)atoi(arg[1] + 4); + arg++; + } else if (!strcmp(*arg, "-days") && arg[1]) { + days = (unsigned int)atoi(arg[1]); + arg++; + } else if (!strcmp(*arg, "-keyout") && arg[1]) { + keypath = arg[1]; + arg++; + } else if (!strcmp(*arg, "-out") && arg[1]) { + certpath = arg[1]; + arg++; + } else if (!strcmp(*arg, "-subj") && arg[1]) { + if (arg[1][0] != '/' || strchr(arg[1], ';')) { + fprintf(stderr, "error: invalid subject"); + return 1; + } + subject = calloc(strlen(arg[1]) + 1, 1); + char *oldc = arg[1] + 1, *newc = subject, *delim; + do { + delim = strchr(oldc, '='); + if (!delim) { + fprintf(stderr, "error: invalid subject"); + return 1; + } + memcpy(newc, oldc, delim - oldc + 1); + newc += delim - oldc + 1; + oldc = delim + 1; + + delim = strchr(oldc, '/'); + if (!delim) { + delim = arg[1] + strlen(arg[1]); + } + memcpy(newc, oldc, delim - oldc); + newc += delim - oldc; + *newc++ = ';'; + oldc = delim + 1; + } while(*delim); + arg++; + } + arg++; + } + + gen_key(&key, ksize, exp, pem); + + if (keypath) + write_key(&key, keypath, pem); + + from = (from < 1000000000) ? 1000000000 : from; + strftime(fstr, sizeof(fstr), "%Y%m%d%H%M%S", gmtime(&from)); + to = from + 60 * 60 * 24 * days; + if (to < from) + to = INT_MAX; + strftime(tstr, sizeof(tstr), "%Y%m%d%H%M%S", gmtime(&to)); + + fprintf(stderr, "Generating selfsigned certificate with subject '%s'" + " and validity %s-%s\n", subject, fstr, tstr); + + x509write_crt_init(&cert); + x509write_crt_set_md_alg(&cert, POLARSSL_MD_SHA1); + x509write_crt_set_issuer_key(&cert, &key); + x509write_crt_set_subject_key(&cert, &key); + x509write_crt_set_subject_name(&cert, subject); + x509write_crt_set_issuer_name(&cert, subject); + x509write_crt_set_validity(&cert, fstr, tstr); + x509write_crt_set_basic_constraints(&cert, 0, -1); + x509write_crt_set_subject_key_identifier(&cert); + x509write_crt_set_authority_key_identifier(&cert); + + mpi_init(&serial); + mpi_read_string(&serial, 10, "1"); + x509write_crt_set_serial(&cert, &serial); + + if (pem) { + if (x509write_crt_pem(&cert, (void *) buf, sizeof(buf), _urandom, NULL) < 0) { + fprintf(stderr, "Failed to generate certificate\n"); + return 1; + } + + len = strlen(buf); + } else { + len = x509write_crt_der(&cert, (void *) buf, sizeof(buf), _urandom, NULL); + if (len < 0) { + fprintf(stderr, "Failed to generate certificate: %d\n", len); + return 1; + } + } + write_file(certpath, len, pem); + + x509write_crt_free(&cert); + mpi_free(&serial); + pk_free(&key); + + return 0; +} + +int main(int argc, char *argv[]) +{ + urandom_fd = open("/dev/urandom", O_RDONLY); + + if (!argv[1]) { + //Usage + } else if (!strcmp(argv[1], "rsakey")) { + return rsakey(argv+2); + } else if (!strcmp(argv[1], "selfsigned")) { + return selfsigned(argv+2); + } + + fprintf(stderr, + "PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY + "\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n"); + fprintf(stderr, "Usage: %s [rsakey|selfsigned]\n", *argv); + return 1; +} |