diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs12/p12_decr.c')
| -rw-r--r-- | src/lib/libcrypto/pkcs12/p12_decr.c | 77 |
1 files changed, 34 insertions, 43 deletions
diff --git a/src/lib/libcrypto/pkcs12/p12_decr.c b/src/lib/libcrypto/pkcs12/p12_decr.c index d3d288e187..394af368f4 100644 --- a/src/lib/libcrypto/pkcs12/p12_decr.c +++ b/src/lib/libcrypto/pkcs12/p12_decr.c | |||
| @@ -65,10 +65,10 @@ | |||
| 65 | 65 | ||
| 66 | 66 | ||
| 67 | /* Encrypt/Decrypt a buffer based on password and algor, result in a | 67 | /* Encrypt/Decrypt a buffer based on password and algor, result in a |
| 68 | * Malloc'ed buffer | 68 | * OPENSSL_malloc'ed buffer |
| 69 | */ | 69 | */ |
| 70 | 70 | ||
| 71 | unsigned char * PKCS12_pbe_crypt (X509_ALGOR *algor, const char *pass, | 71 | unsigned char * PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass, |
| 72 | int passlen, unsigned char *in, int inlen, unsigned char **data, | 72 | int passlen, unsigned char *in, int inlen, unsigned char **data, |
| 73 | int *datalen, int en_de) | 73 | int *datalen, int en_de) |
| 74 | { | 74 | { |
| @@ -76,47 +76,48 @@ unsigned char * PKCS12_pbe_crypt (X509_ALGOR *algor, const char *pass, | |||
| 76 | int outlen, i; | 76 | int outlen, i; |
| 77 | EVP_CIPHER_CTX ctx; | 77 | EVP_CIPHER_CTX ctx; |
| 78 | 78 | ||
| 79 | EVP_CIPHER_CTX_init(&ctx); | ||
| 79 | /* Decrypt data */ | 80 | /* Decrypt data */ |
| 80 | if (!EVP_PBE_CipherInit (algor->algorithm, pass, passlen, | 81 | if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen, |
| 81 | algor->parameter, &ctx, en_de)) { | 82 | algor->parameter, &ctx, en_de)) { |
| 82 | PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR); | 83 | PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR); |
| 83 | return NULL; | 84 | return NULL; |
| 84 | } | 85 | } |
| 85 | 86 | ||
| 86 | if(!(out = Malloc (inlen + EVP_CIPHER_CTX_block_size(&ctx)))) { | 87 | if(!(out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(&ctx)))) { |
| 87 | PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,ERR_R_MALLOC_FAILURE); | 88 | PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,ERR_R_MALLOC_FAILURE); |
| 88 | return NULL; | 89 | goto err; |
| 89 | } | 90 | } |
| 90 | 91 | ||
| 91 | EVP_CipherUpdate (&ctx, out, &i, in, inlen); | 92 | EVP_CipherUpdate(&ctx, out, &i, in, inlen); |
| 92 | outlen = i; | 93 | outlen = i; |
| 93 | if(!EVP_CipherFinal (&ctx, out + i, &i)) { | 94 | if(!EVP_CipherFinal_ex(&ctx, out + i, &i)) { |
| 94 | Free (out); | 95 | OPENSSL_free(out); |
| 96 | out = NULL; | ||
| 95 | PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,PKCS12_R_PKCS12_CIPHERFINAL_ERROR); | 97 | PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,PKCS12_R_PKCS12_CIPHERFINAL_ERROR); |
| 96 | return NULL; | 98 | goto err; |
| 97 | } | 99 | } |
| 98 | outlen += i; | 100 | outlen += i; |
| 99 | if (datalen) *datalen = outlen; | 101 | if (datalen) *datalen = outlen; |
| 100 | if (data) *data = out; | 102 | if (data) *data = out; |
| 103 | err: | ||
| 104 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
| 101 | return out; | 105 | return out; |
| 102 | 106 | ||
| 103 | } | 107 | } |
| 104 | 108 | ||
| 105 | /* Decrypt an OCTET STRING and decode ASN1 structure | 109 | /* Decrypt an OCTET STRING and decode ASN1 structure |
| 106 | * if seq & 1 'obj' is a stack of structures to be encoded | 110 | * if zbuf set zero buffer after use. |
| 107 | * if seq & 2 zero buffer after use | ||
| 108 | * as a sequence. | ||
| 109 | */ | 111 | */ |
| 110 | 112 | ||
| 111 | char * PKCS12_decrypt_d2i (X509_ALGOR *algor, char * (*d2i)(), | 113 | void * PKCS12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it, |
| 112 | void (*free_func)(), const char *pass, int passlen, | 114 | const char *pass, int passlen, ASN1_OCTET_STRING *oct, int zbuf) |
| 113 | ASN1_OCTET_STRING *oct, int seq) | ||
| 114 | { | 115 | { |
| 115 | unsigned char *out, *p; | 116 | unsigned char *out, *p; |
| 116 | char *ret; | 117 | void *ret; |
| 117 | int outlen; | 118 | int outlen; |
| 118 | 119 | ||
| 119 | if (!PKCS12_pbe_crypt (algor, pass, passlen, oct->data, oct->length, | 120 | if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length, |
| 120 | &out, &outlen, 0)) { | 121 | &out, &outlen, 0)) { |
| 121 | PKCS12err(PKCS12_F_PKCS12_DECRYPT_D2I,PKCS12_R_PKCS12_PBE_CRYPT_ERROR); | 122 | PKCS12err(PKCS12_F_PKCS12_DECRYPT_D2I,PKCS12_R_PKCS12_PBE_CRYPT_ERROR); |
| 122 | return NULL; | 123 | return NULL; |
| @@ -134,52 +135,42 @@ char * PKCS12_decrypt_d2i (X509_ALGOR *algor, char * (*d2i)(), | |||
| 134 | fclose(op); | 135 | fclose(op); |
| 135 | } | 136 | } |
| 136 | #endif | 137 | #endif |
| 137 | if (seq & 1) ret = (char *) d2i_ASN1_SET(NULL, &p, outlen, d2i, | 138 | ret = ASN1_item_d2i(NULL, &p, outlen, it); |
| 138 | free_func, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); | 139 | if (zbuf) memset(out, 0, outlen); |
| 139 | else ret = d2i(NULL, &p, outlen); | ||
| 140 | if (seq & 2) memset(out, 0, outlen); | ||
| 141 | if(!ret) PKCS12err(PKCS12_F_PKCS12_DECRYPT_D2I,PKCS12_R_DECODE_ERROR); | 140 | if(!ret) PKCS12err(PKCS12_F_PKCS12_DECRYPT_D2I,PKCS12_R_DECODE_ERROR); |
| 142 | Free (out); | 141 | OPENSSL_free(out); |
| 143 | return ret; | 142 | return ret; |
| 144 | } | 143 | } |
| 145 | 144 | ||
| 146 | /* Encode ASN1 structure and encrypt, return OCTET STRING | 145 | /* Encode ASN1 structure and encrypt, return OCTET STRING |
| 147 | * if 'seq' is non-zero 'obj' is a stack of structures to be encoded | 146 | * if zbuf set zero encoding. |
| 148 | * as a sequence | ||
| 149 | */ | 147 | */ |
| 150 | 148 | ||
| 151 | ASN1_OCTET_STRING *PKCS12_i2d_encrypt (X509_ALGOR *algor, int (*i2d)(), | 149 | ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor, const ASN1_ITEM *it, |
| 152 | const char *pass, int passlen, | 150 | const char *pass, int passlen, |
| 153 | char *obj, int seq) | 151 | void *obj, int zbuf) |
| 154 | { | 152 | { |
| 155 | ASN1_OCTET_STRING *oct; | 153 | ASN1_OCTET_STRING *oct; |
| 156 | unsigned char *in, *p; | 154 | unsigned char *in = NULL; |
| 157 | int inlen; | 155 | int inlen; |
| 158 | if (!(oct = ASN1_OCTET_STRING_new ())) { | 156 | if (!(oct = M_ASN1_OCTET_STRING_new ())) { |
| 159 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,ERR_R_MALLOC_FAILURE); | 157 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,ERR_R_MALLOC_FAILURE); |
| 160 | return NULL; | 158 | return NULL; |
| 161 | } | 159 | } |
| 162 | if (seq) inlen = i2d_ASN1_SET((STACK *)obj, NULL, i2d, V_ASN1_SEQUENCE, | 160 | inlen = ASN1_item_i2d(obj, &in, it); |
| 163 | V_ASN1_UNIVERSAL, IS_SEQUENCE); | 161 | if (!in) { |
| 164 | else inlen = i2d (obj, NULL); | ||
| 165 | if (!inlen) { | ||
| 166 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,PKCS12_R_ENCODE_ERROR); | 162 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,PKCS12_R_ENCODE_ERROR); |
| 167 | return NULL; | 163 | return NULL; |
| 168 | } | 164 | } |
| 169 | if (!(in = Malloc (inlen))) { | 165 | if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data, |
| 170 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 171 | return NULL; | ||
| 172 | } | ||
| 173 | p = in; | ||
| 174 | if (seq) i2d_ASN1_SET((STACK *)obj, &p, i2d, V_ASN1_SEQUENCE, | ||
| 175 | V_ASN1_UNIVERSAL, IS_SEQUENCE); | ||
| 176 | else i2d (obj, &p); | ||
| 177 | if (!PKCS12_pbe_crypt (algor, pass, passlen, in, inlen, &oct->data, | ||
| 178 | &oct->length, 1)) { | 166 | &oct->length, 1)) { |
| 179 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,PKCS12_R_ENCRYPT_ERROR); | 167 | PKCS12err(PKCS12_F_PKCS12_I2D_ENCRYPT,PKCS12_R_ENCRYPT_ERROR); |
| 180 | Free(in); | 168 | OPENSSL_free(in); |
| 181 | return NULL; | 169 | return NULL; |
| 182 | } | 170 | } |
| 183 | Free (in); | 171 | if (zbuf) memset(in, 0, inlen); |
| 172 | OPENSSL_free(in); | ||
| 184 | return oct; | 173 | return oct; |
| 185 | } | 174 | } |
| 175 | |||
| 176 | IMPLEMENT_PKCS12_STACK_OF(PKCS7) | ||
