~cviethen/hipl/pisa-pairing-tng

« back to all changes in this revision

Viewing changes to lib/core/certtools.c

  • Committer: Diego Biurrun
  • Date: 2012-01-06 15:15:52 UTC
  • Revision ID: diego@biurrun.de-20120106151552-398gxjsi8krrwfnu
certtools: Clean up error handling; eliminate some HIP_IFELs; whitespace.

Show diffs side-by-side

added added

removed removed

Lines of Context:
362
362
 *                        cert object, also contains the char table where
363
363
 *                        the cert object is to be stored
364
364
 *
365
 
 * @return 0 if ok -1 if error
 
365
 * @return always 0
366
366
 */
367
367
static int cert_spki_build_cert(struct hip_cert_spki_info *minimal_content)
368
368
{
369
 
    int  err      = 0;
370
369
    char needed[] = "(cert )";
371
370
    memset(minimal_content->public_key, '\0', sizeof(minimal_content->public_key));
372
371
    memset(minimal_content->cert, '\0', sizeof(minimal_content->cert));
373
372
    memset(minimal_content->signature, '\0', sizeof(minimal_content->signature));
374
373
    sprintf(minimal_content->cert, "%s", needed);
375
374
 
376
 
    return err;
 
375
    return 0;
377
376
}
378
377
 
379
378
/**
395
394
 
396
395
    tmp_cert = calloc(1, strlen(to->cert) + strlen(what) + 1);
397
396
    if (!tmp_cert) {
398
 
        return -1;
 
397
        return -ENOMEM;
399
398
    }
400
399
    /* Compiling the regular expression */
