diff options
Diffstat (limited to 'src/lib/libcrypto/pem/pem_all.c')
| -rw-r--r-- | src/lib/libcrypto/pem/pem_all.c | 233 |
1 files changed, 113 insertions, 120 deletions
diff --git a/src/lib/libcrypto/pem/pem_all.c b/src/lib/libcrypto/pem/pem_all.c index 07963314c9..66cbc7eb82 100644 --- a/src/lib/libcrypto/pem/pem_all.c +++ b/src/lib/libcrypto/pem/pem_all.c | |||
| @@ -55,6 +55,59 @@ | |||
| 55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | /* ==================================================================== | ||
| 59 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | ||
| 60 | * | ||
| 61 | * Redistribution and use in source and binary forms, with or without | ||
| 62 | * modification, are permitted provided that the following conditions | ||
| 63 | * are met: | ||
| 64 | * | ||
| 65 | * 1. Redistributions of source code must retain the above copyright | ||
| 66 | * notice, this list of conditions and the following disclaimer. | ||
| 67 | * | ||
| 68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 69 | * notice, this list of conditions and the following disclaimer in | ||
| 70 | * the documentation and/or other materials provided with the | ||
| 71 | * distribution. | ||
| 72 | * | ||
| 73 | * 3. All advertising materials mentioning features or use of this | ||
| 74 | * software must display the following acknowledgment: | ||
| 75 | * "This product includes software developed by the OpenSSL Project | ||
| 76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 77 | * | ||
| 78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 79 | * endorse or promote products derived from this software without | ||
| 80 | * prior written permission. For written permission, please contact | ||
| 81 | * openssl-core@openssl.org. | ||
| 82 | * | ||
| 83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 84 | * nor may "OpenSSL" appear in their names without prior written | ||
| 85 | * permission of the OpenSSL Project. | ||
| 86 | * | ||
| 87 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 88 | * acknowledgment: | ||
| 89 | * "This product includes software developed by the OpenSSL Project | ||
| 90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 91 | * | ||
| 92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 104 | * ==================================================================== | ||
| 105 | * | ||
| 106 | * This product includes cryptographic software written by Eric Young | ||
| 107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 108 | * Hudson (tjh@cryptsoft.com). | ||
| 109 | * | ||
| 110 | */ | ||
| 58 | 111 | ||
| 59 | #include <stdio.h> | 112 | #include <stdio.h> |
| 60 | #undef SSLEAY_MACROS | 113 | #undef SSLEAY_MACROS |
| @@ -64,7 +117,15 @@ | |||
| 64 | #include <openssl/x509.h> | 117 | #include <openssl/x509.h> |
| 65 | #include <openssl/pkcs7.h> | 118 | #include <openssl/pkcs7.h> |
| 66 | #include <openssl/pem.h> | 119 | #include <openssl/pem.h> |
| 67 | #include <openssl/fips.h> | 120 | #ifndef OPENSSL_NO_RSA |
| 121 | #include <openssl/rsa.h> | ||
| 122 | #endif | ||
| 123 | #ifndef OPENSSL_NO_DSA | ||
| 124 | #include <openssl/dsa.h> | ||
| 125 | #endif | ||
| 126 | #ifndef OPENSSL_NO_DH | ||
| 127 | #include <openssl/dh.h> | ||
| 128 | #endif | ||
| 68 | 129 | ||
| 69 | #ifndef OPENSSL_NO_RSA | 130 | #ifndef OPENSSL_NO_RSA |
| 70 | static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa); | 131 | static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa); |
| @@ -73,6 +134,10 @@ static RSA *pkey_get_rsa(EVP_PKEY *key, RSA **rsa); | |||
| 73 | static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa); | 134 | static DSA *pkey_get_dsa(EVP_PKEY *key, DSA **dsa); |
| 74 | #endif | 135 | #endif |
| 75 | 136 | ||
| 137 | #ifndef OPENSSL_NO_EC | ||
| 138 | static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey); | ||
| 139 | #endif | ||
| 140 | |||
| 76 | IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ) | 141 | IMPLEMENT_PEM_rw(X509_REQ, X509_REQ, PEM_STRING_X509_REQ, X509_REQ) |
| 77 | 142 | ||
| 78 | IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ) | 143 | IMPLEMENT_PEM_write(X509_REQ_NEW, X509_REQ, PEM_STRING_X509_REQ_OLD, X509_REQ) |
| @@ -129,50 +194,8 @@ RSA *PEM_read_RSAPrivateKey(FILE *fp, RSA **rsa, pem_password_cb *cb, | |||
| 129 | 194 | ||
| 130 | #endif | 195 | #endif |
| 131 | 196 | ||
| 132 | #ifdef OPENSSL_FIPS | 197 | IMPLEMENT_PEM_write_cb_const(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey) |
| 133 | 198 | IMPLEMENT_PEM_rw_const(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey) | |
| 134 | int PEM_write_bio_RSAPrivateKey(BIO *bp, RSA *x, const EVP_CIPHER *enc, | ||
| 135 | unsigned char *kstr, int klen, | ||
| 136 | pem_password_cb *cb, void *u) | ||
| 137 | { | ||
| 138 | EVP_PKEY *k; | ||
| 139 | int ret; | ||
| 140 | k = EVP_PKEY_new(); | ||
| 141 | if (!k) | ||
| 142 | return 0; | ||
| 143 | EVP_PKEY_set1_RSA(k, x); | ||
| 144 | |||
| 145 | ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u); | ||
| 146 | EVP_PKEY_free(k); | ||
| 147 | return ret; | ||
| 148 | } | ||
| 149 | |||
| 150 | #ifndef OPENSSL_NO_FP_API | ||
| 151 | int PEM_write_RSAPrivateKey(FILE *fp, RSA *x, const EVP_CIPHER *enc, | ||
| 152 | unsigned char *kstr, int klen, | ||
| 153 | pem_password_cb *cb, void *u) | ||
| 154 | { | ||
| 155 | EVP_PKEY *k; | ||
| 156 | int ret; | ||
| 157 | k = EVP_PKEY_new(); | ||
| 158 | if (!k) | ||
| 159 | return 0; | ||
| 160 | |||
| 161 | EVP_PKEY_set1_RSA(k, x); | ||
| 162 | |||
| 163 | ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u); | ||
| 164 | EVP_PKEY_free(k); | ||
| 165 | return ret; | ||
| 166 | } | ||
| 167 | #endif | ||
| 168 | |||
| 169 | #else | ||
| 170 | |||
| 171 | IMPLEMENT_PEM_write_cb(RSAPrivateKey, RSA, PEM_STRING_RSA, RSAPrivateKey) | ||
| 172 | |||
| 173 | #endif | ||
| 174 | |||
| 175 | IMPLEMENT_PEM_rw(RSAPublicKey, RSA, PEM_STRING_RSA_PUBLIC, RSAPublicKey) | ||
| 176 | IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY) | 199 | IMPLEMENT_PEM_rw(RSA_PUBKEY, RSA, PEM_STRING_PUBLIC, RSA_PUBKEY) |
| 177 | 200 | ||
| 178 | #endif | 201 | #endif |
| @@ -201,69 +224,73 @@ DSA *PEM_read_bio_DSAPrivateKey(BIO *bp, DSA **dsa, pem_password_cb *cb, | |||
| 201 | return pkey_get_dsa(pktmp, dsa); | 224 | return pkey_get_dsa(pktmp, dsa); |
| 202 | } | 225 | } |
| 203 | 226 | ||
| 227 | IMPLEMENT_PEM_write_cb_const(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) | ||
| 228 | IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) | ||
| 204 | 229 | ||
| 205 | #ifdef OPENSSL_FIPS | 230 | #ifndef OPENSSL_NO_FP_API |
| 206 | 231 | ||
| 207 | int PEM_write_bio_DSAPrivateKey(BIO *bp, DSA *x, const EVP_CIPHER *enc, | 232 | DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, |
| 208 | unsigned char *kstr, int klen, | 233 | void *u) |
| 209 | pem_password_cb *cb, void *u) | ||
| 210 | { | 234 | { |
| 211 | EVP_PKEY *k; | 235 | EVP_PKEY *pktmp; |
| 212 | int ret; | 236 | pktmp = PEM_read_PrivateKey(fp, NULL, cb, u); |
| 213 | k = EVP_PKEY_new(); | 237 | return pkey_get_dsa(pktmp, dsa); |
| 214 | if (!k) | ||
| 215 | return 0; | ||
| 216 | EVP_PKEY_set1_DSA(k, x); | ||
| 217 | |||
| 218 | ret = PEM_write_bio_PrivateKey(bp, k, enc, kstr, klen, cb, u); | ||
| 219 | EVP_PKEY_free(k); | ||
| 220 | return ret; | ||
| 221 | } | 238 | } |
| 222 | 239 | ||
| 223 | #ifndef OPENSSL_NO_FP_API | 240 | #endif |
| 224 | int PEM_write_DSAPrivateKey(FILE *fp, DSA *x, const EVP_CIPHER *enc, | 241 | |
| 225 | unsigned char *kstr, int klen, | 242 | IMPLEMENT_PEM_rw_const(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams) |
| 226 | pem_password_cb *cb, void *u) | 243 | |
| 244 | #endif | ||
| 245 | |||
| 246 | |||
| 247 | #ifndef OPENSSL_NO_EC | ||
| 248 | static EC_KEY *pkey_get_eckey(EVP_PKEY *key, EC_KEY **eckey) | ||
| 227 | { | 249 | { |
| 228 | EVP_PKEY *k; | 250 | EC_KEY *dtmp; |
| 229 | int ret; | 251 | if(!key) return NULL; |
| 230 | k = EVP_PKEY_new(); | 252 | dtmp = EVP_PKEY_get1_EC_KEY(key); |
| 231 | if (!k) | 253 | EVP_PKEY_free(key); |
| 232 | return 0; | 254 | if(!dtmp) return NULL; |
| 233 | EVP_PKEY_set1_DSA(k, x); | 255 | if(eckey) |
| 234 | ret = PEM_write_PrivateKey(fp, k, enc, kstr, klen, cb, u); | 256 | { |
| 235 | EVP_PKEY_free(k); | 257 | EC_KEY_free(*eckey); |
| 236 | return ret; | 258 | *eckey = dtmp; |
| 259 | } | ||
| 260 | return dtmp; | ||
| 237 | } | 261 | } |
| 238 | #endif | ||
| 239 | 262 | ||
| 240 | #else | 263 | EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb, |
| 264 | void *u) | ||
| 265 | { | ||
| 266 | EVP_PKEY *pktmp; | ||
| 267 | pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u); | ||
| 268 | return pkey_get_eckey(pktmp, key); | ||
| 269 | } | ||
| 241 | 270 | ||
| 242 | IMPLEMENT_PEM_write_cb(DSAPrivateKey, DSA, PEM_STRING_DSA, DSAPrivateKey) | 271 | IMPLEMENT_PEM_rw_const(ECPKParameters, EC_GROUP, PEM_STRING_ECPARAMETERS, ECPKParameters) |
| 243 | 272 | ||
| 244 | #endif | 273 | IMPLEMENT_PEM_write_cb(ECPrivateKey, EC_KEY, PEM_STRING_ECPRIVATEKEY, ECPrivateKey) |
| 245 | 274 | ||
| 246 | IMPLEMENT_PEM_rw(DSA_PUBKEY, DSA, PEM_STRING_PUBLIC, DSA_PUBKEY) | 275 | IMPLEMENT_PEM_rw(EC_PUBKEY, EC_KEY, PEM_STRING_PUBLIC, EC_PUBKEY) |
| 247 | 276 | ||
| 248 | #ifndef OPENSSL_NO_FP_API | 277 | #ifndef OPENSSL_NO_FP_API |
| 249 | 278 | ||
| 250 | DSA *PEM_read_DSAPrivateKey(FILE *fp, DSA **dsa, pem_password_cb *cb, | 279 | EC_KEY *PEM_read_ECPrivateKey(FILE *fp, EC_KEY **eckey, pem_password_cb *cb, |
| 251 | void *u) | 280 | void *u) |
| 252 | { | 281 | { |
| 253 | EVP_PKEY *pktmp; | 282 | EVP_PKEY *pktmp; |
| 254 | pktmp = PEM_read_PrivateKey(fp, NULL, cb, u); | 283 | pktmp = PEM_read_PrivateKey(fp, NULL, cb, u); |
| 255 | return pkey_get_dsa(pktmp, dsa); | 284 | return pkey_get_eckey(pktmp, eckey); |
| 256 | } | 285 | } |
| 257 | 286 | ||
| 258 | #endif | 287 | #endif |
| 259 | 288 | ||
| 260 | IMPLEMENT_PEM_rw(DSAparams, DSA, PEM_STRING_DSAPARAMS, DSAparams) | ||
| 261 | |||
| 262 | #endif | 289 | #endif |
| 263 | 290 | ||
| 264 | #ifndef OPENSSL_NO_DH | 291 | #ifndef OPENSSL_NO_DH |
| 265 | 292 | ||
| 266 | IMPLEMENT_PEM_rw(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) | 293 | IMPLEMENT_PEM_rw_const(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) |
| 267 | 294 | ||
| 268 | #endif | 295 | #endif |
| 269 | 296 | ||
| @@ -274,42 +301,8 @@ IMPLEMENT_PEM_rw(DHparams, DH, PEM_STRING_DHPARAMS, DHparams) | |||
| 274 | * (When reading, parameter PEM_STRING_EVP_PKEY is a wildcard for anything | 301 | * (When reading, parameter PEM_STRING_EVP_PKEY is a wildcard for anything |
| 275 | * appropriate.) | 302 | * appropriate.) |
| 276 | */ | 303 | */ |
| 277 | 304 | IMPLEMENT_PEM_write_cb(PrivateKey, EVP_PKEY, ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:\ | |
| 278 | #ifdef OPENSSL_FIPS | 305 | (x->type == EVP_PKEY_RSA)?PEM_STRING_RSA:PEM_STRING_ECPRIVATEKEY), PrivateKey) |
| 279 | |||
| 280 | int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, | ||
| 281 | unsigned char *kstr, int klen, | ||
| 282 | pem_password_cb *cb, void *u) | ||
| 283 | { | ||
| 284 | if (FIPS_mode()) | ||
| 285 | return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, | ||
| 286 | (char *)kstr, klen, cb, u); | ||
| 287 | else | ||
| 288 | return PEM_ASN1_write_bio((int (*)())i2d_PrivateKey, | ||
| 289 | (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA), | ||
| 290 | bp,(char *)x,enc,kstr,klen,cb,u); | ||
| 291 | } | ||
| 292 | |||
| 293 | #ifndef OPENSSL_NO_FP_API | ||
| 294 | int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, | ||
| 295 | unsigned char *kstr, int klen, | ||
| 296 | pem_password_cb *cb, void *u) | ||
| 297 | { | ||
| 298 | if (FIPS_mode()) | ||
| 299 | return PEM_write_PKCS8PrivateKey(fp, x, enc, | ||
| 300 | (char *)kstr, klen, cb, u); | ||
| 301 | else | ||
| 302 | return PEM_ASN1_write((int (*)())i2d_PrivateKey, | ||
| 303 | (((x)->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA), | ||
| 304 | fp,(char *)x,enc,kstr,klen,cb,u); | ||
| 305 | } | ||
| 306 | #endif | ||
| 307 | |||
| 308 | #else | ||
| 309 | |||
| 310 | IMPLEMENT_PEM_write_cb(PrivateKey, EVP_PKEY, ((x->type == EVP_PKEY_DSA)?PEM_STRING_DSA:PEM_STRING_RSA), PrivateKey) | ||
| 311 | |||
| 312 | #endif | ||
| 313 | 306 | ||
| 314 | IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) | 307 | IMPLEMENT_PEM_rw(PUBKEY, EVP_PKEY, PEM_STRING_PUBLIC, PUBKEY) |
| 315 | 308 | ||
