15#include <openssl/bio.h>
16#include <openssl/evp.h>
17#include <openssl/pem.h>
18#include <openssl/err.h>
20#include "xbps_api_impl.h"
22static unsigned char pSshHeader[11] = {
23 0x00, 0x00, 0x00, 0x07, 0x73, 0x73, 0x68, 0x2D, 0x72, 0x73, 0x61
27SshEncodeBuffer(
unsigned char *pEncoding,
int bufferLen,
unsigned char *pBuffer)
29 int adjustedLen = bufferLen, index;
31 if (*pBuffer & 0x80) {
38 pEncoding[0] = (
unsigned char) (adjustedLen >> 24);
39 pEncoding[1] = (
unsigned char) (adjustedLen >> 16);
40 pEncoding[2] = (
unsigned char) (adjustedLen >> 8);
41 pEncoding[3] = (
unsigned char) (adjustedLen );
42 memcpy(&pEncoding[index], pBuffer, bufferLen);
43 return index + bufferLen;
47fp2str(
unsigned const char *fp,
unsigned int len)
49 unsigned int i, c = 0;
52 for (i = 0; i < len; i++) {
55 sprintf(cur,
"%02x", fp[i]);
68 EVP_MD_CTX *mdctx = NULL;
69 EVP_PKEY *pPubKey = NULL;
72 const void *pubkeydata;
73 unsigned char md_value[EVP_MAX_MD_SIZE];
75 unsigned char *nBytes = NULL, *eBytes = NULL, *pEncoding = NULL;
76 unsigned int md_len = 0;
77 char *hexfpstr = NULL;
78 int index = 0, nLen = 0, eLen = 0, encodingLength = 0;
80 ERR_load_crypto_strings();
81 OpenSSL_add_all_algorithms();
83 mdctx = EVP_MD_CTX_new();
85 pubkeydata = xbps_data_data_nocopy(pubkey);
86 bio = BIO_new_mem_buf(pubkeydata, xbps_data_size(pubkey));
89 pPubKey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
92 "unable to decode public key from the given file: %s\n",
93 ERR_error_string(ERR_get_error(), NULL));
97 if (EVP_PKEY_base_id(pPubKey) != EVP_PKEY_RSA) {
98 xbps_dbg_printf(
"only RSA public keys are currently supported\n");
102 pRsa = EVP_PKEY_get1_RSA(pPubKey);
104 xbps_dbg_printf(
"failed to get RSA public key : %s\n",
105 ERR_error_string(ERR_get_error(), NULL));
109 RSA_get0_key(pRsa, &n, &e, NULL);
111 nLen = BN_num_bytes(n);
112 nBytes = (
unsigned char*) malloc(nLen);
115 BN_bn2bin(n, nBytes);
118 eLen = BN_num_bytes(e);
119 eBytes = (
unsigned char*) malloc(eLen);
122 BN_bn2bin(e, eBytes);
124 encodingLength = 11 + 4 + eLen + 4 + nLen;
126 if (eBytes[0] & 0x80)
128 if (nBytes[0] & 0x80)
131 pEncoding = malloc(encodingLength);
134 memcpy(pEncoding, pSshHeader, 11);
136 index = SshEncodeBuffer(&pEncoding[11], eLen, eBytes);
137 (void)SshEncodeBuffer(&pEncoding[11 + index], nLen, nBytes);
142 EVP_MD_CTX_init(mdctx);
143 EVP_DigestInit_ex(mdctx, EVP_md5(), NULL);
144 EVP_DigestUpdate(mdctx, pEncoding, encodingLength);
145 if (EVP_DigestFinal_ex(mdctx, md_value, &md_len) == 0)
147 EVP_MD_CTX_free(mdctx);
152 hexfpstr = fp2str(md_value, md_len);
156 EVP_MD_CTX_free(mdctx);
162 EVP_PKEY_free(pPubKey);
char * xbps_pubkey2fp(xbps_data_t pubkey)