| 1 |
/* Copyright (c) 2016, Google Inc. |
| 2 |
* |
| 3 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
* purpose with or without fee is hereby granted, provided that the above |
| 5 |
* copyright notice and this permission notice appear in all copies. |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <openssl/bytestring.h> |
| 19 |
#include <openssl/crypto.h> |
| 20 |
#include <openssl/obj.h> |
| 21 |
|
| 22 |
|
| 23 |
static bool TestBasic() { |
| 24 |
static const int kNID = NID_sha256WithRSAEncryption; |
| 25 |
static const char kShortName[] = "RSA-SHA256"; |
| 26 |
static const char kLongName[] = "sha256WithRSAEncryption"; |
| 27 |
static const char kText[] = "1.2.840.113549.1.1.11"; |
| 28 |
static const uint8_t kDER[] = { |
| 29 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, |
| 30 |
}; |
| 31 |
|
| 32 |
CBS cbs; |
| 33 |
CBS_init(&cbs, kDER, sizeof(kDER)); |
| 34 |
if (OBJ_cbs2nid(&cbs) != kNID || |
| 35 |
OBJ_sn2nid(kShortName) != kNID || |
| 36 |
OBJ_ln2nid(kLongName) != kNID || |
| 37 |
OBJ_txt2nid(kShortName) != kNID || |
| 38 |
OBJ_txt2nid(kLongName) != kNID || |
| 39 |
OBJ_txt2nid(kText) != kNID) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if (strcmp(kShortName, OBJ_nid2sn(kNID)) != 0 || |
| 44 |
strcmp(kLongName, OBJ_nid2ln(kNID)) != 0) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
if (OBJ_sn2nid("this is not an OID") != NID_undef || |
| 49 |
OBJ_ln2nid("this is not an OID") != NID_undef || |
| 50 |
OBJ_txt2nid("this is not an OID") != NID_undef) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
CBS_init(&cbs, NULL, 0); |
| 55 |
if (OBJ_cbs2nid(&cbs) != NID_undef) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
// 1.2.840.113554.4.1.72585.2 (https://davidben.net/oid). |
| 60 |
static const uint8_t kUnknownDER[] = { |
| 61 |
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, |
| 62 |
}; |
| 63 |
CBS_init(&cbs, kUnknownDER, sizeof(kUnknownDER)); |
| 64 |
if (OBJ_cbs2nid(&cbs) != NID_undef) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
static bool TestSignatureAlgorithms() { |
| 72 |
int digest_nid, pkey_nid; |
| 73 |
if (!OBJ_find_sigid_algs(NID_sha256WithRSAEncryption, &digest_nid, |
| 74 |
&pkey_nid) || |
| 75 |
digest_nid != NID_sha256 || pkey_nid != NID_rsaEncryption) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
if (OBJ_find_sigid_algs(NID_sha256, &digest_nid, &pkey_nid)) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
int sign_nid; |
| 84 |
if (!OBJ_find_sigid_by_algs(&sign_nid, NID_sha256, NID_rsaEncryption) || |
| 85 |
sign_nid != NID_sha256WithRSAEncryption) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
if (OBJ_find_sigid_by_algs(&sign_nid, NID_dsa, NID_rsaEncryption)) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
int main() { |
| 97 |
CRYPTO_library_init(); |
| 98 |
|
| 99 |
if (!TestBasic() || |
| 100 |
!TestSignatureAlgorithms()) { |
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
printf("PASS\n"); |
| 105 |
return 0; |
| 106 |
} |