diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs12/p12_kiss.c')
| -rw-r--r-- | src/lib/libcrypto/pkcs12/p12_kiss.c | 254 |
1 files changed, 0 insertions, 254 deletions
diff --git a/src/lib/libcrypto/pkcs12/p12_kiss.c b/src/lib/libcrypto/pkcs12/p12_kiss.c deleted file mode 100644 index ee257ffbad..0000000000 --- a/src/lib/libcrypto/pkcs12/p12_kiss.c +++ /dev/null | |||
| @@ -1,254 +0,0 @@ | |||
| 1 | /* p12_kiss.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 1999. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/pkcs12.h> | ||
| 62 | |||
| 63 | /* Simplified PKCS#12 routines */ | ||
| 64 | |||
| 65 | static int parse_pk12( PKCS12 *p12, const char *pass, int passlen, | ||
| 66 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca); | ||
| 67 | |||
| 68 | static int parse_bags( STACK *bags, const char *pass, int passlen, | ||
| 69 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca, | ||
| 70 | ASN1_OCTET_STRING **keyid, char *keymatch); | ||
| 71 | |||
| 72 | static int parse_bag( PKCS12_SAFEBAG *bag, const char *pass, int passlen, | ||
| 73 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca, | ||
| 74 | ASN1_OCTET_STRING **keyid, char *keymatch); | ||
| 75 | |||
| 76 | /* Parse and decrypt a PKCS#12 structure returning user key, user cert | ||
| 77 | * and other (CA) certs. Note either ca should be NULL, *ca should be NULL, | ||
| 78 | * or it should point to a valid STACK structure. pkey and cert can be | ||
| 79 | * passed unitialised. | ||
| 80 | */ | ||
| 81 | |||
| 82 | int PKCS12_parse (PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert, | ||
| 83 | STACK_OF(X509) **ca) | ||
| 84 | { | ||
| 85 | |||
| 86 | /* Check for NULL PKCS12 structure */ | ||
| 87 | |||
| 88 | if(!p12) | ||
| 89 | { | ||
| 90 | PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER); | ||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | /* Allocate stack for ca certificates if needed */ | ||
| 95 | if ((ca != NULL) && (*ca == NULL)) | ||
| 96 | { | ||
| 97 | if (!(*ca = sk_X509_new(NULL))) | ||
| 98 | { | ||
| 99 | PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE); | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | if(pkey) *pkey = NULL; | ||
| 105 | if(cert) *cert = NULL; | ||
| 106 | |||
| 107 | /* Check the mac */ | ||
| 108 | |||
| 109 | if (!PKCS12_verify_mac (p12, pass, -1)) | ||
| 110 | { | ||
| 111 | PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE); | ||
| 112 | goto err; | ||
| 113 | } | ||
| 114 | |||
| 115 | if (!parse_pk12 (p12, pass, -1, pkey, cert, ca)) | ||
| 116 | { | ||
| 117 | PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR); | ||
| 118 | goto err; | ||
| 119 | } | ||
| 120 | |||
| 121 | return 1; | ||
| 122 | |||
| 123 | err: | ||
| 124 | |||
| 125 | if (pkey && *pkey) EVP_PKEY_free (*pkey); | ||
| 126 | if (cert && *cert) X509_free (*cert); | ||
| 127 | if (ca) sk_X509_pop_free (*ca, X509_free); | ||
| 128 | return 0; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | /* Parse the outer PKCS#12 structure */ | ||
| 133 | |||
| 134 | static int parse_pk12 (PKCS12 *p12, const char *pass, int passlen, | ||
| 135 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca) | ||
| 136 | { | ||
| 137 | STACK *asafes, *bags; | ||
| 138 | int i, bagnid; | ||
| 139 | PKCS7 *p7; | ||
| 140 | ASN1_OCTET_STRING *keyid = NULL; | ||
| 141 | char keymatch = 0; | ||
| 142 | if (!( asafes = M_PKCS12_unpack_authsafes (p12))) return 0; | ||
| 143 | for (i = 0; i < sk_num (asafes); i++) { | ||
| 144 | p7 = (PKCS7 *) sk_value (asafes, i); | ||
| 145 | bagnid = OBJ_obj2nid (p7->type); | ||
| 146 | if (bagnid == NID_pkcs7_data) { | ||
| 147 | bags = M_PKCS12_unpack_p7data (p7); | ||
| 148 | } else if (bagnid == NID_pkcs7_encrypted) { | ||
| 149 | bags = M_PKCS12_unpack_p7encdata (p7, pass, passlen); | ||
| 150 | } else continue; | ||
| 151 | if (!bags) { | ||
| 152 | sk_pop_free (asafes, PKCS7_free); | ||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | if (!parse_bags(bags, pass, passlen, pkey, cert, ca, | ||
| 156 | &keyid, &keymatch)) { | ||
| 157 | sk_pop_free(bags, PKCS12_SAFEBAG_free); | ||
| 158 | sk_pop_free(asafes, PKCS7_free); | ||
| 159 | return 0; | ||
| 160 | } | ||
| 161 | sk_pop_free(bags, PKCS12_SAFEBAG_free); | ||
| 162 | } | ||
| 163 | sk_pop_free(asafes, PKCS7_free); | ||
| 164 | if (keyid) M_ASN1_OCTET_STRING_free(keyid); | ||
| 165 | return 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | static int parse_bags (STACK *bags, const char *pass, int passlen, | ||
| 170 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca, | ||
| 171 | ASN1_OCTET_STRING **keyid, char *keymatch) | ||
| 172 | { | ||
| 173 | int i; | ||
| 174 | for (i = 0; i < sk_num(bags); i++) { | ||
| 175 | if (!parse_bag((PKCS12_SAFEBAG *)sk_value (bags, i), | ||
| 176 | pass, passlen, pkey, cert, ca, keyid, | ||
| 177 | keymatch)) return 0; | ||
| 178 | } | ||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | #define MATCH_KEY 0x1 | ||
| 183 | #define MATCH_CERT 0x2 | ||
| 184 | #define MATCH_ALL 0x3 | ||
| 185 | |||
| 186 | static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen, | ||
| 187 | EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca, | ||
| 188 | ASN1_OCTET_STRING **keyid, | ||
| 189 | char *keymatch) | ||
| 190 | { | ||
| 191 | PKCS8_PRIV_KEY_INFO *p8; | ||
| 192 | X509 *x509; | ||
| 193 | ASN1_OCTET_STRING *lkey = NULL; | ||
| 194 | ASN1_TYPE *attrib; | ||
| 195 | |||
| 196 | |||
| 197 | if ((attrib = PKCS12_get_attr (bag, NID_localKeyID))) | ||
| 198 | lkey = attrib->value.octet_string; | ||
| 199 | |||
| 200 | /* Check for any local key id matching (if needed) */ | ||
| 201 | if (lkey && ((*keymatch & MATCH_ALL) != MATCH_ALL)) { | ||
| 202 | if (*keyid) { | ||
| 203 | if (M_ASN1_OCTET_STRING_cmp(*keyid, lkey)) lkey = NULL; | ||
| 204 | } else { | ||
| 205 | if (!(*keyid = M_ASN1_OCTET_STRING_dup(lkey))) { | ||
| 206 | PKCS12err(PKCS12_F_PARSE_BAGS,ERR_R_MALLOC_FAILURE); | ||
| 207 | return 0; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | switch (M_PKCS12_bag_type(bag)) | ||
| 213 | { | ||
| 214 | case NID_keyBag: | ||
| 215 | if (!lkey || !pkey) return 1; | ||
| 216 | if (!(*pkey = EVP_PKCS82PKEY(bag->value.keybag))) return 0; | ||
| 217 | *keymatch |= MATCH_KEY; | ||
| 218 | break; | ||
| 219 | |||
| 220 | case NID_pkcs8ShroudedKeyBag: | ||
| 221 | if (!lkey || !pkey) return 1; | ||
| 222 | if (!(p8 = M_PKCS12_decrypt_skey(bag, pass, passlen))) | ||
| 223 | return 0; | ||
| 224 | *pkey = EVP_PKCS82PKEY(p8); | ||
| 225 | PKCS8_PRIV_KEY_INFO_free(p8); | ||
| 226 | if (!(*pkey)) return 0; | ||
| 227 | *keymatch |= MATCH_KEY; | ||
| 228 | break; | ||
| 229 | |||
| 230 | case NID_certBag: | ||
| 231 | if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate ) | ||
| 232 | return 1; | ||
| 233 | if (!(x509 = M_PKCS12_certbag2x509(bag))) return 0; | ||
| 234 | if (lkey) { | ||
| 235 | *keymatch |= MATCH_CERT; | ||
| 236 | if (cert) *cert = x509; | ||
| 237 | } else { | ||
| 238 | if(ca) sk_X509_push (*ca, x509); | ||
| 239 | else X509_free(x509); | ||
| 240 | } | ||
| 241 | break; | ||
| 242 | |||
| 243 | case NID_safeContentsBag: | ||
| 244 | return parse_bags(bag->value.safes, pass, passlen, | ||
| 245 | pkey, cert, ca, keyid, keymatch); | ||
| 246 | break; | ||
| 247 | |||
| 248 | default: | ||
| 249 | return 1; | ||
| 250 | break; | ||
| 251 | } | ||
| 252 | return 1; | ||
| 253 | } | ||
| 254 | |||
