| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #include "signature_digest.h" | 6 #include "signature_digest.h" |
| 7 #define OPENSSL_NO_SHA | 7 #define OPENSSL_NO_SHA |
| 8 #include <openssl/engine.h> | 8 #include <openssl/engine.h> |
| 9 #include <openssl/pem.h> | 9 #include <openssl/pem.h> |
| 10 #include <openssl/rsa.h> | 10 #include <openssl/rsa.h> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 int algorithm) { | 45 int algorithm) { |
| 46 FILE* key_fp = NULL; | 46 FILE* key_fp = NULL; |
| 47 RSA* key = NULL; | 47 RSA* key = NULL; |
| 48 uint8_t* signature = NULL; | 48 uint8_t* signature = NULL; |
| 49 uint8_t* signature_digest = SignatureDigest(buf, len, algorithm); | 49 uint8_t* signature_digest = SignatureDigest(buf, len, algorithm); |
| 50 int signature_digest_len = (hash_size_map[algorithm] + | 50 int signature_digest_len = (hash_size_map[algorithm] + |
| 51 digestinfo_size_map[algorithm]); | 51 digestinfo_size_map[algorithm]); |
| 52 key_fp = fopen(key_file, "r"); | 52 key_fp = fopen(key_file, "r"); |
| 53 if (!key_fp) { | 53 if (!key_fp) { |
| 54 fprintf(stderr, "SignatureBuf(): Couldn't open key file: %s\n", key_file); | 54 fprintf(stderr, "SignatureBuf(): Couldn't open key file: %s\n", key_file); |
| 55 Free(signature_digest); |
| 55 return NULL; | 56 return NULL; |
| 56 } | 57 } |
| 57 if ((key = PEM_read_RSAPrivateKey(key_fp, NULL, NULL, NULL))) | 58 if ((key = PEM_read_RSAPrivateKey(key_fp, NULL, NULL, NULL))) |
| 58 signature = (uint8_t*) Malloc(siglen_map[algorithm]); | 59 signature = (uint8_t*) Malloc(siglen_map[algorithm]); |
| 59 else | 60 else |
| 60 fprintf(stderr, "SignatureBuf(): Couldn't read private key from file: %s\n", | 61 fprintf(stderr, "SignatureBuf(): Couldn't read private key from file: %s\n", |
| 61 key_file); | 62 key_file); |
| 62 if (signature) { | 63 if (signature) { |
| 63 if (-1 == RSA_private_encrypt(signature_digest_len, /* Input length. */ | 64 if (-1 == RSA_private_encrypt(signature_digest_len, /* Input length. */ |
| 64 signature_digest, /* Input data. */ | 65 signature_digest, /* Input data. */ |
| 65 signature, /* Output signature. */ | 66 signature, /* Output signature. */ |
| 66 key, /* Key to use. */ | 67 key, /* Key to use. */ |
| 67 RSA_PKCS1_PADDING)) /* Padding to use. */ | 68 RSA_PKCS1_PADDING)) /* Padding to use. */ |
| 68 fprintf(stderr, "SignatureBuf(): RSA_private_encrypt() failed.\n"); | 69 fprintf(stderr, "SignatureBuf(): RSA_private_encrypt() failed.\n"); |
| 69 } | 70 } |
| 71 fclose(key_fp); |
| 70 if (key) | 72 if (key) |
| 71 RSA_free(key); | 73 RSA_free(key); |
| 72 Free(signature_digest); | 74 Free(signature_digest); |
| 73 return signature; | 75 return signature; |
| 74 } | 76 } |
| OLD | NEW |