Changeset 4647
- Timestamp:
- 02/17/07 16:22:38 (2 years ago)
- Files:
-
- libwired/trunk/libwired/misc/wi-crypto.c (modified) (7 diffs)
- libwired/trunk/libwired/misc/wi-crypto.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libwired/trunk/libwired/misc/wi-crypto.c
r4645 r4647 47 47 RSA *rsa; 48 48 wi_data_t *public_key; 49 wi_data_t *private_key; 49 50 }; 50 51 51 52 static void _wi_rsa_dealloc(wi_runtime_instance_t *); 52 53 static wi_string_t * _wi_rsa_description(wi_runtime_instance_t *); 53 54 static wi_data_t * _wi_rsa_public_key(wi_rsa_t *);55 54 56 55 static wi_runtime_id_t _wi_rsa_runtime_id = WI_RUNTIME_ID_NULL; … … 132 131 } 133 132 134 rsa->public_key = wi_retain(_wi_rsa_public_key(rsa));135 136 if(!rsa->public_key) {137 wi_release(rsa);138 139 return NULL;140 }141 142 133 return rsa; 143 134 } … … 170 161 } 171 162 172 rsa->public_key = wi_retain(_wi_rsa_public_key(rsa)); 173 174 if(!rsa->public_key) { 163 return rsa; 164 } 165 166 167 168 wi_rsa_t * wi_rsa_init_with_private_key(wi_rsa_t *rsa, wi_data_t *data) { 169 const unsigned char *buffer; 170 long length; 171 172 buffer = wi_data_bytes(data); 173 length = wi_data_length(data); 174 175 rsa->rsa = d2i_RSAPrivateKey(NULL, &buffer, length); 176 177 if(!rsa->rsa) { 178 wi_error_set_openssl_error(); 179 175 180 wi_release(rsa); 176 177 return NULL; 178 } 181 182 return NULL; 183 } 184 185 rsa->private_key = wi_retain(data); 179 186 180 187 return rsa; … … 184 191 185 192 wi_rsa_t * wi_rsa_init_with_public_key(wi_rsa_t *rsa, wi_data_t *data) { 186 unsigned char *buffer;187 long length;188 189 buffer = (unsigned char *)wi_data_bytes(data);193 const unsigned char *buffer; 194 long length; 195 196 buffer = wi_data_bytes(data); 190 197 length = wi_data_length(data); 191 198 192 rsa->rsa = d2i_RSA _PUBKEY(NULL,&buffer, length);199 rsa->rsa = d2i_RSAPublicKey(NULL, (const unsigned char **) &buffer, length); 193 200 194 201 if(!rsa->rsa) { … … 211 218 212 219 RSA_free(rsa->rsa); 220 213 221 wi_release(rsa->public_key); 222 wi_release(rsa->private_key); 214 223 } 215 224 … … 223 232 rsa, 224 233 rsa->rsa, 225 RSA_size(rsa->rsa) * 8); 226 } 227 228 229 230 #pragma mark - 231 232 static wi_data_t * _wi_rsa_public_key(wi_rsa_t *rsa) { 233 wi_data_t *data; 234 wi_rsa_bits(rsa)); 235 } 236 237 238 239 #pragma mark - 240 241 wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { 234 242 unsigned char *buffer; 235 243 int length; 236 244 237 buffer = NULL; 238 length = i2d_RSA_PUBKEY(rsa->rsa, &buffer); 239 240 if(length <= 0) { 241 wi_error_set_openssl_error(); 242 243 return NULL; 244 } 245 246 data = wi_data_with_bytes(buffer, length); 247 248 OPENSSL_free(buffer); 249 250 return data; 251 } 252 253 254 255 #pragma mark - 256 257 wi_data_t * wi_rsa_public_key(wi_rsa_t *rsa) { 245 if(!rsa->public_key) { 246 buffer = NULL; 247 length = i2d_RSAPublicKey(rsa->rsa, &buffer); 248 249 if(length <= 0) { 250 wi_error_set_openssl_error(); 251 252 return NULL; 253 } 254 255 rsa->public_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); 256 257 OPENSSL_free(buffer); 258 } 259 258 260 return rsa->public_key; 261 } 262 263 264 265 wi_data_t * wi_rsa_private_key(wi_rsa_t *rsa) { 266 unsigned char *buffer; 267 int length; 268 269 if(!rsa->private_key) { 270 buffer = NULL; 271 length = i2d_RSAPrivateKey(rsa->rsa, &buffer); 272 273 if(length <= 0) { 274 wi_error_set_openssl_error(); 275 276 return NULL; 277 } 278 279 rsa->private_key = wi_data_init_with_bytes(wi_data_alloc(), buffer, length); 280 281 OPENSSL_free(buffer); 282 } 283 284 return rsa->private_key; 285 } 286 287 288 289 wi_uinteger_t wi_rsa_bits(wi_rsa_t *rsa) { 290 return RSA_size(rsa->rsa) * 8; 259 291 } 260 292 … … 532 564 533 565 534 #pragma mark -535 536 566 wi_cipher_type_t wi_cipher_type(wi_cipher_t *cipher) { 537 567 return cipher->type; libwired/trunk/libwired/misc/wi-crypto.h
r4645 r4647 50 50 WI_EXPORT wi_rsa_t * wi_rsa_init_with_bits(wi_rsa_t *, wi_uinteger_t); 51 51 WI_EXPORT wi_rsa_t * wi_rsa_init_with_pem_file(wi_rsa_t *, wi_string_t *); 52 WI_EXPORT wi_rsa_t * wi_rsa_init_with_private_key(wi_rsa_t *, wi_data_t *); 52 53 WI_EXPORT wi_rsa_t * wi_rsa_init_with_public_key(wi_rsa_t *, wi_data_t *); 53 54 54 55 WI_EXPORT wi_data_t * wi_rsa_public_key(wi_rsa_t *); 56 WI_EXPORT wi_data_t * wi_rsa_private_key(wi_rsa_t *); 57 WI_EXPORT wi_uinteger_t wi_rsa_bits(wi_rsa_t *); 55 58 56 59 WI_EXPORT wi_data_t * wi_rsa_encrypt(wi_rsa_t *, wi_data_t *); … … 68 71 WI_EXPORT wi_data_t * wi_cipher_key(wi_cipher_t *); 69 72 WI_EXPORT wi_data_t * wi_cipher_iv(wi_cipher_t *); 70 71 73 WI_EXPORT wi_cipher_type_t wi_cipher_type(wi_cipher_t *); 72 74 WI_EXPORT wi_string_t * wi_cipher_name(wi_cipher_t *);
