/* * Copyright (C) 2020 iopsys Software Solutions AB * * This program 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 * * Author: Omar Kallel */ #include "security.h" #define DATE_LEN 128 struct certificate_profile { char *path; #ifdef LOPENSSL X509 *openssl_cert; #elif LMBEDTLS mbedtls_x509_crt mbdtls_cert; #endif struct uci_section *dmmap_sect; }; /************************************************************ * Init function *************************************************************/ void init_certificate(char *path, #ifdef LOPENSSL X509 *cert, #elif LMBEDTLS mbedtls_x509_crt cert, #endif struct uci_section *dmsect, struct certificate_profile *certprofile) { certprofile->path = path; #ifdef LOPENSSL certprofile->openssl_cert = cert; #elif LMBEDTLS certprofile->mbdtls_cert = cert; #endif certprofile->dmmap_sect = dmsect; } #ifdef LOPENSSL static int convert_ASN1TIME(ASN1_TIME *t, char* buf, size_t len) { int rc; BIO *b = BIO_new(BIO_s_mem()); rc = ASN1_TIME_print(b, t); if (rc <= 0) { BIO_free(b); return EXIT_FAILURE; } rc = BIO_gets(b, buf, len); if (rc <= 0) { BIO_free(b); return EXIT_FAILURE; } BIO_free(b); return EXIT_SUCCESS; } static char *get_certificate_sig_alg(int sig_nid) { switch(sig_nid) { case NID_sha256WithRSAEncryption: return LN_sha256WithRSAEncryption; case NID_sha384WithRSAEncryption: return LN_sha384WithRSAEncryption; case NID_sha512WithRSAEncryption: return LN_sha512WithRSAEncryption; case NID_sha224WithRSAEncryption: return LN_sha224WithRSAEncryption; case NID_sha512_224WithRSAEncryption: return LN_sha512_224WithRSAEncryption; case NID_sha512_256WithRSAEncryption: return LN_sha512_224WithRSAEncryption; case NID_pbeWithMD2AndDES_CBC: return LN_pbeWithMD2AndDES_CBC; case NID_pbeWithMD5AndDES_CBC: return LN_pbeWithMD5AndDES_CBC; case NID_pbeWithMD2AndRC2_CBC: return LN_pbeWithMD5AndDES_CBC; case NID_pbeWithMD5AndRC2_CBC: return LN_pbeWithMD5AndRC2_CBC; case NID_pbeWithSHA1AndDES_CBC: return LN_pbeWithSHA1AndDES_CBC; case NID_pbeWithSHA1AndRC2_CBC: return LN_pbeWithSHA1AndDES_CBC; case NID_pbe_WithSHA1And128BitRC4: return LN_pbe_WithSHA1And128BitRC4; case NID_pbe_WithSHA1And40BitRC4: return LN_pbe_WithSHA1And40BitRC4; case NID_pbe_WithSHA1And3_Key_TripleDES_CBC: return LN_pbe_WithSHA1And3_Key_TripleDES_CBC; case NID_pbe_WithSHA1And2_Key_TripleDES_CBC: return LN_pbe_WithSHA1And2_Key_TripleDES_CBC; case NID_pbe_WithSHA1And128BitRC2_CBC: return LN_pbe_WithSHA1And128BitRC2_CBC; case NID_pbe_WithSHA1And40BitRC2_CBC: return LN_pbe_WithSHA1And40BitRC2_CBC; case NID_sm3WithRSAEncryption: return LN_sm3WithRSAEncryption; case NID_shaWithRSAEncryption: return LN_shaWithRSAEncryption; case NID_md2WithRSAEncryption: return LN_md2WithRSAEncryption; case NID_md4WithRSAEncryption: return LN_md4WithRSAEncryption; case NID_md5WithRSAEncryption: return LN_md5WithRSAEncryption; case NID_sha1WithRSAEncryption: return LN_sha1WithRSAEncryption; default: return ""; } } #elif LMBEDTLS static char *get_certificate_md(mbedtls_md_type_t sig_md) { switch(sig_md) { case MBEDTLS_MD_MD2: return "md2"; case MBEDTLS_MD_MD4: return "md4"; case MBEDTLS_MD_MD5: return "md5"; case MBEDTLS_MD_SHA1: return "sha1"; case MBEDTLS_MD_SHA224: return "sha224"; case MBEDTLS_MD_SHA256: return "sha256"; case MBEDTLS_MD_SHA384: return "sha384"; case MBEDTLS_MD_SHA512: return "sha512"; case MBEDTLS_MD_RIPEMD160: return "ripemd160"; default: return ""; } return ""; } static char *get_certificate_pk(mbedtls_pk_type_t sig_pk) { switch(sig_pk) { case MBEDTLS_PK_RSA: return "RSA"; case MBEDTLS_PK_ECKEY: return "ECKEY"; case MBEDTLS_PK_ECKEY_DH: return "ECKEYDH"; case MBEDTLS_PK_ECDSA: return "ECDSA"; case MBEDTLS_PK_RSA_ALT: return "RSAALT"; case MBEDTLS_PK_RSASSA_PSS: return "RSASSAPSS"; default: return ""; } return ""; } #endif /************************************************************* * ENTRY METHOD **************************************************************/ static int browseSecurityCertificateInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) { #if defined(LOPENSSL) || defined(LMBEDTLS) char **certifcates_paths; int length, i; char *cert_inst= NULL, *cert_inst_last= NULL, *v = NULL; struct uci_section *dmmap_sect = NULL; struct certificate_profile certificateprofile = {}; certifcates_paths = get_all_iop_certificates(&length); check_create_dmmap_package("dmmap_security"); for (i=0; ipath, &b)) strftime(t, 100, "%Y-%m-%dT%H:%M:%SZ", localtime( &b.st_mtime)); *value = dmstrdup(t); return 0; } static int get_SecurityCertificate_SerialNumber(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { *value = ""; #ifdef LOPENSSL struct certificate_profile *cert_profile = (struct certificate_profile*)data; ASN1_INTEGER *serial = X509_get_serialNumber(cert_profile->openssl_cert); *value = stringToHex((char *)serial->data, serial->length); #elif LMBEDTLS struct certificate_profile *cert_profile = (struct certificate_profile*)data; *value = stringToHex(cert_profile->mbdtls_cert.serial.p, cert_profile->mbdtls_cert.serial.len); #endif return 0; } static int get_SecurityCertificate_Issuer(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { *value = ""; #ifdef LOPENSSL struct certificate_profile *cert_profile = (struct certificate_profile*)data; *value = X509_NAME_oneline(X509_get_issuer_name(cert_profile->openssl_cert), NULL, 0); if (*value[0] == '/') (*value)++; *value = replace_char(*value, '/', ' '); #elif LMBEDTLS struct certificate_profile *cert_profile = (struct certificate_profile*)data; size_t olen; unsigned char issuer[4096]; int ret2 = mbedtls_base64_encode(issuer, 4096, &olen, cert_profile->mbdtls_cert.issuer.val.p, cert_profile->mbdtls_cert.issuer.val.len ); if(ret2 != 0) return 0; *value = decode64(issuer); #endif return 0; } static int get_SecurityCertificate_NotBefore(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { *value = ""; #ifdef LOPENSSL struct certificate_profile *cert_profile = (struct certificate_profile*)data; char not_before_str[DATE_LEN]; ASN1_TIME *not_before = X509_get_notBefore(cert_profile->openssl_cert); convert_ASN1TIME(not_before, not_before_str, DATE_LEN); *value = dmstrdup(not_before_str); #elif LMBEDTLS struct certificate_profile *cert_profile = (struct certificate_profile*)data; dmasprintf(value, "%d-%d-%dT%d:%d:%dZ", cert_profile->mbdtls_cert.valid_from.year, cert_profile->mbdtls_cert.valid_from.mon, cert_profile->mbdtls_cert.valid_from.day, cert_profile->mbdtls_cert.valid_from.hour, cert_profile->mbdtls_cert.valid_from.min, cert_profile->mbdtls_cert.valid_from.sec); #endif return 0; } static int get_SecurityCertificate_NotAfter(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { *value = ""; #ifdef LOPENSSL struct certificate_profile *cert_profile = (struct certificate_profile*)data; char not_after_str[DATE_LEN]; ASN1_TIME *not_after = X509_get_notAfter(cert_profile->openssl_cert); convert_ASN1TIME(not_after, not_after_str, DATE_LEN); *value = dmstrdup(not_after_str); #elif LMBEDTLS struct certificate_profile *cert_profile = (struct certificate_profile*)data; dmasprintf(value, "%d-%d-%dT%d:%d:%dZ", cert_profile->mbdtls_cert.valid_to.year, cert_profile->mbdtls_cert.valid_to.mon, cert_profile->mbdtls_cert.valid_to.day, cert_profile->mbdtls_cert.valid_to.hour, cert_profile->mbdtls_cert.valid_to.min, cert_profile->mbdtls_cert.valid_to.sec); #endif return 0; } static int get_SecurityCertificate_Subject(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { *value = ""; #ifdef LOPENSSL struct certificate_profile *cert_profile = (struct certificate_profile*)data; *value = X509_NAME_oneline(X509_get_subject_name(cert_profile->openssl_cert), NULL, 0); if (*value[0] == '/') (*value)++; *value = replace_char(*value, '/', ' '); #elif LMBEDTLS struct certificate_profile *cert_profile = (struct certificate_profile*)data; size_t olen; unsigned char issuer[4096]; int ret2 = mbedtls_base64_encode(issuer, 4096, &olen, cert_profile->mbdtls_cert.subject.val.p, cert_profile->mbdtls_cert.subject.val.len ); if(ret2 != 0) return 0; *value = decode64(issuer); #endif return 0; } static int get_SecurityCertificate_SignatureAlgorithm(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) { *value = ""; #ifdef LOPENSSL struct certificate_profile *cert_profile = (struct certificate_profile*)data; *value = dmstrdup(get_certificate_sig_alg(X509_get_signature_nid(cert_profile->openssl_cert))); #elif LMBEDTLS struct certificate_profile *cert_profile = (struct certificate_profile*)data; dmasprintf(value, "%sWith%sEncryptionn", get_certificate_md(cert_profile->mbdtls_cert.sig_md), get_certificate_pk(cert_profile->mbdtls_cert.sig_pk)); #endif return 0; } /* *** Device.Security. *** */ DMOBJ tSecurityObj[] = { /* OBJ, permission, addobj, delobj, checkobj, browseinstobj, forced_inform, notification, nextdynamicobj, nextobj, leaf, linker, bbfdm_type*/ {"Certificate", &DMREAD, NULL, NULL, NULL, browseSecurityCertificateInst, NULL, NULL, NULL, NULL, tSecurityCertificateParams, NULL, BBFDM_BOTH}, {0} }; DMLEAF tSecurityParams[] = { /* PARAM, permission, type, getvalue, setvalue, forced_inform, notification, bbfdm_type*/ {"CertificateNumberOfEntries", &DMREAD, DMT_UNINT, get_Security_CertificateNumberOfEntries, NULL, NULL, NULL, BBFDM_BOTH}, {0} }; /* *** Device.Security.Certificate.{i}. *** */ DMLEAF tSecurityCertificateParams[] = { /* PARAM, permission, type, getvalue, setvalue, forced_inform, notification, bbfdm_type*/ //{"Enable", &DMWRITE, DMT_BOOL, get_SecurityCertificate_Enable, set_SecurityCertificate_Enable, NULL, NULL, BBFDM_BOTH}, {"LastModif", &DMREAD, DMT_TIME, get_SecurityCertificate_LastModif, NULL, NULL, NULL, BBFDM_BOTH}, {"SerialNumber", &DMREAD, DMT_STRING, get_SecurityCertificate_SerialNumber, NULL, NULL, NULL, BBFDM_BOTH}, {"Issuer", &DMREAD, DMT_STRING, get_SecurityCertificate_Issuer, NULL, NULL, NULL, BBFDM_BOTH}, {"NotBefore", &DMREAD, DMT_TIME, get_SecurityCertificate_NotBefore, NULL, NULL, NULL, BBFDM_BOTH}, {"NotAfter", &DMREAD, DMT_TIME, get_SecurityCertificate_NotAfter, NULL, NULL, NULL, BBFDM_BOTH}, {"Subject", &DMREAD, DMT_STRING, get_SecurityCertificate_Subject, NULL, NULL, NULL, BBFDM_BOTH}, //{"SubjectAlt", &DMREAD, DMT_STRING, get_SecurityCertificate_SubjectAlt, NULL, NULL, NULL, BBFDM_BOTH}, {"SignatureAlgorithm", &DMREAD, DMT_STRING, get_SecurityCertificate_SignatureAlgorithm, NULL, NULL, NULL, BBFDM_BOTH}, {0} };