Home | History | Annotate | Line # | Download | only in helpers
      1 /*
      2  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 #include <stdio.h>
     11 #include <string.h>
     12 #include <stdlib.h>
     13 
     14 #include "internal/nelem.h"
     15 
     16 #include <openssl/pkcs12.h>
     17 #include <openssl/x509.h>
     18 #include <openssl/x509v3.h>
     19 #include <openssl/pem.h>
     20 
     21 #include "../testutil.h"
     22 
     23 /* -------------------------------------------------------------------------
     24  * PKCS#12 Test structures
     25  */
     26 
     27 /* Holds a set of Attributes */
     28 typedef struct pkcs12_attr {
     29     char *oid;
     30     char *value;
     31 } PKCS12_ATTR;
     32 
     33 /* Holds encryption parameters */
     34 typedef struct pkcs12_enc {
     35     int nid;
     36     const char *pass;
     37     int iter;
     38 } PKCS12_ENC;
     39 
     40 /* Set of variables required for constructing the PKCS#12 structure */
     41 typedef struct pkcs12_builder {
     42     const char *filename;
     43     int success;
     44     BIO *p12bio;
     45     STACK_OF(PKCS7) *safes;
     46     int safe_idx;
     47     STACK_OF(PKCS12_SAFEBAG) *bags;
     48     int bag_idx;
     49 } PKCS12_BUILDER;
     50 
     51 /* -------------------------------------------------------------------------
     52  * PKCS#12 Test function declarations
     53  */
     54 
     55 /* Global settings */
     56 void PKCS12_helper_set_write_files(int enable);
     57 void PKCS12_helper_set_legacy(int enable);
     58 void PKCS12_helper_set_libctx(OSSL_LIB_CTX *libctx);
     59 void PKCS12_helper_set_propq(const char *propq);
     60 
     61 /* Allocate and initialise a PKCS#12 builder object */
     62 PKCS12_BUILDER *new_pkcs12_builder(const char *filename);
     63 
     64 /* Finalise and free the PKCS#12 builder object, returning the success/fail flag */
     65 int end_pkcs12_builder(PKCS12_BUILDER *pb);
     66 
     67 /* Encode/build functions */
     68 void start_pkcs12(PKCS12_BUILDER *pb);
     69 void end_pkcs12(PKCS12_BUILDER *pb);
     70 void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
     71 
     72 void start_contentinfo(PKCS12_BUILDER *pb);
     73 void end_contentinfo(PKCS12_BUILDER *pb);
     74 void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc);
     75 
     76 void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
     77     const PKCS12_ATTR *attrs);
     78 void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
     79     const PKCS12_ATTR *attrs, const PKCS12_ENC *enc);
     80 void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
     81     const PKCS12_ATTR *attrs);
     82 void add_extra_attr(PKCS12_BUILDER *pb);
     83 
     84 /* Decode/check functions */
     85 void start_check_pkcs12(PKCS12_BUILDER *pb);
     86 void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
     87 void start_check_pkcs12_file(PKCS12_BUILDER *pb);
     88 void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
     89 void end_check_pkcs12(PKCS12_BUILDER *pb);
     90 
     91 void start_check_contentinfo(PKCS12_BUILDER *pb);
     92 void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc);
     93 void end_check_contentinfo(PKCS12_BUILDER *pb);
     94 
     95 void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
     96     const PKCS12_ATTR *attrs);
     97 void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
     98     const PKCS12_ATTR *attrs, const PKCS12_ENC *enc);
     99 void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
    100     const PKCS12_ATTR *attrs);
    101