401
400
    HIP_IFEL(regcomp(&re, after, REG_EXTENDED), -1,
583
582
 */
584
583
int hip_cert_spki_char2certinfo(char *from, struct hip_cert_spki_info *to)
585
584
{
586
 
    int err = 0, start = 0, stop = 0;
 
585
    int start = 0, stop = 0;
587
586
    /*
588
587
     * p_rule looks for string "(public_key " after which there can be
589
588
     * pretty much anything until string "|)))" is encountered.
604
603
    char s_rule[] = "[(]signature [ A-Za-z0-9+/|()=]*[|][)][)]";
605
604
 
606
605
    /* Look for the public key */
607
 
    HIP_IFEL(hip_cert_regex(p_rule, from, &start, &stop), -1,
608
 
             "Failed to run hip_cert_regex (public-key)\n");
 
606
    if (hip_cert_regex(p_rule, from, &start, &stop)) {
 
607
        HIP_ERROR("Failed to run hip_cert_regex (public-key)\n");
 
608
        return -1;
 
609
    }
609
610
    snprintf(to->public_key, stop - start + 1, "%s", &from[start]);
610
611
 
611
612
    /* Look for the cert sequence */
612
613
    start = stop = 0;
613
 
    HIP_IFEL(hip_cert_regex(c_rule, from, &start, &stop), -1,
614
 
             "Failed to run hip_cert_regex (cert)\n");
 
614
    if (hip_cert_regex(c_rule, from, &start, &stop)) {
 
615
        HIP_ERROR("Failed to run hip_cert_regex (cert)\n");
 
616
        return -1;
 
617
    }
615
618
    snprintf(to->cert, stop - start + 1, "%s", &from[start]);
616
619
 
617
620
    /* look for the signature sequence */
618
621
    start = stop = 0;
619
 
    HIP_IFEL(hip_cert_regex(s_rule, from, &start, &stop), -1,
620
 
             "Failed to run hip_cert_regex (signature)\n");
 
622
    if (hip_cert_regex(s_rule, from, &start, &stop)) {
 
623
        HIP_ERROR("Failed to run hip_cert_regex (signature)\n");
 
624
        return -1;
 
625
    }
621
626
    snprintf(to->signature, stop - start + 1, "%s", &from[start]);
622
627
 
623
 
out_err:
624
 
    return err;
 
628
    return 0;
625
629
}
626
630
 
627
631
/**
640
644
    struct hip_common               *msg;
641
645
    const struct hip_cert_spki_info *returned;
642
646
 
643
 
    HIP_IFEL(!(msg = malloc(HIP_MAX_PACKET)), -1,
644
 
             "Malloc for msg failed\n");
 
647
    if (!(msg = malloc(HIP_MAX_PACKET))) {
 
648
        HIP_ERROR("Malloc for msg failed\n");
 
649
        return -ENOMEM;
 
650
    }
645
651
    hip_msg_init(msg);
646
652
    /* build the msg to be sent to the daemon */
647
653
    HIP_IFEL(hip_build_user_hdr(msg, HIP_MSG_CERT_SPKI_VERIFY, 0), -1,
686
692
    struct hip_common               *msg;
687
693
    const struct hip_cert_x509_resp *p;
688
694
 
689
 
    HIP_IFEL(!(msg = malloc(HIP_MAX_PACKET)), -1,
690
 
             "Malloc for msg failed\n");
 
695
    if (!(msg = malloc(HIP_MAX_PACKET))) {
 
696
        HIP_ERROR("Malloc for msg failed\n");
 
697
        return -ENOMEM;
 
698
    }
691
699
    hip_msg_init(msg);
692
700
    /* build the msg to be sent to the daemon */
693
701
 
727
735
    struct hip_common               *msg;
728
736
    const struct hip_cert_x509_resp *received;
729
737
 
730
 
    HIP_IFEL(!(msg = malloc(HIP_MAX_PACKET)), -1,
731
 
             "Malloc for msg failed\n");
 
738
    if (!(msg = malloc(HIP_MAX_PACKET))) {
 
739
        HIP_ERROR("Malloc for msg failed\n");
 
740
        return -ENOMEM;
 
741
    }
732
742
    hip_msg_init(msg);
733
743
 
734
744
    /* build the msg to be sent to the daemon */
773
783
 *       the conf with NCONF_free().
774
784
 *
775
785
 */
776
 
STACK_OF(CONF_VALUE) * hip_cert_read_conf_section(const char *section_name,
777
 
                                                  CONF * conf)
 
786
STACK_OF(CONF_VALUE) *hip_cert_read_conf_section(const char *section_name,
 
787
                                                 CONF *conf)
778
788
{
779
 
    long err = 0;
780
 
    STACK_OF(CONF_VALUE) * sec = NULL;
 
789
    long                  err = 0;
 
790
    STACK_OF(CONF_VALUE) *sec = NULL;
781
791
 
782
792
    /* XXTODO conf is opened and reopened here why -Samu */
783
793
    conf = NCONF_new(NCONF_default());
784
 
    HIP_IFEL(!NCONF_load(conf, HIP_CERT_CONF_PATH, &err),
785
 
             -1, "Error opening the configuration file");
786
 
 
787
 
    HIP_IFEL(!(sec = NCONF_get_section(conf, section_name)), -1,
788
 
             "Section %s was not in the configuration (%s)\n",
789
 
             section_name, HIP_CERT_CONF_PATH);
790
 
 
791
 
out_err:
792
 
    if (err == -1) {
793
 
        return NULL;
794
 
    }
 
794
    if (!NCONF_load(conf, HIP_CERT_CONF_PATH, &err)) {
 
795
        HIP_ERROR("Error opening the configuration file");
 
796
        return NULL;
 
797
    }
 
798
    if (!(sec = NCONF_get_section(conf, section_name))) {
 
799
        HIP_ERROR("Section %s was not in the configuration (%s)\n",
 
800
                  section_name, HIP_CERT_CONF_PATH);
 
801
        return NULL;
 
802
    }
 
803
 
795
804
    return sec;
796
805
}
797
806
 
806
815
    CONF *conf = NULL;
807
816
 
808
817
    conf = NCONF_new(NCONF_default());
809
 
    HIP_IFEL(!NCONF_load(conf, HIP_CERT_CONF_PATH, &err),
810
 
             -1, "Error opening the configuration file");
811
 
out_err:
812
 
    if (err == -1) {
 
818
    if (!NCONF_load(conf, HIP_CERT_CONF_PATH, &err)) {
 
819
        HIP_ERROR("Error opening the configuration file");
813
820
        return NULL;
814
821
    }
815
822
    return conf;
836
843
    *start = *stop = 0;
837
844
 
838
845
    /* Compiling the regular expression */
839
 
    HIP_IFEL(regcomp(&re, what, REG_EXTENDED), -1,
840
 
             "Compilation of the regular expression failed\n");
 
846
    if (regcomp(&re, what, REG_EXTENDED)) {
 
847
        HIP_ERROR("Compilation of the regular expression failed\n");
 
848
        return -1;
 
849
    }
841
850
    /* Running the regular expression */
842
851
    // TODO this might need to be an error!?
843
852
    // this needs to be separated to found, not found, and error -Samu