[331] | 1 | /*
|
---|
| 2 | * Copyright 2007-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include "internal/cryptlib.h"
|
---|
| 12 | #include <openssl/evp.h>
|
---|
| 13 | #include "internal/asn1_int.h"
|
---|
| 14 |
|
---|
| 15 | #define HMAC_TEST_PRIVATE_KEY_FORMAT
|
---|
| 16 |
|
---|
| 17 | /*
|
---|
| 18 | * HMAC "ASN1" method. This is just here to indicate the maximum HMAC output
|
---|
| 19 | * length and to free up an HMAC key.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | static int hmac_size(const EVP_PKEY *pkey)
|
---|
| 23 | {
|
---|
| 24 | return EVP_MAX_MD_SIZE;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | static void hmac_key_free(EVP_PKEY *pkey)
|
---|
| 28 | {
|
---|
| 29 | ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
|
---|
| 30 | if (os) {
|
---|
| 31 | if (os->data)
|
---|
| 32 | OPENSSL_cleanse(os->data, os->length);
|
---|
| 33 | ASN1_OCTET_STRING_free(os);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
|
---|
| 38 | {
|
---|
| 39 | switch (op) {
|
---|
| 40 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
|
---|
| 41 | *(int *)arg2 = NID_sha256;
|
---|
| 42 | return 1;
|
---|
| 43 |
|
---|
| 44 | default:
|
---|
| 45 | return -2;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | static int hmac_pkey_public_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
---|
| 50 | {
|
---|
| 51 | return ASN1_OCTET_STRING_cmp(EVP_PKEY_get0(a), EVP_PKEY_get0(b));
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
|
---|
| 55 | /*
|
---|
| 56 | * A bogus private key format for test purposes. This is simply the HMAC key
|
---|
| 57 | * with "HMAC PRIVATE KEY" in the headers. When enabled the genpkey utility
|
---|
| 58 | * can be used to "generate" HMAC keys.
|
---|
| 59 | */
|
---|
| 60 |
|
---|
| 61 | static int old_hmac_decode(EVP_PKEY *pkey,
|
---|
| 62 | const unsigned char **pder, int derlen)
|
---|
| 63 | {
|
---|
| 64 | ASN1_OCTET_STRING *os;
|
---|
| 65 | os = ASN1_OCTET_STRING_new();
|
---|
| 66 | if (os == NULL || !ASN1_OCTET_STRING_set(os, *pder, derlen))
|
---|
| 67 | goto err;
|
---|
| 68 | if (!EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os))
|
---|
| 69 | goto err;
|
---|
| 70 | return 1;
|
---|
| 71 |
|
---|
| 72 | err:
|
---|
| 73 | ASN1_OCTET_STRING_free(os);
|
---|
| 74 | return 0;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder)
|
---|
| 78 | {
|
---|
| 79 | int inc;
|
---|
| 80 | ASN1_OCTET_STRING *os = EVP_PKEY_get0(pkey);
|
---|
| 81 | if (pder) {
|
---|
| 82 | if (!*pder) {
|
---|
| 83 | *pder = OPENSSL_malloc(os->length);
|
---|
| 84 | if (*pder == NULL)
|
---|
| 85 | return -1;
|
---|
| 86 | inc = 0;
|
---|
| 87 | } else
|
---|
| 88 | inc = 1;
|
---|
| 89 |
|
---|
| 90 | memcpy(*pder, os->data, os->length);
|
---|
| 91 |
|
---|
| 92 | if (inc)
|
---|
| 93 | *pder += os->length;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | return os->length;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | #endif
|
---|
| 100 |
|
---|
| 101 | const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = {
|
---|
| 102 | EVP_PKEY_HMAC,
|
---|
| 103 | EVP_PKEY_HMAC,
|
---|
| 104 | 0,
|
---|
| 105 |
|
---|
| 106 | "HMAC",
|
---|
| 107 | "OpenSSL HMAC method",
|
---|
| 108 |
|
---|
| 109 | 0, 0, hmac_pkey_public_cmp, 0,
|
---|
| 110 |
|
---|
| 111 | 0, 0, 0,
|
---|
| 112 |
|
---|
| 113 | hmac_size,
|
---|
| 114 | 0, 0,
|
---|
| 115 | 0, 0, 0, 0, 0, 0, 0,
|
---|
| 116 |
|
---|
| 117 | hmac_key_free,
|
---|
| 118 | hmac_pkey_ctrl,
|
---|
| 119 | #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT
|
---|
| 120 | old_hmac_decode,
|
---|
| 121 | old_hmac_encode
|
---|
| 122 | #else
|
---|
| 123 | 0, 0
|
---|
| 124 | #endif
|
---|
| 125 | };
|
---|